Exemplo n.º 1
0
def test_constraint():
    """Test Constraint."""
    c1 = Constraint("author", ConstraintType("==", "Stephen King"))
    c2 = Constraint("author", ConstraintType("in", ["Stephen King"]))
    book_1 = Description({
        "author": "Stephen King",
        "year": 1991,
        "genre": "horror"
    })
    book_2 = Description({
        "author": "George Orwell",
        "year": 1948,
        "genre": "horror"
    })
    assert c1.check(book_1)
    assert not c1.check(book_2)

    # empty description
    assert not c1.check(Description({}))

    # bad type
    assert not c1.check(Description({"author": 12}))

    # bad type
    assert not c2.check(Description({"author": 12}))

    assert c1.is_valid(generate_data_model("test", {"author": "some author"}))

    assert not c1.is_valid(
        generate_data_model("test", {"not_author": "some author"}))

    assert c1 == c1
    assert c1 != c2
Exemplo n.º 2
0
def test_constraints_expression():
    """Test constraint expressions: And, Or, Not, Constraint."""
    and_expression = And([
        Constraint("number", ConstraintType(ConstraintTypes.LESS_THAN, 15)),
        Constraint("number", ConstraintType(ConstraintTypes.GREATER_THAN, 10)),
    ])
    and_expression.check_validity()
    assert and_expression.check(Description({"number": 12}))
    assert and_expression.is_valid(
        DataModel("some_name", [Attribute("number", int, True)]))
    and_expression_pb = ConstraintExpr._encode(and_expression)
    actual_and_expression = ConstraintExpr._decode(and_expression_pb)
    assert actual_and_expression == and_expression

    or_expression = Or([
        Constraint("number", ConstraintType(ConstraintTypes.EQUAL, 12)),
        Constraint("number", ConstraintType(ConstraintTypes.EQUAL, 13)),
    ])
    or_expression.check_validity()
    assert or_expression.check(Description({"number": 12}))
    assert or_expression.is_valid(
        DataModel("some_name", [Attribute("number", int, True)]))
    or_expression_pb = ConstraintExpr._encode(or_expression)
    actual_or_expression = ConstraintExpr._decode(or_expression_pb)
    assert actual_or_expression == or_expression

    not_expression = Not(
        And([
            Constraint("number", ConstraintType(ConstraintTypes.EQUAL, 12)),
            Constraint("number", ConstraintType(ConstraintTypes.EQUAL, 12)),
        ]))
    not_expression.check_validity()
    assert not_expression.check(Description({"number": 13}))
    assert not_expression.is_valid(
        DataModel("some_name", [Attribute("number", int, True)]))
    not_expression_pb = ConstraintExpr._encode(not_expression)
    actual_not_expression = ConstraintExpr._decode(not_expression_pb)
    assert actual_not_expression == not_expression

    # constraint
    constraint_expression = Constraint("author",
                                       ConstraintType("==", "Stephen King"))
    constraint_expression.check_validity()
    assert constraint_expression.check(Description({"author": "Stephen King"}))
    assert constraint_expression.is_valid(
        DataModel("some_name", [Attribute("author", str, True)]))
    constraint_expression_pb = ConstraintExpr._encode(constraint_expression)
    actual_constraint_expression = ConstraintExpr._decode(
        constraint_expression_pb)
    assert actual_constraint_expression == constraint_expression

    incorrect_expression = Location(1.1, 2.2)
    with pytest.raises(
            ValueError,
            match=
            f"Invalid expression type. Expected either of 'And', 'Or', 'Not', 'Constraint'. Found {type(incorrect_expression)}.",
    ):
        ConstraintExpr._encode(incorrect_expression)
Exemplo n.º 3
0
def test_constraint():
    """Test Constraint."""
    c1 = Constraint("author", ConstraintType("==", "Stephen King"))
    c2 = Constraint("year", ConstraintType("within", (2000, 2010)))
    c3 = Constraint("author",
                    ConstraintType("in", ("Stephen King", "J. K. Rowling")))
    c4 = Constraint(
        "author", ConstraintType("not_in", ("Stephen King", "J. K. Rowling")))
    c5 = Constraint("address",
                    ConstraintType("distance", (Location(1.1, 2.2), 2.2)))

    book_1 = Description({
        "author": "Stephen King",
        "year": 2005,
        "address": Location(1.1, 2.2)
    })
    book_2 = Description({
        "author": "George Orwell",
        "year": 1948,
        "address": Location(1.1, 2.2)
    })

    assert c1.check(book_1)
    assert not c1.check(book_2)
    # empty description
    assert not c1.check(Description({}))
    # bad type
    assert not c1.check(Description({"author": 12}))
    # bad type
    assert not c2.check(Description({"author": 12}))

    assert c1.is_valid(generate_data_model("test", {"author": "some author"}))
    assert not c1.is_valid(
        generate_data_model("test", {"not_author": "some author"}))

    assert c1 == c1
    assert c1 != c2

    assert (
        str(c1) ==
        f"Constraint(attribute_name=author,constraint_type={c1.constraint_type})"
    )
    assert (
        str(c2) ==
        f"Constraint(attribute_name=year,constraint_type={c2.constraint_type})"
    )

    c1_pb = c1.encode()
    actual_c1 = Constraint.decode(c1_pb)
    assert actual_c1 == c1

    c2_pb = c2.encode()
    actual_c2 = Constraint.decode(c2_pb)
    assert actual_c2 == c2

    c3_pb = c3.encode()
    actual_c3 = Constraint.decode(c3_pb)
    assert actual_c3 == c3

    c4_pb = c4.encode()
    actual_c4 = Constraint.decode(c4_pb)
    assert actual_c4 == c4

    c5_pb = c5.encode()
    actual_c5 = Constraint.decode(c5_pb)
    assert actual_c5 == c5