Skip to content

Commit aa11df4

Browse files
committed
wip: json schema test
1 parent 9673997 commit aa11df4

File tree

2 files changed

+28
-43
lines changed

2 files changed

+28
-43
lines changed

tests/test_dtype/conftest.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class TestB(TestExample):
5353
param_a = [1, 2, 100, 10]
5454
5555
"""
56+
# Iterate over all the fixtures defined in the class
57+
# and parametrize them with the values defined in the class
58+
# This allows us to define class-scoped fixtures as class attributes
59+
# and then generate the parametrize calls for pytest
5660
for fixture_name in metafunc.fixturenames:
5761
if hasattr(metafunc.cls, fixture_name):
58-
metafunc.parametrize(fixture_name, getattr(metafunc.cls, fixture_name), scope="class")
62+
params = getattr(metafunc.cls, fixture_name)
63+
if len(params) == 0:
64+
msg = f"{metafunc.cls}.{fixture_name} is empty. Please provide a non-empty sequence of values."
65+
raise ValueError(msg)
66+
metafunc.parametrize(fixture_name, params, scope="class")

tests/test_dtype/test_wrapper.py

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55
if TYPE_CHECKING:
66
from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar, ZDType
77

8+
import pytest
9+
import requests
10+
11+
12+
class _TestZDTypeSchema:
13+
# subclasses define the URL for the schema, if available
14+
schema_url: ClassVar[str] = ""
15+
16+
@pytest.fixture(scope="class")
17+
def get_schema(self) -> object:
18+
response = requests.get(self.schema_url)
19+
response.raise_for_status()
20+
return json_schema.loads(response.text)
21+
22+
def test_schema(self, schema: json_schema.Schema) -> None:
23+
assert schema.is_valid(self.test_cls.to_json(zarr_format=2))
24+
825

926
class _TestZDType:
1027
test_cls: type[ZDType[TBaseDType, TBaseScalar]]
@@ -47,50 +64,10 @@ def test_scalar_roundtrip_v2(self, scalar_v2_params: Any) -> None:
4764
dtype_json, scalar_json = scalar_v2_params
4865
zdtype = self.test_cls.from_json(dtype_json, zarr_format=2)
4966
scalar = zdtype.from_json_value(scalar_json, zarr_format=2)
50-
assert self._scalar_equals(scalar_json, zdtype.to_json_value(scalar, zarr_format=2))
67+
assert scalar_json == zdtype.to_json_value(scalar, zarr_format=2)
5168

5269
def test_scalar_roundtrip_v3(self, scalar_v3_params: Any) -> None:
5370
dtype_json, scalar_json = scalar_v3_params
5471
zdtype = self.test_cls.from_json(dtype_json, zarr_format=3)
5572
scalar = zdtype.from_json_value(scalar_json, zarr_format=3)
56-
assert self._scalar_equals(scalar_json, zdtype.to_json_value(scalar, zarr_format=3))
57-
58-
@staticmethod
59-
def _scalar_equals(a: object, b: object) -> bool:
60-
"""
61-
Compare two scalars for equality. Subclasses that test dtypes with scalars that don't allow
62-
simple equality like nans should override this method.
63-
"""
64-
return a == b
65-
66-
""" @abc.abstractmethod
67-
def test_cast_value(self, value: Any) -> None:
68-
raise NotImplementedError
69-
70-
@abc.abstractmethod
71-
def test_check_value(self) -> None:
72-
raise NotImplementedError
73-
74-
@abc.abstractmethod
75-
def test_default_value(self) -> None:
76-
raise NotImplementedError
77-
78-
@abc.abstractmethod
79-
def test_check_json(self, value: Any) -> None:
80-
raise NotImplementedError
81-
82-
@abc.abstractmethod
83-
def test_from_json_roundtrip_v2(self, value: Any) -> None:
84-
raise NotImplementedError
85-
86-
@abc.abstractmethod
87-
def test_from_json_roundtrip_v3(self, value: Any) -> None:
88-
raise NotImplementedError
89-
90-
@abc.abstractmethod
91-
def test_from_json_value_roundtrip_v2(self, value: Any) -> None:
92-
raise NotImplementedError
93-
94-
@abc.abstractmethod
95-
def test_from_json_value_roundtrip_v3(self, value: Any) -> None:
96-
raise NotImplementedError """
73+
assert scalar_json == zdtype.to_json_value(scalar, zarr_format=3)

0 commit comments

Comments
 (0)