Skip to content

Commit 08fc138

Browse files
committed
remove uses of pathlib walk, as it didn't exist in python 3.11
1 parent 46e958d commit 08fc138

File tree

1 file changed

+62
-52
lines changed

1 file changed

+62
-52
lines changed

tests/test_metadata/test_converter_v2_v3.py

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,57 @@ def expected_paths_no_metadata() -> list[Path]:
7979

8080

8181
@pytest.fixture
82-
def expected_paths_v3_metadata(expected_paths_no_metadata: list[Path]) -> list[Path]:
83-
"""Expected paths from create_nested_zarr, with v3 metadata files"""
84-
v3_paths = [
85-
Path("array_0/zarr.json"),
86-
Path("group_1/array_1/zarr.json"),
87-
Path("group_1/group_2/array_2/zarr.json"),
88-
Path("zarr.json"),
89-
Path("group_1/zarr.json"),
90-
Path("group_1/group_2/zarr.json"),
91-
]
92-
expected_paths_no_metadata.extend(v3_paths)
82+
def expected_v3_metadata() -> list[Path]:
83+
"""Expected v3 metadata for create_nested_zarr"""
84+
return sorted(
85+
[
86+
Path("array_0/zarr.json"),
87+
Path("group_1/array_1/zarr.json"),
88+
Path("group_1/group_2/array_2/zarr.json"),
89+
Path("zarr.json"),
90+
Path("group_1/zarr.json"),
91+
Path("group_1/group_2/zarr.json"),
92+
]
93+
)
94+
95+
96+
@pytest.fixture
97+
def expected_v2_metadata() -> list[Path]:
98+
"""Expected v2 metadata for create_nested_zarr"""
99+
return sorted(
100+
[
101+
Path("array_0/.zarray"),
102+
Path("array_0/.zattrs"),
103+
Path("group_1/array_1/.zarray"),
104+
Path("group_1/array_1/.zattrs"),
105+
Path("group_1/group_2/array_2/.zarray"),
106+
Path("group_1/group_2/array_2/.zattrs"),
107+
Path(".zgroup"),
108+
Path(".zattrs"),
109+
Path("group_1/.zgroup"),
110+
Path("group_1/.zattrs"),
111+
Path("group_1/group_2/.zgroup"),
112+
Path("group_1/group_2/.zattrs"),
113+
]
114+
)
115+
116+
117+
@pytest.fixture
118+
def expected_paths_v3_metadata(
119+
expected_paths_no_metadata: list[Path], expected_v3_metadata: list[Path]
120+
) -> list[Path]:
121+
"""Expected paths from create_nested_zarr + v3 metadata files"""
122+
expected_paths_no_metadata.extend(expected_v3_metadata)
93123

94124
return sorted(expected_paths_no_metadata)
95125

96126

97127
@pytest.fixture
98-
def expected_paths_v2_metadata(expected_paths_no_metadata: list[Path]) -> list[Path]:
99-
"""Expected paths from create_nested_zarr, with v2 metadata files"""
100-
v2_paths = [
101-
Path("array_0/.zarray"),
102-
Path("array_0/.zattrs"),
103-
Path("group_1/array_1/.zarray"),
104-
Path("group_1/array_1/.zattrs"),
105-
Path("group_1/group_2/array_2/.zarray"),
106-
Path("group_1/group_2/array_2/.zattrs"),
107-
Path(".zgroup"),
108-
Path(".zattrs"),
109-
Path("group_1/.zgroup"),
110-
Path("group_1/.zattrs"),
111-
Path("group_1/group_2/.zgroup"),
112-
Path("group_1/group_2/.zattrs"),
113-
]
114-
expected_paths_no_metadata.extend(v2_paths)
128+
def expected_paths_v2_metadata(
129+
expected_paths_no_metadata: list[Path], expected_v2_metadata: list[Path]
130+
) -> list[Path]:
131+
"""Expected paths from create_nested_zarr + v2 metadata files"""
132+
expected_paths_no_metadata.extend(expected_v2_metadata)
115133

116134
return sorted(expected_paths_no_metadata)
117135

@@ -174,7 +192,9 @@ def test_convert_group(local_store: Store) -> None:
174192

175193

176194
@pytest.mark.parametrize("separator", [".", "/"])
177-
def test_convert_nested_groups_and_arrays(local_store: Store, separator: str) -> None:
195+
def test_convert_nested_groups_and_arrays(
196+
local_store: Store, separator: str, expected_v3_metadata: list[Path]
197+
) -> None:
178198
"""Test that zarr.json are made at the correct points in a hierarchy of groups and arrays
179199
(including when there are additional dirs due to using a / separator)"""
180200

@@ -184,17 +204,9 @@ def test_convert_nested_groups_and_arrays(local_store: Store, separator: str) ->
184204
result = runner.invoke(cli.app, ["convert", str(local_store.root)])
185205
assert result.exit_code == 0
186206

187-
# check zarr.json were created for every group and array
188-
total_zarr_jsons = 0
189-
for _, _, filenames in local_store.root.walk():
190-
# group / array directories
191-
if ".zattrs" in filenames:
192-
assert "zarr.json" in filenames
193-
total_zarr_jsons += 1
194-
# other directories e.g. for chunks when separator is /
195-
else:
196-
assert "zarr.json" not in filenames
197-
assert total_zarr_jsons == 6
207+
zarr_json_paths = sorted(local_store.root.rglob("zarr.json"))
208+
expected_zarr_json_paths = [local_store.root / p for p in expected_v3_metadata]
209+
assert zarr_json_paths == expected_zarr_json_paths
198210

199211
# Check converted zarr can be opened + metadata accessed at all levels
200212
zarr_array = zarr.open(local_store.root, zarr_format=3)
@@ -206,7 +218,9 @@ def test_convert_nested_groups_and_arrays(local_store: Store, separator: str) ->
206218

207219

208220
@pytest.mark.parametrize("separator", [".", "/"])
209-
def test_convert_nested_with_path(local_store: Store, separator: str) -> None:
221+
def test_convert_nested_with_path(
222+
local_store: Store, separator: str, expected_v3_metadata: list[Path]
223+
) -> None:
210224
"""Test that only arrays/groups within group_1 are converted (+ no other files in store)"""
211225

212226
create_nested_zarr(local_store, {}, separator)
@@ -216,17 +230,13 @@ def test_convert_nested_with_path(local_store: Store, separator: str) -> None:
216230

217231
group_path = local_store.root / "group_1"
218232

219-
total_zarr_jsons = 0
220-
for dirpath, _, filenames in local_store.root.walk():
221-
inside_group = (dirpath == group_path) or (group_path in dirpath.parents)
222-
if (".zattrs" in filenames) and inside_group:
223-
# group / array directories inside the group
224-
assert "zarr.json" in filenames
225-
total_zarr_jsons += 1
226-
else:
227-
assert "zarr.json" not in filenames
228-
229-
assert total_zarr_jsons == 4
233+
zarr_json_paths = sorted(local_store.root.rglob("zarr.json"))
234+
expected_zarr_json_paths = [
235+
local_store.root / p
236+
for p in expected_v3_metadata
237+
if group_path in (local_store.root / p).parents
238+
]
239+
assert zarr_json_paths == expected_zarr_json_paths
230240

231241

232242
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)