From c828d04f05dff5f19d0888ead8ad5a325156271d Mon Sep 17 00:00:00 2001 From: reverend Date: Sun, 13 Sep 2020 20:27:05 +0200 Subject: [PATCH] Mixins for testing abstract model/partials in views --- .../lostplaces_app/tests/views/__init__.py | 118 +++++++++++++++++- .../tests/views/test_place_views.py | 92 ++++++++++---- 2 files changed, 185 insertions(+), 25 deletions(-) diff --git a/lostplaces/lostplaces_app/tests/views/__init__.py b/lostplaces/lostplaces_app/tests/views/__init__.py index b83b71b..8c324c9 100644 --- a/lostplaces/lostplaces_app/tests/views/__init__.py +++ b/lostplaces/lostplaces_app/tests/views/__init__.py @@ -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 @@ -105,4 +109,116 @@ class ViewTestCase(TestCase): self.assertHttpCode(response, 404) def assertHttpMethodNotAllowed(self, response): - self.assertHttpCode(response, 405) \ No newline at end of file + 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' + ) + + \ No newline at end of file diff --git a/lostplaces/lostplaces_app/tests/views/test_place_views.py b/lostplaces/lostplaces_app/tests/views/test_place_views.py index d3216d8..c0270d1 100644 --- a/lostplaces/lostplaces_app/tests/views/test_place_views.py +++ b/lostplaces/lostplaces_app/tests/views/test_place_views.py @@ -1,27 +1,33 @@ 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 - + @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(), @@ -33,27 +39,28 @@ class TestPlaceCreateView(ViewTestCase): ) 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')) - + 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 - + @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(), @@ -65,18 +72,55 @@ class TestPlaceListView(ViewTestCase): ) place.tags.add('I a tag', 'testlocation') place.save() - + def setUp(self): - self.client = Client() - + 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']) - - \ No newline at end of file + response = self.client.get(reverse('place_list')) + + 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'])