|
5 | 5 | import pytest |
6 | 6 | from numpy.testing import assert_array_equal |
7 | 7 |
|
8 | | -from tests.conftest import as_immutable |
9 | 8 | import zarr |
10 | 9 | import zarr.api.asynchronous |
11 | 10 | import zarr.core.group |
| 11 | +from tests.conftest import as_immutable |
12 | 12 | from zarr import Array, Group |
13 | 13 | from zarr.abc.store import Store |
14 | 14 | from zarr.api.synchronous import ( |
@@ -64,26 +64,30 @@ def test_open_normalized_path( |
64 | 64 | assert node.path == normalize_path(path) |
65 | 65 |
|
66 | 66 |
|
67 | | -@pytest.mark.parametrize("store", ["local", "memory", "remote", "zip"], indirect=True) |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "store", |
| 69 | + ["local", "memory", "remote", pytest.param("zip", marks=pytest.mark.xfail)], |
| 70 | + indirect=True, |
| 71 | +) |
68 | 72 | async def test_open_array(store: Store, zarr_format: ZarrFormat) -> None: |
69 | 73 | # open array, create if doesn't exist |
70 | 74 | z = open(store=store, shape=100, zarr_format=zarr_format) |
71 | 75 | assert isinstance(z, Array) |
72 | 76 | assert z.shape == (100,) |
73 | 77 |
|
74 | | - |
75 | | - z = open(store=store, shape=200, zarr_format=zarr_format, mode='w') |
| 78 | + # invoke open again, with a different shape and mode w. |
| 79 | + # We expect the store to be wiped at the current path and new array to come out. |
| 80 | + z = open(store=store, shape=200, zarr_format=zarr_format, mode="w") |
76 | 81 | assert isinstance(z, Array) |
77 | 82 | assert z.shape == (200,) |
78 | 83 |
|
79 | 84 | store_r = as_immutable(store) |
80 | | - z = open(store=store_r, zarr_format=zarr_format, mode='r') |
| 85 | + z = open(store=store_r, zarr_format=zarr_format, mode="r") |
81 | 86 | assert isinstance(z, Array) |
82 | 87 | assert z.shape == (200,) |
83 | 88 | assert z.read_only |
84 | 89 |
|
85 | 90 |
|
86 | | -# zipstore is marked as xfail because you cannot open a zipstore in read-only mode if it doesn't exist in the first place. |
87 | 91 | @pytest.mark.parametrize( |
88 | 92 | "store", |
89 | 93 | ["local", "memory", "remote", "zip"], |
@@ -112,7 +116,7 @@ async def test_open_group(store: Store) -> None: |
112 | 116 | # assert "foo" not in g |
113 | 117 | store_r = as_immutable(store) |
114 | 118 |
|
115 | | - g = open_group(store=store_r) |
| 119 | + g = open_group(store=store_r, mode="r") |
116 | 120 | assert isinstance(g, Group) |
117 | 121 |
|
118 | 122 | if isinstance(store, ZipStore): |
@@ -1103,3 +1107,19 @@ async def test_metadata_validation_error() -> None: |
1103 | 1107 | match="Invalid value for 'zarr_format'. Expected '2, 3, or None'. Got '3.0'.", |
1104 | 1108 | ): |
1105 | 1109 | await zarr.api.asynchronous.open_array(shape=(1,), zarr_format="3.0") # type: ignore[arg-type] |
| 1110 | + |
| 1111 | + |
| 1112 | +@pytest.mark.parametrize( |
| 1113 | + "store", |
| 1114 | + ["local", "memory", "remote", "zip"], |
| 1115 | + indirect=True, |
| 1116 | +) |
| 1117 | +def test_open_array_with_mode_r_plus(store: Store) -> None: |
| 1118 | + # 'r+' means read/write (must exist) |
| 1119 | + with pytest.raises(FileNotFoundError): |
| 1120 | + zarr.open_array(store=store, mode="r+") |
| 1121 | + zarr.ones(store=store, shape=(3, 3)) |
| 1122 | + z2 = zarr.open(store=store, mode="r+") |
| 1123 | + assert isinstance(z2, Array) |
| 1124 | + assert (z2[:] == 1).all() |
| 1125 | + z2[:] = 3 |
0 commit comments