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 lostplaces_app.models import Taggable, MapablePoint
|
||||
|
||||
from taggit.models import Tag
|
||||
|
||||
class ViewTestCase(TestCase):
|
||||
'''
|
||||
This is a mixni for testing views. It provides functionality to
|
||||
@ -106,3 +110,115 @@ class ViewTestCase(TestCase):
|
||||
|
||||
def assertHttpMethodNotAllowed(self, response):
|
||||
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,16 +1,22 @@
|
||||
import datetime
|
||||
|
||||
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 lostplaces_app.models import Place
|
||||
from lostplaces_app.views import (
|
||||
PlaceCreateView,
|
||||
PlaceListView
|
||||
PlaceListView,
|
||||
PlaceDetailView
|
||||
)
|
||||
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):
|
||||
view = PlaceCreateView
|
||||
@ -39,11 +45,12 @@ class TestPlaceCreateView(ViewTestCase):
|
||||
|
||||
def test_has_forms(self):
|
||||
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_form', PlaceForm)
|
||||
|
||||
|
||||
class TestPlaceListView(ViewTestCase):
|
||||
view = PlaceListView
|
||||
|
||||
@ -71,12 +78,49 @@ class TestPlaceListView(ViewTestCase):
|
||||
|
||||
def test_list_view(self):
|
||||
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')
|
||||
|
||||
def test_test(self):
|
||||
response = self.client.get(reverse_lazy('place_list'))
|
||||
print(response['location'])
|
||||
self.assertContext(response, 'mapping_config')
|
||||
|
||||
|
||||
class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapablePointViewTestCaseMixin, ViewTestCase):
|
||||
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