|
| 1 | +import importlib |
| 2 | +import sys |
| 3 | +from types import SimpleNamespace |
| 4 | + |
| 5 | + |
| 6 | +class DummyMlflowAlgorithmClient: |
| 7 | + def __init__(self, *args, **kwargs): |
| 8 | + self.modelname_list = [] |
| 9 | + |
| 10 | + def load_from_mlflow(self, algorithm_type="segmentation"): |
| 11 | + return None |
| 12 | + |
| 13 | + def __getitem__(self, key): |
| 14 | + raise KeyError(key) |
| 15 | + |
| 16 | + |
| 17 | +class FakeContainer: |
| 18 | + def __init__(self, uri="tiled://root"): |
| 19 | + self.uri = uri |
| 20 | + self._children = {} |
| 21 | + self.metadata = None |
| 22 | + |
| 23 | + def keys(self): |
| 24 | + return self._children.keys() |
| 25 | + |
| 26 | + def create_container(self, key, metadata=None): |
| 27 | + container = FakeContainer(uri=f"{self.uri}/{key}") |
| 28 | + container.metadata = metadata |
| 29 | + self._children[key] = container |
| 30 | + return container |
| 31 | + |
| 32 | + def write_array(self, key, array): |
| 33 | + array_client = SimpleNamespace(uri=f"{self.uri}/{key}", array=array) |
| 34 | + self._children[key] = array_client |
| 35 | + return array_client |
| 36 | + |
| 37 | + def __getitem__(self, key): |
| 38 | + return self._children[key] |
| 39 | + |
| 40 | + |
| 41 | +def test_save_annotations_data(mocker, monkeypatch): |
| 42 | + monkeypatch.setenv("DATA_TILED_URI", "http://example.com/api/v1/metadata/data") |
| 43 | + monkeypatch.setenv("MASK_TILED_URI", "http://example.com/api/v1/metadata/masks") |
| 44 | + monkeypatch.setenv("SEG_TILED_URI", "http://example.com/api/v1/metadata/seg") |
| 45 | + |
| 46 | + mocker.patch("tiled.client.from_uri", return_value={}) |
| 47 | + mocker.patch( |
| 48 | + "mlex_utils.mlflow_utils.mlflow_algorithm_client.MlflowAlgorithmClient", |
| 49 | + DummyMlflowAlgorithmClient, |
| 50 | + ) |
| 51 | + |
| 52 | + sys.modules.pop("utils.data_utils", None) |
| 53 | + data_utils = importlib.import_module("utils.data_utils") |
| 54 | + |
| 55 | + root_container = FakeContainer() |
| 56 | + mask_handler = data_utils.TiledMaskHandler.__new__(data_utils.TiledMaskHandler) |
| 57 | + mask_handler.mask_client = root_container |
| 58 | + |
| 59 | + mocker.patch.object( |
| 60 | + data_utils, "from_uri", return_value=SimpleNamespace(access_blob={}) |
| 61 | + ) |
| 62 | + mocker.patch.object(data_utils, "copy_tiled_access_info", return_value=None) |
| 63 | + mocker.patch.object( |
| 64 | + data_utils.tiled_datasets, |
| 65 | + "get_data_uri_by_trimmed_uri", |
| 66 | + return_value="http://example.com/data/project/sample", |
| 67 | + ) |
| 68 | + mocker.patch.object( |
| 69 | + data_utils.tiled_datasets, |
| 70 | + "get_data_sequence_by_trimmed_uri", |
| 71 | + return_value=SimpleNamespace(access_blob={}), |
| 72 | + ) |
| 73 | + |
| 74 | + all_annotations = [ |
| 75 | + { |
| 76 | + "class_id": "class-1", |
| 77 | + "label": "Class 1", |
| 78 | + "color": "#ffffff", |
| 79 | + "annotations": { |
| 80 | + "0": [ |
| 81 | + { |
| 82 | + "type": "rect", |
| 83 | + "x0": 0, |
| 84 | + "y0": 0, |
| 85 | + "x1": 1, |
| 86 | + "y1": 1, |
| 87 | + } |
| 88 | + ] |
| 89 | + }, |
| 90 | + } |
| 91 | + ] |
| 92 | + |
| 93 | + uri, num_classes, message = mask_handler.save_annotations_data( |
| 94 | + global_store={"image_shapes": [(4, 4)]}, |
| 95 | + all_annotations=all_annotations, |
| 96 | + trimmed_uri="project/sample", |
| 97 | + ) |
| 98 | + |
| 99 | + assert uri is not None |
| 100 | + assert num_classes == 1 |
| 101 | + assert message == "Annotations saved successfully." |
| 102 | + assert "project/sample" in uri |
| 103 | + |
| 104 | + user_container = root_container[data_utils.USER_NAME] |
| 105 | + project_container = user_container["project"]["sample"] |
| 106 | + assert len(list(project_container.keys())) == 1 |
| 107 | + |
| 108 | + saved_hash = next(iter(project_container.keys())) |
| 109 | + saved_container = project_container[saved_hash] |
| 110 | + assert saved_container.metadata["project_name"] == "project/sample" |
| 111 | + assert "mask" in saved_container.keys() |
0 commit comments