2020-02-03 13:05:44 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-02-03 13:34:43 +00:00
|
|
|
from openapi_core.deserializing.exceptions import DeserializeError
|
|
|
|
from openapi_core.deserializing.media_types.factories import (
|
|
|
|
MediaTypeDeserializersFactory,
|
|
|
|
)
|
2020-02-03 13:05:44 +00:00
|
|
|
from openapi_core.deserializing.parameters.factories import (
|
2020-02-03 13:34:43 +00:00
|
|
|
ParameterDeserializersFactory,
|
|
|
|
)
|
2020-02-03 13:05:44 +00:00
|
|
|
from openapi_core.deserializing.parameters.exceptions import (
|
|
|
|
EmptyParameterValue,
|
|
|
|
)
|
2020-02-03 13:34:43 +00:00
|
|
|
from openapi_core.schema.media_types.models import MediaType
|
2020-02-03 13:05:44 +00:00
|
|
|
from openapi_core.schema.parameters.models import Parameter
|
|
|
|
|
|
|
|
|
|
|
|
class TestParameterDeserialise(object):
|
|
|
|
|
2020-02-03 13:34:43 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def deserializer_factory(self):
|
|
|
|
def create_deserializer(param):
|
|
|
|
return ParameterDeserializersFactory().create(param)
|
|
|
|
return create_deserializer
|
|
|
|
|
2020-02-03 13:05:44 +00:00
|
|
|
def test_deprecated(self, deserializer_factory):
|
|
|
|
param = Parameter('param', 'query', deprecated=True)
|
|
|
|
value = 'test'
|
|
|
|
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
result = deserializer_factory(param)(value)
|
|
|
|
|
|
|
|
assert result == value
|
|
|
|
|
|
|
|
def test_query_empty(self, deserializer_factory):
|
|
|
|
param = Parameter('param', 'query')
|
|
|
|
value = ''
|
|
|
|
|
|
|
|
with pytest.raises(EmptyParameterValue):
|
|
|
|
deserializer_factory(param)(value)
|
|
|
|
|
|
|
|
def test_query_valid(self, deserializer_factory):
|
|
|
|
param = Parameter('param', 'query')
|
|
|
|
value = 'test'
|
|
|
|
|
|
|
|
result = deserializer_factory(param)(value)
|
|
|
|
|
|
|
|
assert result == value
|
2020-02-03 13:34:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestMediaTypeDeserialise(object):
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def deserializer_factory(self):
|
|
|
|
def create_deserializer(media_type):
|
|
|
|
return MediaTypeDeserializersFactory().create(media_type)
|
|
|
|
return create_deserializer
|
|
|
|
|
|
|
|
def test_empty(self, deserializer_factory):
|
|
|
|
media_type = MediaType('application/json')
|
|
|
|
value = ''
|
|
|
|
|
|
|
|
with pytest.raises(DeserializeError):
|
|
|
|
deserializer_factory(media_type)(value)
|
|
|
|
|
|
|
|
def test_no_schema_deserialised(self, deserializer_factory):
|
|
|
|
media_type = MediaType('application/json')
|
|
|
|
value = "{}"
|
|
|
|
|
|
|
|
result = deserializer_factory(media_type)(value)
|
|
|
|
|
|
|
|
assert result == {}
|