|
| 1 | +import os |
| 2 | +import time |
| 3 | +from typing import List |
| 4 | +import contextvars |
| 5 | +from functools import lru_cache |
| 6 | + |
| 7 | +from tests import DATA_SET_PATH |
| 8 | +from tests.base import BaseApplicationTestCase |
| 9 | +from src.superannotate import SAClient, AppException |
| 10 | + |
| 11 | +sa = SAClient() |
| 12 | + |
| 13 | +attached_item_names = contextvars.ContextVar('attached_item_names') |
| 14 | +attached_items_status = contextvars.ContextVar('attached_items_status') |
| 15 | +contributors_role = contextvars.ContextVar('contributors_role') |
| 16 | +annotation_classes_count = contextvars.ContextVar('annotation_classes_count') |
| 17 | + |
| 18 | + |
| 19 | +class TestWorkflow(BaseApplicationTestCase): |
| 20 | + PROJECT_NAME = 'TestWorkflow' |
| 21 | + |
| 22 | + CLASSES_PATH = "sample_project_vector/classes/classes.json" |
| 23 | + ANNOTATIONS_PATH = "sample_project_vector" |
| 24 | + PROJECT_TYPE = "GenAi" |
| 25 | + |
| 26 | + @property |
| 27 | + def classes_path(self): |
| 28 | + return os.path.join(DATA_SET_PATH, self.CLASSES_PATH) |
| 29 | + |
| 30 | + @property |
| 31 | + def annotations_path(self): |
| 32 | + return os.path.join(DATA_SET_PATH, self.ANNOTATIONS_PATH) |
| 33 | + |
| 34 | + @lru_cache() |
| 35 | + def get_not_admin_contributor_emails(self) -> List[str]: |
| 36 | + contributor_emails = [] |
| 37 | + for i in sa.search_team_contributors(): |
| 38 | + if i['user_role'] != 2: # skipping admins etc |
| 39 | + contributor_emails.append(i['email']) |
| 40 | + return contributor_emails |
| 41 | + |
| 42 | + def step_1_add_contributors(self): |
| 43 | + contributor_emails = self.get_not_admin_contributor_emails() |
| 44 | + assert len(contributor_emails) > 0, "for the test there should be more then 1 contributor" |
| 45 | + # Give the backend time to think |
| 46 | + time.sleep(3) |
| 47 | + contributors_role.set("Annotator") |
| 48 | + added, skipped, = sa.add_contributors_to_project(self.PROJECT_NAME, contributor_emails, "Annotator") |
| 49 | + assert len(skipped) == 0 |
| 50 | + assert len(added) == len(contributor_emails) |
| 51 | + |
| 52 | + |
| 53 | + def step_3_attach_items(self): |
| 54 | + name_prefix, count = 'example_image_', 4 |
| 55 | + items_to_attach = [ |
| 56 | + {"name": f"{name_prefix}{i}.jpg", "url": f"url_{i}"} |
| 57 | + for i in range(1, count + 1) |
| 58 | + ] |
| 59 | + attached_item_names.set([i['name'] for i in items_to_attach]) |
| 60 | + attached_items_status.set("Completed") |
| 61 | + uploaded, _, __ = sa.attach_items( |
| 62 | + self.PROJECT_NAME, |
| 63 | + items_to_attach, # noqa |
| 64 | + annotation_status="Completed" |
| 65 | + ) |
| 66 | + assert len(uploaded) == count |
| 67 | + items = sa.search_items(self.PROJECT_NAME) |
| 68 | + assert all(i['annotation_status'] == 'Completed' for i in items) |
| 69 | + |
| 70 | + def step_4_create_annotation_classes(self): |
| 71 | + created = sa.create_annotation_classes_from_classes_json(self.PROJECT_NAME, self.classes_path) |
| 72 | + annotation_classes_count.set(4) |
| 73 | + assert len(created) == 4 |
| 74 | + |
| 75 | + def step_5_upload_annotations(self): |
| 76 | + uploaded, _, __ = sa.upload_annotations_from_folder_to_project( |
| 77 | + self.PROJECT_NAME, self.annotations_path, keep_status=True |
| 78 | + ) |
| 79 | + attached_items_count = len(attached_item_names.get()) |
| 80 | + assert len(uploaded) == attached_items_count |
| 81 | + # assert that all items have a status of "attached_items_status" |
| 82 | + items = sa.search_items(self.PROJECT_NAME, annotation_status=attached_items_status.get()) |
| 83 | + assert len(items) == attached_items_count |
| 84 | + |
| 85 | + def step_6_get_annotations(self): |
| 86 | + annotations = sa.get_annotations(self.PROJECT_NAME) |
| 87 | + assert len(annotations) == len(attached_item_names.get()) |
| 88 | + assert all(i['metadata']['status'] == attached_items_status.get() for i in annotations) |
| 89 | + |
| 90 | + def step_7_assign_item(self): |
| 91 | + item = attached_item_names.get()[0] |
| 92 | + user = self.get_not_admin_contributor_emails()[0] |
| 93 | + sa.assign_items(self.PROJECT_NAME, [item], user) |
| 94 | + item_data = sa.get_item_metadata(self.PROJECT_NAME, item) |
| 95 | + if contributors_role.get() in ("Annotator", 'QA'): |
| 96 | + assert item_data[f"{contributors_role.get().lower()}_email"] == user |
| 97 | + assert len(item_data['assignments']) == 1 |
| 98 | + assert item_data['assignments'][0]['user_role'] == contributors_role.get() |
| 99 | + |
| 100 | + def step_8_unassign_item(self): |
| 101 | + item = attached_item_names.get()[0] |
| 102 | + sa.unassign_items(self.PROJECT_NAME, [item]) |
| 103 | + item_data = sa.get_item_metadata(self.PROJECT_NAME, item) |
| 104 | + if contributors_role.get() in ("Annotator", 'QA'): |
| 105 | + assert item_data[f"{contributors_role.get().lower()}_email"] is None |
| 106 | + assert len(item_data['assignments']) == 0 |
| 107 | + |
| 108 | + def step_999_clone(self): |
| 109 | + new_name = 'step_clone' |
| 110 | + try: |
| 111 | + sa.delete_project(new_name) |
| 112 | + except AppException: |
| 113 | + ... |
| 114 | + project = sa.clone_project(new_name, self.PROJECT_NAME, copy_contributors=True) |
| 115 | + contributors = self.get_not_admin_contributor_emails() |
| 116 | + assert len(contributors) == len(project['users']) |
| 117 | + assert all(i['user_role'] == contributors_role.get() for i in project['users']) |
| 118 | + assert len(project['classes']) == annotation_classes_count.get() |
| 119 | + |
| 120 | + |
| 121 | + def _steps(self): |
| 122 | + for name in dir(self): |
| 123 | + if name.startswith("step"): |
| 124 | + yield name, getattr(self, name) |
| 125 | + |
| 126 | + def test_steps(self): |
| 127 | + for name, step in self._steps(): |
| 128 | + try: |
| 129 | + step() |
| 130 | + except Exception as e: |
| 131 | + self.fail("{} failed ({}: {})".format(step, type(e), e)) |
0 commit comments