|
| 1 | +import pytest |
| 2 | + |
| 3 | +from qs_codec.models.key_path_node import KeyPathNode |
| 4 | + |
| 5 | + |
| 6 | +class TestKeyPathNode: |
| 7 | + def test_append_empty_segment_returns_same_node(self) -> None: |
| 8 | + root = KeyPathNode.from_materialized("root") |
| 9 | + assert root.append("") is root |
| 10 | + |
| 11 | + def test_materialize_builds_nested_paths(self) -> None: |
| 12 | + path = KeyPathNode.from_materialized("a").append("[b]").append("[c]") |
| 13 | + assert path.materialize() == "a[b][c]" |
| 14 | + |
| 15 | + def test_materialize_uses_cached_value(self) -> None: |
| 16 | + path = KeyPathNode.from_materialized("a").append("[b]").append("[c]") |
| 17 | + first = path.materialize() |
| 18 | + second = path.materialize() |
| 19 | + assert first == second == "a[b][c]" |
| 20 | + |
| 21 | + def test_as_dot_encoded_replaces_literal_dots(self) -> None: |
| 22 | + path = KeyPathNode.from_materialized("a.b").append("[c.d]") |
| 23 | + encoded = path.as_dot_encoded() |
| 24 | + assert encoded.materialize() == "a%2Eb[c%2Ed]" |
| 25 | + |
| 26 | + def test_as_dot_encoded_reuses_cached_node(self) -> None: |
| 27 | + path = KeyPathNode.from_materialized("a.b").append("[c]") |
| 28 | + first = path.as_dot_encoded() |
| 29 | + second = path.as_dot_encoded() |
| 30 | + assert first is second |
| 31 | + |
| 32 | + def test_as_dot_encoded_returns_self_when_no_segments_need_encoding(self) -> None: |
| 33 | + path = KeyPathNode.from_materialized("a").append("[c]") |
| 34 | + assert path.as_dot_encoded() is path |
| 35 | + |
| 36 | + def test_as_dot_encoded_handles_deep_paths_without_recursion_error(self) -> None: |
| 37 | + path = KeyPathNode.from_materialized("root") |
| 38 | + for i in range(12_000): |
| 39 | + path = path.append(f".k{i}") |
| 40 | + |
| 41 | + encoded = path.as_dot_encoded().materialize() |
| 42 | + assert encoded.startswith("root%2Ek0%2Ek1") |
| 43 | + |
| 44 | + @pytest.mark.parametrize( |
| 45 | + "path, expected", |
| 46 | + [ |
| 47 | + (KeyPathNode.from_materialized("a"), "a"), |
| 48 | + (KeyPathNode.from_materialized("a").append(".b"), "a.b"), |
| 49 | + (KeyPathNode.from_materialized("a").append("[0]").append("[b]"), "a[0][b]"), |
| 50 | + ], |
| 51 | + ) |
| 52 | + def test_materialize_path_variants(self, path: KeyPathNode, expected: str) -> None: |
| 53 | + assert path.materialize() == expected |
0 commit comments