|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, Any, ClassVar |
| 4 | + |
| 5 | +if TYPE_CHECKING: |
| 6 | + from zarr.abc.codec import Codec |
| 7 | + from zarr.core.common import CodecJSON_V2, CodecJSON_V3 |
| 8 | + |
| 9 | + |
| 10 | +class BaseTestCodec: |
| 11 | + """ |
| 12 | + A base class for testing codec classes. |
| 13 | +
|
| 14 | + Attributes |
| 15 | + ---------- |
| 16 | +
|
| 17 | + test_cls : type[Codec] |
| 18 | + The codec class being tested |
| 19 | + valid_json_v2 : ClassVar[tuple[CodecJSON_V2, ...]] |
| 20 | + A tuple of valid JSON representations for Zarr format version 2. |
| 21 | + valid_json_v2 : ClassVar[tuple[CodecJSON_V2, ...]] |
| 22 | + A tuple of valid JSON representations for Zarr format version 2. |
| 23 | + """ |
| 24 | + |
| 25 | + test_cls: type[Codec] |
| 26 | + valid_json_v2: ClassVar[tuple[CodecJSON_V2, ...]] |
| 27 | + valid_json_v3: ClassVar[tuple[CodecJSON_V3, ...]] |
| 28 | + |
| 29 | + def test_from_json_roundtrip_v2(self, valid_json_v2: CodecJSON_V2) -> None: |
| 30 | + codec = self.test_cls.from_json(valid_json_v2) |
| 31 | + assert codec.to_json(zarr_format=2) == valid_json_v2 |
| 32 | + |
| 33 | + def test_from_json_roundtrip_v3(self, valid_json_v3: CodecJSON_V3) -> None: |
| 34 | + codec = self.test_cls.from_json(valid_json_v3) |
| 35 | + assert codec.to_json(zarr_format=3) == valid_json_v3 |
| 36 | + |
| 37 | + |
| 38 | +def pytest_generate_tests(metafunc: Any) -> None: |
| 39 | + """ |
| 40 | + This is a pytest hook to parametrize class-scoped fixtures. |
| 41 | +
|
| 42 | + This hook allows us to define class-scoped fixtures as class attributes and then |
| 43 | + generate the parametrize calls for pytest. This allows the fixtures to be |
| 44 | + reused across multiple tests within the same class. |
| 45 | +
|
| 46 | + For example, if you had a regular pytest class like this: |
| 47 | +
|
| 48 | + class TestClass: |
| 49 | + @pytest.mark.parametrize("param_a", [1, 2, 3]) |
| 50 | + def test_method(self, param_a): |
| 51 | + ... |
| 52 | +
|
| 53 | + Child classes inheriting from ``TestClass`` would not be able to override the ``param_a`` fixture |
| 54 | +
|
| 55 | + this implementation of ``pytest_generate_tests`` allows you to define class-scoped fixtures as |
| 56 | + class attributes, which allows the following to work: |
| 57 | +
|
| 58 | + class TestExample: |
| 59 | + param_a = [1, 2, 3] |
| 60 | +
|
| 61 | + def test_example(self, param_a): |
| 62 | + ... |
| 63 | +
|
| 64 | + # this class will have its test_example method parametrized with the values of TestB.param_a |
| 65 | + class TestB(TestExample): |
| 66 | + param_a = [1, 2, 100, 10] |
| 67 | +
|
| 68 | + """ |
| 69 | + # Iterate over all the fixtures defined in the class |
| 70 | + # and parametrize them with the values defined in the class |
| 71 | + # This allows us to define class-scoped fixtures as class attributes |
| 72 | + # and then generate the parametrize calls for pytest |
| 73 | + for fixture_name in metafunc.fixturenames: |
| 74 | + if hasattr(metafunc.cls, fixture_name): |
| 75 | + params = getattr(metafunc.cls, fixture_name) |
| 76 | + # Only parametrize if params is a tuple or list, not a function/method |
| 77 | + if isinstance(params, (tuple, list)): |
| 78 | + metafunc.parametrize(fixture_name, params, scope="class", ids=str) |
0 commit comments