Ejemplo n.º 1
0
 def test_schema_custom_response_factory_empty_response(self):
     schema = Schema(schemas.project_page,
                     response_factory=json_response_factory)
     schema.validate_request({'project_id': 1})
     response = schema.make_response({})
     self.assertIsInstance(response, web.Response)
     self.assertEqual(response.content_type, 'application/json')
Ejemplo n.º 2
0
 def test_schema_custom_response_factory(self):
     schema = Schema(schemas.index, response_factory=json_response_factory)
     schema.validate_request({'name': TEST_NAME})
     response = schema.make_response({'name': TEST_NAME,
                                      'time': time.time()})
     self.assertIsInstance(response, web.Response)
     self.assertEqual(response.content_type, 'application/json')
Ejemplo n.º 3
0
    def test_schema_default_values(self):
        schema = Schema(schemas.default_values)
        data = schema.validate_request({})
        self.assertEqual(data, {"include_data_id": "0"})

        data = schema.make_response({"data": {"name": "Igor"}})
        self.assertEqual(data, {"data": {"name": "Igor", "main": False}, "system": "dev"})
Ejemplo n.º 4
0
 def test_schema_multiple_request_data(self):
     schema = Schema(schemas.project_page)
     data = schema.validate_request({'project_id': 1}, {
         'project_id': 2,
         'include_stories': True
     })
     self.assertEqual(data, {'project_id': 1, 'include_stories': True})
Ejemplo n.º 5
0
 def test_schema_multiple_request_data(self):
     schema = Schema(schemas.project_page)
     data = schema.validate_request(
         {'project_id': 1},
         {'project_id': 2, 'include_stories': True}
     )
     self.assertEqual(data, {'project_id': 1, 'include_stories': True})
Ejemplo n.º 6
0
 def test_schema_custom_response_factory_empty_response(self):
     schema = Schema(schemas.project_page,
                     response_factory=json_response_factory)
     schema.validate_request({'project_id': 1})
     response = schema.make_response({})
     self.assertIsInstance(response, web.Response)
     self.assertEqual(response.content_type, 'application/json')
Ejemplo n.º 7
0
 def test_schema_custom_response_factory(self):
     schema = Schema(schemas.index, response_factory=json_response_factory)
     schema.validate_request({'name': TEST_NAME})
     response = schema.make_response({
         'name': TEST_NAME,
         'time': time.time()
     })
     self.assertIsInstance(response, web.Response)
     self.assertEqual(response.content_type, 'application/json')
Ejemplo n.º 8
0
    def test_schema(self):
        schema = Schema(schemas.index)
        with self.assertRaises(SchemaError):
            raise schema.make_error('Dummy error')

        data = schema.validate_request({'name': TEST_NAME})
        self.assertEqual(data, {'name': TEST_NAME})

        timestamp = time.time()
        response = schema.make_response({'name': TEST_NAME, 'time': timestamp})
        self.assertEqual(response, {'name': TEST_NAME, 'time': timestamp})
Ejemplo n.º 9
0
    def test_schema(self):
        schema = Schema(schemas.index)
        with self.assertRaises(SchemaError):
            raise schema.make_error('Dummy error')

        data = schema.validate_request({'name': TEST_NAME})
        self.assertEqual(data, {'name': TEST_NAME})

        timestamp = time.time()
        response = schema.make_response({'name': TEST_NAME, 'time': timestamp})
        self.assertEqual(response, {'name': TEST_NAME, 'time': timestamp})
Ejemplo n.º 10
0
 def test_schema_multiple_request_data_merged_class(self):
     schema = Schema(schemas.project_page)
     data = schema.validate_request({'project_id': 1}, {'archived': True},
                                    {'include_stories': False},
                                    merged_class=types.MappingProxyType)
     self.assertEqual(
         data,
         types.MappingProxyType({
             'project_id': 1,
             'archived': True,
             'include_stories': False,
         }))
Ejemplo n.º 11
0
    def test_schema_default_values(self):
        schema = Schema(schemas.default_values)
        data = schema.validate_request({})
        self.assertEqual(data, {'include_data_id': '0'})

        data = schema.make_response({'data': {'name': 'Igor'}})
        self.assertEqual(data, {
            'data': {
                'name': 'Igor',
                'main': False
            },
            'system': 'dev',
        })
Ejemplo n.º 12
0
    def test_schema_default_values(self):
        schema = Schema(schemas.default_values)
        data = schema.validate_request({})
        self.assertEqual(data, {'include_data_id': '0'})

        data = schema.make_response({'data': {'name': 'Igor'}})
        self.assertEqual(
            data,
            {
                'data': {'name': 'Igor', 'main': False},
                'system': 'dev',
            }
        )
Ejemplo n.º 13
0
    def test_schema_default_values_override_defaults(self):
        schema = Schema(schemas.default_values)

        request_data = {"include_data_id": "1"}
        data = schema.validate_request(request_data)
        self.assertEqual(data, request_data)

        response_data = {
            "data": {"name": "Igor", "uri": "http://igordavydenko.com/", "main": True},
            "data_id": 1,
            "system": "production",
        }
        data = schema.make_response(response_data)
        self.assertEqual(data, response_data)
Ejemplo n.º 14
0
 def test_schema_multiple_request_data_merged_class(self):
     schema = Schema(schemas.project_page)
     data = schema.validate_request(
         {'project_id': 1},
         {'archived': True},
         {'include_stories': False},
         merged_class=types.MappingProxyType
     )
     self.assertEqual(
         data,
         types.MappingProxyType({
             'project_id': 1,
             'archived': True,
             'include_stories': False,
         })
     )
Ejemplo n.º 15
0
    def test_schema_default_values_override_defaults(self):
        schema = Schema(schemas.default_values)

        request_data = {'include_data_id': '1'}
        data = schema.validate_request(request_data)
        self.assertEqual(data, request_data)

        response_data = {
            'data': {
                'name': 'Igor',
                'uri': 'http://igordavydenko.com/',
                'main': True,
            },
            'data_id': 1,
            'system': 'production',
        }
        data = schema.make_response(response_data)
        self.assertEqual(data, response_data)
Ejemplo n.º 16
0
    def test_schema_default_values_override_defaults(self):
        schema = Schema(schemas.default_values)

        request_data = {'include_data_id': '1'}
        data = schema.validate_request(request_data)
        self.assertEqual(data, request_data)

        response_data = {
            'data': {
                'name': 'Igor',
                'uri': 'http://igordavydenko.com/',
                'main': True,
            },
            'data_id': 1,
            'system': 'production',
        }
        data = schema.make_response(response_data)
        self.assertEqual(data, response_data)
Ejemplo n.º 17
0
    def check_wrapped_data(self, klass):
        schema = Schema(schemas.index)

        # Validate request should understand wrapped data
        base = {"name": TEST_NAME}
        wrapped = klass(MultiDict(base)) if klass == MultiDictProxy else klass(base)

        data = schema.validate_request(wrapped)
        self.assertEqual(data, base)
        self.assertIsInstance(data, klass)

        # Make response should understand wrapped data
        base = {"name": TEST_NAME, "time": time.time()}
        wrapped = klass(MultiDict(base)) if klass == MultiDictProxy else klass(base)

        data = schema.make_response(wrapped)
        self.assertEqual(data, base)
        self.assertIsInstance(data, klass)
Ejemplo n.º 18
0
 def test_schema_invalid_request(self):
     schema = Schema(schemas.index)
     self.assertRaises(ValidationError, schema.validate_request, {})
     self.assertRaises(ValidationError, schema.validate_request,
                       {'name': 'Something'})
     self.assertRaises(SchemaError, schema.make_response, {
         'name': TEST_NAME,
         'time': time.time()
     })
Ejemplo n.º 19
0
    def test_fastjsonschema(self):
        error_class = fastjsonschema.JsonSchemaException
        schema = Schema(FastSchemas(), validate_func=fast_validate, validation_error_class=error_class)

        # Default error
        with self.assertRaises(SchemaError):
            raise schema.make_error("Dummy error")

        # Validation error propagated
        self.assertRaises(error_class, schema.validate_request, {"name": "Something"})

        # Proper request
        data = schema.validate_request({"name": TEST_NAME})
        self.assertEqual(data, {"name": TEST_NAME})

        # Proper response
        # NOTE: fastjsonschema expects number as an int, not float
        timestamp = int(time.time())
        response = schema.make_response({"name": TEST_NAME, "time": timestamp})
        self.assertEqual(response, {"name": TEST_NAME, "time": timestamp})
Ejemplo n.º 20
0
    def check_wrapped_data(self, klass):
        schema = Schema(schemas.index)

        # Validate request should understand wrapped data
        base = {'name': TEST_NAME}
        wrapped = (klass(MultiDict(base))
                   if klass == MultiDictProxy else klass(base))

        data = schema.validate_request(wrapped)
        self.assertEqual(data, base)
        self.assertIsInstance(data, klass)

        # Make response should understand wrapped data
        base = {'name': TEST_NAME, 'time': time.time()}
        wrapped = (klass(MultiDict(base))
                   if klass == MultiDictProxy else klass(base))

        data = schema.make_response(wrapped)
        self.assertEqual(data, base)
        self.assertIsInstance(data, klass)
Ejemplo n.º 21
0
    def test_schema_custom_error_class(self):
        schema = Schema(schemas.index, error_class=CustomError)

        with self.assertRaises(CustomError):
            raise schema.make_error('Custom Error')

        self.assertRaises(CustomError, schema.validate_request, {})
        schema.validate_request({'name': TEST_NAME})

        self.assertRaises(CustomError, schema.make_response, {})
        schema.make_response({'name': TEST_NAME, 'time': time.time()})
Ejemplo n.º 22
0
    def test_schema_custom_error_class(self):
        schema = Schema(schemas.index, error_class=CustomError)

        with self.assertRaises(CustomError):
            raise schema.make_error('Custom Error')

        self.assertRaises(CustomError, schema.validate_request, {})
        schema.validate_request({'name': TEST_NAME})

        self.assertRaises(CustomError, schema.make_response, {})
        schema.make_response({'name': TEST_NAME, 'time': time.time()})
Ejemplo n.º 23
0
 def test_shema_make_error_custom_error_class(self):
     schema = Schema(schemas.index)
     schema.validate_request({"name": "Igor"})
     with self.assertRaisesRegexp(CustomError, "Something"):
         raise schema.make_error("Something", error_class=CustomError)
Ejemplo n.º 24
0
 def test_schema_array_empty(self):
     schema = Schema(schemas.array)
     assert schema.validate_request([]) == []
     assert schema.make_response([]) == []
Ejemplo n.º 25
0
 def test_schema_array(self):
     schema = Schema(schemas.array)
     assert schema.validate_request(TEST_ARRAY) == TEST_ARRAY
     assert schema.make_response(TEST_ARRAY) == TEST_ARRAY
Ejemplo n.º 26
0
 def test_schema_no_response_defined(self, module=None):
     schema = Schema(module or schemas.no_response)
     schema.validate_request({})
     data = schema.make_response({'dummy': True})
     self.assertEqual(data, {'dummy': True})
Ejemplo n.º 27
0
 def test_schema_custom_response_class(self):
     schema = Schema(schemas.project_page, response_factory=web.Response)
     schema.validate_request({'project_id': 1})
     response = schema.make_response(status=204)
     self.assertIsInstance(response, web.Response)
     self.assertEqual(response.status, 204)
Ejemplo n.º 28
0
 def test_schema_empty_response(self):
     schema = Schema(schemas.null_response)
     schema.validate_request({})
     self.assertRaises(SchemaError, schema.make_response)
     self.assertRaises(SchemaError, schema.make_response, status=204)
Ejemplo n.º 29
0
 def test_shema_make_error_custom_error_class(self):
     schema = Schema(schemas.index)
     schema.validate_request({'name': 'Igor'})
     with self.assertRaisesRegexp(CustomError, 'Something'):
         raise schema.make_error('Something', error_class=CustomError)
Ejemplo n.º 30
0
 def test_schema_empty_response(self):
     schema = Schema(schemas.null_response)
     schema.validate_request({})
     self.assertRaises(SchemaError, schema.make_response)
     self.assertRaises(SchemaError, schema.make_response, status=204)
Ejemplo n.º 31
0
 def test_shema_make_error_custom_error_class(self):
     schema = Schema(schemas.index)
     schema.validate_request({'name': 'Igor'})
     with self.assertRaisesRegexp(CustomError, 'Something'):
         raise schema.make_error('Something', error_class=CustomError)
Ejemplo n.º 32
0
 def test_schema_make_response_request_not_validated(self):
     schema = Schema(schemas.index)
     self.assertRaises(SchemaError, schema.make_response, {
         'name': 'world',
         'time': time.time()
     })
Ejemplo n.º 33
0
 def test_schema_custom_response_class(self):
     schema = Schema(schemas.project_page, response_factory=web.Response)
     schema.validate_request({'project_id': 1})
     response = schema.make_response(status=204)
     self.assertIsInstance(response, web.Response)
     self.assertEqual(response.status, 204)
Ejemplo n.º 34
0
 def test_schema_no_request_defined(self, module=None):
     schema = Schema(module or schemas.no_request)
     self.assertRaises(SchemaError, schema.validate_request, {})
Ejemplo n.º 35
0
 def test_schema_no_response_defined(self, module=None):
     schema = Schema(module or schemas.no_response)
     schema.validate_request({})
     data = schema.make_response({'dummy': True})
     self.assertEqual(data, {'dummy': True})
Ejemplo n.º 36
0
 def test_schema_multiple_request_data_merged_class(self):
     schema = Schema(schemas.project_page)
     data = schema.validate_request(
         {"project_id": 1}, {"archived": True}, {"include_stories": False}, merged_class=types.MappingProxyType
     )
     self.assertEqual(data, types.MappingProxyType({"project_id": 1, "archived": True, "include_stories": False}))