|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from zarr.core.buffer import default_buffer_prototype |
| 6 | +from zarr.storage import MemoryStore |
| 7 | +from zarr.testing.store import LatencyStore |
| 8 | + |
| 9 | + |
| 10 | +async def test_latency_store_with_read_only_round_trip() -> None: |
| 11 | + """ |
| 12 | + Ensure that LatencyStore.with_read_only returns another LatencyStore with |
| 13 | + the requested read_only state, preserves latency configuration, and does |
| 14 | + not change the original wrapper. |
| 15 | + """ |
| 16 | + base = await MemoryStore.open() |
| 17 | + # Start from a read-only underlying store |
| 18 | + ro_base = base.with_read_only(read_only=True) |
| 19 | + latency_ro = LatencyStore(ro_base, get_latency=0.01, set_latency=0.02) |
| 20 | + |
| 21 | + assert latency_ro.read_only |
| 22 | + assert latency_ro.get_latency == pytest.approx(0.01) |
| 23 | + assert latency_ro.set_latency == pytest.approx(0.02) |
| 24 | + |
| 25 | + buf = default_buffer_prototype().buffer.from_bytes(b"abcd") |
| 26 | + |
| 27 | + # Cannot write through the read-only wrapper |
| 28 | + with pytest.raises( |
| 29 | + ValueError, match="store was opened in read-only mode and does not support writing" |
| 30 | + ): |
| 31 | + await latency_ro.set("key", buf) |
| 32 | + |
| 33 | + # Create a writable wrapper from the read-only one |
| 34 | + writer = latency_ro.with_read_only(read_only=False) |
| 35 | + assert isinstance(writer, LatencyStore) |
| 36 | + assert not writer.read_only |
| 37 | + # Latency configuration is preserved |
| 38 | + assert writer.get_latency == latency_ro.get_latency |
| 39 | + assert writer.set_latency == latency_ro.set_latency |
| 40 | + |
| 41 | + # Writes via the writable wrapper succeed |
| 42 | + await writer.set("key", buf) |
| 43 | + out = await writer.get("key", prototype=default_buffer_prototype()) |
| 44 | + assert out is not None |
| 45 | + assert out.to_bytes() == buf.to_bytes() |
| 46 | + |
| 47 | + # Creating a read-only copy from the writable wrapper works and is enforced |
| 48 | + reader = writer.with_read_only(read_only=True) |
| 49 | + assert isinstance(reader, LatencyStore) |
| 50 | + assert reader.read_only |
| 51 | + with pytest.raises( |
| 52 | + ValueError, match="store was opened in read-only mode and does not support writing" |
| 53 | + ): |
| 54 | + await reader.set("other", buf) |
| 55 | + |
| 56 | + # The original read-only wrapper remains read-only |
| 57 | + assert latency_ro.read_only |
0 commit comments