mirror of
https://github.com/sprockets/sprockets.mixins.mediatype.git
synced 2024-11-21 19:28:38 +00:00
Increase test coverage to 100%
This commit is contained in:
parent
9ff5ca3781
commit
2047fe8d16
3 changed files with 75 additions and 4 deletions
|
@ -72,7 +72,7 @@ warning-is-error = 1
|
|||
strict = 1
|
||||
|
||||
[coverage:report]
|
||||
fail_under = 95
|
||||
fail_under = 100
|
||||
show_missing = 1
|
||||
|
||||
[coverage:run]
|
||||
|
|
|
@ -16,7 +16,7 @@ import collections.abc
|
|||
|
||||
try:
|
||||
import umsgpack
|
||||
except ImportError:
|
||||
except ImportError: # pragma: no cover
|
||||
umsgpack = None # type: ignore
|
||||
|
||||
from sprockets.mixins.mediatype import handlers, type_info
|
||||
|
|
75
tests.py
75
tests.py
|
@ -4,10 +4,12 @@ import json
|
|||
import os
|
||||
import pickle
|
||||
import struct
|
||||
import unittest
|
||||
import typing
|
||||
import unittest.mock
|
||||
import uuid
|
||||
|
||||
from tornado import testing
|
||||
from ietfparse import algorithms
|
||||
from tornado import httputil, testing, web
|
||||
import umsgpack
|
||||
|
||||
from sprockets.mixins.mediatype import content, handlers, transcoders
|
||||
|
@ -61,6 +63,8 @@ def pack_bytes(payload):
|
|||
|
||||
|
||||
class SendResponseTests(testing.AsyncHTTPTestCase):
|
||||
application: typing.Union[None, web.Application]
|
||||
|
||||
def setUp(self):
|
||||
self.application = None
|
||||
super().setUp()
|
||||
|
@ -159,6 +163,18 @@ class SendResponseTests(testing.AsyncHTTPTestCase):
|
|||
headers={'Content-Type': 'application/json'})
|
||||
self.assertEqual(response.code, 500)
|
||||
|
||||
def test_that_response_content_type_can_be_set(self):
|
||||
class FooGenerator(content.ContentMixin, web.RequestHandler):
|
||||
def get(self):
|
||||
self.set_header('Content-Type', 'application/foo+json')
|
||||
self.send_response({'foo': 'bar'}, set_content_type=False)
|
||||
|
||||
self.application.add_handlers(r'.*', [web.url(r'/foo', FooGenerator)])
|
||||
response = self.fetch('/foo')
|
||||
self.assertEqual(200, response.code)
|
||||
self.assertEqual('application/foo+json',
|
||||
response.headers.get('Content-Type'))
|
||||
|
||||
|
||||
class GetRequestBodyTests(testing.AsyncHTTPTestCase):
|
||||
def setUp(self):
|
||||
|
@ -218,6 +234,49 @@ class GetRequestBodyTests(testing.AsyncHTTPTestCase):
|
|||
self.assertEqual(response.code, 400)
|
||||
|
||||
|
||||
class MixinCacheTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
self.transcoder = transcoders.JSONTranscoder()
|
||||
|
||||
application = unittest.mock.Mock()
|
||||
application.settings = {}
|
||||
application.ui_methods = {}
|
||||
content.install(application, 'application/json', 'utf-8')
|
||||
content.add_transcoder(application, self.transcoder)
|
||||
|
||||
request = httputil.HTTPServerRequest(
|
||||
'POST',
|
||||
'/',
|
||||
body=b'{}',
|
||||
connection=unittest.mock.Mock(),
|
||||
headers=httputil.HTTPHeaders({'Content-Type': 'application/json'}),
|
||||
)
|
||||
|
||||
self.handler = content.ContentMixin(application, request)
|
||||
|
||||
def test_that_best_response_type_is_cached(self):
|
||||
with unittest.mock.patch(
|
||||
'sprockets.mixins.mediatype.content.algorithms.'
|
||||
'select_content_type',
|
||||
side_effect=algorithms.select_content_type
|
||||
) as select_content_type:
|
||||
first = self.handler.get_response_content_type()
|
||||
second = self.handler.get_response_content_type()
|
||||
|
||||
self.assertIs(first, second)
|
||||
self.assertEqual(1, select_content_type.call_count)
|
||||
|
||||
def test_that_request_body_is_cached(self):
|
||||
self.transcoder.from_bytes = unittest.mock.Mock(
|
||||
wraps=self.transcoder.from_bytes)
|
||||
first = self.handler.get_request_body()
|
||||
second = self.handler.get_request_body()
|
||||
self.assertIs(first, second)
|
||||
self.assertEqual(1, self.transcoder.from_bytes.call_count)
|
||||
|
||||
|
||||
class JSONTranscoderTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
@ -449,3 +508,15 @@ class MsgPackTranscoderTests(unittest.TestCase):
|
|||
dumped = self.transcoder.packb(data)
|
||||
self.assertEqual(self.transcoder.unpackb(dumped), data)
|
||||
self.assertEqual(dumped, pack_bytes(data))
|
||||
|
||||
def test_that_dicts_are_sent_as_maps(self):
|
||||
data = {'compact': True, 'schema': 0}
|
||||
dumped = self.transcoder.packb(data)
|
||||
self.assertEqual(b'\x82\xA7compact\xC3\xA6schema\x00', dumped)
|
||||
|
||||
def test_that_transcoder_creation_fails_if_umsgpack_is_missing(self):
|
||||
with unittest.mock.patch(
|
||||
'sprockets.mixins.mediatype.transcoders.umsgpack',
|
||||
new_callable=lambda: None):
|
||||
with self.assertRaises(RuntimeError):
|
||||
transcoders.MsgPackTranscoder()
|
||||
|
|
Loading…
Reference in a new issue