Ejemplo n.º 1
0
def test_from_json():
    rules = [
        '{"a": 1, "b": 1, "py/object": "test_rule_base.ABRule"}',
        '{"a": "x", "b": "y", "py/object": "test_rule_base.ABRule"}',
    ]
    c1 = Rule.from_json(rules[0])
    c2 = Rule.from_json(rules[1])
    assert isinstance(c1, ABRule)
    assert isinstance(c2, ABRule)
    assert c1.satisfied()
    assert not c2.satisfied()
Ejemplo n.º 2
0
def test_from_json():
    rules = [
        '{"contents": {"a": 1, "b": 1}, "type": "test_rule_base.ABRule"}',
        '{"contents": {"a": "x", "b": "y"}, "type": "test_rule_base.ABRule"}',
    ]
    c1 = Rule.from_json(rules[0])
    c2 = Rule.from_json(rules[1])
    assert isinstance(c1, ABRule)
    assert isinstance(c2, ABRule)
    assert c1.satisfied()
    assert not c2.satisfied()
Ejemplo n.º 3
0
def test_not_eq_satisfied(val, against, result):
    c = NotEq(val)
    assert result == c.satisfied(against)
    # test after (de)serialization
    jsn = NotEq(val).to_json()
    c1 = Rule.from_json(jsn)
    assert result == c1.satisfied(against)
Ejemplo n.º 4
0
def test_less_or_equal_satisfied(val, against, result):
    c = LessOrEqual(val)
    assert result == c.satisfied(against)
    # test after (de)serialization
    jsn = LessOrEqual(val).to_json()
    c1 = Rule.from_json(jsn)
    assert result == c1.satisfied(against)
Ejemplo n.º 5
0
def test_greater_satisfied(val, against, result):
    c = Greater(val)
    assert result == c.satisfied(against)
    # test after (de)serialization
    jsn = Greater(val).to_json()
    c1 = Rule.from_json(jsn)
    assert result == c1.satisfied(against)
Ejemplo n.º 6
0
def test_regex_match_rule_satisfied(arg, against, result):
    c = RegexMatch(arg)
    assert result == c.satisfied(against)
    # test after (de)serialization
    js = RegexMatch(arg).to_json()
    assert result == Rule.from_json(js).satisfied(against)
    # test deprecated class
    with pytest.deprecated_call():
        c = RegexMatchRule(arg)
        assert result == c.satisfied(against)
        assert result == RegexMatchRule.from_json(
            c.to_json()).satisfied(against)
Ejemplo n.º 7
0
def test_or_rule(rules, what, inquiry, result):
    r = Or(*rules)
    assert result == r.satisfied(what, inquiry)
    # test after (de)serialization
    assert result == Rule.from_json(Or(*rules).to_json()).satisfied(
        what, inquiry)
Ejemplo n.º 8
0
def test_falsy_satisfied(against, result):
    assert result == Falsy().satisfied(against)
    # test after (de)serialization
    jsn = Falsy().to_json()
    assert result == Rule.from_json(jsn).satisfied(against)
Ejemplo n.º 9
0
def test_any_and_neither_rules(what):
    assert Any().satisfied(what)
    assert not Neither().satisfied(what)
    # test after (de)serialization
    assert Rule.from_json(Any().to_json()).satisfied(what)
    assert not Rule.from_json(Neither().to_json()).satisfied(what)
Ejemplo n.º 10
0
def test_not_rule(rule, what, inquiry, result):
    r = Not(rule)
    assert result == r.satisfied(what, inquiry)
    # test after (de)serialization
    assert result == Rule.from_json(Not(rule).to_json()).satisfied(
        what, inquiry)
Ejemplo n.º 11
0
def test_from_json_fails(data, msg):
    with pytest.raises(RuleCreationError) as excinfo:
        Rule.from_json(data)
    assert msg in str(excinfo.value)
Ejemplo n.º 12
0
def test_json_roundtrip(rule, satisfied):
    c1 = Rule.from_json(rule.to_json())
    assert isinstance(c1, rule.__class__)
    assert c1.__dict__ == rule.__dict__
    assert satisfied == c1.satisfied(None, None)
Ejemplo n.º 13
0
def test_is_true_satisfied(against, result):
    assert result == Truthy().satisfied(against)
    # test after (de)serialization
    jsn = Truthy().to_json()
    assert result == Rule.from_json(jsn).satisfied(against)