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

158 lines
4.1 KiB
Python
Raw Normal View History

import datetime
2017-09-21 11:51:37 +00:00
import mock
import pytest
2018-04-18 10:39:03 +00:00
from openapi_core.schema.schemas.exceptions import InvalidSchemaValue
2018-04-17 12:18:40 +00:00
from openapi_core.schema.schemas.models import Schema
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):
2017-09-21 11:51:37 +00:00
for name in schema.properties.keys():
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
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
def test_string_none(self):
schema = Schema('string')
value = None
2018-04-18 10:39:03 +00:00
with pytest.raises(InvalidSchemaValue):
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
2018-04-18 10:39:03 +00:00
with pytest.raises(InvalidSchemaValue):
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)
def test_string_format_custom(self):
custom_format = 'custom'
schema = Schema('string', schema_format=custom_format)
value = 'x'
with mock.patch.dict(
Schema.FORMAT_CALLABLE_GETTER,
{custom_format: lambda x: x + '-custom'},
):
result = schema.unmarshal(value)
assert result == 'x-custom'
def test_string_format_unknown(self):
unknown_format = 'unknown'
schema = Schema('string', schema_format=unknown_format)
value = 'x'
result = schema.unmarshal(value)
assert result == 'x'
def test_string_format_invalid_value(self):
custom_format = 'custom'
schema = Schema('string', schema_format=custom_format)
value = 'x'
with mock.patch.dict(
Schema.FORMAT_CALLABLE_GETTER,
{custom_format: mock.Mock(side_effect=ValueError())},
), pytest.raises(
InvalidSchemaValue, message='Failed to format value'
):
schema.unmarshal(value)
2017-10-17 13:02:21 +00:00
def test_integer_valid(self):
schema = Schema('integer')
value = '123'
result = schema.unmarshal(value)
assert result == int(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'
2018-04-18 10:39:03 +00:00
with pytest.raises(InvalidSchemaValue):
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])
2017-10-17 13:23:26 +00:00
value = '2'
result = schema.unmarshal(value)
assert result == int(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
2018-04-18 10:39:03 +00:00
with pytest.raises(InvalidSchemaValue):
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'
2018-04-18 10:39:03 +00:00
with pytest.raises(InvalidSchemaValue):
2017-10-17 13:02:21 +00:00
schema.unmarshal(value)