diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py new file mode 100644 index 0000000..d7efce2 --- /dev/null +++ b/tests/fixtures/__init__.py @@ -0,0 +1,3 @@ +from .paths import * +from .generation import * +from .models import * \ No newline at end of file diff --git a/tests/fixtures.py b/tests/fixtures/generation.py similarity index 55% rename from tests/fixtures.py rename to tests/fixtures/generation.py index b090c7d..a6dd6f6 100644 --- a/tests/fixtures.py +++ b/tests/fixtures/generation.py @@ -1,107 +1,15 @@ -import shutil -from pathlib import Path -from typing import Any, Callable, Optional, Tuple, Type, Union -from warnings import warn from datetime import datetime, timezone +from pathlib import Path +from typing import Callable, Tuple, Union +import cv2 import h5py import numpy as np import pytest -from pydantic import BaseModel, Field import zarr -import cv2 from numpydantic.interface.hdf5 import H5ArrayPath from numpydantic.interface.zarr import ZarrArrayPath -from numpydantic import NDArray, Shape -from numpydantic.maps import python_to_nptyping -from numpydantic.dtype import Number - - -@pytest.fixture(scope="session") -def tmp_output_dir(request: pytest.FixtureRequest) -> Path: - path = Path(__file__).parent.resolve() / "__tmp__" - if path.exists(): - shutil.rmtree(str(path)) - path.mkdir() - - yield path - - if not request.config.getvalue("--with-output"): - try: - shutil.rmtree(str(path)) - except PermissionError as e: - # sporadic error on windows machines... - warn( - f"Temporary directory could not be removed due to a permissions error: \n{str(e)}" - ) - - -@pytest.fixture(scope="function") -def tmp_output_dir_func(tmp_output_dir, request: pytest.FixtureRequest) -> Path: - """ - tmp output dir that gets cleared between every function - cleans at the start rather than at cleanup in case the output is to be inspected - """ - subpath = tmp_output_dir / f"__tmpfunc_{request.node.name}__" - if subpath.exists(): - shutil.rmtree(str(subpath)) - subpath.mkdir() - return subpath - - -@pytest.fixture(scope="module") -def tmp_output_dir_mod(tmp_output_dir, request: pytest.FixtureRequest) -> Path: - """ - tmp output dir that gets cleared between every function - cleans at the start rather than at cleanup in case the output is to be inspected - """ - subpath = tmp_output_dir / f"__tmpmod_{request.module}__" - if subpath.exists(): - shutil.rmtree(str(subpath)) - subpath.mkdir() - return subpath - - -@pytest.fixture(scope="function") -def array_model() -> ( - Callable[[Tuple[int, ...], Union[Type, np.dtype]], Type[BaseModel]] -): - def _model( - shape: Tuple[int, ...] = (10, 10), dtype: Union[Type, np.dtype] = float - ) -> Type[BaseModel]: - shape_str = ", ".join([str(s) for s in shape]) - - class MyModel(BaseModel): - array: NDArray[Shape[shape_str], dtype] - - return MyModel - - return _model - - -@pytest.fixture(scope="session") -def model_rgb() -> Type[BaseModel]: - class RGB(BaseModel): - array: Optional[ - Union[ - NDArray[Shape["* x, * y"], Number], - NDArray[Shape["* x, * y, 3 r_g_b"], Number], - NDArray[Shape["* x, * y, 3 r_g_b, 4 r_g_b_a"], Number], - ] - ] = Field(None) - - return RGB - - -@pytest.fixture(scope="session") -def model_blank() -> Type[BaseModel]: - """A model with any shape and dtype""" - - class BlankModel(BaseModel): - array: NDArray[Shape["*, ..."], Any] - - return BlankModel @pytest.fixture(scope="function") diff --git a/tests/fixtures/models.py b/tests/fixtures/models.py new file mode 100644 index 0000000..088c661 --- /dev/null +++ b/tests/fixtures/models.py @@ -0,0 +1,49 @@ +from typing import Callable, Tuple, Union, Type, Optional, Any + +import numpy as np +import pytest +from pydantic import BaseModel, Field + +from numpydantic import NDArray, Shape +from numpydantic.dtype import Number + + +@pytest.fixture(scope="function") +def array_model() -> ( + Callable[[Tuple[int, ...], Union[Type, np.dtype]], Type[BaseModel]] +): + def _model( + shape: Tuple[int, ...] = (10, 10), dtype: Union[Type, np.dtype] = float + ) -> Type[BaseModel]: + shape_str = ", ".join([str(s) for s in shape]) + + class MyModel(BaseModel): + array: NDArray[Shape[shape_str], dtype] + + return MyModel + + return _model + + +@pytest.fixture(scope="session") +def model_rgb() -> Type[BaseModel]: + class RGB(BaseModel): + array: Optional[ + Union[ + NDArray[Shape["* x, * y"], Number], + NDArray[Shape["* x, * y, 3 r_g_b"], Number], + NDArray[Shape["* x, * y, 3 r_g_b, 4 r_g_b_a"], Number], + ] + ] = Field(None) + + return RGB + + +@pytest.fixture(scope="session") +def model_blank() -> Type[BaseModel]: + """A model with any shape and dtype""" + + class BlankModel(BaseModel): + array: NDArray[Shape["*, ..."], Any] + + return BlankModel diff --git a/tests/fixtures/paths.py b/tests/fixtures/paths.py new file mode 100644 index 0000000..c3cab21 --- /dev/null +++ b/tests/fixtures/paths.py @@ -0,0 +1,50 @@ +import shutil +from _warnings import warn +from pathlib import Path + +import pytest + + +@pytest.fixture(scope="session") +def tmp_output_dir(request: pytest.FixtureRequest) -> Path: + path = Path(__file__).parents[1].resolve() / "__tmp__" + if path.exists(): + shutil.rmtree(str(path)) + path.mkdir() + + yield path + + if not request.config.getvalue("--with-output"): + try: + shutil.rmtree(str(path)) + except PermissionError as e: + # sporadic error on windows machines... + warn( + f"Temporary directory could not be removed due to a permissions error: \n{str(e)}" + ) + + +@pytest.fixture(scope="function") +def tmp_output_dir_func(tmp_output_dir, request: pytest.FixtureRequest) -> Path: + """ + tmp output dir that gets cleared between every function + cleans at the start rather than at cleanup in case the output is to be inspected + """ + subpath = tmp_output_dir / f"__tmpfunc_{request.node.name}__" + if subpath.exists(): + shutil.rmtree(str(subpath)) + subpath.mkdir() + return subpath + + +@pytest.fixture(scope="module") +def tmp_output_dir_mod(tmp_output_dir, request: pytest.FixtureRequest) -> Path: + """ + tmp output dir that gets cleared between every function + cleans at the start rather than at cleanup in case the output is to be inspected + """ + subpath = tmp_output_dir / f"__tmpmod_{request.module}__" + if subpath.exists(): + shutil.rmtree(str(subpath)) + subpath.mkdir() + return subpath