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
|
This is a mixni for testing views. It provides functionality to
|
||||||
test the context, forms and HTTP Response of responses.
|
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
|
view = None
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.view_name = self.view.__name__
|
self.view_name = self.view.__name__
|
||||||
self. client = Client()
|
self.client = Client()
|
||||||
|
|
||||||
def assertHasContextKey(self, response, context_key):
|
def assertContext(self, response, key, value=None):
|
||||||
self.assertTrue( context_key in response.context,
|
'''
|
||||||
|
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\'' % (
|
msg='Expecting the context of %s to have an attribute \'%s\'' % (
|
||||||
self.view_name,
|
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):
|
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(
|
self.assertEqual(
|
||||||
type(response.context[context_key]),
|
type(response.context[context_key]),
|
||||||
form_class,
|
form_class,
|
||||||
@ -33,6 +53,9 @@ class ViewTestCaseMixin:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def assertHttpCode(self, response, code):
|
def assertHttpCode(self, response, code):
|
||||||
|
'''
|
||||||
|
Checks if the response has the given status code
|
||||||
|
'''
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
response.status_code, code,
|
response.status_code, code,
|
||||||
"Expected an HTTP %s response, but got HTTP %s" % (
|
"Expected an HTTP %s response, but got HTTP %s" % (
|
||||||
@ -47,9 +70,10 @@ class ViewTestCaseMixin:
|
|||||||
def assertHttpCreated(self, response):
|
def assertHttpCreated(self, response):
|
||||||
self.assertHttpCode(response, 201)
|
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(
|
self.assertTrue(
|
||||||
@ -57,8 +81,15 @@ class ViewTestCaseMixin:
|
|||||||
'Expected an HTTP 3XX (redirect) response, but got HTTP %s' %
|
'Expected an HTTP 3XX (redirect) response, but got HTTP %s' %
|
||||||
response.status_code
|
response.status_code
|
||||||
)
|
)
|
||||||
|
if redirect_to:
|
||||||
self.assertEqual(response['Location'], 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):
|
def assertHttpBadRequest(self, response):
|
||||||
|
@ -68,6 +68,6 @@ class TestPlaceListView(ViewTestCaseMixin, TestCase):
|
|||||||
self.client.login(username='testpeter', password='Develop123')
|
self.client.login(username='testpeter', password='Develop123')
|
||||||
response = self.client.get(reverse_lazy('place_list'))
|
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