@@ -152,10 +152,53 @@ async def test_read_only_store_raises(self, open_kwargs: dict[str, Any]) -> None
152152 async def test_with_read_only_store (self , open_kwargs : dict [str , Any ]) -> None :
153153 kwargs = {** open_kwargs , "read_only" : True }
154154 store = await self .store_cls .open (** kwargs )
155+ assert store .read_only
156+
157+ # Test that you cannot write to a read-only store
155158 with pytest .raises (
156- NotImplementedError , match = "with_read_only is not implemented for this store type. "
159+ ValueError , match = "store was opened in read-only mode and does not support writing "
157160 ):
158- store .with_read_only (read_only = False )
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+
167+ # Test that you can write to a new store copy
168+ assert not writer ._is_open
169+ assert not writer .read_only
170+ await writer .set ("foo" , self .buffer_cls .from_bytes (b"bar" ))
171+ await writer .delete ("foo" )
172+
173+ # Test that you cannot write to the original store
174+ assert store .read_only
175+ with pytest .raises (
176+ ValueError , match = "store was opened in read-only mode and does not support writing"
177+ ):
178+ await store .set ("foo" , self .buffer_cls .from_bytes (b"bar" ))
179+ with pytest .raises (
180+ ValueError , match = "store was opened in read-only mode and does not support writing"
181+ ):
182+ await store .delete ("foo" )
183+
184+ # Test that you cannot write to a read-only store copy
185+ reader = store .with_read_only (read_only = True )
186+ assert reader .read_only
187+ with pytest .raises (
188+ ValueError , match = "store was opened in read-only mode and does not support writing"
189+ ):
190+ await reader .set ("foo" , self .buffer_cls .from_bytes (b"bar" ))
191+ with pytest .raises (
192+ ValueError , match = "store was opened in read-only mode and does not support writing"
193+ ):
194+ await reader .delete ("foo" )
195+
196+ except NotImplementedError :
197+ # Test that stores that do not implement with_read_only raise NotImplementedError with the correct message
198+ with pytest .raises (
199+ NotImplementedError , match = "with_read_only is not implemented for this store type."
200+ ):
201+ store .with_read_only (read_only = False )
159202
160203 @pytest .mark .parametrize ("key" , ["c/0" , "foo/c/0.0" , "foo/0/0" ])
161204 @pytest .mark .parametrize (
0 commit comments