numpydantic/tests/test_interface/conftest.py

97 lines
2.5 KiB
Python
Raw Normal View History

import inspect
from typing import Callable, Tuple, Type
2024-10-04 02:57:54 +00:00
import pytest
from pydantic import BaseModel
2024-04-09 01:36:47 +00:00
2024-10-04 02:57:54 +00:00
from numpydantic import NDArray, interface
from numpydantic.testing.helpers import InterfaceCase
from numpydantic.testing.interfaces import (
DaskCase,
HDF5Case,
NumpyCase,
VideoCase,
ZarrCase,
ZarrDirCase,
ZarrNestedCase,
)
2024-04-09 01:36:47 +00:00
@pytest.fixture(
scope="function",
params=[
2024-09-23 20:28:38 +00:00
pytest.param(
([[1, 2], [3, 4]], interface.NumpyInterface),
marks=pytest.mark.numpy,
id="numpy-list",
),
pytest.param(
(NumpyCase, interface.NumpyInterface),
2024-09-23 20:28:38 +00:00
marks=pytest.mark.numpy,
id="numpy",
),
pytest.param(
(HDF5Case, interface.H5Interface),
2024-09-23 20:28:38 +00:00
marks=pytest.mark.hdf5,
id="h5-array-path",
),
pytest.param(
(DaskCase, interface.DaskInterface),
2024-09-23 20:28:38 +00:00
marks=pytest.mark.dask,
id="dask",
),
pytest.param(
(ZarrCase, interface.ZarrInterface),
2024-09-23 20:28:38 +00:00
marks=pytest.mark.zarr,
id="zarr-memory",
),
pytest.param(
(ZarrNestedCase, interface.ZarrInterface),
2024-09-23 20:28:38 +00:00
marks=pytest.mark.zarr,
id="zarr-nested",
),
pytest.param(
(ZarrDirCase, interface.ZarrInterface),
2024-09-23 20:28:38 +00:00
marks=pytest.mark.zarr,
id="zarr-dir",
2024-09-23 20:28:38 +00:00
),
pytest.param(
(VideoCase, interface.VideoInterface), marks=pytest.mark.video, id="video"
2024-09-23 20:28:38 +00:00
),
2024-04-09 01:36:47 +00:00
],
)
def interface_type(
request, tmp_output_dir_func
) -> Tuple[NDArray, Type[interface.Interface]]:
"""
Test cases for each interface's ``check`` method - each input should match the
provided interface and that interface only
"""
if inspect.isclass(request.param[0]) and issubclass(
request.param[0], InterfaceCase
):
array = request.param[0].make_array(path=tmp_output_dir_func)
if array is None:
pytest.skip()
return array, request.param[1]
2024-05-09 05:06:41 +00:00
else:
return request.param
@pytest.fixture()
def all_interfaces(interface_type) -> BaseModel:
"""
An instantiated version of each interface within a basemodel,
with the array in an `array` field
"""
array, interface = interface_type
if isinstance(array, Callable):
array = array()
class MyModel(BaseModel):
array: NDArray
instance = MyModel(array=array)
return instance