More tests of views

This commit is contained in:
reverend 2021-06-12 15:07:14 +02:00
parent 822a536ebe
commit b0bf40aa0b
2 changed files with 106 additions and 22 deletions

View File

@ -1,10 +1,14 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import re
from os import path
from django.test import TestCase, Client from django.test import TestCase, Client
from django.urls import reverse from django.urls import reverse
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils import timezone from django.utils import timezone
from django.conf import settings
from lostplaces.models import Place from lostplaces.models import Place
@ -23,6 +27,9 @@ from lostplaces.tests.views import (
class TestPlaceListView(GlobalTemplateTestCaseMixin, ViewTestCase): class TestPlaceListView(GlobalTemplateTestCaseMixin, ViewTestCase):
"""
Tests the view listing all placs
"""
view = PlaceListView view = PlaceListView
@classmethod @classmethod
@ -32,15 +39,16 @@ class TestPlaceListView(GlobalTemplateTestCaseMixin, ViewTestCase):
password='Develop123' password='Develop123'
) )
place = Place.objects.create( for i in range(12):
name='Im a place', place = Place.objects.create(
submitted_when=timezone.now(), name='Im a place %d' % i,
submitted_by=user.explorer, submitted_when=timezone.now(),
location='Testtown', submitted_by=user.explorer,
latitude=50.5, location='Test %d town' % i,
longitude=7.0, latitude=50.5 + i/10,
description='This is just a test, do not worry' longitude=7.0 - i/10,
) description='This is just a test, do not worry'
)
place.tags.add('I a tag', 'testlocation') place.tags.add('I a tag', 'testlocation')
place.save() place.save()
@ -53,7 +61,20 @@ class TestPlaceListView(GlobalTemplateTestCaseMixin, ViewTestCase):
self.assertContext(response, 'mapping_config') self.assertContext(response, 'mapping_config')
self.assertGlobal(response) self.assertGlobal(response)
def test_pagination(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.get(reverse('place_list'))
self.assertNotEqual(
None,
re.search(
"""<ul.*pagination.*>\s*(<li.*>.*</li>\s*){6,}</ul>""",
response.content.decode().replace('\n', '').lower()
),
msg='Expecting the place list to be paginated like [first] [previous] [item] at least 2 times [next] [last]'
)
class TestPlaceCreateView(ViewTestCase): class TestPlaceCreateView(ViewTestCase):
view = PlaceCreateView view = PlaceCreateView
@ -64,27 +85,90 @@ class TestPlaceCreateView(ViewTestCase):
password='Develop123' 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): 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('place_create')) response = self.client.get(reverse('place_create'))
self.assertHasForm(response, 'place_image_form', PlaceImageForm) self.assertHasForm(response, 'place_image_form', PlaceImageForm)
self.assertHasForm(response, 'place_form', PlaceForm) self.assertHasForm(response, 'place_form', PlaceForm)
def test_positive_no_image(self):
self.client.login(username='testpeter', password='Develop123')
response = self.client.post(
reverse('place_create'),
{
'name': 'test place 486',
'location': 'test location',
'latitude': 45.804192,
'longitude': 1.860222,
'description': """
Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt.
"""
}
)
self.assertHttpRedirect(response)
place = Place.objects.get(name='test place 486')
self.assertNotEqual(
None,
place,
msg='Submitted place not found in database / model'
),
self.assertNotEqual(
None,
re.search(
""".*%s""" % reverse(
'place_detail', kwargs={'pk': place.id}
),
response.url
)
)
def test_positive_image(self):
self.client.login(username='testpeter', password='Develop123')
image = open(
path.join(
settings.BASE_DIR,
'testdata',
'test_image.jpg'
)
)
response = self.client.post(
reverse('place_create'),
{
'name': 'test place 894',
'location': 'test location',
'latitude': 45.804192,
'longitude': 1.860222,
'description': """
Cupiditate harum reprehenderit ipsam iure consequuntur eaque eos reiciendis. Blanditiis vel minima minus repudiandae voluptate aut quia sed. Provident ex omnis illo molestiae. Ullam eos et est provident enim deserunt.
""",
'images': [image]
}
)
self.assertHttpRedirect(response)
place = Place.objects.get(name='test place 894')
self.assertNotEqual(
None,
place,
msg='Submitted place not found in database / model'
),
self.assertNotEqual(
None,
re.search(
""".*%s""" % reverse(
'place_detail', kwargs={'pk': place.id}
),
response.url
)
)
self.assertEqual(
len(place.placeimages.all()),
1,
msg='Expecting the place to have exactly 1 place image'
)
class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase): class PlaceDetailViewTestCase(TaggableViewTestCaseMixin, MapableViewTestCaseMixin, ViewTestCase):
view = PlaceDetailView view = PlaceDetailView

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB