From 875b90d3376c060dea469bc7110c51e20b2eee0a Mon Sep 17 00:00:00 2001 From: Jeff Dairiki Date: Wed, 15 Apr 2015 12:12:19 -0700 Subject: Add tests for temporary_response context manager These tests test for, among other things, the nits described in comments on #1563, namely: - ``Request.response`` should be restored even if the renderer raises an exception - If ``request.response`` is initially set to ``None``, it should be restored to ``None`` (rather than deleted). (Some of these tests currently fail.) --- pyramid/tests/test_renderers.py | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pyramid/tests/test_renderers.py b/pyramid/tests/test_renderers.py index ed6344a40..542eea9aa 100644 --- a/pyramid/tests/test_renderers.py +++ b/pyramid/tests/test_renderers.py @@ -592,6 +592,48 @@ class Test_render_to_response(unittest.TestCase): self.assertEqual(result.body, b'{"a": 1}') self.assertFalse('response' in request.__dict__) +class Test_temporary_response(unittest.TestCase): + def _callFUT(self, request): + from pyramid.renderers import temporary_response + return temporary_response(request) + + def test_restores_response(self): + request = testing.DummyRequest() + orig_response = request.response + with self._callFUT(request): + request.response = object() + self.assertEqual(request.response, orig_response) + + def test_restores_response_on_exception(self): + request = testing.DummyRequest() + orig_response = request.response + try: + with self._callFUT(request): + request.response = object() + raise RuntimeError() + except RuntimeError: + self.assertEqual(request.response, orig_response) + else: # pragma: no cover + self.fail("RuntimeError not raised") + + def test_restores_response_to_none(self): + request = testing.DummyRequest(response=None) + with self._callFUT(request): + request.response = object() + self.assertEqual(request.response, None) + + def test_deletes_response(self): + request = testing.DummyRequest() + with self._callFUT(request): + request.response = object() + self.assertTrue('response' not in request.__dict__) + + def test_does_not_delete_response_if_no_response_to_delete(self): + request = testing.DummyRequest() + with self._callFUT(request): + pass + self.assertTrue('response' not in request.__dict__) + class Test_get_renderer(unittest.TestCase): def setUp(self): self.config = testing.setUp() -- cgit v1.2.3