From 2047fe8d161837ce8a24e479dfb0533c21f251dd Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Mon, 4 Oct 2021 07:28:06 -0400 Subject: [PATCH] Increase test coverage to 100% --- setup.cfg | 2 +- sprockets/mixins/mediatype/transcoders.py | 2 +- tests.py | 75 ++++++++++++++++++++++- 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index 710253a..7086654 100644 --- a/setup.cfg +++ b/setup.cfg @@ -72,7 +72,7 @@ warning-is-error = 1 strict = 1 [coverage:report] -fail_under = 95 +fail_under = 100 show_missing = 1 [coverage:run] diff --git a/sprockets/mixins/mediatype/transcoders.py b/sprockets/mixins/mediatype/transcoders.py index 464a0d9..1f8d2aa 100644 --- a/sprockets/mixins/mediatype/transcoders.py +++ b/sprockets/mixins/mediatype/transcoders.py @@ -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 diff --git a/tests.py b/tests.py index ad1d1d7..f2ea89a 100644 --- a/tests.py +++ b/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()