diff --git a/tests/v04/conftest.py b/tests/v04/conftest.py index 6eb7fbd..3489bbf 100644 --- a/tests/v04/conftest.py +++ b/tests/v04/conftest.py @@ -1,5 +1,6 @@ from collections.abc import Sequence -from typing import Any, Literal +from pathlib import Path +from typing import Any, Literal, TypeVar import numcodecs import numpy as np @@ -8,6 +9,7 @@ from pydantic_zarr.v2 import ArraySpec, GroupSpec from zarr.util import guess_chunks +from ome_zarr_models.base import Base from ome_zarr_models.v04.axes import Axis from ome_zarr_models.v04.image import Image, ImageAttrs from ome_zarr_models.v04.multiscales import ( @@ -15,6 +17,13 @@ Multiscale, ) +T = TypeVar("T", bound=Base) + + +def read_in_json(*, json_fname: str, model_cls: type[T]) -> T: + with open(Path(__file__).parent / "data" / json_fname) as f: + return model_cls.model_validate_json(f.read()) + def normalize_chunks( chunks: Any, diff --git a/tests/v04/data/well_example_1.json b/tests/v04/data/well_example_1.json index 1452bc5..048b742 100644 --- a/tests/v04/data/well_example_1.json +++ b/tests/v04/data/well_example_1.json @@ -1,23 +1,21 @@ { - "well": { - "images": [ - { - "acquisition": 1, - "path": "0" - }, - { - "acquisition": 1, - "path": "1" - }, - { - "acquisition": 2, - "path": "2" - }, - { - "acquisition": 2, - "path": "3" - } - ], - "version": "0.4" - } + "images": [ + { + "acquisition": 1, + "path": "0" + }, + { + "acquisition": 1, + "path": "1" + }, + { + "acquisition": 2, + "path": "2" + }, + { + "acquisition": 2, + "path": "3" + } + ], + "version": "0.4" } diff --git a/tests/v04/data/well_example_2.json b/tests/v04/data/well_example_2.json index dc863f3..53ee856 100644 --- a/tests/v04/data/well_example_2.json +++ b/tests/v04/data/well_example_2.json @@ -1,15 +1,13 @@ { - "well": { - "images": [ - { - "acquisition": 0, - "path": "0" - }, - { - "acquisition": 3, - "path": "1" - } - ], - "version": "0.4" - } + "images": [ + { + "acquisition": 0, + "path": "0" + }, + { + "acquisition": 3, + "path": "1" + } + ], + "version": "0.4" } diff --git a/tests/v04/test_bioformats2raw.py b/tests/v04/test_bioformats2raw.py index a09e00e..25d95ef 100644 --- a/tests/v04/test_bioformats2raw.py +++ b/tests/v04/test_bioformats2raw.py @@ -1,4 +1,4 @@ -from pathlib import Path +from tests.v04.conftest import read_in_json from ome_zarr_models.v04.bioformats2raw import BioFormats2RawAttrs from ome_zarr_models.v04.plate import ( @@ -10,9 +10,10 @@ ) -def test_bioformats2raw_exmaple_json(): - with open(Path(__file__).parent / "data" / "bioformats2raw_example.json") as f: - model = BioFormats2RawAttrs.model_validate_json(f.read()) +def test_bioformats2raw_exmaple_json() -> None: + model = read_in_json( + json_fname="bioformats2raw_example.json", model_cls=BioFormats2RawAttrs + ) assert model == BioFormats2RawAttrs( bioformats2raw_layout=3, diff --git a/tests/v04/test_omero.py b/tests/v04/test_omero.py index 25433e4..7355a8c 100644 --- a/tests/v04/test_omero.py +++ b/tests/v04/test_omero.py @@ -1,14 +1,12 @@ -import json -from pathlib import Path +from tests.v04.conftest import read_in_json from ome_zarr_models.v04.omero import Channel, Omero, Window -def test_load_example_json(): - with open(Path(__file__).parent / "data" / "omero_example.json") as f: - data = json.load(f) +def test_load_example_json() -> None: + model = read_in_json(json_fname="omero_example.json", model_cls=Omero) - assert Omero(**data) == Omero( + assert model == Omero( channels=[ Channel( color="0000FF", diff --git a/tests/v04/test_well.py b/tests/v04/test_well.py index e652b13..1145965 100644 --- a/tests/v04/test_well.py +++ b/tests/v04/test_well.py @@ -3,6 +3,7 @@ import pytest from pydantic import BaseModel +from tests.v04.conftest import read_in_json from ome_zarr_models.v04.well import Well, WellImage @@ -15,7 +16,7 @@ def check_against_json(json_path: Path, expected_model: BaseModel) -> None: @pytest.mark.parametrize( - ("filename", "model"), + ("filename", "model_expected"), [ ( "well_example_1.json", @@ -41,11 +42,9 @@ def check_against_json(json_path: Path, expected_model: BaseModel) -> None: ), ], ) -def test_examples_valid(filename: str, model: Well): - with open(Path(__file__).parent / "data" / filename) as f: - data = json.load(f) - - assert Well(**data["well"]) == model +def test_examples_valid(filename: str, model_expected: Well) -> None: + model = read_in_json(json_fname=filename, model_cls=Well) + assert model == model_expected def test_get_paths():