forked from zarr-developers/zarr-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
179 lines (132 loc) · 4.86 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from __future__ import annotations
import pathlib
from dataclasses import dataclass, field
from typing import TYPE_CHECKING
import numpy as np
import numpy.typing as npt
import pytest
from hypothesis import HealthCheck, Verbosity, settings
from zarr import AsyncGroup, config
from zarr.abc.store import Store
from zarr.core.sync import sync
from zarr.storage import FsspecStore, LocalStore, MemoryStore, StorePath, ZipStore
if TYPE_CHECKING:
from collections.abc import Generator
from typing import Any, Literal
from _pytest.compat import LEGACY_PATH
from zarr.core.common import ChunkCoords, MemoryOrder, ZarrFormat
async def parse_store(
store: Literal["local", "memory", "fsspec", "zip"], path: str
) -> LocalStore | MemoryStore | FsspecStore | ZipStore:
if store == "local":
return await LocalStore.open(path)
if store == "memory":
return await MemoryStore.open()
if store == "fsspec":
return await FsspecStore.open(url=path)
if store == "zip":
return await ZipStore.open(path + "/zarr.zip", mode="w")
raise AssertionError
@pytest.fixture(params=[str, pathlib.Path])
def path_type(request: pytest.FixtureRequest) -> Any:
return request.param
# todo: harmonize this with local_store fixture
@pytest.fixture
async def store_path(tmpdir: LEGACY_PATH) -> StorePath:
store = await LocalStore.open(str(tmpdir))
return StorePath(store)
@pytest.fixture
async def local_store(tmpdir: LEGACY_PATH) -> LocalStore:
return await LocalStore.open(str(tmpdir))
@pytest.fixture
async def remote_store(url: str) -> FsspecStore:
return await FsspecStore.open(url)
@pytest.fixture
async def memory_store() -> MemoryStore:
return await MemoryStore.open()
@pytest.fixture
async def zip_store(tmpdir: LEGACY_PATH) -> ZipStore:
return await ZipStore.open(str(tmpdir / "zarr.zip"), mode="w")
@pytest.fixture
async def store(request: pytest.FixtureRequest, tmpdir: LEGACY_PATH) -> Store:
param = request.param
return await parse_store(param, str(tmpdir))
@pytest.fixture(params=["local", "memory", "zip"])
def sync_store(request: pytest.FixtureRequest, tmp_path: LEGACY_PATH) -> Store:
result = sync(parse_store(request.param, str(tmp_path)))
if not isinstance(result, Store):
raise TypeError("Wrong store class returned by test fixture! got " + result + " instead")
return result
@dataclass
class AsyncGroupRequest:
zarr_format: ZarrFormat
store: Literal["local", "fsspec", "memory", "zip"]
attributes: dict[str, Any] = field(default_factory=dict)
@pytest.fixture
async def async_group(request: pytest.FixtureRequest, tmpdir: LEGACY_PATH) -> AsyncGroup:
param: AsyncGroupRequest = request.param
store = await parse_store(param.store, str(tmpdir))
return await AsyncGroup.from_store(
store,
attributes=param.attributes,
zarr_format=param.zarr_format,
overwrite=False,
)
@pytest.fixture(params=["numpy", "cupy"])
def xp(request: pytest.FixtureRequest) -> Any:
"""Fixture to parametrize over numpy-like libraries"""
if request.param == "cupy":
request.node.add_marker(pytest.mark.gpu)
return pytest.importorskip(request.param)
@pytest.fixture(autouse=True)
def reset_config() -> Generator[None, None, None]:
config.reset()
yield
config.reset()
@dataclass
class ArrayRequest:
shape: ChunkCoords
dtype: str
order: MemoryOrder
@pytest.fixture
def array_fixture(request: pytest.FixtureRequest) -> npt.NDArray[Any]:
array_request: ArrayRequest = request.param
return (
np.arange(np.prod(array_request.shape))
.reshape(array_request.shape, order=array_request.order)
.astype(array_request.dtype)
)
@pytest.fixture(params=(2, 3), ids=["zarr2", "zarr3"])
def zarr_format(request: pytest.FixtureRequest) -> ZarrFormat:
if request.param == 2:
return 2
elif request.param == 3:
return 3
msg = f"Invalid zarr format requested. Got {request.param}, expected on of (2,3)."
raise ValueError(msg)
def pytest_addoption(parser: Any) -> None:
parser.addoption(
"--run-slow-hypothesis",
action="store_true",
default=False,
help="run slow hypothesis tests",
)
def pytest_collection_modifyitems(config: Any, items: Any) -> None:
if config.getoption("--run-slow-hypothesis"):
return
skip_slow_hyp = pytest.mark.skip(reason="need --run-slow-hypothesis option to run")
for item in items:
if "slow_hypothesis" in item.keywords:
item.add_marker(skip_slow_hyp)
settings.register_profile(
"ci",
max_examples=1000,
deadline=None,
suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow],
)
settings.register_profile(
"local",
max_examples=300,
suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow],
verbosity=Verbosity.verbose,
)