async def test_str() -> None: assert "foo~=^f" == str(filter.Filter({"~=": ("foo", "^f")})) assert "-foo=1" == str(filter.Filter({"-": {"=": ("foo", 1)}})) assert "foo" == str(filter.Filter({"=": ("foo", True)})) assert "-bar" == str(filter.Filter({"=": ("bar", False)})) with pytest.raises(filter.InvalidOperator): str(filter.Filter({">=": ("bar", False)}))
async def test_contains() -> None: f = filter.Filter({"=": ("foo", 1)}) assert await f(FakePR({"foo": [1, 2]})) assert not await f(FakePR({"foo": [2, 3]})) assert await f(FakePR({"foo": (1, 2)})) f = filter.Filter({">": ("foo", 2)}) assert not await f(FakePR({"foo": [1, 2]})) assert await f(FakePR({"foo": [2, 3]}))
def test_contains(): f = filter.Filter({"=": ("foo", 1)}) assert f(foo=[1, 2]) assert not f(foo=[2, 3]) assert f(foo=(1, 2)) f = filter.Filter({">": ("foo", 2)}) assert not f(foo=[1, 2]) assert f(foo=[2, 3])
def test_str(): assert "foo~=^f" == str(filter.Filter({ "~=": ("foo", "^f"), })) assert "-foo=1" == str(filter.Filter({ "-": { "=": ("foo", 1) }, }))
def test_regexp(): f = filter.Filter({"~=": ("foo", "^f")}) assert f(foo="foobar") assert f(foo="foobaz") assert not f(foo="x") assert not f(foo=None) f = filter.Filter({"~=": ("foo", "^$")}) assert f(foo="") assert not f(foo="x")
async def test_regexp() -> None: f = filter.Filter({"~=": ("foo", "^f")}) assert await f(FakePR({"foo": "foobar"})) assert await f(FakePR({"foo": "foobaz"})) assert not await f(FakePR({"foo": "x"})) assert not await f(FakePR({"foo": None})) f = filter.Filter({"~=": ("foo", "^$")}) assert await f(FakePR({"foo": ""})) assert not await f(FakePR({"foo": "x"}))
async def test_set_value_expanders() -> None: f = filter.Filter( {"=": ("foo", "@bar")}, value_expanders={"foo": lambda x: [x.replace("@", "foo")]}, ) assert await f(FakePR({"foo": "foobar"})) assert not await f(FakePR({"foo": "x"}))
def test_set_value_expanders(): f = filter.Filter({ "=": ("foo", "@bar"), }) f.set_value_expanders("foo", lambda x: [x.replace("@", "foo")]) assert f(foo="foobar") assert not f(foo="x")
def test_does_not_contain(): f = filter.Filter({ "!=": ("foo", 1), }) assert f(foo=[]) assert f(foo=[2, 3]) assert not f(foo=(1, 2))
def test_not(): f = filter.Filter({ "-": { "=": ("foo", 1) }, }) assert not f(foo=1) assert f(foo=2)
def test_len(): f = filter.Filter({"=": ("#foo", 3)}) assert f(foo="bar") with pytest.raises(filter.InvalidOperator): f(foo=2) assert not f(foo="a") assert not f(foo="abcedf") assert f(foo=[10, 20, 30]) assert not f(foo=[10, 20]) assert not f(foo=[10, 20, 40, 50]) f = filter.Filter({">": ("#foo", 3)}) assert f(foo="barz") with pytest.raises(filter.InvalidOperator): f(foo=2) assert not f(foo="a") assert f(foo="abcedf") assert f(foo=[10, "abc", 20, 30]) assert not f(foo=[10, 20]) assert not f(foo=[])
async def test_len() -> None: f = filter.Filter({"=": ("#foo", 3)}) assert await f(FakePR({"foo": "bar"})) with pytest.raises(filter.InvalidOperator): await f(FakePR({"foo": 2})) assert not await f(FakePR({"foo": "a"})) assert not await f(FakePR({"foo": "abcedf"})) assert await f(FakePR({"foo": [10, 20, 30]})) assert not await f(FakePR({"foo": [10, 20]})) assert not await f(FakePR({"foo": [10, 20, 40, 50]})) f = filter.Filter({">": ("#foo", 3)}) assert await f(FakePR({"foo": "barz"})) with pytest.raises(filter.InvalidOperator): await f(FakePR({"foo": 2})) assert not await f(FakePR({"foo": "a"})) assert await f(FakePR({"foo": "abcedf"})) assert await f(FakePR({"foo": [10, "abc", 20, 30]})) assert not await f(FakePR({"foo": [10, 20]})) assert not await f(FakePR({"foo": []}))
def test_binary(): f = filter.Filter({ "=": ("foo", 1), }) assert f(foo=1) assert not f(foo=2)
async def test_string() -> None: f = filter.Filter({"=": ("foo", "bar")}) assert await f(FakePR({"foo": "bar"})) assert not await f(FakePR({"foo": 2}))
async def test_not() -> None: f = filter.Filter({"-": {"=": ("foo", 1)}}) assert not await f(FakePR({"foo": 1})) assert await f(FakePR({"foo": 2}))
async def test_invalid_arguments() -> None: with pytest.raises(filter.InvalidArguments): filter.Filter({"=": (1, 2, 3)}) # type: ignore[typeddict-item]
def test_set_value_expanders_does_not_contain(): f = filter.Filter({"!=": ("foo", "@bar")}) f.set_value_expanders("foo", lambda x: ["foobaz", "foobar"]) assert not f(foo="foobar") assert not f(foo="foobaz") assert f(foo="foobiz")
async def test_parse_error() -> None: with pytest.raises(filter.ParseError): filter.Filter({})
async def test_unknown_operator() -> None: with pytest.raises(filter.UnknownOperator): filter.Filter({"oops": (1, 2)}) # type: ignore[typeddict-item]
def test_regexp_invalid(): with pytest.raises(filter.InvalidArguments): filter.Filter({ "~=": ("foo", "([^\s\w])(\s*\1+"), # noqa })
async def test_unknown_attribute() -> None: f = filter.Filter({"=": ("foo", 1)}) with pytest.raises(filter.UnknownAttribute): await f(FakePR({"bar": 1}))
async def test_set_value_expanders_does_not_contain() -> None: f = filter.Filter({"!=": ("foo", "@bar")}, value_expanders={"foo": lambda x: ["foobaz", "foobar"]}) assert not await f(FakePR({"foo": "foobar"})) assert not await f(FakePR({"foo": "foobaz"})) assert await f(FakePR({"foo": "foobiz"}))
def test_unknown_attribute(): f = filter.Filter({ "=": ("foo", 1), }) with pytest.raises(filter.UnknownAttribute): f(bar=1)
def test_parse_error(): with pytest.raises(filter.ParseError): filter.Filter({})
def test_unknown_operator(): with pytest.raises(filter.UnknownOperator): filter.Filter({ "oops": (1, 2), })
def test_invalid_arguments(): with pytest.raises(filter.InvalidArguments): filter.Filter({ "=": (1, 2, 3), })
async def test_regexp_invalid() -> None: with pytest.raises(filter.InvalidArguments): filter.Filter({"~=": ("foo", r"([^\s\w])(\s*\1+")})
def test_string(): f = filter.Filter({ "=": ("foo", "bar"), }) assert f(foo="bar") assert not f(foo=2)
async def test_does_not_contain() -> None: f = filter.Filter({"!=": ("foo", 1)}) assert await f(FakePR({"foo": []})) assert await f(FakePR({"foo": [2, 3]})) assert not await f(FakePR({"foo": (1, 2)}))
def test_set_value_expanders_unset_at_init() -> None: f = filter.Filter({"=": ("foo", "@bar")}) f.value_expanders = {"foo": lambda x: [x.replace("@", "foo")]} assert f(FakePR({"foo": "foobar"})) assert not f(FakePR({"foo": "x"}))