Skip to content

Commit c0c5b75

Browse files
committed
test: Add missing tests for _dereference_path
Also, change the `ExpectFail` helper class to require the `msg` parameter be set. Allowing a default value of `None` will make all `pytest.raises(case.exception, match=case.msg)` checks pass, which is not the intended behavior.
1 parent e2132e2 commit c0c5b75

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class ExpectFail[TIn]:
7474
input: TIn
7575
exception: type[Exception]
7676
id: str
77-
msg: str | None = None
77+
msg: str
7878

7979

8080
async def parse_store(

tests/test_metadata/test_v3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def test_array_metadata_roundtrip(case: Expect[dict[str, Any], dict[str, Any]])
225225
ExpectFail(
226226
input={"data_type": "uint8", "fill_value": {}},
227227
exception=TypeError,
228+
msg=".*",
228229
id="invalid_fill_value_type",
229230
),
230231
],

tests/test_store/test_utils.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
import pytest
77

8-
from zarr.storage._utils import ParsedStoreUrl, parse_store_url
8+
from tests.conftest import Expect, ExpectFail
9+
from zarr.storage._utils import ParsedStoreUrl, _dereference_path, parse_store_url
910

1011

1112
class TestParseStoreUrl:
@@ -95,3 +96,47 @@ def test_drive_letter_not_special_on_non_windows(self, url: str) -> None:
9596
result = parse_store_url(url)
9697
# urlparse interprets the drive letter as a scheme
9798
assert result.scheme == "c"
99+
100+
101+
@pytest.mark.parametrize(
102+
"case",
103+
[
104+
Expect(input=("root", "path"), output="root/path", id="basic"),
105+
Expect(input=("root", ""), output="root", id="empty_path"),
106+
Expect(input=("", "path"), output="path", id="empty_root"),
107+
Expect(input=("", ""), output="", id="both_empty"),
108+
Expect(input=("root/", "path"), output="root/path", id="root_trailing_slash"),
109+
Expect(input=("root//", "path"), output="root/path", id="root_multiple_trailing_slashes"),
110+
Expect(input=("root", "path/"), output="root/path", id="path_trailing_slash"),
111+
Expect(input=("a/b", "c/d"), output="a/b/c/d", id="nested"),
112+
Expect(input=("memory://store", "key"), output="memory://store/key", id="url_root"),
113+
],
114+
ids=lambda case: case.id,
115+
)
116+
def test_dereference_path(case: Expect[tuple[str, str], str]) -> None:
117+
"""
118+
Test the normal behavior of _dereference_path
119+
"""
120+
root, path = case.input
121+
assert _dereference_path(root, path) == case.output
122+
123+
124+
@pytest.mark.parametrize(
125+
"case",
126+
[
127+
ExpectFail(
128+
input=(1, "path"), exception=TypeError, msg="root=.*not a string", id="root_not_str"
129+
),
130+
ExpectFail(
131+
input=("root", 1), exception=TypeError, msg="path=.*not a string", id="path_not_str"
132+
),
133+
],
134+
ids=lambda case: case.id,
135+
)
136+
def test_dereference_path_errors(case: ExpectFail[tuple[object, object]]) -> None:
137+
"""
138+
Test that _dereference_path raises TypeError for non-string inputs.
139+
"""
140+
root, path = case.input
141+
with pytest.raises(case.exception, match=case.msg):
142+
_dereference_path(root, path) # type: ignore[arg-type]

0 commit comments

Comments
 (0)