Refactoring
This commit is contained in:
parent
cea3a909b5
commit
b3db6643b9
@ -1,17 +1,14 @@
|
||||
from django.test import Client
|
||||
from django.test import TestCase
|
||||
|
||||
class ViewTestCaseMixin:
|
||||
class ViewTestCase(TestCase):
|
||||
'''
|
||||
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 and provides a self.client
|
||||
All methods take responses, so this base class can be used
|
||||
with django's RequestFactory and Test-Client
|
||||
'''
|
||||
view = None
|
||||
|
||||
def setUp(self):
|
||||
self.view_name = self.view.__name__
|
||||
self.client = Client()
|
||||
|
||||
def assertContext(self, response, key, value=None):
|
||||
'''
|
||||
Checks weather the response's context has the given key
|
||||
@ -20,7 +17,7 @@ class ViewTestCaseMixin:
|
||||
self.assertTrue(
|
||||
key in response.context,
|
||||
msg='Expecting the context of %s to have an attribute \'%s\'' % (
|
||||
self.view_name,
|
||||
self.view.__name__,
|
||||
key
|
||||
)
|
||||
)
|
||||
@ -30,24 +27,24 @@ class ViewTestCaseMixin:
|
||||
value,
|
||||
response.context[key],
|
||||
msg='Expecting the context of %s to have %s set to \'%s\'' % (
|
||||
self.view_name,
|
||||
self.view.__name__,
|
||||
key,
|
||||
str(value)
|
||||
)
|
||||
)
|
||||
|
||||
def assertHasForm(self, response, context_key, form_class):
|
||||
def assertHasForm(self, response, key, form_class):
|
||||
'''
|
||||
Checks if response has a form under the given key and if
|
||||
the forms class matches.
|
||||
'''
|
||||
self.assertContext(response, context_key)
|
||||
self.assertContext(response, key)
|
||||
self.assertEqual(
|
||||
type(response.context[context_key]),
|
||||
type(response.context[key]),
|
||||
form_class,
|
||||
msg='Expecting %s\'s context.%s to be of the type %s' % (
|
||||
self.view_name,
|
||||
context_key,
|
||||
self.view.__name__,
|
||||
key,
|
||||
form_class.__name__
|
||||
)
|
||||
)
|
||||
@ -64,12 +61,6 @@ class ViewTestCaseMixin:
|
||||
)
|
||||
)
|
||||
|
||||
def assertHttpOK(self, response):
|
||||
self.assertHttpCode(response, 200)
|
||||
|
||||
def assertHttpCreated(self, response):
|
||||
self.assertHttpCode(response, 201)
|
||||
|
||||
def assertHttpRedirect(self, response, redirect_to=None):
|
||||
'''
|
||||
Checks weather the response redirected, and if passed,
|
||||
@ -81,16 +72,25 @@ class ViewTestCaseMixin:
|
||||
'Expected an HTTP 3XX (redirect) response, but got HTTP %s' %
|
||||
response.status_code
|
||||
)
|
||||
self.assertTrue(
|
||||
'location' in response,
|
||||
msg='Expecting a redirect to have an location, got none'
|
||||
)
|
||||
if redirect_to:
|
||||
self.assertEqual(
|
||||
response['Location'],
|
||||
response['location'],
|
||||
redirect_to,
|
||||
msg='Expecing the response to redirect to %s, where redirected to %s instea' % (
|
||||
str(redirect_to),
|
||||
str(response['Location'])
|
||||
str(response['location'])
|
||||
)
|
||||
)
|
||||
|
||||
def assertHttpOK(self, response):
|
||||
self.assertHttpCode(response, 200)
|
||||
|
||||
def assertHttpCreated(self, response):
|
||||
self.assertHttpCode(response, 201)
|
||||
|
||||
def assertHttpBadRequest(self, response):
|
||||
self.assertHttpCode(response, 400)
|
||||
|
@ -7,9 +7,9 @@ from django.contrib.messages.storage.fallback import FallbackStorage
|
||||
|
||||
from lostplaces_app.models import Place
|
||||
from lostplaces_app.views import IsAuthenticatedMixin
|
||||
from lostplaces_app.tests.views import ViewTestCaseMixin
|
||||
from lostplaces_app.tests.views import ViewTestCase
|
||||
|
||||
class TestIsAuthenticated(ViewTestCaseMixin, TestCase):
|
||||
class TestIsAuthenticated(ViewTestCase):
|
||||
view = IsAuthenticatedMixin
|
||||
|
||||
@classmethod
|
||||
@ -19,6 +19,9 @@ class TestIsAuthenticated(ViewTestCaseMixin, TestCase):
|
||||
password='Develop123'
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
def test_logged_in(self):
|
||||
request = RequestFactory().get('/')
|
||||
request.user = User.objects.get(id=1)
|
||||
@ -64,6 +67,9 @@ class TestIsPlaceSubmitterMixin(TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
def setUp(self):
|
||||
self. client = Client()
|
||||
|
||||
def test_is_submitter(self):
|
||||
self.client.login(username='testpeter', password='Develop123')
|
||||
response = self.client.get(reverse_lazy('place_edit', kwargs={'pk': 1}))
|
||||
|
@ -1,6 +1,6 @@
|
||||
import datetime
|
||||
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, Client
|
||||
from django.urls import reverse_lazy
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
@ -10,10 +10,9 @@ from lostplaces_app.views import (
|
||||
PlaceListView
|
||||
)
|
||||
from lostplaces_app.forms import PlaceImageCreateForm, PlaceForm
|
||||
from lostplaces_app.tests.views import ViewTestCaseMixin
|
||||
|
||||
class TestPlaceCreateView(ViewTestCaseMixin, TestCase):
|
||||
from lostplaces_app.tests.views import ViewTestCase
|
||||
|
||||
class TestPlaceCreateView(ViewTestCase):
|
||||
view = PlaceCreateView
|
||||
|
||||
@classmethod
|
||||
@ -35,6 +34,9 @@ class TestPlaceCreateView(ViewTestCaseMixin, TestCase):
|
||||
place.tags.add('I a tag', 'testlocation')
|
||||
place.save()
|
||||
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
def test_has_forms(self):
|
||||
self.client.login(username='testpeter', password='Develop123')
|
||||
response = self.client.get(reverse_lazy('place_create'))
|
||||
@ -42,7 +44,7 @@ class TestPlaceCreateView(ViewTestCaseMixin, TestCase):
|
||||
self.assertHasForm(response, 'place_image_form', PlaceImageCreateForm)
|
||||
self.assertHasForm(response, 'place_form', PlaceForm)
|
||||
|
||||
class TestPlaceListView(ViewTestCaseMixin, TestCase):
|
||||
class TestPlaceListView(ViewTestCase):
|
||||
view = PlaceListView
|
||||
|
||||
@classmethod
|
||||
@ -64,10 +66,17 @@ class TestPlaceListView(ViewTestCaseMixin, TestCase):
|
||||
place.tags.add('I a tag', 'testlocation')
|
||||
place.save()
|
||||
|
||||
def setUp(self):
|
||||
self.client = Client()
|
||||
|
||||
def test_list_view(self):
|
||||
self.client.login(username='testpeter', password='Develop123')
|
||||
response = self.client.get(reverse_lazy('place_list'))
|
||||
|
||||
self.assertContext(response, 'map_config')
|
||||
|
||||
def test_test(self):
|
||||
response = self.client.get(reverse_lazy('place_list'))
|
||||
print(response['location'])
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user