def test_validate_dynamic_data(self): dynamic_string = '{ "string_field": "ABC", "date_field": "2019-01-01T11:30:00.00", ' \ '"int_field": 123, "float_field": 123.456 }' dynamic_object = json.loads(dynamic_string) schema = ObjectSchema() \ .with_required_property("string_field", TypeCode.String) \ .with_required_property("date_field", TypeCode.DateTime) \ .with_required_property("int_field", TypeCode.Integer) \ .with_required_property("float_field", TypeCode.Float) results = schema.validate(dynamic_object) assert len(results) == 0
def test_object_types(self): schema = ObjectSchema() \ .with_required_property("int_field", int) \ .with_required_property("string_property", str) \ .with_optional_property("null_property", object) \ .with_required_property("int_array_property", list) \ .with_required_property("string_list_property", list) \ .with_required_property("map_property", dict) \ .with_required_property("sub_object_property", TestSubObject) \ .with_required_property("sub_array_property", list) obj = TestObject() results = schema.validate(obj) assert 0 == len(results)
def test_string_types(self): schema = ObjectSchema() \ .with_required_property("int_field", "Integer") \ .with_required_property("string_property", "String") \ .with_optional_property("null_property", "Object") \ .with_required_property("int_array_property", "int[]") \ .with_required_property("string_list_property", "list") \ .with_required_property("map_property", "dict") \ .with_required_property("sub_object_property", "TestSubObject") \ .with_required_property("sub_array_property", "TestSubObject[]") obj = TestObject() results = schema.validate(obj) assert 0 == len(results)
def test_optional_properties(self): schema = ObjectSchema() \ .with_optional_property("int_field", None) \ .with_optional_property("string_property", None) \ .with_optional_property("null_property", None) \ .with_optional_property("int_array_property", None) \ .with_optional_property("string_list_property", None) \ .with_optional_property("map_property", None) \ .with_optional_property("sub_object_property", None) \ .with_optional_property("sub_array_property", None) obj = TestObject() results = schema.validate(obj) assert 0 == len(results)
def test_required_properties(self): schema = ObjectSchema() \ .with_required_property("int_field", None) \ .with_required_property("string_property", None) \ .with_required_property("null_property", None) \ .with_required_property("int_array_property", None) \ .with_required_property("string_list_property", None) \ .with_required_property("map_property", None) \ .with_required_property("sub_object_property", None) \ .with_required_property("sub_array_property", None) obj = TestObject() obj.sub_array_property = None results = schema.validate(obj) assert 2 == len(results)
def test_object_types(self): schema = ObjectSchema() \ .with_required_property('__private_field') \ .with_required_property('__private_property') \ .with_required_property("int_field", TypeCode.Long) \ .with_required_property("string_property", TypeCode.String) \ .with_optional_property("null_property", TypeCode.Object) \ .with_required_property("int_array_property", TypeCode.Array) \ .with_required_property("string_list_property", TypeCode.Array) \ .with_required_property("map_property", TypeCode.Map) \ .with_required_property("sub_object_property", TypeCode.Object) \ .with_required_property("sub_array_property", TypeCode.Array) obj = ObjectTest() results = schema.validate(obj) assert 0 == len(results)
def test_array_and_map_schema(self): sub_schema = ObjectSchema() \ .with_required_property("Id", "String") \ .with_required_property("FLOAT_FIELD", "float") \ .with_optional_property("null_property", "Object") schema = ObjectSchema() \ .with_required_property("int_field", "Integer") \ .with_required_property("string_property", "String") \ .with_optional_property("null_property", "Object") \ .with_required_property("int_array_property", ArraySchema("Integer")) \ .with_required_property("string_list_property", ArraySchema("String")) \ .with_required_property("map_property", MapSchema("String", "Integer")) \ .with_required_property("sub_object_property", sub_schema) \ .with_required_property("sub_array_property", ArraySchema(sub_schema)) obj = TestObject() results = schema.validate(obj) assert 0 == len(results)
def test_array_and_map_schema(self): sub_schema = ObjectSchema() \ .with_required_property("id", TypeCode.String) \ .with_required_property("float_field", TypeCode.Double) \ .with_optional_property("null_property", TypeCode.Map) schema = ObjectSchema() \ .with_required_property('__private_field') \ .with_required_property('__private_property') \ .with_required_property("int_field", TypeCode.Long) \ .with_required_property("string_property", TypeCode.String) \ .with_optional_property("null_property", TypeCode.Object) \ .with_required_property("int_array_property", ArraySchema(TypeCode.Long)) \ .with_required_property("string_list_property", ArraySchema(TypeCode.String)) \ .with_required_property("map_property", MapSchema(TypeCode.String, TypeCode.Long)) \ .with_required_property("sub_object_property", sub_schema) \ .with_required_property("sub_array_property", ArraySchema(sub_schema)) obj = ObjectTest() results = schema.validate(obj) assert 0 == len(results)
def test_unexpected(self): schema = ObjectSchema() obj = TestObject() results = schema.validate(obj) assert 8 == len(results)
def test_empty_schema(self): schema = ObjectSchema() results = schema.validate(None) assert 0 == len(results)