Mixins for testing abstract model/partials in views
This commit is contained in:
parent
f919fe30fa
commit
c828d04f05
@ -1,5 +1,9 @@
|
|||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from lostplaces_app.models import Taggable, MapablePoint
|
||||||
|
|
||||||
|
from taggit.models import Tag
|
||||||
|
|
||||||
class ViewTestCase(TestCase):
|
class ViewTestCase(TestCase):
|
||||||
'''
|
'''
|
||||||
This is a mixni for testing views. It provides functionality to
|
This is a mixni for testing views. It provides functionality to
|
||||||
@ -105,4 +109,116 @@ class ViewTestCase(TestCase):
|
|||||||
self.assertHttpCode(response, 404)
|
self.assertHttpCode(response, 404)
|
||||||
|
|
||||||
def assertHttpMethodNotAllowed(self, response):
|
def assertHttpMethodNotAllowed(self, response):
|
||||||
self.assertHttpCode(response, 405)
|
self.assertHttpCode(response, 405)
|
||||||
|
|
||||||
|
class TaggableViewTestCaseMixin:
|
||||||
|
|
||||||
|
def assertTaggableContext(self, context):
|
||||||
|
self.assertTrue(
|
||||||
|
'all_tags' in context,
|
||||||
|
msg='Expecting the context for taggable to contain an \'all_tags\' attribute'
|
||||||
|
)
|
||||||
|
|
||||||
|
for tag in context['all_tags']:
|
||||||
|
self.assertTrue(
|
||||||
|
isinstance(tag, Tag),
|
||||||
|
msg='Expecting all entries to be an instance of %s, got %s' % (
|
||||||
|
str(Tag),
|
||||||
|
str(type(tag))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
'submit_form' in context,
|
||||||
|
msg='Expecting the context for taggable to contain \'submit_form\' attribute'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
'tagged_item' in context,
|
||||||
|
msg='Expecting the context for taggable to contain \'tagged_item\' attribute'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
isinstance(context['tagged_item'], Taggable),
|
||||||
|
msg='Expecting the tagged_item to be an instance of %s' % (
|
||||||
|
str(Taggable)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
'submit_url_name' in context,
|
||||||
|
msg='Expecting the context for taggable to contain \'submit_url_name\' attribute'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
type(context['submit_url_name']) == str,
|
||||||
|
msg='Expecting submit_url_name to be of type string'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
'delete_url_name' in context,
|
||||||
|
msg='Expecting the context for taggable to contain \'delete_url_name\' attribute'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
type(context['delete_url_name']) == str,
|
||||||
|
msg='Expecting delete_url_name to be of type string'
|
||||||
|
)
|
||||||
|
|
||||||
|
class MapablePointViewTestCaseMixin:
|
||||||
|
|
||||||
|
def assertMapablePointContext(self, context):
|
||||||
|
self.assertTrue(
|
||||||
|
'all_points' in context,
|
||||||
|
msg='Expecting the context for mapable point to contain \'all_points\' attribute'
|
||||||
|
)
|
||||||
|
|
||||||
|
for point in context['all_points']:
|
||||||
|
self.assertTrue(
|
||||||
|
isinstance(point, MapablePoint),
|
||||||
|
msg='Expecting all entries to be an instance of %s, got %s' % (
|
||||||
|
str(MapablePoint),
|
||||||
|
str(type(point))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
'map_center' in context,
|
||||||
|
msg='Expecting the context for mapable point to contain \'map_center\' attribute'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
'latitude' in context['map_center'],
|
||||||
|
msg='Expecting the map center to contain an \'latitude\' attribute'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
isinstance(context['map_center']['latitude'], float) or isinstance(context['map_center']['latitude'], int),
|
||||||
|
msg='Expecting the latitude of the map center to be numeric, type %s given' % (
|
||||||
|
str(type(context['map_center']['latitude']))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
-90 <= context['map_center']['latitude'] <= 90,
|
||||||
|
msg='Expecting the latitude of map center to be in the range of -90 and 90'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
'longitude' in context['map_center'],
|
||||||
|
msg='Expecting the map center to contain an \'longitude\' attribute'
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
isinstance(context['map_center']['longitude'], float) or isinstance(context['map_center']['longitude'], int),
|
||||||
|
msg='Expecting the longitude of the map center to be numeric, type %s given' % (
|
||||||
|
str(type(context['map_center']['longitude']))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
-180 <= context['map_center']['longitude'] <= 180,
|
||||||
|
msg='Expecting the longitude of map center to be in the range of -180 and 180'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
@ -1,27 +1,33 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from django.test import TestCase, Client
|
from django.test import TestCase, Client
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
from lostplaces_app.models import Place
|
from lostplaces_app.models import Place
|
||||||
from lostplaces_app.views import (
|
from lostplaces_app.views import (
|
||||||
PlaceCreateView,
|
PlaceCreateView,
|
||||||
PlaceListView
|
PlaceListView,
|
||||||
|
PlaceDetailView
|
||||||
)
|
)
|
||||||
from lostplaces_app.forms import PlaceImageCreateForm, PlaceForm
|
from lostplaces_app.forms import PlaceImageCreateForm, PlaceForm
|
||||||
from lostplaces_app.tests.views import ViewTestCase
|
from lostplaces_app.tests.views import (
|
||||||
|
ViewTestCase,
|
||||||
|
TaggableViewTestCaseMixin,
|
||||||
|
MapablePointViewTestCaseMixin
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestPlaceCreateView(ViewTestCase):
|
class TestPlaceCreateView(ViewTestCase):
|
||||||
view = PlaceCreateView
|
view = PlaceCreateView
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
user = User.objects.create_user(
|
user = User.objects.create_user(
|
||||||
username='testpeter',
|
username='testpeter',
|
||||||
password='Develop123'
|
password='Develop123'
|
||||||
)
|
)
|
||||||
|
|
||||||
place = Place.objects.create(
|
place = Place.objects.create(
|
||||||
name='Im a place',
|
name='Im a place',
|
||||||
submitted_when=datetime.datetime.now(),
|
submitted_when=datetime.datetime.now(),
|
||||||
@ -33,27 +39,28 @@ class TestPlaceCreateView(ViewTestCase):
|
|||||||
)
|
)
|
||||||
place.tags.add('I a tag', 'testlocation')
|
place.tags.add('I a tag', 'testlocation')
|
||||||
place.save()
|
place.save()
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = Client()
|
self.client = Client()
|
||||||
|
|
||||||
def test_has_forms(self):
|
def test_has_forms(self):
|
||||||
self.client.login(username='testpeter', password='Develop123')
|
self.client.login(username='testpeter', password='Develop123')
|
||||||
response = self.client.get(reverse_lazy('place_create'))
|
response = self.client.get(reverse('place_create'))
|
||||||
|
|
||||||
self.assertHasForm(response, 'place_image_form', PlaceImageCreateForm)
|
self.assertHasForm(response, 'place_image_form', PlaceImageCreateForm)
|
||||||
self.assertHasForm(response, 'place_form', PlaceForm)
|
self.assertHasForm(response, 'place_form', PlaceForm)
|
||||||
|
|
||||||
|
|
||||||
class TestPlaceListView(ViewTestCase):
|
class TestPlaceListView(ViewTestCase):
|
||||||
view = PlaceListView
|
view = PlaceListView
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
user = User.objects.create_user(
|
user = User.objects.create_user(
|
||||||
username='testpeter',
|
username='testpeter',
|
||||||
password='Develop123'
|
password='Develop123'
|
||||||
)
|
)
|
||||||
|
|
||||||
place = Place.objects.create(
|
place = Place.objects.create(
|
||||||
name='Im a place',
|
name='Im a place',
|
||||||
submitted_when=datetime.datetime.now(),
|
submitted_when=datetime.datetime.now(),
|
||||||
@ -65,18 +72,55 @@ class TestPlaceListView(ViewTestCase):
|
|||||||
)
|
)
|
||||||
place.tags.add('I a tag', 'testlocation')
|
place.tags.add('I a tag', 'testlocation')
|
||||||
place.save()
|
place.save()
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = Client()
|
self.client = Client()
|
||||||
|
|
||||||
def test_list_view(self):
|
def test_list_view(self):
|
||||||
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('place_list'))
|
||||||
|
|
||||||
self.assertContext(response, 'map_config')
|
self.assertContext(response, 'mapping_config')
|
||||||
|
|
||||||
def test_test(self):
|
|
||||||
response = self.client.get(reverse_lazy('place_list'))
|
class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapablePointViewTestCaseMixin, ViewTestCase):
|
||||||
print(response['location'])
|
view = PlaceDetailView
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
user = User.objects.create_user(
|
||||||
|
username='testpeter',
|
||||||
|
password='Develop123'
|
||||||
|
)
|
||||||
|
|
||||||
|
place = Place.objects.create(
|
||||||
|
name='Im a place',
|
||||||
|
submitted_when=datetime.datetime.now(),
|
||||||
|
submitted_by=user.explorer,
|
||||||
|
location='Testtown',
|
||||||
|
latitude=50.5,
|
||||||
|
longitude=7.0,
|
||||||
|
description='This is just a test, do not worry'
|
||||||
|
)
|
||||||
|
place.tags.add('I a tag', 'testlocation')
|
||||||
|
place.save()
|
||||||
|
|
||||||
|
def test_context(self):
|
||||||
|
self.client.login(username='testpeter', password='Develop123')
|
||||||
|
response = self.client.get(reverse('place_detail', kwargs={'pk': 1}))
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
'tagging_config' in response.context,
|
||||||
|
msg='Expecting the context of %s to have an \'tagging_config\'' % (
|
||||||
|
str(self.view)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertTaggableContext(response.context['tagging_config'])
|
||||||
|
|
||||||
|
self.assertTrue(
|
||||||
|
'mapping_config' in response.context,
|
||||||
|
msg='Expecting the context of %s to have an \'mapping_config\'' % (
|
||||||
|
str(self.view)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertMapablePointContext(response.context['mapping_config'])
|
||||||
|
Loading…
Reference in New Issue
Block a user