def test_constraint_type(): """Test ConstraintType.""" for cons_type in [ ConstraintTypes.EQUAL, ConstraintTypes.NOT_EQUAL, ConstraintTypes.LESS_THAN, ConstraintTypes.LESS_THAN_EQ, ConstraintTypes.GREATER_THAN, ConstraintTypes.GREATER_THAN_EQ, ]: constrant_type = ConstraintType(cons_type, 12) constrant_type.is_valid(Attribute("test", int, True)) constrant_type.check(13) constrant_type = ConstraintType(ConstraintTypes.WITHIN, [1, 2]) constrant_type.is_valid(Attribute("test", int, True)) constrant_type.check(13) assert str(constrant_type) == "ConstraintType(value=[1, 2],type=within)" constrant_type = ConstraintType(ConstraintTypes.IN, [1, 2]) constrant_type.is_valid(Attribute("test", int, True)) constrant_type.check(13) constrant_type = ConstraintType(ConstraintTypes.NOT_IN, [1, 2]) constrant_type.is_valid(Attribute("test", int, True)) constrant_type.check(13) constrant_type = ConstraintType(ConstraintTypes.DISTANCE, [Location(1.1, 2.2), 2.2]) constrant_type.is_valid(Attribute("test", int, True)) constrant_type.check(Location(1.1, 2.2)) with pytest.raises(ValueError): ConstraintType("something", [Location(1.1, 2.2), 2.2]).is_valid( Attribute("test", int, True)) with pytest.raises(AEAEnforceError): ConstraintType(ConstraintTypes.GREATER_THAN, str) assert ConstraintType(ConstraintTypes.IN, [1, 2]) == ConstraintType(ConstraintTypes.IN, [1, 2])
def test_constraint_type(): """Test ConstraintType.""" constraint_type_values = { "int": 12, "bool": True, "float": 10.4, "str": "some_string", "location": Location(1.1, 2.2), } constraint_type_types = { "int": int, "bool": bool, "float": float, "str": str, "location": Location, } to_check = { "int": 13, "bool": False, "float": 9.3, "str": "some_other_string", "location": Location(1.2, 2.3), } # = and != for constraint_types_type in [ ConstraintTypes.EQUAL, ConstraintTypes.NOT_EQUAL ]: for allowed_type in ["int", "bool", "float", "str"]: constraint_type = ConstraintType( constraint_types_type, constraint_type_values[allowed_type]) constraint_type.is_valid( Attribute("test", constraint_type_types[allowed_type], True)) constraint_type.check(to_check[allowed_type]) assert constraint_type == ConstraintType( constraint_types_type, constraint_type_values[allowed_type]) assert ( str(constraint_type) == f"ConstraintType(value={constraint_type_values[allowed_type]},type={constraint_types_type})" ) constraint_type_pb = constraint_type.encode() actual_constraint_type = ConstraintType.decode( constraint_type_pb, "relation") assert actual_constraint_type == constraint_type # < and <= and > and >= for constraint_types_type in [ ConstraintTypes.LESS_THAN, ConstraintTypes.LESS_THAN_EQ, ConstraintTypes.GREATER_THAN, ConstraintTypes.GREATER_THAN_EQ, ]: for allowed_type in ["int", "float", "str"]: constraint_type = ConstraintType( constraint_types_type, constraint_type_values[allowed_type]) constraint_type.is_valid( Attribute("test", constraint_type_types[allowed_type], True)) constraint_type.check(to_check[allowed_type]) assert constraint_type == ConstraintType( constraint_types_type, constraint_type_values[allowed_type]) assert ( str(constraint_type) == f"ConstraintType(value={constraint_type_values[allowed_type]},type={constraint_types_type})" ) constraint_type_pb = constraint_type.encode() actual_constraint_type = ConstraintType.decode( constraint_type_pb, "relation") assert actual_constraint_type == constraint_type # within constraint_type_values = { "int": (1, 2), "float": (2.4, 5.4), "str": ("str_1", "str_2"), "location": (Location(1.1, 2.2), Location(1.2, 5.2)), } constraint_type_types = { "int": int, "float": float, "str": str, "location": Location, } to_check = { "int": 13, "float": 9.3, "str": "some_other_string", "location": Location(1.2, 2.3), } for range_constraint_type in ["int", "float", "str"]: # location is not working constraint_type = ConstraintType( ConstraintTypes.WITHIN, constraint_type_values[range_constraint_type]) constraint_type.is_valid( Attribute("test", constraint_type_types[range_constraint_type], True)) constraint_type.check(to_check[range_constraint_type]) assert constraint_type == ConstraintType( ConstraintTypes.WITHIN, constraint_type_values[range_constraint_type]) assert ( str(constraint_type) == f"ConstraintType(value={constraint_type_values[range_constraint_type]},type=within)" ) constraint_type_pb = constraint_type.encode() actual_constraint_type = ConstraintType.decode(constraint_type_pb, "range") assert actual_constraint_type == constraint_type # in and not_in constraint_type_values = { "int": (1, 2), "bool": (True, False), "float": (2.4, 5.4), "str": ("str_1", "str_2"), "location": (Location(1.1, 2.2), Location(1.2, 5.2)), } constraint_type_types = { "int": int, "bool": bool, "float": float, "str": str, "location": Location, } to_check = { "int": 13, "bool": False, "float": 9.3, "str": "some_other_string", "location": Location(1.2, 2.3), } for constraint_types_type in [ConstraintTypes.IN, ConstraintTypes.NOT_IN]: for constraint_set in ["int", "bool", "float", "str", "location"]: constraint_type = ConstraintType( constraint_types_type, constraint_type_values[constraint_set]) constraint_type.is_valid( Attribute("test", constraint_type_types[constraint_set], True)) constraint_type.check(to_check[constraint_set]) assert constraint_type == ConstraintType( constraint_types_type, constraint_type_values[constraint_set]) assert ( str(constraint_type) == f"ConstraintType(value={constraint_type_values[constraint_set]},type={constraint_types_type})" ) constraint_type_pb = constraint_type.encode() actual_constraint_type = ConstraintType.decode( constraint_type_pb, "set") assert actual_constraint_type == constraint_type # distance constraint_location = (Location(1.1, 2.2), 2.2) constraint_type_distance = ConstraintType(ConstraintTypes.DISTANCE, constraint_location) constraint_type_distance.is_valid(Attribute("test", int, True)) constraint_type_distance.check(Location(1.1, 2.2)) assert constraint_type_distance == ConstraintType(ConstraintTypes.DISTANCE, constraint_location) constraint_type_distance_pb = constraint_type_distance.encode() actual_constraint_type_distance = ConstraintType.decode( constraint_type_distance_pb, "distance") assert actual_constraint_type_distance == constraint_type_distance # failures with pytest.raises(ValueError): ConstraintType("something", [Location(1.1, 2.2), 2.2]).is_valid( Attribute("test", int, True)) with pytest.raises(AEAEnforceError, match=""): ConstraintType(ConstraintTypes.GREATER_THAN, str) list_value = [1, 2] set_value = {1, 2} list_location = [Location(1.1, 2.2), 2.2] with pytest.raises(AEAEnforceError, match=f"Expected tuple, got {type(list_value)}"): ConstraintType(ConstraintTypes.WITHIN, list_value) with pytest.raises(AEAEnforceError, match=f"Expected tuple, got {type(list_value)}"): ConstraintType(ConstraintTypes.IN, list_value) with pytest.raises(AEAEnforceError, match=f"Expected tuple, got {type(set_value)}"): ConstraintType(ConstraintTypes.IN, set_value) with pytest.raises(AEAEnforceError, match=f"Expected tuple, got {type(list_value)}"): ConstraintType(ConstraintTypes.NOT_IN, list_value) with pytest.raises(AEAEnforceError, match=f"Expected tuple, got {type(set_value)}"): ConstraintType(ConstraintTypes.NOT_IN, set_value) with pytest.raises(AEAEnforceError, match=f"Expected tuple, got {type(list_location)}"): ConstraintType(ConstraintTypes.DISTANCE, list_location) incorrect_category = "some_incorrect_category" with pytest.raises( ValueError, match= r"Incorrect category. Expected either of .* Found some_incorrect_category.", ): constraint_type_distance_pb = constraint_type_distance.encode() ConstraintType.decode(constraint_type_distance_pb, incorrect_category)