|
3 | 3 | import json |
4 | 4 | import pathlib |
5 | 5 | import re |
| 6 | +from typing import TYPE_CHECKING |
6 | 7 |
|
7 | 8 | import numpy as np |
8 | 9 | import pytest |
9 | 10 |
|
10 | 11 | import zarr |
11 | 12 | from zarr import create_array |
12 | 13 | from zarr.core.buffer import Buffer, cpu |
13 | | -from zarr.core.buffer.core import BufferPrototype, default_buffer_prototype |
14 | 14 | from zarr.core.sync import sync |
15 | 15 | from zarr.storage import LocalStore |
16 | 16 | from zarr.storage._local import _atomic_write |
17 | 17 | from zarr.testing.store import StoreTests |
18 | 18 | from zarr.testing.utils import assert_bytes_equal |
19 | 19 |
|
| 20 | +if TYPE_CHECKING: |
| 21 | + from zarr.core.buffer import BufferPrototype |
| 22 | + |
20 | 23 |
|
21 | 24 | class TestLocalStore(StoreTests[LocalStore, cpu.Buffer]): |
22 | 25 | store_cls = LocalStore |
@@ -111,6 +114,54 @@ async def test_move( |
111 | 114 | ): |
112 | 115 | await store2.move(destination) |
113 | 116 |
|
| 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 | + |
114 | 165 |
|
115 | 166 | @pytest.mark.parametrize("exclusive", [True, False]) |
116 | 167 | 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: |
153 | 204 | f.write(b"abc") |
154 | 205 | assert path.read_bytes() == b"xyz" |
155 | 206 | 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