Skip to content

Commit

Permalink
Merge pull request #61 from BioImageTools/de-dupe-example-tests
Browse files Browse the repository at this point in the history
De-duplicate example JSON testing
  • Loading branch information
dstansby authored Nov 29, 2024
2 parents 9093464 + 01ce20b commit 1c9e4e3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 51 deletions.
11 changes: 10 additions & 1 deletion tests/v04/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,13 +9,21 @@
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 (
Dataset,
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,
Expand Down
40 changes: 19 additions & 21 deletions tests/v04/data/well_example_1.json
Original file line number Diff line number Diff line change
@@ -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"
}
24 changes: 11 additions & 13 deletions tests/v04/data/well_example_2.json
Original file line number Diff line number Diff line change
@@ -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"
}
9 changes: 5 additions & 4 deletions tests/v04/test_bioformats2raw.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions tests/v04/test_omero.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 5 additions & 6 deletions tests/v04/test_well.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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",
Expand All @@ -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():
Expand Down

0 comments on commit 1c9e4e3

Please sign in to comment.