Skip to content

Commit 83c97ac

Browse files
committed
Consolidate test
1 parent 9756165 commit 83c97ac

2 files changed

Lines changed: 45 additions & 42 deletions

File tree

src/zarr/testing/store.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,53 @@ async def test_read_only_store_raises(self, open_kwargs: dict[str, Any]) -> None
152152
async def test_with_read_only_store(self, open_kwargs: dict[str, Any]) -> None:
153153
kwargs = {**open_kwargs, "read_only": True}
154154
store = await self.store_cls.open(**kwargs)
155+
assert store.read_only
156+
157+
# Test that you cannot write to a read-only store
155158
with pytest.raises(
156-
NotImplementedError, match="with_read_only is not implemented for this store type."
159+
ValueError, match="store was opened in read-only mode and does not support writing"
157160
):
158-
store.with_read_only(read_only=False)
161+
await store.set("foo", self.buffer_cls.from_bytes(b"bar"))
162+
163+
# Check if the store implements with_read_only
164+
try:
165+
writer = store.with_read_only(read_only=False)
166+
167+
# Test that you can write to a new store copy
168+
assert not writer._is_open
169+
assert not writer.read_only
170+
await writer.set("foo", self.buffer_cls.from_bytes(b"bar"))
171+
await writer.delete("foo")
172+
173+
# Test that you cannot write to the original store
174+
assert store.read_only
175+
with pytest.raises(
176+
ValueError, match="store was opened in read-only mode and does not support writing"
177+
):
178+
await store.set("foo", self.buffer_cls.from_bytes(b"bar"))
179+
with pytest.raises(
180+
ValueError, match="store was opened in read-only mode and does not support writing"
181+
):
182+
await store.delete("foo")
183+
184+
# Test that you cannot write to a read-only store copy
185+
reader = store.with_read_only(read_only=True)
186+
assert reader.read_only
187+
with pytest.raises(
188+
ValueError, match="store was opened in read-only mode and does not support writing"
189+
):
190+
await reader.set("foo", self.buffer_cls.from_bytes(b"bar"))
191+
with pytest.raises(
192+
ValueError, match="store was opened in read-only mode and does not support writing"
193+
):
194+
await reader.delete("foo")
195+
196+
except NotImplementedError:
197+
# Test that stores that do not implement with_read_only raise NotImplementedError with the correct message
198+
with pytest.raises(
199+
NotImplementedError, match="with_read_only is not implemented for this store type."
200+
):
201+
store.with_read_only(read_only=False)
159202

160203
@pytest.mark.parametrize("key", ["c/0", "foo/c/0.0", "foo/0/0"])
161204
@pytest.mark.parametrize(

tests/test_store/test_memory.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,46 +78,6 @@ async def test_deterministic_size(
7878
np.testing.assert_array_equal(a[:3], 1)
7979
np.testing.assert_array_equal(a[3:], 0)
8080

81-
async def test_with_read_only_store(self, open_kwargs: dict[str, Any]) -> None:
82-
kwargs = {**open_kwargs, "read_only": True}
83-
store = await self.store_cls.open(**kwargs)
84-
assert store.read_only
85-
86-
# Test that you cannot write to a read-only store
87-
with pytest.raises(
88-
ValueError, match="store was opened in read-only mode and does not support writing"
89-
):
90-
await store.set("foo", self.buffer_cls.from_bytes(b"bar"))
91-
92-
# Test that you can write to a copy that is not read-only
93-
writer = store.with_read_only(read_only=False)
94-
assert not writer._is_open
95-
assert not writer.read_only
96-
await writer.set("foo", self.buffer_cls.from_bytes(b"bar"))
97-
await writer.delete("foo")
98-
99-
# Test that you cannot write to the original store
100-
assert store.read_only
101-
with pytest.raises(
102-
ValueError, match="store was opened in read-only mode and does not support writing"
103-
):
104-
await store.set("foo", self.buffer_cls.from_bytes(b"bar"))
105-
with pytest.raises(
106-
ValueError, match="store was opened in read-only mode and does not support writing"
107-
):
108-
await store.delete("foo")
109-
# Test that you cannot write to a read-only store copy
110-
reader = store.with_read_only(read_only=True)
111-
assert reader.read_only
112-
with pytest.raises(
113-
ValueError, match="store was opened in read-only mode and does not support writing"
114-
):
115-
await reader.set("foo", self.buffer_cls.from_bytes(b"bar"))
116-
with pytest.raises(
117-
ValueError, match="store was opened in read-only mode and does not support writing"
118-
):
119-
await reader.delete("foo")
120-
12181

12282
# TODO: fix this warning
12383
@pytest.mark.filterwarnings("ignore:Unclosed client session:ResourceWarning")

0 commit comments

Comments
 (0)