openapi-core/tests/unit/schema/test_schemas.py

1067 lines
30 KiB
Python
Raw Normal View History

import datetime
2019-02-26 10:37:49 +00:00
import uuid
2017-09-21 11:51:37 +00:00
import mock
import pytest
2018-08-21 17:33:24 +00:00
from openapi_core.extensions.models.models import Model
from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType
2018-08-21 17:33:24 +00:00
from openapi_core.schema.schemas.exceptions import (
2019-10-20 12:00:14 +00:00
InvalidSchemaValue, OpenAPISchemaError, UnmarshallerStrictTypeError,
2019-10-20 13:38:41 +00:00
UnmarshalValueError, UnmarshalError, InvalidCustomFormatSchemaValue,
2019-10-20 12:00:14 +00:00
FormatterNotFoundError,
2018-08-21 17:33:24 +00:00
)
2018-04-17 12:18:40 +00:00
from openapi_core.schema.schemas.models import Schema
2017-09-21 11:51:37 +00:00
2018-08-22 10:51:06 +00:00
from six import b, u
2017-09-21 11:51:37 +00:00
2017-10-17 13:02:21 +00:00
class TestSchemaIteritems(object):
2017-09-21 11:51:37 +00:00
@pytest.fixture
def schema(self):
properties = {
'application/json': mock.sentinel.application_json,
'text/csv': mock.sentinel.text_csv,
}
return Schema('object', properties=properties)
@property
2017-10-17 13:02:21 +00:00
def test_valid(self, schema):
2019-06-18 11:39:07 +00:00
for name in schema.properties:
2017-09-21 11:51:37 +00:00
assert schema[name] == schema.properties[name]
2017-10-17 13:02:21 +00:00
class TestSchemaUnmarshal(object):
2017-10-17 13:33:46 +00:00
def test_deprecated(self):
schema = Schema('string', deprecated=True)
value = 'test'
with pytest.warns(DeprecationWarning):
result = schema.unmarshal(value)
assert result == value
2019-09-03 00:38:19 +00:00
@pytest.mark.parametrize('schema_type', [
'boolean', 'array', 'integer', 'number',
])
def test_non_string_empty_value(self, schema_type):
schema = Schema(schema_type)
value = ''
result = schema.unmarshal(value)
assert result is None
2017-10-17 13:02:21 +00:00
def test_string_valid(self):
schema = Schema('string')
value = 'test'
result = schema.unmarshal(value)
assert result == value
2019-03-22 14:05:54 +00:00
def test_string_format_uuid_valid(self):
schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
value = str(uuid.uuid4())
result = schema.unmarshal(value)
assert result == uuid.UUID(value)
2019-03-22 14:14:42 +00:00
def test_string_format_uuid_uuid_quirks_valid(self):
schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
value = uuid.uuid4()
2019-03-22 14:05:54 +00:00
result = schema.unmarshal(value, strict=False)
2017-10-17 13:02:21 +00:00
assert result == value
Add missing STRING_FORMAT_CALLABLE_GETTER: SchemaFormat.PASSWORD `password` is a valid OpenAPIv3 string format, that is used as a UI hint for frontend clients to mask the input field. It was already present in the `SchemaFormat` enum, but it was not handled in `_unmarshal_string` that uses `STRING_FORMAT_CALLABLE_GETTER` to decide how to unmarshal a string, which would result in an error like this one: ``` Traceback (most recent call last): [... snip ...] File ".venv/lib/python3.7/site-packages/openapi_core/validation/request/validators.py", line 37, in validate body, body_errors = self._get_body(request, operation) File ".venv/lib/python3.7/site-packages/openapi_core/validation/request/validators.py", line 82, in _get_body body = media_type.unmarshal(raw_body, self.custom_formatters) File ".venv/lib/python3.7/site-packages/openapi_core/schema/media_types/models.py", line 45, in unmarshal unmarshalled = self.schema.unmarshal(deserialized, custom_formatters=custom_formatters) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 189, in unmarshal casted = self.cast(value, custom_formatters=custom_formatters, strict=strict) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 179, in cast return cast_callable(value) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 295, in _unmarshal_object value, custom_formatters=custom_formatters) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 335, in _unmarshal_properties prop_value, custom_formatters=custom_formatters) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 189, in unmarshal casted = self.cast(value, custom_formatters=custom_formatters, strict=strict) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 179, in cast return cast_callable(value) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 295, in _unmarshal_object value, custom_formatters=custom_formatters) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 335, in _unmarshal_properties prop_value, custom_formatters=custom_formatters) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 189, in unmarshal casted = self.cast(value, custom_formatters=custom_formatters, strict=strict) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 179, in cast return cast_callable(value) File ".venv/lib/python3.7/site-packages/openapi_core/schema/schemas/models.py", line 215, in _unmarshal_string formatstring = self.STRING_FORMAT_CALLABLE_GETTER[schema_format] KeyError: <SchemaFormat.PASSWORD: 'password'> ```
2019-04-26 19:22:54 +00:00
def test_string_format_password(self):
schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.PASSWORD)
value = 'password'
result = schema.unmarshal(value)
assert result == 'password'
2019-03-22 01:51:47 +00:00
def test_string_float_invalid(self):
schema = Schema('string')
value = 1.23
2019-10-20 12:00:14 +00:00
with pytest.raises(UnmarshallerStrictTypeError):
2019-03-22 01:51:47 +00:00
schema.unmarshal(value)
2017-10-17 13:02:21 +00:00
def test_string_none(self):
schema = Schema('string')
value = None
2019-10-20 13:38:41 +00:00
with pytest.raises(UnmarshalError):
2017-10-17 13:02:21 +00:00
schema.unmarshal(value)
def test_string_default(self):
default_value = 'default'
schema = Schema('string', default=default_value)
value = None
2019-10-20 13:38:41 +00:00
with pytest.raises(UnmarshalError):
2017-10-17 13:02:21 +00:00
schema.unmarshal(value)
def test_string_default_nullable(self):
default_value = 'default'
schema = Schema('string', default=default_value, nullable=True)
value = None
result = schema.unmarshal(value)
assert result == default_value
def test_string_format_date(self):
schema = Schema('string', schema_format='date')
value = '2018-01-02'
result = schema.unmarshal(value)
assert result == datetime.date(2018, 1, 2)
2018-08-22 10:51:06 +00:00
def test_string_format_datetime(self):
schema = Schema('string', schema_format='date-time')
value = '2018-01-02T00:00:00Z'
2018-08-22 10:51:06 +00:00
result = schema.unmarshal(value)
assert result == datetime.datetime(2018, 1, 2, 0, 0)
2018-08-22 10:51:06 +00:00
def test_string_format_custom(self):
2019-09-03 00:38:19 +00:00
def custom_formatter(value):
return 'x-custom'
custom_format = 'custom'
schema = Schema('string', schema_format=custom_format)
value = 'x'
2019-09-03 00:38:19 +00:00
result = schema.unmarshal(
value, custom_formatters={custom_format: custom_formatter})
assert result == custom_formatter(value)
2019-09-03 00:38:19 +00:00
def test_string_format_custom_value_error(self):
def custom_formatter(value):
raise ValueError
custom_format = 'custom'
schema = Schema('string', schema_format=custom_format)
value = 'x'
2019-10-20 12:00:14 +00:00
with pytest.raises(InvalidCustomFormatSchemaValue):
2019-09-03 00:38:19 +00:00
schema.unmarshal(
value, custom_formatters={custom_format: custom_formatter})
def test_string_format_unknown(self):
unknown_format = 'unknown'
schema = Schema('string', schema_format=unknown_format)
value = 'x'
2018-08-22 10:51:06 +00:00
with pytest.raises(OpenAPISchemaError):
schema.unmarshal(value)
def test_string_format_invalid_value(self):
custom_format = 'custom'
schema = Schema('string', schema_format=custom_format)
value = 'x'
with pytest.raises(
2019-10-20 12:00:14 +00:00
FormatterNotFoundError,
message=(
'Formatter not found for custom format to unmarshal value x'
),
):
schema.unmarshal(value)
2017-10-17 13:02:21 +00:00
def test_integer_valid(self):
schema = Schema('integer')
2019-03-22 01:51:47 +00:00
value = 123
2017-10-17 13:02:21 +00:00
result = schema.unmarshal(value)
assert result == int(value)
2019-03-22 01:51:47 +00:00
def test_integer_string_invalid(self):
schema = Schema('integer')
value = '123'
2019-10-20 12:00:14 +00:00
with pytest.raises(UnmarshallerStrictTypeError):
2019-03-22 01:51:47 +00:00
schema.unmarshal(value)
2017-10-17 13:23:26 +00:00
def test_integer_enum_invalid(self):
2017-10-17 13:49:00 +00:00
schema = Schema('integer', enum=[1, 2, 3])
2017-10-17 13:23:26 +00:00
value = '123'
2019-10-20 13:38:41 +00:00
with pytest.raises(UnmarshalError):
2017-10-17 13:23:26 +00:00
schema.unmarshal(value)
def test_integer_enum(self):
2017-10-17 13:49:00 +00:00
schema = Schema('integer', enum=[1, 2, 3])
2019-03-22 01:51:47 +00:00
value = 2
2017-10-17 13:23:26 +00:00
result = schema.unmarshal(value)
assert result == int(value)
2019-03-22 01:51:47 +00:00
def test_integer_enum_string_invalid(self):
schema = Schema('integer', enum=[1, 2, 3])
value = '2'
2019-10-20 13:38:41 +00:00
with pytest.raises(UnmarshalError):
2019-03-22 01:51:47 +00:00
schema.unmarshal(value)
2017-10-17 13:02:21 +00:00
def test_integer_default(self):
default_value = '123'
schema = Schema('integer', default=default_value)
value = None
2019-10-20 13:38:41 +00:00
with pytest.raises(UnmarshalError):
2017-10-17 13:02:21 +00:00
schema.unmarshal(value)
def test_integer_default_nullable(self):
default_value = '123'
schema = Schema('integer', default=default_value, nullable=True)
value = None
result = schema.unmarshal(value)
assert result == default_value
def test_integer_invalid(self):
schema = Schema('integer')
value = 'abc'
2019-10-20 12:00:14 +00:00
with pytest.raises(UnmarshallerStrictTypeError):
2017-10-17 13:02:21 +00:00
schema.unmarshal(value)
2018-08-17 14:54:01 +00:00
2019-03-22 01:51:47 +00:00
def test_array_valid(self):
schema = Schema('array', items=Schema('integer'))
value = [1, 2, 3]
result = schema.unmarshal(value)
assert result == value
def test_array_of_string_string_invalid(self):
schema = Schema('array', items=Schema('string'))
value = '123'
2019-10-20 13:38:41 +00:00
with pytest.raises(UnmarshalValueError):
2019-03-22 01:51:47 +00:00
schema.unmarshal(value)
def test_array_of_integer_string_invalid(self):
schema = Schema('array', items=Schema('integer'))
value = '123'
2019-10-20 13:38:41 +00:00
with pytest.raises(UnmarshalValueError):
2019-03-22 01:51:47 +00:00
schema.unmarshal(value)
def test_boolean_valid(self):
schema = Schema('boolean')
value = True
result = schema.unmarshal(value)
assert result == value
def test_boolean_string_invalid(self):
schema = Schema('boolean')
value = 'True'
2019-10-20 12:00:14 +00:00
with pytest.raises(UnmarshallerStrictTypeError):
2019-03-22 01:51:47 +00:00
schema.unmarshal(value)
def test_number_valid(self):
schema = Schema('number')
value = 1.23
result = schema.unmarshal(value)
assert result == value
def test_number_string_invalid(self):
schema = Schema('number')
value = '1.23'
2019-10-20 12:00:14 +00:00
with pytest.raises(UnmarshallerStrictTypeError):
2019-03-22 01:51:47 +00:00
schema.unmarshal(value)
def test_number_int(self):
2019-03-22 01:51:47 +00:00
schema = Schema('number')
value = 1
result = schema.unmarshal(value)
2019-03-22 01:51:47 +00:00
2019-05-21 11:54:13 +00:00
assert result == 1
assert type(result) == int
def test_number_float(self):
schema = Schema('number')
value = 1.2
result = schema.unmarshal(value)
assert result == 1.2
assert type(result) == float
def test_number_format_float(self):
schema = Schema('number', schema_format='float')
value = 1.2
result = schema.unmarshal(value)
assert result == 1.2
def test_number_format_double(self):
schema = Schema('number', schema_format='double')
value = 1.2
result = schema.unmarshal(value)
assert result == 1.2
2019-03-22 01:51:47 +00:00
2019-03-02 20:44:01 +00:00
def test_schema_any_one_of(self):
schema = Schema(one_of=[
Schema('string'),
Schema('array', items=Schema('string')),
])
assert schema.unmarshal(['hello']) == ['hello']
def test_schema_any(self):
schema = Schema()
assert schema.unmarshal('string') == 'string'
2018-08-17 14:54:01 +00:00
2019-09-11 21:47:16 +00:00
class TestSchemaValidate(object):
2018-08-17 14:54:01 +00:00
@pytest.mark.parametrize('schema_type', [
'boolean', 'array', 'integer', 'number', 'string',
])
def test_null(self, schema_type):
schema = Schema(schema_type)
value = None
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-17 14:54:01 +00:00
@pytest.mark.parametrize('schema_type', [
'boolean', 'array', 'integer', 'number', 'string',
])
def test_nullable(self, schema_type):
schema = Schema(schema_type, nullable=True)
value = None
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-17 14:54:01 +00:00
assert result is None
2019-09-11 21:47:16 +00:00
@pytest.mark.xfail(
reason="validation does not care about custom formats atm")
2019-09-03 00:38:19 +00:00
def test_string_format_custom_missing(self):
custom_format = 'custom'
schema = Schema('string', schema_format=custom_format)
value = 'x'
with pytest.raises(OpenAPISchemaError):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2019-09-03 00:38:19 +00:00
2018-08-17 14:54:01 +00:00
@pytest.mark.parametrize('value', [False, True])
def test_boolean(self, value):
schema = Schema('boolean')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-17 14:54:01 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-17 14:54:01 +00:00
2018-08-22 10:51:06 +00:00
@pytest.mark.parametrize('value', [1, 3.14, u('true'), [True, False]])
2018-08-17 14:54:01 +00:00
def test_boolean_invalid(self, value):
schema = Schema('boolean')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-17 14:54:01 +00:00
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [(1, 2)])
2018-08-22 09:29:39 +00:00
def test_array_no_schema(self, value):
2018-08-17 14:54:01 +00:00
schema = Schema('array')
2018-08-22 09:29:39 +00:00
with pytest.raises(OpenAPISchemaError):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-22 09:29:39 +00:00
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [[1, 2]])
2018-08-22 09:29:39 +00:00
def test_array(self, value):
schema = Schema('array', items=Schema('integer'))
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-17 14:54:01 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-17 14:54:01 +00:00
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [False, 1, 3.14, u('true'), (3, 4)])
2018-08-17 14:54:01 +00:00
def test_array_invalid(self, value):
schema = Schema('array')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-17 14:54:01 +00:00
@pytest.mark.parametrize('value', [1, 3])
def test_integer(self, value):
schema = Schema('integer')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-17 14:54:01 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-17 14:54:01 +00:00
2018-08-22 10:51:06 +00:00
@pytest.mark.parametrize('value', [False, 3.14, u('true'), [1, 2]])
2018-08-17 14:54:01 +00:00
def test_integer_invalid(self, value):
schema = Schema('integer')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-17 14:54:01 +00:00
@pytest.mark.parametrize('value', [0, 1, 2])
def test_integer_minimum_invalid(self, value):
schema = Schema('integer', minimum=3)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [4, 5, 6])
def test_integer_minimum(self, value):
schema = Schema('integer', minimum=3)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [4, 5, 6])
def test_integer_maximum_invalid(self, value):
schema = Schema('integer', maximum=3)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [0, 1, 2])
def test_integer_maximum(self, value):
schema = Schema('integer', maximum=3)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [1, 2, 4])
def test_integer_multiple_of_invalid(self, value):
schema = Schema('integer', multiple_of=3)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [3, 6, 18])
def test_integer_multiple_of(self, value):
schema = Schema('integer', multiple_of=3)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-17 14:54:01 +00:00
@pytest.mark.parametrize('value', [1, 3.14])
def test_number(self, value):
schema = Schema('number')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-17 14:54:01 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-17 14:54:01 +00:00
@pytest.mark.parametrize('value', [False, 'true', [1, 3]])
def test_number_invalid(self, value):
schema = Schema('number')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-17 14:54:01 +00:00
@pytest.mark.parametrize('value', [0, 1, 2])
def test_number_minimum_invalid(self, value):
schema = Schema('number', minimum=3)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [3, 4, 5])
def test_number_minimum(self, value):
schema = Schema('number', minimum=3)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [1, 2, 3])
def test_number_exclusive_minimum_invalid(self, value):
schema = Schema('number', minimum=3, exclusive_minimum=3)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [4, 5, 6])
def test_number_exclusive_minimum(self, value):
schema = Schema('number', minimum=3)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [4, 5, 6])
def test_number_maximum_invalid(self, value):
schema = Schema('number', maximum=3)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [1, 2, 3])
def test_number_maximum(self, value):
schema = Schema('number', maximum=3)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [3, 4, 5])
def test_number_exclusive_maximum_invalid(self, value):
schema = Schema('number', maximum=3, exclusive_maximum=True)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [0, 1, 2])
def test_number_exclusive_maximum(self, value):
schema = Schema('number', maximum=3, exclusive_maximum=True)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [1, 2, 4])
def test_number_multiple_of_invalid(self, value):
schema = Schema('number', multiple_of=3)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [3, 6, 18])
def test_number_multiple_of(self, value):
schema = Schema('number', multiple_of=3)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [u('true'), b('test')])
2018-08-17 14:54:01 +00:00
def test_string(self, value):
schema = Schema('string')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-17 14:54:01 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-17 14:54:01 +00:00
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [False, 1, 3.14, [1, 3]])
2018-08-17 14:54:01 +00:00
def test_string_invalid(self, value):
schema = Schema('string')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-21 17:33:24 +00:00
2018-08-22 10:51:06 +00:00
@pytest.mark.parametrize('value', [
b('true'), u('test'), False, 1, 3.14, [1, 3],
datetime.datetime(1989, 1, 2),
])
def test_string_format_date_invalid(self, value):
schema = Schema('string', schema_format='date')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-22 10:51:06 +00:00
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
u('1989-01-02'), u('2018-01-02'),
2018-08-22 10:51:06 +00:00
])
def test_string_format_date(self, value):
schema = Schema('string', schema_format='date')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-22 10:51:06 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-22 10:51:06 +00:00
2019-02-26 10:37:49 +00:00
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
u('12345678-1234-5678-1234-567812345678'),
2019-02-26 10:37:49 +00:00
])
def test_string_format_uuid(self, value):
schema = Schema('string', schema_format='uuid')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-02-26 10:37:49 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2019-02-26 10:37:49 +00:00
@pytest.mark.parametrize('value', [
b('true'), u('true'), False, 1, 3.14, [1, 3],
datetime.date(2018, 1, 2), datetime.datetime(2018, 1, 2, 23, 59, 59),
])
def test_string_format_uuid_invalid(self, value):
schema = Schema('string', schema_format='uuid')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2019-02-26 10:37:49 +00:00
2018-08-22 10:51:06 +00:00
@pytest.mark.parametrize('value', [
b('true'), u('true'), False, 1, 3.14, [1, 3],
2019-09-11 21:47:16 +00:00
u('1989-01-02'),
2018-08-22 10:51:06 +00:00
])
def test_string_format_datetime_invalid(self, value):
schema = Schema('string', schema_format='date-time')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-22 10:51:06 +00:00
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
u('1989-01-02T00:00:00Z'),
u('2018-01-02T23:59:59Z'),
2018-08-22 10:51:06 +00:00
])
@mock.patch(
'openapi_core.schema.schemas._format.'
'DATETIME_HAS_STRICT_RFC3339', True
)
@mock.patch(
'openapi_core.schema.schemas._format.'
'DATETIME_HAS_ISODATE', False
)
def test_string_format_datetime_strict_rfc3339(self, value):
schema = Schema('string', schema_format='date-time')
result = schema.validate(value)
assert result is None
@pytest.mark.parametrize('value', [
u('1989-01-02T00:00:00Z'),
u('2018-01-02T23:59:59Z'),
])
@mock.patch(
'openapi_core.schema.schemas._format.'
'DATETIME_HAS_STRICT_RFC3339', False
)
@mock.patch(
'openapi_core.schema.schemas._format.'
'DATETIME_HAS_ISODATE', True
)
def test_string_format_datetime_isodate(self, value):
2018-08-22 10:51:06 +00:00
schema = Schema('string', schema_format='date-time')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-22 10:51:06 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-22 10:51:06 +00:00
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
u('true'), False, 1, 3.14, [1, 3], u('1989-01-02'),
u('1989-01-02T00:00:00Z'),
2018-08-22 10:51:06 +00:00
])
def test_string_format_binary_invalid(self, value):
schema = Schema('string', schema_format='binary')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-22 10:51:06 +00:00
@pytest.mark.parametrize('value', [
b('stream'), b('text'),
])
def test_string_format_binary(self, value):
schema = Schema('string', schema_format='binary')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-22 10:51:06 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-22 10:51:06 +00:00
2019-02-26 16:49:25 +00:00
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
b('dGVzdA=='), u('dGVzdA=='),
2019-02-26 16:49:25 +00:00
])
2019-09-11 21:47:16 +00:00
def test_string_format_byte(self, value):
2019-02-26 16:49:25 +00:00
schema = Schema('string', schema_format='byte')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
assert result is None
2019-02-26 16:49:25 +00:00
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
u('tsssst'), b('tsssst'), b('tesddddsdsdst'),
2019-02-26 16:49:25 +00:00
])
2019-09-11 21:47:16 +00:00
def test_string_format_byte_invalid(self, value):
2019-02-26 16:49:25 +00:00
schema = Schema('string', schema_format='byte')
2019-09-11 21:47:16 +00:00
with pytest.raises(OpenAPISchemaError):
schema.validate(value)
2019-02-26 16:49:25 +00:00
2018-08-22 10:51:06 +00:00
@pytest.mark.parametrize('value', [
u('test'), b('stream'), datetime.date(1989, 1, 2),
datetime.datetime(1989, 1, 2, 0, 0, 0),
])
def test_string_format_unknown(self, value):
unknown_format = 'unknown'
schema = Schema('string', schema_format=unknown_format)
with pytest.raises(OpenAPISchemaError):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [u(""), u("a"), u("ab")])
def test_string_min_length_invalid(self, value):
schema = Schema('string', min_length=3)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [u("abc"), u("abcd")])
def test_string_min_length(self, value):
schema = Schema('string', min_length=3)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [u(""), ])
def test_string_max_length_invalid_schema(self, value):
schema = Schema('string', max_length=-1)
with pytest.raises(OpenAPISchemaError):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [u("ab"), u("abc")])
def test_string_max_length_invalid(self, value):
schema = Schema('string', max_length=1)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [u(""), u("a")])
def test_string_max_length(self, value):
schema = Schema('string', max_length=1)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [u("foo"), u("bar")])
def test_string_pattern_invalid(self, value):
schema = Schema('string', pattern='baz')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [u("bar"), u("foobar")])
def test_string_pattern(self, value):
schema = Schema('string', pattern='bar')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
2018-08-21 17:33:24 +00:00
@pytest.mark.parametrize('value', ['true', False, 1, 3.14, [1, 3]])
def test_object_not_an_object(self, value):
schema = Schema('object')
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2018-08-21 17:33:24 +00:00
@pytest.mark.parametrize('value', [Model(), ])
def test_object_multiple_one_of(self, value):
one_of = [
Schema('object'), Schema('object'),
]
schema = Schema('object', one_of=one_of)
2019-09-11 21:47:16 +00:00
with pytest.raises(InvalidSchemaValue):
schema.validate(value)
2018-08-21 17:33:24 +00:00
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [{}, ])
2018-08-21 17:33:24 +00:00
def test_object_defferent_type_one_of(self, value):
one_of = [
Schema('integer'), Schema('string'),
]
schema = Schema('object', one_of=one_of)
2019-09-11 21:47:16 +00:00
with pytest.raises(InvalidSchemaValue):
schema.validate(value)
2018-08-21 17:33:24 +00:00
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [{}, ])
2018-08-21 17:33:24 +00:00
def test_object_no_one_of(self, value):
one_of = [
Schema(
'object',
properties={'test1': Schema('string')},
required=['test1', ],
),
Schema(
'object',
properties={'test2': Schema('string')},
required=['test2', ],
),
]
schema = Schema('object', one_of=one_of)
2019-09-11 21:47:16 +00:00
with pytest.raises(InvalidSchemaValue):
schema.validate(value)
2018-08-21 17:33:24 +00:00
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
{
'foo': u("FOO"),
2019-09-11 21:47:16 +00:00
},
{
'foo': u("FOO"),
'bar': u("BAR"),
2019-09-11 21:47:16 +00:00
},
])
def test_unambiguous_one_of(self, value):
one_of = [
Schema(
'object',
properties={
'foo': Schema('string'),
},
additional_properties=False,
required=['foo'],
),
Schema(
'object',
properties={
'foo': Schema('string'),
'bar': Schema('string'),
},
additional_properties=False,
required=['foo', 'bar'],
),
]
schema = Schema('object', one_of=one_of)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [{}, ])
2018-08-21 17:33:24 +00:00
def test_object_default_property(self, value):
schema = Schema('object', default='value1')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2018-08-21 17:33:24 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [Model(), ])
def test_object_min_properties_invalid_schema(self, value):
schema = Schema('object', min_properties=-1)
with pytest.raises(OpenAPISchemaError):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
{'a': 1},
{'a': 1, 'b': 2},
{'a': 1, 'b': 2, 'c': 3},
])
def test_object_min_properties_invalid(self, value):
schema = Schema(
'object',
properties={k: Schema('number')
for k in ['a', 'b', 'c']},
min_properties=4,
)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
{'a': 1},
{'a': 1, 'b': 2},
{'a': 1, 'b': 2, 'c': 3},
])
def test_object_min_properties(self, value):
schema = Schema(
'object',
properties={k: Schema('number')
for k in ['a', 'b', 'c']},
min_properties=1,
)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [Model(), ])
def test_object_max_properties_invalid_schema(self, value):
schema = Schema('object', max_properties=-1)
with pytest.raises(OpenAPISchemaError):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
{'a': 1},
{'a': 1, 'b': 2},
{'a': 1, 'b': 2, 'c': 3},
])
def test_object_max_properties_invalid(self, value):
schema = Schema(
'object',
properties={k: Schema('number')
for k in ['a', 'b', 'c']},
max_properties=0,
)
with pytest.raises(InvalidSchemaValue):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
{'a': 1},
{'a': 1, 'b': 2},
{'a': 1, 'b': 2, 'c': 3},
])
def test_object_max_properties(self, value):
schema = Schema(
'object',
properties={k: Schema('number')
for k in ['a', 'b', 'c']},
max_properties=3,
)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [{'additional': 1}, ])
def test_object_additional_propetries(self, value):
schema = Schema('object')
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
assert result is None
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [{'additional': 1}, ])
def test_object_additional_propetries_false(self, value):
schema = Schema('object', additional_properties=False)
2019-09-11 21:47:16 +00:00
with pytest.raises(InvalidSchemaValue):
schema.validate(value)
2019-09-11 21:47:16 +00:00
@pytest.mark.parametrize('value', [{'additional': 1}, ])
def test_object_additional_propetries_object(self, value):
additional_properties = Schema('integer')
schema = Schema('object', additional_properties=additional_properties)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [[], [1], [1, 2]])
def test_list_min_items_invalid(self, value):
schema = Schema(
'array',
items=Schema('number'),
min_items=3,
)
with pytest.raises(Exception):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [[], [1], [1, 2]])
def test_list_min_items(self, value):
schema = Schema(
'array',
items=Schema('number'),
min_items=0,
)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-09-11 21:47:16 +00:00
assert result is None
@pytest.mark.parametrize('value', [[], ])
def test_list_max_items_invalid_schema(self, value):
schema = Schema(
'array',
items=Schema('number'),
max_items=-1,
)
with pytest.raises(OpenAPISchemaError):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [[1, 2], [2, 3, 4]])
def test_list_max_items_invalid(self, value):
schema = Schema(
'array',
items=Schema('number'),
max_items=1,
)
with pytest.raises(Exception):
2019-09-11 21:47:16 +00:00
schema.validate(value)
@pytest.mark.parametrize('value', [[1, 2, 1], [2, 2]])
def test_list_unique_items_invalid(self, value):
schema = Schema(
'array',
items=Schema('number'),
unique_items=True,
)
with pytest.raises(Exception):
2019-09-11 21:47:16 +00:00
schema.validate(value)
2019-03-22 01:51:47 +00:00
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
{
2019-03-22 01:51:47 +00:00
'someint': 123,
2019-09-11 21:47:16 +00:00
},
{
2019-03-22 01:51:47 +00:00
'somestr': u('content'),
2019-09-11 21:47:16 +00:00
},
{
2019-03-22 01:51:47 +00:00
'somestr': u('content'),
'someint': 123,
2019-09-11 21:47:16 +00:00
},
2019-03-22 01:51:47 +00:00
])
def test_object_with_properties(self, value):
schema = Schema(
'object',
properties={
'somestr': Schema('string'),
'someint': Schema('integer'),
},
)
2019-09-11 21:47:16 +00:00
result = schema.validate(value)
2019-03-22 01:51:47 +00:00
2019-09-11 21:47:16 +00:00
assert result is None
2019-03-22 01:51:47 +00:00
@pytest.mark.parametrize('value', [
2019-09-11 21:47:16 +00:00
{
2019-03-22 01:51:47 +00:00
'somestr': {},
'someint': 123,
2019-09-11 21:47:16 +00:00
},
{
2019-03-22 01:51:47 +00:00
'somestr': [
'content1', 'content2'
],
'someint': 123,
2019-09-11 21:47:16 +00:00
},
{
2019-03-22 01:51:47 +00:00
'somestr': 123,
'someint': 123,
2019-09-11 21:47:16 +00:00
},
{
2019-03-22 01:51:47 +00:00
'somestr': 'content',
'someint': 123,
'not_in_scheme_prop': 123,
2019-09-11 21:47:16 +00:00
},
2019-03-22 01:51:47 +00:00
])
def test_object_with_invalid_properties(self, value):
schema = Schema(
'object',
properties={
'somestr': Schema('string'),
'someint': Schema('integer'),
},
additional_properties=False,
2019-03-22 01:51:47 +00:00
)
with pytest.raises(Exception):
2019-09-11 21:47:16 +00:00
schema.validate(value)