Skip to content

Commit

Permalink
Fix negative filter
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Sep 19, 2024
1 parent 411aae6 commit cfa26fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/jsonyx/_manipulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ def _run_filter_query(
filter_nodes, end = self._run_select_query(
nodes, query, end, mapping=True, relative=True,
)
filtered_pairs: list[tuple[_Node, Any]] = [
(node, filter_target[filter_key]) # type: ignore
filtered_pairs: list[tuple[_Node, _Node]] = [
(node, (filter_target, filter_key)) # type: ignore
for node, (filter_target, filter_key) in zip(
nodes, filter_nodes, strict=True,
)
Expand All @@ -256,7 +256,7 @@ def _run_filter_query(

operator, end = _scan_query_operator(query, end)
if operator is None:
nodes = [node for node, _filter_target in filtered_pairs]
nodes = [node for node, _filter_node in filtered_pairs]
elif negate_filter:
raise SyntaxError
else:
Expand All @@ -267,8 +267,10 @@ def _run_filter_query(
value, end = self._scan_query_value(query, end)
nodes = [
node
for node, filter_target in filtered_pairs
if operator(filter_target, value)
for node, (filter_target, filter_key) in filtered_pairs
if operator(
filter_target[filter_key], value, # type: ignore
)
]
else:
filter2_nodes, end = self._run_select_query(
Expand All @@ -277,11 +279,11 @@ def _run_filter_query(
nodes = [
node
for (
(node, filter_target),
(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_target[filter_key], # type: ignore
filter2_target[filter2_key], # type: ignore
)
]
Expand Down
1 change: 0 additions & 1 deletion src/jsonyx/test/test_jsonyx.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (C) 2024 Nice Zombies
# TODO(Nice Zombies): test apply_patch
# TODO(Nice Zombies): test run_filter_query
# TODO(Nice Zombies): test schema
"""JSON tests."""
from __future__ import annotations
Expand Down
18 changes: 18 additions & 0 deletions src/jsonyx/test/test_run_filter_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (C) 2024 Nice Zombies
"""JSON run_filter_query tests."""
# TODO(Nice Zombies): add more tests
from __future__ import annotations

__all__: list[str] = []

from jsonyx import run_filter_query


def test_exist() -> None:
"""Test exist."""
assert run_filter_query(([], 0), "@") == []


def test_not_exist() -> None:
"""Test not exist."""
assert run_filter_query(([], 0), "!@") == [([], 0)]

0 comments on commit cfa26fc

Please sign in to comment.