|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from superannotate import SAClient |
| 4 | +from unittest import TestCase |
| 5 | + |
| 6 | + |
| 7 | +sa = SAClient() |
| 8 | + |
| 9 | + |
| 10 | +class TestAsyncFunctions(TestCase): |
| 11 | + PROJECT_NAME = "TestAsync" |
| 12 | + PROJECT_DESCRIPTION = "Desc" |
| 13 | + PROJECT_TYPE = "Vector" |
| 14 | + ATTACH_PAYLOAD = [{"name": f"name_{i}", "url": "url"} for i in range(4)] |
| 15 | + UPLOAD_PAYLOAD = [{"metadata": {"name": f"name_{i}"}} for i in range(4)] |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def setUpClass(cls): |
| 19 | + cls.tearDownClass() |
| 20 | + cls._project = sa.create_project( |
| 21 | + cls.PROJECT_NAME, cls.PROJECT_DESCRIPTION, cls.PROJECT_TYPE |
| 22 | + ) |
| 23 | + sa.attach_items(cls.PROJECT_NAME, cls.ATTACH_PAYLOAD) |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def tearDownClass(cls): |
| 27 | + sa.delete_project(cls.PROJECT_NAME) |
| 28 | + |
| 29 | + def test_get_annotations_in_running_event_loop(self): |
| 30 | + async def _test(): |
| 31 | + annotations = sa.get_annotations(self.PROJECT_NAME) |
| 32 | + assert len(annotations) == 4 |
| 33 | + asyncio.run(_test()) |
| 34 | + |
| 35 | + def test_multiple_get_annotations_in_running_event_loop(self): |
| 36 | + # TODO add handling of nested loop |
| 37 | + async def nested(): |
| 38 | + sa.attach_items(self.PROJECT_NAME, self.ATTACH_PAYLOAD) |
| 39 | + annotations = sa.get_annotations(self.PROJECT_NAME) |
| 40 | + assert len(annotations) == 4 |
| 41 | + async def create_task_test(): |
| 42 | + import nest_asyncio |
| 43 | + nest_asyncio.apply() |
| 44 | + task1 = asyncio.create_task(nested()) |
| 45 | + task2 = asyncio.create_task(nested()) |
| 46 | + await task1 |
| 47 | + await task2 |
| 48 | + asyncio.run(create_task_test()) |
| 49 | + |
| 50 | + async def gather_test(): |
| 51 | + import nest_asyncio |
| 52 | + nest_asyncio.apply() |
| 53 | + await asyncio.gather(nested(), nested()) |
| 54 | + asyncio.run(gather_test()) |
| 55 | + |
| 56 | + def test_upload_annotations_in_running_event_loop(self): |
| 57 | + async def _test(): |
| 58 | + sa.attach_items(self.PROJECT_NAME, self.ATTACH_PAYLOAD) |
| 59 | + annotations = sa.upload_annotations(self.PROJECT_NAME, annotations=self.UPLOAD_PAYLOAD) |
| 60 | + assert len(annotations['succeeded']) == 4 |
| 61 | + asyncio.run(_test()) |
| 62 | + |
| 63 | + |
0 commit comments