|
1 | 1 | import tempfile |
2 | 2 | from pathlib import Path |
| 3 | +from typing import Literal |
3 | 4 |
|
4 | 5 | import pytest |
| 6 | +from _pytest.compat import LEGACY_PATH |
5 | 7 | from upath import UPath |
6 | 8 |
|
7 | 9 | from zarr.storage._utils import normalize_path |
|
11 | 13 | from zarr.storage.remote import RemoteStore |
12 | 14 |
|
13 | 15 |
|
14 | | -async def test_make_store_path(tmpdir: str) -> None: |
15 | | - # None |
16 | | - store_path = await make_store_path(None) |
| 16 | +@pytest.mark.parametrize("path", [None, "", "bar"]) |
| 17 | +async def test_make_store_path_none(path: str) -> None: |
| 18 | + """ |
| 19 | + Test that creating a store_path with None creates a memorystore |
| 20 | + """ |
| 21 | + store_path = await make_store_path(None, path=path) |
17 | 22 | assert isinstance(store_path.store, MemoryStore) |
18 | | - |
19 | | - # str |
20 | | - store_path = await make_store_path(str(tmpdir)) |
| 23 | + assert store_path.path == normalize_path(path) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize("path", [None, "", "bar"]) |
| 27 | +@pytest.mark.parametrize("store_type", [str, Path, LocalStore]) |
| 28 | +@pytest.mark.parametrize("mode", ["r", "w", "a"]) |
| 29 | +async def test_make_store_path_local( |
| 30 | + tmpdir: LEGACY_PATH, |
| 31 | + store_type: type[str] | type[Path] | type[LocalStore], |
| 32 | + path: str, |
| 33 | + mode: Literal["r", "w", "a"], |
| 34 | +) -> None: |
| 35 | + """ |
| 36 | + Test the various ways of invoking make_store_path that create a LocalStore |
| 37 | + """ |
| 38 | + store_like = store_type(str(tmpdir)) |
| 39 | + store_path = await make_store_path(store_like, path=path, mode=mode) |
21 | 40 | assert isinstance(store_path.store, LocalStore) |
22 | 41 | assert Path(store_path.store.root) == Path(tmpdir) |
23 | | - |
24 | | - # Path |
25 | | - store_path = await make_store_path(Path(tmpdir)) |
| 42 | + assert store_path.path == normalize_path(path) |
| 43 | + assert store_path.store.mode.str == mode |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize("path", [None, "", "bar"]) |
| 47 | +@pytest.mark.parametrize("mode", ["r", "w", "a"]) |
| 48 | +async def test_make_store_path_store_path( |
| 49 | + tmpdir: LEGACY_PATH, path: str, mode: Literal["r", "w", "a"] |
| 50 | +) -> None: |
| 51 | + """ |
| 52 | + Test invoking make_store_path when the input is another store_path. In particular we want to ensure |
| 53 | + that a new path is handled correctly. |
| 54 | + """ |
| 55 | + store_like = StorePath(LocalStore(str(tmpdir)), path="root") |
| 56 | + store_path = await make_store_path(store_like, path=path, mode=mode) |
26 | 57 | assert isinstance(store_path.store, LocalStore) |
27 | 58 | assert Path(store_path.store.root) == Path(tmpdir) |
| 59 | + path_normalized = normalize_path(path) |
| 60 | + assert store_path.path == (store_like / path_normalized).path |
28 | 61 |
|
29 | | - # Store |
30 | | - store_path = await make_store_path(store_path.store) |
31 | | - assert isinstance(store_path.store, LocalStore) |
32 | | - assert Path(store_path.store.root) == Path(tmpdir) |
| 62 | + assert store_path.store.mode.str == mode |
33 | 63 |
|
34 | | - # StorePath |
35 | | - store_path = await make_store_path(store_path) |
36 | | - assert isinstance(store_path.store, LocalStore) |
37 | | - assert Path(store_path.store.root) == Path(tmpdir) |
38 | 64 |
|
| 65 | +async def test_make_store_path_invalid() -> None: |
| 66 | + """ |
| 67 | + Test that invalid types raise TypeError |
| 68 | + """ |
39 | 69 | with pytest.raises(TypeError): |
40 | 70 | await make_store_path(1) # type: ignore[arg-type] |
41 | 71 |
|
|
0 commit comments