@@ -149,6 +149,58 @@ async def test_read_only_store_raises(self, open_kwargs: dict[str, Any]) -> None
149
149
):
150
150
await store .delete ("foo" )
151
151
152
+ async def test_with_read_only_store (self , open_kwargs : dict [str , Any ]) -> None :
153
+ kwargs = {** open_kwargs , "read_only" : True }
154
+ store = await self .store_cls .open (** kwargs )
155
+ assert store .read_only
156
+
157
+ # Test that you cannot write to a read-only store
158
+ with pytest .raises (
159
+ ValueError , match = "store was opened in read-only mode and does not support writing"
160
+ ):
161
+ await store .set ("foo" , self .buffer_cls .from_bytes (b"bar" ))
162
+
163
+ # Check if the store implements with_read_only
164
+ try :
165
+ writer = store .with_read_only (read_only = False )
166
+ except NotImplementedError :
167
+ # Test that stores that do not implement with_read_only raise NotImplementedError with the correct message
168
+ with pytest .raises (
169
+ NotImplementedError ,
170
+ match = f"with_read_only is not implemented for the { type (store )} store type." ,
171
+ ):
172
+ store .with_read_only (read_only = False )
173
+ return
174
+
175
+ # Test that you can write to a new store copy
176
+ assert not writer ._is_open
177
+ assert not writer .read_only
178
+ await writer .set ("foo" , self .buffer_cls .from_bytes (b"bar" ))
179
+ await writer .delete ("foo" )
180
+
181
+ # Test that you cannot write to the original store
182
+ assert store .read_only
183
+ with pytest .raises (
184
+ ValueError , match = "store was opened in read-only mode and does not support writing"
185
+ ):
186
+ await store .set ("foo" , self .buffer_cls .from_bytes (b"bar" ))
187
+ with pytest .raises (
188
+ ValueError , match = "store was opened in read-only mode and does not support writing"
189
+ ):
190
+ await store .delete ("foo" )
191
+
192
+ # Test that you cannot write to a read-only store copy
193
+ reader = store .with_read_only (read_only = True )
194
+ assert reader .read_only
195
+ with pytest .raises (
196
+ ValueError , match = "store was opened in read-only mode and does not support writing"
197
+ ):
198
+ await reader .set ("foo" , self .buffer_cls .from_bytes (b"bar" ))
199
+ with pytest .raises (
200
+ ValueError , match = "store was opened in read-only mode and does not support writing"
201
+ ):
202
+ await reader .delete ("foo" )
203
+
152
204
@pytest .mark .parametrize ("key" , ["c/0" , "foo/c/0.0" , "foo/0/0" ])
153
205
@pytest .mark .parametrize (
154
206
("data" , "byte_range" ),
0 commit comments