From b77c5d1d7f16f6dc7754555029f7320782fc24f9 Mon Sep 17 00:00:00 2001 From: reverend Date: Sun, 13 Sep 2020 12:49:26 +0200 Subject: [PATCH] Comments and refactoring viewtestmixin --- .../lostplaces_app/tests/views/__init__.py | 51 +++++++++++++++---- .../tests/views/test_place_views.py | 2 +- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lostplaces/lostplaces_app/tests/views/__init__.py b/lostplaces/lostplaces_app/tests/views/__init__.py index 5e991c6..044b2fb 100644 --- a/lostplaces/lostplaces_app/tests/views/__init__.py +++ b/lostplaces/lostplaces_app/tests/views/__init__.py @@ -4,24 +4,44 @@ class ViewTestCaseMixin: ''' This is a mixni for testing views. It provides functionality to test the context, forms and HTTP Response of responses. - Also works with django's ReqeustFactory. + Also works with django's ReqeustFactory and provides a self.client ''' view = None def setUp(self): self.view_name = self.view.__name__ - self. client = Client() + self.client = Client() - def assertHasContextKey(self, response, context_key): - self.assertTrue( context_key in response.context, + def assertContext(self, response, key, value=None): + ''' + Checks weather the response's context has the given key + and, if passed, checks the value + ''' + self.assertTrue( + key in response.context, msg='Expecting the context of %s to have an attribute \'%s\'' % ( self.view_name, - context_key + key ) ) + + if value: + self.assertEqual( + value, + response.context[key], + msg='Expecting the context of %s to have %s set to \'%s\'' % ( + self.view_name, + key, + str(value) + ) + ) def assertHasForm(self, response, context_key, form_class): - self.assertHasContextKey(response, context_key) + ''' + Checks if response has a form under the given key and if + the forms class matches. + ''' + self.assertContext(response, context_key) self.assertEqual( type(response.context[context_key]), form_class, @@ -33,6 +53,9 @@ class ViewTestCaseMixin: ) def assertHttpCode(self, response, code): + ''' + Checks if the response has the given status code + ''' self.assertEqual( response.status_code, code, "Expected an HTTP %s response, but got HTTP %s" % ( @@ -47,9 +70,10 @@ class ViewTestCaseMixin: def assertHttpCreated(self, response): self.assertHttpCode(response, 201) - def assertHttpRedirect(self, response, redirect_to): + def assertHttpRedirect(self, response, redirect_to=None): ''' - Assert that we had any redirect status code. + Checks weather the response redirected, and if passed, + if it redirected to the expected loaction ''' self.assertTrue( @@ -57,8 +81,15 @@ class ViewTestCaseMixin: 'Expected an HTTP 3XX (redirect) response, but got HTTP %s' % response.status_code ) - - self.assertEqual(response['Location'], redirect_to) + if redirect_to: + self.assertEqual( + response['Location'], + redirect_to, + msg='Expecing the response to redirect to %s, where redirected to %s instea' % ( + str(redirect_to), + str(response['Location']) + ) + ) def assertHttpBadRequest(self, response): diff --git a/lostplaces/lostplaces_app/tests/views/test_place_views.py b/lostplaces/lostplaces_app/tests/views/test_place_views.py index a987663..89a17d4 100644 --- a/lostplaces/lostplaces_app/tests/views/test_place_views.py +++ b/lostplaces/lostplaces_app/tests/views/test_place_views.py @@ -68,6 +68,6 @@ class TestPlaceListView(ViewTestCaseMixin, TestCase): self.client.login(username='testpeter', password='Develop123') response = self.client.get(reverse_lazy('place_list')) - self.assertHasContextKey(response, 'map_config') + self.assertContext(response, 'map_config') \ No newline at end of file