Skip to content

Commit bdc4ef8

Browse files
committed
refactor new test functions
1 parent 38ff517 commit bdc4ef8

File tree

1 file changed

+52
-70
lines changed

1 file changed

+52
-70
lines changed

tests/test_store/test_local.py

Lines changed: 52 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@
33
import json
44
import pathlib
55
import re
6+
from typing import TYPE_CHECKING
67

78
import numpy as np
89
import pytest
910

1011
import zarr
1112
from zarr import create_array
1213
from zarr.core.buffer import Buffer, cpu
13-
from zarr.core.buffer.core import BufferPrototype, default_buffer_prototype
1414
from zarr.core.sync import sync
1515
from zarr.storage import LocalStore
1616
from zarr.storage._local import _atomic_write
1717
from zarr.testing.store import StoreTests
1818
from zarr.testing.utils import assert_bytes_equal
1919

20+
if TYPE_CHECKING:
21+
from zarr.core.buffer import BufferPrototype
22+
2023

2124
class TestLocalStore(StoreTests[LocalStore, cpu.Buffer]):
2225
store_cls = LocalStore
@@ -111,6 +114,54 @@ async def test_move(
111114
):
112115
await store2.move(destination)
113116

117+
@pytest.mark.parametrize("buffer_cls", [None, cpu.buffer_prototype])
118+
async def test_get_bytes_with_prototype_none(
119+
self, store: LocalStore, buffer_cls: None | BufferPrototype
120+
) -> None:
121+
"""Test that get_bytes works with prototype=None."""
122+
data = b"hello world"
123+
key = "test_key"
124+
await self.set(store, key, self.buffer_cls.from_bytes(data))
125+
126+
result = await store.get_bytes(key, prototype=buffer_cls)
127+
assert result == data
128+
129+
@pytest.mark.parametrize("buffer_cls", [None, cpu.buffer_prototype])
130+
def test_get_bytes_sync_with_prototype_none(
131+
self, store: LocalStore, buffer_cls: None | BufferPrototype
132+
) -> None:
133+
"""Test that get_bytes_sync works with prototype=None."""
134+
data = b"hello world"
135+
key = "test_key"
136+
sync(self.set(store, key, self.buffer_cls.from_bytes(data)))
137+
138+
result = store.get_bytes_sync(key, prototype=buffer_cls)
139+
assert result == data
140+
141+
@pytest.mark.parametrize("buffer_cls", [None, cpu.buffer_prototype])
142+
async def test_get_json_with_prototype_none(
143+
self, store: LocalStore, buffer_cls: None | BufferPrototype
144+
) -> None:
145+
"""Test that get_json works with prototype=None."""
146+
data = {"foo": "bar", "number": 42}
147+
key = "test.json"
148+
await self.set(store, key, self.buffer_cls.from_bytes(json.dumps(data).encode()))
149+
150+
result = await store.get_json(key, prototype=buffer_cls)
151+
assert result == data
152+
153+
@pytest.mark.parametrize("buffer_cls", [None, cpu.buffer_prototype])
154+
def test_get_json_sync_with_prototype_none(
155+
self, store: LocalStore, buffer_cls: None | BufferPrototype
156+
) -> None:
157+
"""Test that get_json_sync works with prototype=None."""
158+
data = {"foo": "bar", "number": 42}
159+
key = "test.json"
160+
sync(self.set(store, key, self.buffer_cls.from_bytes(json.dumps(data).encode())))
161+
162+
result = store.get_json_sync(key, prototype=buffer_cls)
163+
assert result == data
164+
114165

115166
@pytest.mark.parametrize("exclusive", [True, False])
116167
def test_atomic_write_successful(tmp_path: pathlib.Path, exclusive: bool) -> None:
@@ -153,72 +204,3 @@ def test_atomic_write_exclusive_preexisting(tmp_path: pathlib.Path) -> None:
153204
f.write(b"abc")
154205
assert path.read_bytes() == b"xyz"
155206
assert list(path.parent.iterdir()) == [path] # no temp files
156-
157-
158-
async def test_get_bytes_with_prototype_none(tmp_path: pathlib.Path) -> None:
159-
"""Test that get_bytes works with prototype=None."""
160-
from zarr.core.buffer import cpu
161-
162-
store = await LocalStore.open(root=tmp_path)
163-
data = b"hello world"
164-
key = "test_key"
165-
await store.set(key, cpu.Buffer.from_bytes(data))
166-
167-
# Test with None (default)
168-
result_none = await store.get_bytes(key)
169-
assert result_none == data
170-
171-
# Test with explicit prototype
172-
result_proto = await store.get_bytes(key, prototype=default_buffer_prototype())
173-
assert result_proto == data
174-
175-
176-
def test_get_bytes_sync_with_prototype_none(tmp_path: pathlib.Path) -> None:
177-
"""Test that get_bytes_sync works with prototype=None."""
178-
from zarr.core.buffer import cpu
179-
from zarr.core.sync import sync
180-
181-
store = sync(LocalStore.open(root=tmp_path))
182-
data = b"hello world"
183-
key = "test_key"
184-
sync(store.set(key, cpu.Buffer.from_bytes(data)))
185-
186-
# Test with None (default)
187-
result_none = store.get_bytes_sync(key)
188-
assert result_none == data
189-
190-
# Test with explicit prototype
191-
result_proto = store.get_bytes_sync(key, prototype=default_buffer_prototype())
192-
assert result_proto == data
193-
194-
195-
@pytest.mark.parametrize("buffer_cls", [None, cpu.buffer_prototype])
196-
async def test_get_json_with_prototype_none(
197-
tmp_path: pathlib.Path, buffer_cls: None | BufferPrototype
198-
) -> None:
199-
"""Test that get_json works with prototype=None."""
200-
201-
store = await LocalStore.open(root=tmp_path)
202-
data = {"foo": "bar", "number": 42}
203-
key = "test.json"
204-
await store.set(key, cpu.Buffer.from_bytes(json.dumps(data).encode()))
205-
206-
# Test with None (default)
207-
result = await store.get_json(key, prototype=buffer_cls)
208-
assert result == data
209-
210-
211-
@pytest.mark.parametrize("buffer_cls", [None, cpu.buffer_prototype])
212-
def test_get_json_sync_with_prototype(
213-
tmp_path: pathlib.Path, buffer_cls: None | BufferPrototype
214-
) -> None:
215-
"""Test that get_json_sync works with prototype=None."""
216-
217-
store = sync(LocalStore.open(root=tmp_path))
218-
data = {"foo": "bar", "number": 42}
219-
key = "test.json"
220-
sync(store.set(key, cpu.Buffer.from_bytes(json.dumps(data).encode())))
221-
222-
# Test with None (default)
223-
result = store.get_json_sync(key, prototype=buffer_cls)
224-
assert result == data

0 commit comments

Comments
 (0)