-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
test_dim_separator.py
136 lines (111 loc) · 4.12 KB
/
test_dim_separator.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
import pathlib
import pytest
from numpy.testing import assert_array_equal
from functools import partial
import zarr
from zarr.core import Array
from zarr.storage import DirectoryStore, NestedDirectoryStore, FSStore
from zarr.tests.util import have_fsspec
needs_fsspec = pytest.mark.skipif(not have_fsspec, reason="needs fsspec")
@pytest.fixture(
params=(
"static_flat",
"static_flat_legacy",
"static_nested",
"static_nested_legacy",
"directory_nested",
"directory_flat",
"directory_default",
"nesteddirectory_nested",
"nesteddirectory_default",
pytest.param("fs_nested", marks=needs_fsspec),
pytest.param("fs_flat", marks=needs_fsspec),
pytest.param("fs_default", marks=needs_fsspec),
)
)
def dataset(tmpdir, request):
"""
Generate a variety of different Zarrs using
different store implementations as well as
different dimension_separator arguments.
"""
loc = tmpdir.join("dim_sep_test.zarr")
which = request.param
kwargs = {}
if which.startswith("static"):
project_root = pathlib.Path(zarr.__file__).resolve().parent.parent
suffix = which[len("static_") :]
static = project_root / "fixture" / suffix
if not static.exists(): # pragma: no cover
if "nested" in which:
# No way to reproduce the nested_legacy file via code
generator = NestedDirectoryStore
else:
if "legacy" in suffix:
# No dimension_separator metadata included
generator = DirectoryStore
else:
# Explicit dimension_separator metadata included
generator = partial(DirectoryStore, dimension_separator=".")
# store the data - should be one-time operation
s = generator(str(static))
a = zarr.open(store=s, mode="w", shape=(2, 2), dtype="<i8")
a[:] = [[1, 2], [3, 4]]
return str(static)
if which.startswith("directory"):
store_class = DirectoryStore
elif which.startswith("nested"):
store_class = NestedDirectoryStore
else:
store_class = FSStore
kwargs["mode"] = "w"
kwargs["auto_mkdir"] = True
if which.endswith("nested"):
kwargs["dimension_separator"] = "/"
elif which.endswith("flat"):
kwargs["dimension_separator"] = "."
store = store_class(str(loc), **kwargs)
zarr.creation.array(store=store, data=[[1, 2], [3, 4]])
return str(loc)
def verify(array, expect_failure=False):
try:
assert_array_equal(array[:], [[1, 2], [3, 4]])
except AssertionError:
if expect_failure:
pytest.xfail()
else:
raise # pragma: no cover
def test_open(dataset):
"""
Use zarr.open to open the dataset fixture. Legacy nested datasets
without the dimension_separator metadata are not expected to be
openable.
"""
failure = "nested_legacy" in dataset
verify(zarr.open(dataset, "r"), failure)
@needs_fsspec
def test_fsstore(dataset):
"""
Use FSStore to open the dataset fixture. Legacy nested datasets
without the dimension_separator metadata are not expected to be
openable.
"""
failure = "nested_legacy" in dataset
verify(Array(store=FSStore(dataset)), failure)
def test_directory(dataset):
"""
Use DirectoryStore to open the dataset fixture. Legacy nested datasets
without the dimension_separator metadata are not expected to be
openable.
"""
failure = "nested_legacy" in dataset
verify(zarr.Array(store=DirectoryStore(dataset)), failure)
def test_nested(dataset):
"""
Use NestedDirectoryStore to open the dataset fixture. This is the only
method that is expected to successfully open legacy nested datasets
without the dimension_separator metadata. However, for none-Nested
datasets without any metadata, NestedDirectoryStore will fail.
"""
failure = "flat_legacy" in dataset or "directory_default" in dataset or "fs_default" in dataset
verify(Array(store=NestedDirectoryStore(dataset)), failure)