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')
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')
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"})
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})
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})
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')
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})
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, }))
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', })
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', } )
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)
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, }) )
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)
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)
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() })
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})
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)
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()})
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)
def test_schema_array_empty(self): schema = Schema(schemas.array) assert schema.validate_request([]) == [] assert schema.make_response([]) == []
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
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})
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)
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)
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)
def test_schema_make_response_request_not_validated(self): schema = Schema(schemas.index) self.assertRaises(SchemaError, schema.make_response, { 'name': 'world', 'time': time.time() })
def test_schema_no_request_defined(self, module=None): schema = Schema(module or schemas.no_request) self.assertRaises(SchemaError, schema.validate_request, {})
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}))