Skip to content

Commit 9def17d

Browse files
committed
add mutability altering funcs
1 parent da03f87 commit 9def17d

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@
3535
test_bucket_name = "test"
3636
secure_bucket_name = "test-secure"
3737

38+
def as_mutable(store: Store) -> Store:
39+
"""
40+
Return a mutable version of the store
41+
"""
42+
if isinstance(store, LocalStore):
43+
return LocalStore(store.root, read_only=False)
44+
if isinstance(store, MemoryStore):
45+
return MemoryStore(store._store_dict, read_only=False)
46+
if isinstance(store, RemoteStore):
47+
return RemoteStore(fs=store.fs, path=store.path, read_only=False)
48+
if isinstance(store, ZipStore):
49+
store.close()
50+
return sync(ZipStore.open(path=store.path, read_only=False))
51+
raise ValueError(f'Unknown store type: {type(store)}')
52+
53+
def as_immutable(store: Store) -> Store:
54+
"""
55+
Return an immutable version of the store
56+
"""
57+
if isinstance(store, LocalStore):
58+
return LocalStore(store.root, read_only=True)
59+
if isinstance(store, MemoryStore):
60+
return MemoryStore(store._store_dict, read_only=True)
61+
if isinstance(store, RemoteStore):
62+
return RemoteStore(fs=store.fs, path=store.path, read_only=True)
63+
if isinstance(store, ZipStore):
64+
store.close()
65+
return sync(ZipStore.open(path=store.path, read_only=True))
66+
raise ValueError(f'Unknown store type: {type(store)}')
3867

3968
async def parse_store(
4069
store: str,

tests/test_api.py

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66
from numpy.testing import assert_array_equal
77

8+
from tests.conftest import as_immutable
89
import zarr
910
import zarr.api.asynchronous
1011
import zarr.core.group
@@ -70,29 +71,13 @@ async def test_open_array(store: Store, zarr_format: ZarrFormat) -> None:
7071
assert isinstance(z, Array)
7172
assert z.shape == (100,)
7273

73-
store_w: Store
7474

75-
if isinstance(store, ZipStore):
76-
store.close()
77-
store_w = await ZipStore.open(store.path, mode="w")
78-
else:
79-
# open array, overwrite
80-
store_w = store.with_mode("w")
81-
82-
z = open(store=store_w, shape=200, zarr_format=zarr_format)
75+
z = open(store=store, shape=200, zarr_format=zarr_format, mode='w')
8376
assert isinstance(z, Array)
8477
assert z.shape == (200,)
8578

86-
store_r: Store
87-
88-
if isinstance(store, ZipStore):
89-
store_w.close()
90-
store_r = await ZipStore.open(store.path, mode="r")
91-
else:
92-
# open array, read-only
93-
store_r = store.with_mode("r")
94-
95-
z = open(store=store_r, zarr_format=zarr_format)
79+
store_r = as_immutable(store)
80+
z = open(store=store_r, zarr_format=zarr_format, mode='r')
9681
assert isinstance(z, Array)
9782
assert z.shape == (200,)
9883
assert z.read_only
@@ -125,15 +110,7 @@ async def test_open_group(store: Store) -> None:
125110
# g = open_group(store=store)
126111
# assert isinstance(g, Group)
127112
# assert "foo" not in g
128-
store_r: Store
129-
130-
# open group, read-only
131-
if isinstance(store, ZipStore):
132-
store.close()
133-
store_r = await ZipStore.open(store.path, mode="r")
134-
else:
135-
# open array, read-only
136-
store_r = store.with_mode("r")
113+
store_r = as_immutable(store)
137114

138115
g = open_group(store=store_r)
139116
assert isinstance(g, Group)
@@ -1059,13 +1036,14 @@ def test_tree() -> None:
10591036
# copy(source["foo"], dest, dry_run=True, log=True)
10601037

10611038

1062-
def test_open_positional_args_deprecated() -> None:
1063-
store = MemoryStore()
1039+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
1040+
def test_open_positional_args_deprecated(store: MemoryStore) -> None:
10641041
with pytest.warns(FutureWarning, match="pass"):
10651042
open(store, "w", shape=(1,))
10661043

10671044

1068-
def test_save_array_positional_args_deprecated() -> None:
1045+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
1046+
def test_save_array_positional_args_deprecated(store: MemoryStore) -> None:
10691047
store = MemoryStore()
10701048
with warnings.catch_warnings():
10711049
warnings.filterwarnings(
@@ -1081,14 +1059,14 @@ def test_save_array_positional_args_deprecated() -> None:
10811059
)
10821060

10831061

1084-
def test_group_positional_args_deprecated() -> None:
1085-
store = MemoryStore()
1062+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
1063+
def test_group_positional_args_deprecated(store: MemoryStore) -> None:
10861064
with pytest.warns(FutureWarning, match="pass"):
10871065
group(store, True)
10881066

10891067

1090-
def test_open_group_positional_args_deprecated() -> None:
1091-
store = MemoryStore()
1068+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
1069+
def test_open_group_positional_args_deprecated(store: MemoryStore) -> None:
10921070
with pytest.warns(FutureWarning, match="pass"):
10931071
open_group(store, "w")
10941072

0 commit comments

Comments
 (0)