def test_not_check(self): """Test the not().check function.""" attribute_foo = Attribute("foo", int, True, "a foo attribute.") attribute_bar = Attribute("bar", str, True, "a bar attribute.") data_model_foobar = DataModel("foobar", [attribute_foo, attribute_bar], "A foobar data model.") description_foobar = Description({"foo": 1, "bar": "baz"}, data_model=data_model_foobar) no_constraint = Not(Constraint("foo", ConstraintType("==", 5))) assert no_constraint.check(description_foobar)
def test_query_check(self): """Test that the query.check() method works.""" attribute_foo = Attribute("foo", int, True, "a foo attribute.") attribute_bar = Attribute("bar", str, True, "a bar attribute.") data_model_foobar = DataModel("foobar", [attribute_foo, attribute_bar], "A foobar data model.") description_foobar = Description({"foo": 1, "bar": "baz"}, data_model=data_model_foobar) query = Query([ And([ Or([ Not(Constraint("foo", ConstraintType("==", 1))), Not(Constraint("bar", ConstraintType("==", "baz"))) ]), Constraint("foo", ConstraintType("<", 2)), ]) ], data_model_foobar) assert not query.check(description=description_foobar)
def test_query(self): """Test that the translation for the Query class works.""" attribute_foo = Attribute("foo", int, True, "a foo attribute.") attribute_bar = Attribute("bar", str, True, "a bar attribute.") data_model_foobar = DataModel("foobar", [attribute_foo, attribute_bar], "A foobar data model.") query = Query([ And([ Or([ Not(Constraint("foo", ConstraintType("==", 1))), Not(Constraint("bar", ConstraintType("==", "baz"))) ]), Constraint("foo", ConstraintType("<", 2)), ]) ], data_model_foobar) oef_query = OEFObjectTranslator.to_oef_query(query) expected_query = OEFObjectTranslator.from_oef_query(oef_query) actual_query = query assert expected_query == actual_query
def test_pickable_query(self): """Test that an istance of the Query class is pickable.""" attribute_foo = Attribute("foo", int, True, "a foo attribute.") attribute_bar = Attribute("bar", str, True, "a bar attribute.") data_model_foobar = DataModel("foobar", [attribute_foo, attribute_bar], "A foobar data model.") query = Query([ And([ Or([ Not(Constraint("foo", ConstraintType("==", 1))), Not(Constraint("bar", ConstraintType("==", "baz"))) ]), Constraint("foo", ConstraintType("<", 2)), ]) ], data_model_foobar) try: pickle.dumps(query) except Exception: pytest.fail("Error during pickling.")
def from_oef_constraint_expr( cls, oef_constraint_expr: OEFConstraintExpr) -> ConstraintExpr: """From our query to OEF query.""" if isinstance(oef_constraint_expr, OEFAnd): return And([ cls.from_oef_constraint_expr(c) for c in oef_constraint_expr.constraints ]) elif isinstance(oef_constraint_expr, OEFOr): return Or([ cls.from_oef_constraint_expr(c) for c in oef_constraint_expr.constraints ]) elif isinstance(oef_constraint_expr, OEFNot): return Not( cls.from_oef_constraint_expr(oef_constraint_expr.constraint)) elif isinstance(oef_constraint_expr, OEFConstraint): return Constraint(oef_constraint_expr.attribute_name, oef_constraint_expr.constraint) else: raise ValueError("OEF Constraint not supported.")