Skip to content

Commit

Permalink
remove value comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Sep 25, 2024
1 parent b5399eb commit b9c940b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 51 deletions.
33 changes: 7 additions & 26 deletions src/jsonyx/_manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""JSON manipulator."""
# TODO(Nice Zombies): add error messages
# TODO(Nice Zombies): raise JSONSyntaxError
# TODO(Nice Zombies): remove value comparison
# TODO(Nice Zombies): update schema ID
from __future__ import annotations

Expand Down Expand Up @@ -260,32 +259,14 @@ def _run_filter_query(
if match := _match_whitespace(query, end):
end = match.end()

if query[end:end + 1] != "@":
value, end = self._scan_query_value(query, end)
nodes = [
node
for node, (filter_target, filter_key) in filtered_pairs
if operator(
filter_target[filter_key], value, # type: ignore
)
]
else:
nodes = [node for node, _filter_node in filtered_pairs]
filter2_nodes, end = self._run_select_query(
nodes, query, end, mapping=True, relative=True,
value, end = self._scan_query_value(query, end)
nodes = [
node
for node, (filter_target, filter_key) in filtered_pairs
if operator(
filter_target[filter_key], value, # type: ignore
)
nodes = [
node
for (
(node, (filter_target, filter_key)),
(filter2_target, filter2_key),
) in zip(filtered_pairs, filter2_nodes, strict=True)
if _has_key(filter2_target, filter2_key) and operator(
filter_target[filter_key], # type: ignore
filter2_target[filter2_key], # type: ignore
)
]

]
old_end = end
if match := _match_whitespace(query, end):
end = match.end()
Expand Down
28 changes: 3 additions & 25 deletions src/jsonyx/test/test_run_filter_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,6 @@ def test_operator_whitespace(query: str) -> None:
assert not run_filter_query([], query)


@pytest.mark.parametrize(("obj", "keep"), [
# Missing first key
({"b": 0}, False),
# Missing second key
({"a": 0}, False),
# Both keys present
({"a": 0, "b": 1}, False), # Different value
({"a": 0, "b": 0}, True), # Same value
])
def test_value_comparison(
obj: dict[Any, Any], keep: bool, # noqa: FBT001
) -> None:
"""Test comparison of two values."""
expected: list[_Node] = [([obj], 0)] if keep else []
assert run_filter_query(([obj], 0), "@.a == @.b") == expected


def test_and() -> None:
"""Test and."""
query: str = "@ != 1 && @ != 2 && @ != 3"
Expand All @@ -134,10 +115,8 @@ def test_whitespace(query: str) -> None:
("!", "Expecting a relative query", 2, -1),
("@?", "Optional marker is not allowed", 2, 3),
("@ == ", "Expecting value", 6, -1),
("@ == $", "Expecting value", 6, -1),
("@ == @?", "Optional marker is not allowed", 7, 8),
("@ && ", "Expecting a relative query", 6, -1),
("!@ == @", "Unexpected operator", 4, 6),
("!@ == 0", "Unexpected operator", 4, 6),
("@ @ @", "Expecting end of file", 2, -1),
])
def test_invalid_query(
Expand All @@ -150,8 +129,7 @@ def test_invalid_query(
check_syntax_err(exc_info, msg, colno, end_colno)


@pytest.mark.parametrize("query", ["@[:]", "@ == @[:]"])
def test_slice(query: str) -> None:
def test_slice() -> None:
"""Test slice."""
with pytest.raises(TypeError, match="List index must be int"):
run_filter_query(([[]], 0), query)
run_filter_query(([[]], 0), "@[:]")

0 comments on commit b9c940b

Please sign in to comment.