|
| 1 | +"""Tests for WorkflowSchema.requires_zarr and requires_plate computed fields.""" |
| 2 | +import json |
| 3 | +import pytest |
| 4 | +from pathlib import Path |
| 5 | +from biomero_schema.models import WorkflowSchema |
| 6 | + |
| 7 | + |
| 8 | +TESTS_DIR = Path(__file__).parent |
| 9 | + |
| 10 | + |
| 11 | +def load_example() -> dict: |
| 12 | + return json.loads((TESTS_DIR / "example_workflow.json").read_text()) |
| 13 | + |
| 14 | + |
| 15 | +def make_schema(image_inputs: list) -> WorkflowSchema: |
| 16 | + """Build a minimal WorkflowSchema with the given image input list.""" |
| 17 | + base = load_example() |
| 18 | + base["inputs"] = image_inputs |
| 19 | + return WorkflowSchema.model_validate(base) |
| 20 | + |
| 21 | + |
| 22 | +def image_input(fmt=None, subtype=None) -> dict: |
| 23 | + inp = { |
| 24 | + "id": "input_image", |
| 25 | + "type": "image", |
| 26 | + "name": "Input", |
| 27 | + "value-key": "[INPUT_IMAGE]", |
| 28 | + "command-line-flag": "--input", |
| 29 | + } |
| 30 | + if fmt is not None: |
| 31 | + inp["format"] = fmt |
| 32 | + if subtype is not None: |
| 33 | + inp["sub-type"] = subtype |
| 34 | + return inp |
| 35 | + |
| 36 | + |
| 37 | +# --------------------------------------------------------------------------- |
| 38 | +# requires_zarr |
| 39 | +# --------------------------------------------------------------------------- |
| 40 | + |
| 41 | +class TestRequiresZarr: |
| 42 | + def test_false_for_tiff(self): |
| 43 | + schema = make_schema([image_input(fmt="tif")]) |
| 44 | + assert schema.requires_zarr is False |
| 45 | + |
| 46 | + def test_false_for_ometiff(self): |
| 47 | + schema = make_schema([image_input(fmt="ometiff")]) |
| 48 | + assert schema.requires_zarr is False |
| 49 | + |
| 50 | + def test_false_for_png(self): |
| 51 | + schema = make_schema([image_input(fmt="png")]) |
| 52 | + assert schema.requires_zarr is False |
| 53 | + |
| 54 | + def test_false_for_no_image_inputs(self): |
| 55 | + base = load_example() |
| 56 | + base["inputs"] = [ |
| 57 | + {"id": "x", "type": "float", "value-key": "[X]", "command-line-flag": "--x"} |
| 58 | + ] |
| 59 | + schema = WorkflowSchema.model_validate(base) |
| 60 | + assert schema.requires_zarr is False |
| 61 | + |
| 62 | + def test_true_for_zarr(self): |
| 63 | + schema = make_schema([image_input(fmt="zarr")]) |
| 64 | + assert schema.requires_zarr is True |
| 65 | + |
| 66 | + def test_true_for_omezarr(self): |
| 67 | + schema = make_schema([image_input(fmt="omezarr")]) |
| 68 | + assert schema.requires_zarr is True |
| 69 | + |
| 70 | + def test_true_for_ome_zarr_dot(self): |
| 71 | + schema = make_schema([image_input(fmt="ome.zarr")]) |
| 72 | + assert schema.requires_zarr is True |
| 73 | + |
| 74 | + def test_true_for_ome_zarr_dash(self): |
| 75 | + schema = make_schema([image_input(fmt="ome-zarr")]) |
| 76 | + assert schema.requires_zarr is True |
| 77 | + |
| 78 | + def test_true_for_format_list_containing_zarr(self): |
| 79 | + schema = make_schema([image_input(fmt=["tif", "zarr"])]) |
| 80 | + assert schema.requires_zarr is True |
| 81 | + |
| 82 | + def test_true_when_plate_subtype(self): |
| 83 | + """Plate implies ZARR.""" |
| 84 | + schema = make_schema([image_input(fmt="omezarr", subtype="plate")]) |
| 85 | + assert schema.requires_zarr is True |
| 86 | + |
| 87 | + def test_false_for_zarr_format_on_non_image_type(self): |
| 88 | + """format field on a non-image input should not trigger zarr.""" |
| 89 | + base = load_example() |
| 90 | + base["inputs"] = [ |
| 91 | + {"id": "x", "type": "file", "format": "zarr", "value-key": "[X]", "command-line-flag": "--x"} |
| 92 | + ] |
| 93 | + schema = WorkflowSchema.model_validate(base) |
| 94 | + assert schema.requires_zarr is False |
| 95 | + |
| 96 | + |
| 97 | +# --------------------------------------------------------------------------- |
| 98 | +# requires_plate |
| 99 | +# --------------------------------------------------------------------------- |
| 100 | + |
| 101 | +class TestRequiresPlate: |
| 102 | + def test_false_by_default(self): |
| 103 | + schema = make_schema([image_input(fmt="tif", subtype="grayscale")]) |
| 104 | + assert schema.requires_plate is False |
| 105 | + |
| 106 | + def test_false_for_no_subtype(self): |
| 107 | + schema = make_schema([image_input(fmt="zarr")]) |
| 108 | + assert schema.requires_plate is False |
| 109 | + |
| 110 | + def test_true_for_plate_subtype(self): |
| 111 | + schema = make_schema([image_input(fmt="omezarr", subtype="plate")]) |
| 112 | + assert schema.requires_plate is True |
| 113 | + |
| 114 | + def test_true_for_plate_in_subtype_list(self): |
| 115 | + schema = make_schema([image_input(fmt="omezarr", subtype=["plate"])]) |
| 116 | + assert schema.requires_plate is True |
| 117 | + |
| 118 | + def test_false_for_other_subtype(self): |
| 119 | + schema = make_schema([image_input(fmt="tif", subtype="grayscale")]) |
| 120 | + assert schema.requires_plate is False |
| 121 | + |
| 122 | + def test_plate_also_sets_zarr(self): |
| 123 | + """Plate always implies requires_zarr too.""" |
| 124 | + schema = make_schema([image_input(fmt="omezarr", subtype="plate")]) |
| 125 | + assert schema.requires_zarr is True |
| 126 | + assert schema.requires_plate is True |
| 127 | + |
| 128 | + |
| 129 | +# --------------------------------------------------------------------------- |
| 130 | +# Serialisation — flags appear in model_dump(by_alias=True) |
| 131 | +# --------------------------------------------------------------------------- |
| 132 | + |
| 133 | +class TestSerialisedOutput: |
| 134 | + def test_keys_present_in_dump(self): |
| 135 | + schema = make_schema([image_input(fmt="zarr", subtype="plate")]) |
| 136 | + data = schema.model_dump(by_alias=True) |
| 137 | + assert "requires-zarr" in data |
| 138 | + assert "requires-plate" in data |
| 139 | + |
| 140 | + def test_values_correct_in_dump(self): |
| 141 | + schema = make_schema([image_input(fmt="zarr", subtype="plate")]) |
| 142 | + data = schema.model_dump(by_alias=True) |
| 143 | + assert data["requires-zarr"] is True |
| 144 | + assert data["requires-plate"] is True |
| 145 | + |
| 146 | + def test_false_values_in_dump(self): |
| 147 | + schema = make_schema([image_input(fmt="tif")]) |
| 148 | + data = schema.model_dump(by_alias=True) |
| 149 | + assert data["requires-zarr"] is False |
| 150 | + assert data["requires-plate"] is False |
| 151 | + |
| 152 | + |
| 153 | +# --------------------------------------------------------------------------- |
| 154 | +# Round-trip through example_workflow.json (tiff input → both False) |
| 155 | +# --------------------------------------------------------------------------- |
| 156 | + |
| 157 | +def test_example_workflow_no_zarr(): |
| 158 | + schema = WorkflowSchema.model_validate(load_example()) |
| 159 | + assert schema.requires_zarr is False |
| 160 | + assert schema.requires_plate is False |
0 commit comments