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
コード例 #2
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)
コード例 #3
0
    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)
コード例 #4
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)
コード例 #5
0
    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
コード例 #7
0
    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)
コード例 #8
0
 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)
コード例 #9
0
 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)
コード例 #10
0
    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)
コード例 #11
0
    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)
コード例 #12
0
    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)
コード例 #13
0
    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)
コード例 #14
0
    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)
コード例 #15
0
    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)
コード例 #16
0
    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)
コード例 #17
0
    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)
コード例 #18
0
    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)
コード例 #19
0
    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)
コード例 #20
0
    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)
コード例 #21
0
 def test_empty_schema(self):
     schema = ObjectSchema()
     results = schema.validate(None)
     assert 0 == len(results)
コード例 #22
0
 def test_unexpected(self):
     schema = ObjectSchema()
     obj = TestObject()
     results = schema.validate(obj)
     assert 8 == len(results)