2017-09-21 11:51:37 +00:00
|
|
|
import mock
|
|
|
|
import pytest
|
|
|
|
|
2017-10-17 13:23:26 +00:00
|
|
|
from openapi_core.exceptions import InvalidValueType, InvalidValue
|
2017-09-21 11:51:37 +00:00
|
|
|
from openapi_core.schemas import Schema
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
with pytest.raises(InvalidValueType):
|
|
|
|
schema.unmarshal(value)
|
|
|
|
|
|
|
|
def test_string_default(self):
|
|
|
|
default_value = 'default'
|
|
|
|
schema = Schema('string', default=default_value)
|
|
|
|
value = None
|
|
|
|
|
|
|
|
with pytest.raises(InvalidValueType):
|
|
|
|
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_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):
|
|
|
|
schema = Schema('integer', enum=[1,2,3])
|
|
|
|
value = '123'
|
|
|
|
|
|
|
|
with pytest.raises(InvalidValue):
|
|
|
|
schema.unmarshal(value)
|
|
|
|
|
|
|
|
def test_integer_enum(self):
|
|
|
|
schema = Schema('integer', enum=[1,2,3])
|
|
|
|
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
|
|
|
|
|
|
|
|
with pytest.raises(InvalidValueType):
|
|
|
|
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'
|
|
|
|
|
|
|
|
with pytest.raises(InvalidValueType):
|
|
|
|
schema.unmarshal(value)
|