Testing home view

This commit is contained in:
reverend 2021-05-15 23:27:25 +02:00
parent 29d067002e
commit 9c86eab14c
2 changed files with 146 additions and 34 deletions

View File

@ -17,10 +17,43 @@ from lostplaces.forms import PlaceImageForm, PlaceForm
from lostplaces.tests.views import (
ViewTestCase,
TaggableViewTestCaseMixin,
MapableViewTestCaseMixin
MapableViewTestCaseMixin,
GlobalTemplateTestCaseMixin
)
class TestPlaceListView(GlobalTemplateTestCaseMixin, 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=timezone.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 setUp(self):
self.client = Client()
def test_list_view(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_list'))
self.assertContext(response, 'mapping_config')
self.assertGlobal(response)
class TestPlaceCreateView(ViewTestCase):
view = PlaceCreateView
@ -53,39 +86,6 @@ class TestPlaceCreateView(ViewTestCase):
self.assertHasForm(response, 'place_image_form', PlaceImageForm)
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=timezone.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 setUp(self):
self.client = Client()
def test_list_view(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_list'))
self.assertContext(response, 'mapping_config')
class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase):
view = PlaceDetailView

View File

@ -0,0 +1,112 @@
import re
from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth.models import User
from django.utils import timezone
from lostplaces.models import Place
from lostplaces.tests.views import (
ViewTestCase,
GlobalTemplateTestCaseMixin
)
class TestHomeView(GlobalTemplateTestCaseMixin, ViewTestCase):
@classmethod
def setUpTestData(cls):
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
place = Place.objects.create(
name='Im a place',
submitted_when=timezone.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 setUp(self):
self.client = Client()
def test_global(self):
response = self.client.get(reverse('lostplaces_home'))
self.assertGlobal(response)
def test_place_list_authenticated(self):
"""
Testing there is the place list containing the name,
location and description of the latest place for
authenticated users.
"""
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('lostplaces_home'))
self.assertNotEqual(
None,
re.search(
"""Im a place.*Testtown.*This is just a test, do not worry""",
response.content.decode().replace('\n', '')
),
msg='Expecting the test place to show up on the homepage'
)
def test_place_list_unauthenticated(self):
"""
Testing there is a place list of the lates places
containing the place names only for unauthenticated users.
"""
response = self.client.get(reverse('lostplaces_home'))
self.assertNotEqual(
None,
re.search(
"""Im a place""",
response.content.decode().replace('\n', '')
),
msg='Expecting the test place to show up on the homepage'
)
self.assertEqual(
None,
re.search(
"""Testtown.*This is just a test, do not worry""",
response.content.decode().replace('\n', '')
),
msg='Expecting the test place to show up on the homepage'
)
def test_map_authenticated(self):
"""
Testing there is a map showing all the lates places
on a map for authenticated users.
"""
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('lostplaces_home'))
self.assertNotEqual(
None,
re.search(
"""map.*7.0,.*50.5.*Im a place""",
response.content.decode().replace('\n', '')
),
msg='Expecting the test place to show up on the map'
)
def test_map_unauthenticated(self):
"""
Testing there is no map for unauthenticated users.
"""
response = self.client.get(reverse('lostplaces_home'))
self.assertEqual(
None,
re.search(
"""7.0,.*50.5.*Im a place""",
response.content.decode().replace('\n', '')
),
msg='Expecting the test place to show up on the map'
)