def test_unmarshal_validation_error(): schema = {'type': str('string')} instance = 123 message = "123 is not of type 'string'" with pytest.raises(ValidationError) as exc_info: SchemaUnmarshaler().unmarshal(instance, schema) assert exc_info.value.errors[0].message == message
def test_unmarshal_array(): schema = {'type': 'array', 'items': {'type': 'string', 'format': 'date'}} instance = ['2018-01-02', '2018-02-03', '2018-03-04'] unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == [ datetime.date(2018, 1, 2), datetime.date(2018, 2, 3), datetime.date(2018, 3, 4), ]
def test_unmarshal_object_properties_and_additional_properties( properties, additional_properties, instance, expected): schema = {'type': 'object'} if properties is not None: schema['properties'] = properties if additional_properties is not None: schema['additionalProperties'] = additional_properties unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == expected
def test_unmarshal_all_of_primitive_string_enum(): schema = { 'allOf': [{ 'type': 'string' }, { 'type': 'string', 'enum': ['a', 'b'] }] } instance = 'a' unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == instance
def test_unmarshal_one_of_or_any_of(schema_type): schema = { schema_type: [ { 'type': 'integer' }, { 'type': 'string', 'format': 'date' }, ] } instance = '2018-01-02' unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == datetime.date(2018, 1, 2)
def test_unmarshal_all_of_primitive_string_pattern(): schema = { 'allOf': [ { 'type': 'string' }, { 'type': 'string', 'pattern': '^[a-z]+$' }, ] } instance = 'abc' unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == instance
def test_unmarshal_all_of(): schema = { 'allOf': [ { 'type': 'object', 'properties': { 'from': { 'type': 'string', 'format': 'date' } }, }, { 'type': 'object', 'properties': { 'from': { 'type': 'string' } } }, { 'type': 'object', 'properties': { 'to': { 'type': 'string' } } }, { 'type': 'object', 'properties': { 'to': { 'type': 'string', 'format': 'date' } }, }, ] } instance = {'from': '2018-01-02', 'to': '2018-01-03'} unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == { 'from': datetime.date(2018, 1, 2), 'to': datetime.date(2018, 1, 3), }
def test_unmarshal_all_of_array(): schema = { 'allOf': [ { 'type': 'array', 'items': { 'type': 'number' } }, { 'type': 'array', 'items': { 'type': 'integer' } }, ] } instance = [1, 2] unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == instance
def test_unmarshal_all_of_array_min_items(): schema = { 'allOf': [ { 'type': 'array', 'items': { 'type': 'number' } }, { 'type': 'array', 'items': {}, 'minItems': 2 }, ] } instance = [1] with pytest.raises(ValidationError) as exc_info: SchemaUnmarshaler().unmarshal(instance, schema) assert exc_info.value.errors[0].message == '[1] is too short'
def test_unmarshal_all_of_required_only(): schema = { 'allOf': [ { 'type': 'object', 'properties': { 'id': { 'type': 'integer' } } }, { 'type': 'object', 'required': [str('id')] }, ] } instance = {} with pytest.raises(ValidationError) as exc_info: SchemaUnmarshaler().unmarshal(instance, schema) assert exc_info.value.errors[0].message == "'id' is a required property"
def process_resource(self, req, resp, resource, params): oas_req = _RequestAdapter(req, params) operation = self._spec.get_operation(oas_req.uri_template, oas_req.method, oas_req.media_type) if operation is None: return schema_unmarshaler = SchemaUnmarshaler(spec=self._spec, formats=self._formats) req.context['oas'] = context = _Context(schema_unmarshaler) user = self._access_control.handle(oas_req, operation) parameters, request_body = unmarshal_request(schema_unmarshaler, oas_req, operation) if 'path' in parameters: params.update(parameters['path']) context.user = user context.parameters = parameters context.request_body = request_body
def test_unmarshal_object(): schema = { 'type': 'object', 'properties': { 'id': { 'type': 'integer' }, 'date': { 'type': 'string', 'format': 'date' }, 'date-default': { 'type': 'string', 'format': 'date', 'default': '2020-01-01', }, }, } instance = {'date': '2018-01-02'} unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == { 'date': datetime.date(2018, 1, 2), 'date-default': datetime.date(2020, 1, 1), }
def test_unmarshal_primitive_without_formats(): schema = {'type': 'string', 'format': 'date'} instance = '2018-01-02' unmarshaled = SchemaUnmarshaler(formats=Formats()).unmarshal( instance, schema) assert unmarshaled == instance
def test_unmarshal_primitive_enum(): schema = {'type': 'string', 'enum': ['a', 'b']} instance = 'a' unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == 'a'
def test_unmarshal_primitive_format(): schema = {'type': 'string', 'format': 'date'} instance = '2018-01-02' unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == datetime.date(2018, 1, 2)
def test_unmarshal_primitive(schema, instance): unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == instance
def test_unmarshal_nullable(schema): instance = None unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled is None
def unmarshal(): return functools.partial(unmarshal_content, SchemaUnmarshaler())
def unmarshal(): return functools.partial(unmarshal_parameters, SchemaUnmarshaler())
def test_unmarshal_all_of_primitive_number_integer(): schema = {'allOf': [{'type': 'number'}, {'type': 'integer'}]} instance = 1 unmarshaled = SchemaUnmarshaler().unmarshal(instance, schema) assert unmarshaled == instance
def unmarshal(): return functools.partial(unmarshal_request_body, SchemaUnmarshaler())