Skip to content

Commit

Permalink
Enable stateful tests for ZipStore
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Feb 6, 2025
1 parent a52048d commit 800266d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/zarr/testing/stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def add_array(
# self.model.rename(from_group, new_path)
# self.repo.store.rename(from_group, new_path)

@precondition(lambda self: self.store.supports_deletes)
@precondition(lambda self: len(self.all_arrays) >= 1)
@rule(data=st.data())
def delete_array_using_del(self, data: DataObject) -> None:
Expand All @@ -149,6 +150,7 @@ def delete_array_using_del(self, data: DataObject) -> None:
del group[array_name]
self.all_arrays.remove(array_path)

@precondition(lambda self: self.store.supports_deletes)
@precondition(lambda self: len(self.all_groups) >= 2) # fixme don't delete root
@rule(data=st.data())
def delete_group_using_del(self, data: DataObject) -> None:
Expand Down Expand Up @@ -284,6 +286,10 @@ def supports_partial_writes(self) -> bool:
def supports_writes(self) -> bool:
return self.store.supports_writes

@property
def supports_deletes(self) -> bool:
return self.store.supports_deletes

Check warning on line 291 in src/zarr/testing/stateful.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/testing/stateful.py#L291

Added line #L291 was not covered by tests


class ZarrStoreStateMachine(RuleBasedStateMachine):
""" "
Expand Down Expand Up @@ -366,6 +372,7 @@ def get_partial_values(self, data: DataObject) -> None:
model_vals_ls,
)

@precondition(lambda self: self.store.supports_deletes)
@precondition(lambda self: len(self.model.keys()) > 0)
@rule(data=st.data())
def delete(self, data: DataObject) -> None:
Expand Down
11 changes: 8 additions & 3 deletions src/zarr/testing/strategies.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Any

import hypothesis.extra.numpy as npst
Expand Down Expand Up @@ -209,7 +210,7 @@ def basic_indices(draw: st.DrawFn, *, shape: tuple[int], **kwargs: Any) -> Any:


def key_ranges(
keys: SearchStrategy = node_names, max_size: int | None = None
keys: SearchStrategy = node_names, max_size: int = sys.maxsize
) -> SearchStrategy[list[int]]:
"""
Function to generate key_ranges strategy for get_partial_values()
Expand All @@ -218,10 +219,14 @@ def key_ranges(
[(key, (range_start, range_end)),
(key, (range_start, range_end)),...]
"""

def make_request(start: int, length: int) -> RangeByteRequest:
return RangeByteRequest(start, end=min(start + length, max_size))

Check warning on line 224 in src/zarr/testing/strategies.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/testing/strategies.py#L223-L224

Added lines #L223 - L224 were not covered by tests

byte_ranges = st.builds(
RangeByteRequest,
make_request,
start=st.integers(min_value=0, max_value=max_size),
end=st.integers(min_value=0, max_value=max_size),
length=st.integers(min_value=0, max_value=max_size),
)
key_tuple = st.tuples(keys, byte_ranges)
return st.lists(key_tuple, min_size=1, max_size=10)
20 changes: 2 additions & 18 deletions tests/test_store/test_stateful.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
# Stateful tests for arbitrary Zarr stores.
import pytest
from hypothesis.stateful import (
Settings,
run_state_machine_as_test,
)

from zarr.abc.store import Store
from zarr.storage import LocalStore, MemoryStore, ZipStore
from zarr.testing.stateful import ZarrHierarchyStateMachine, ZarrStoreStateMachine


def test_zarr_hierarchy(sync_store: Store):
def mk_test_instance_sync() -> ZarrHierarchyStateMachine:
return ZarrHierarchyStateMachine(sync_store)

if isinstance(sync_store, ZipStore):
pytest.skip(reason="ZipStore does not support delete")
if isinstance(sync_store, MemoryStore):
run_state_machine_as_test(
mk_test_instance_sync, settings=Settings(report_multiple_bugs=False, max_examples=50)
)
run_state_machine_as_test(mk_test_instance_sync, settings=Settings(max_examples=50))


def test_zarr_store(sync_store: Store) -> None:
def mk_test_instance_sync() -> None:
return ZarrStoreStateMachine(sync_store)

if isinstance(sync_store, ZipStore):
pytest.skip(reason="ZipStore does not support delete")
elif isinstance(sync_store, LocalStore):
pytest.skip(reason="This test has errors")
elif isinstance(sync_store, MemoryStore):
run_state_machine_as_test(mk_test_instance_sync, settings=Settings(max_examples=50))
else:
run_state_machine_as_test(
mk_test_instance_sync, settings=Settings(report_multiple_bugs=True)
)
run_state_machine_as_test(mk_test_instance_sync, settings=Settings(max_examples=50))

0 comments on commit 800266d

Please sign in to comment.