Skip to content

Commit

Permalink
Fix str() and repr() in selectors (#93)
Browse files Browse the repository at this point in the history
(DIS-2565)
  • Loading branch information
Schamper authored Oct 26, 2023
1 parent ecbd912 commit fdcecba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions flow/record/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ def __init__(self, record):
def __getattr__(self, k):
return getattr(self.record, k, NONE_OBJECT)

def __str__(self) -> str:
return str(self.record)

def __repr__(self) -> str:
return repr(self.record)


class CompiledSelector:
"""CompiledSelector is faster than Selector but unsafe if you don't trust the query."""
Expand Down Expand Up @@ -546,6 +552,7 @@ def matches(self, rec):
"True": True,
"False": False,
"str": str,
"repr": repr,
"fields": rec._desc.getfields,
"any": any,
"all": all,
Expand Down
24 changes: 24 additions & 0 deletions tests/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ def test_selector():
assert TestRecord() in Selector("invalid_func(r.invalid_field, 1337) or r.id == 4")


def test_selector_str_repr():
TestRecord = RecordDescriptor(
"test/record",
[
("string", "query"),
("string", "url"),
],
)

assert TestRecord("foo", "bar") in Selector("'foo' in str(r)")
assert TestRecord("foo", "bar") in Selector("'test/record' in str(r)")
assert TestRecord("foo", "bar") in Selector("'foo' in repr(r)")
assert TestRecord("foo", "bar") in Selector("'test/record' in repr(r)")
assert TestRecord("foo", "bar") in CompiledSelector("'foo' in str(r)")
assert TestRecord("foo", "bar") in CompiledSelector("'test/record' in str(r)")
assert TestRecord("foo", "bar") in CompiledSelector("'foo' in repr(r)")
assert TestRecord("foo", "bar") in CompiledSelector("'test/record' in repr(r)")

assert TestRecord("foo", "bar") not in Selector("'nope' in str(r)")
assert TestRecord("foo", "bar") not in Selector("'nope' in repr(r)")
assert TestRecord("foo", "bar") not in CompiledSelector("'nope' in str(r)")
assert TestRecord("foo", "bar") not in CompiledSelector("'nope' in repr(r)")


def test_selector_meta_query_true():
source = "internal/flow.record.test"

Expand Down

0 comments on commit fdcecba

Please sign in to comment.