|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, cast |
| 4 | + |
| 5 | +import pytest |
| 6 | +import zarr |
| 7 | +from zarr.storage import LocalStore |
| 8 | + |
| 9 | +from ome_zarr_models._cli import main |
| 10 | + |
| 11 | +from .conftest import Version, json_to_zarr_group |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from collections.abc import Mapping, Sequence |
| 15 | + from pathlib import Path |
| 16 | + from typing import Any |
| 17 | + |
| 18 | + |
| 19 | +def populate_fake_data( |
| 20 | + zarr_group: zarr.Group, |
| 21 | + default_dtype: str = "uint8", |
| 22 | +) -> None: |
| 23 | + # Get the ome metadata from the group attributes |
| 24 | + # version 0.4 uses the root attributes, version 0.5 uses the "ome" attribute |
| 25 | + ome_attrs = cast("Mapping[str, Any]", zarr_group.attrs.get("ome", zarr_group.attrs)) |
| 26 | + multiscales = ome_attrs.get("multiscales") |
| 27 | + if isinstance(multiscales, list): |
| 28 | + create_multiscales_data(zarr_group, multiscales, default_dtype) |
| 29 | + return |
| 30 | + |
| 31 | + # TODO? could support fake data for other node types |
| 32 | + |
| 33 | + |
| 34 | +def create_multiscales_data( |
| 35 | + zarr_group: zarr.Group, |
| 36 | + multiscales: Sequence[Mapping[str, Any]], |
| 37 | + default_dtype: str = "uint8", |
| 38 | +) -> None: |
| 39 | + # Use the first multiscale (most common case) |
| 40 | + for multiscale in multiscales: |
| 41 | + if not (axes := multiscale.get("axes")): |
| 42 | + raise ValueError( |
| 43 | + f"No axes found in multiscale metadata from {zarr_group.store_path}" |
| 44 | + ) |
| 45 | + if not (datasets := multiscale.get("datasets")): |
| 46 | + raise ValueError( |
| 47 | + f"No datasets found in multiscale metadata from {zarr_group.store_path}" |
| 48 | + ) |
| 49 | + |
| 50 | + dimension_names = [axis["name"] for axis in axes] |
| 51 | + shape = (1,) * len(dimension_names) |
| 52 | + kwargs = {} |
| 53 | + if zarr_group.metadata.zarr_format >= 3: |
| 54 | + kwargs.update({"dimension_names": dimension_names}) |
| 55 | + |
| 56 | + # Create arrays for each dataset path |
| 57 | + for dataset in datasets: |
| 58 | + if path := dataset.get("path"): |
| 59 | + zarr_group.create_array( |
| 60 | + path, |
| 61 | + shape=shape, |
| 62 | + dtype=default_dtype, |
| 63 | + **kwargs, # type: ignore[arg-type] |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "version,json_fname", |
| 69 | + [ |
| 70 | + ("0.4", "multiscales_example.json"), |
| 71 | + ("0.5", "image_example.json"), |
| 72 | + ("0.5", "image_label_example.json"), |
| 73 | + ("0.5", "plate_example_1.json"), |
| 74 | + ], |
| 75 | +) |
| 76 | +@pytest.mark.parametrize("cmd", ["validate", "info"]) |
| 77 | +def test_cli_validate( |
| 78 | + version: Version, |
| 79 | + json_fname: str, |
| 80 | + cmd: str, |
| 81 | + monkeypatch: pytest.MonkeyPatch, |
| 82 | + tmp_path: Path, |
| 83 | + capsys: pytest.CaptureFixture[str], |
| 84 | +) -> None: |
| 85 | + """Test the CLI commands.""" |
| 86 | + |
| 87 | + zarr_group = json_to_zarr_group( |
| 88 | + version=version, json_fname=json_fname, store=LocalStore(root=tmp_path) |
| 89 | + ) |
| 90 | + populate_fake_data(zarr_group) |
| 91 | + monkeypatch.setattr("sys.argv", ["ome-zarr-models", cmd, str(tmp_path)]) |
| 92 | + main() |
| 93 | + if cmd == "validate": |
| 94 | + assert "Valid OME-Zarr" in capsys.readouterr().out |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize("cmd", ["validate", "info"]) |
| 98 | +def test_cli_invalid( |
| 99 | + tmp_path: Path, |
| 100 | + cmd: str, |
| 101 | + monkeypatch: pytest.MonkeyPatch, |
| 102 | + capsys: pytest.CaptureFixture[str], |
| 103 | +) -> None: |
| 104 | + """Test the CLI with no command.""" |
| 105 | + zarr.create_group(tmp_path) |
| 106 | + monkeypatch.setattr("sys.argv", ["ome-zarr-models", cmd, str(tmp_path)]) |
| 107 | + with pytest.raises(SystemExit) as excinfo: |
| 108 | + main() |
| 109 | + assert excinfo.value.code == 1 |
| 110 | + assert "Invalid OME-Zarr" in capsys.readouterr().out |
0 commit comments