@@ -442,23 +442,46 @@ async def test_list(self, store: S) -> None:
442442 async def test_list_prefix (self , store : S ) -> None :
443443 """
444444 Test that the `list_prefix` method works as intended. Given a prefix, it should return
445- all the keys in storage that start with this prefix.
445+ all the keys under that prefix, treating the prefix as a directory path .
446446 """
447- prefixes = ("" , "a/" , "a/b/" , "a/b/c/" )
448447 data = self .buffer_cls .from_bytes (b"" )
449- fname = "zarr.json"
450- store_dict = {p + fname : data for p in prefixes }
451-
448+ store_dict = {
449+ "zarr.json" : data ,
450+ "a/zarr.json" : data ,
451+ "a/b/zarr.json" : data ,
452+ "a/b/c/zarr.json" : data ,
453+ "a_extra/zarr.json" : data ,
454+ }
452455 await store ._set_many (store_dict .items ())
456+ all_keys = sorted (store_dict .keys ())
457+
458+ a_keys = ["a/b/c/zarr.json" , "a/b/zarr.json" , "a/zarr.json" ]
459+ ab_keys = ["a/b/c/zarr.json" , "a/b/zarr.json" ]
460+
461+ # query prefix -> expected keys
462+ test_cases : dict [str , list [str ]] = {
463+ # empty prefix returns everything
464+ "" : all_keys ,
465+ # with trailing /
466+ "a/" : a_keys ,
467+ "a/b/" : ab_keys ,
468+ "a/b/c/" : ["a/b/c/zarr.json" ],
469+ "a_extra/" : ["a_extra/zarr.json" ],
470+ # without trailing / should behave the same as with /
471+ "a" : a_keys ,
472+ "a/b" : ab_keys ,
473+ "a/b/c" : ["a/b/c/zarr.json" ],
474+ "a_extra" : ["a_extra/zarr.json" ],
475+ # partial prefix that doesn't match any directory
476+ "a_e" : [],
477+ # prefix that doesn't match anything
478+ "b" : [],
479+ "b/" : [],
480+ }
453481
454- for prefix in prefixes :
455- observed = tuple (sorted (await _collect_aiterator (store .list_prefix (prefix ))))
456- expected : tuple [str , ...] = ()
457- for key in store_dict :
458- if key .startswith (prefix ):
459- expected += (key ,)
460- expected = tuple (sorted (expected ))
461- assert observed == expected
482+ for prefix , expected in test_cases .items ():
483+ observed = sorted (await _collect_aiterator (store .list_prefix (prefix )))
484+ assert observed == expected , f"list_prefix({ prefix !r} ): { observed } != { expected } "
462485
463486 async def test_list_empty_path (self , store : S ) -> None :
464487 """
0 commit comments