Comments and refactoring viewtestmixin
This commit is contained in:
parent
05481fc0c8
commit
b77c5d1d7f
@ -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):
|
||||
|
@ -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')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user