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_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_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_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 __create_property_data(self, schema: ObjectSchema, include_required: bool) -> Dict[str, Any]: properties = {} required = [] for property in schema.get_properties(): if property.get_type() is None: properties[property.get_name()] = self._object_type else: property_name = property.get_name() property_type = property.get_type() if isinstance(property_type, ArraySchema): properties[property_name] = { 'type': 'array', 'items': self.__create_property_type_data( property_type.get_value_type()) } else: properties[ property_name] = self.__create_property_type_data( property_type) if include_required and property.is_required(): required.append(property_name) data = {'properties': properties} if len(required) > 0: data['required'] = required return data
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 make_get_beacons_command(self): def handler(correlation_id, args): filter = FilterParams.from_value(args.get("filter")) paging = PagingParams.from_value(args.get("paging")) return self._controller.get_beacons_by_filter(correlation_id, filter, paging) return Command("get_beacons", ObjectSchema().with_optional_property("filter", FilterParamsSchema()) .with_optional_property("paging", PagingParamsSchema()), handler)
def make_calculate_position_command(self): def handler(correlation_id, args): site_id = args.get_as_string("site_id") udis = args.get_as_nullable_string("udis") return self._controller.calculate_position(correlation_id, site_id, udis) return Command("calculate_position", ObjectSchema().with_required_property("site_id", "String") .with_required_property("udis", ArraySchema("String")), handler)
def make_delete_beacon_by_id_command(self): def handler(correlation_id, args): id = args.get_as_string("id") return self._controller.delete_beacon_by_id(correlation_id, id) return Command("delete_beacon_by_id", ObjectSchema().with_required_property("id", "String"), handler)
def make_get_beacon_by_udi_command(self): def handler(correlation_id, args): id = args.get_as_string("udi") return self._controller.get_beacon_by_udi(correlation_id, id) return Command("get_beacon_by_udi", ObjectSchema().with_required_property("udi", "String"), handler)
def make_update_beacon_command(self): def handler(correlation_id, args): entity = args.get("beacon") return self._controller.update_beacon(correlation_id, entity) return Command( "update_beacon", ObjectSchema().with_optional_property("beacon", BeaconV1Schema()), handler)
def __make_get_beacon_by_id_command(self) -> ICommand: def handler(correlation_id, args): id = args.get_as_string("id") return self.__controller.get_beacon_by_id(correlation_id, id) return Command( "get_beacon_by_id", ObjectSchema().with_required_property("id", TypeCode.String), handler)
def _make_delete_by_id_command(self) -> ICommand: def handler(correlation_id: Optional[str], args: Parameters): id = args.get_as_string("dummy_id") return self._controller.delete_by_id(correlation_id, id) return Command( "delete_dummy", ObjectSchema(True).with_required_property("dummy_id", TypeCode.String), handler)
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 _make_update_command(self) -> ICommand: def handler(correlation_id: Optional[str], args: Parameters): entity = args.get("dummy") if isinstance(entity, dict): entity = Dummy.from_json(entity) return self._controller.update(correlation_id, entity) return Command( "update_dummy", ObjectSchema(True).with_required_property("dummy", DummySchema()), handler)
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 register(self): self.register_interceptor('/dummies$', self._increment_number_of_calls) self.register_route('get', '/dummies', ObjectSchema(True) .with_optional_property("skip", TypeCode.String) .with_optional_property("take", TypeCode.String) .with_optional_property("total", TypeCode.String) .with_optional_property("body", FilterParamsSchema()), self.__get_page_by_filter) self.register_route('get', '/dummies/<dummy_id>', ObjectSchema(True) .with_required_property("dummy_id", TypeCode.String), self.__get_one_by_id) self.register_route('post', '/dummies', ObjectSchema(True) .with_required_property("body", DummySchema()), self.__create) self.register_route('put', '/dummies', ObjectSchema(True) .with_required_property("body", DummySchema()), self.__update) self.register_route('delete', '/dummies/<dummy_id>', ObjectSchema(True) .with_required_property("dummy_id", TypeCode.String), self.__delete_by_id) self.register_route("get", "/dummies/check/correlation_id", ObjectSchema(True), self.__check_correlation_id) self.register_route('post', '/about', None, AboutOperations().get_about) if self._swagger_content: self._register_open_api_spec(self._swagger_content) if self._swagger_path: self._register_open_api_spec_from_file(self._swagger_path)
def _make_get_page_by_filter_command(self) -> ICommand: def handler(correlation_id: Optional[str], args: Parameters): filter = FilterParams.from_value(args.get("filter")) paging = PagingParams.from_value(args.get("paging")) page = self._controller.get_page_by_filter(correlation_id, filter, paging) return page return Command( "get_dummies", ObjectSchema(True).with_optional_property( "filter", FilterParamsSchema()).with_optional_property( "paging", PagingParamsSchema()), handler)
def _make_check_correlation_id(self) -> ICommand: def handler(correlation_id: Optional[str], args: Parameters): value = self._controller.check_correlation_id(correlation_id) return {'correlation_id': value} return Command("check_correlation_id", ObjectSchema(True), handler)
def test_empty_schema(self): schema = ObjectSchema() results = schema.validate(None) assert 0 == len(results)
def test_unexpected(self): schema = ObjectSchema() obj = TestObject() results = schema.validate(obj) assert 8 == len(results)