webpage tests created (no database)

This commit is contained in:
Philip Sargent 2020-07-19 01:23:07 +01:00
parent edd5a3efd9
commit eb923af44f
3 changed files with 72 additions and 4 deletions

View File

@ -10,8 +10,9 @@ https://docs.python.org/3.8/library/doctest.html
https://docs.djangoproject.com/en/3.0/topics/testing/tools/ https://docs.djangoproject.com/en/3.0/topics/testing/tools/
""" """
import unittest
from django.test import TestCase, SimpleTestCase import re
from django.test import TestCase, SimpleTestCase, Client
class SimpleTest(SimpleTestCase): class SimpleTest(SimpleTestCase):
def test_basic_addition(self): def test_basic_addition(self):
@ -53,6 +54,8 @@ class SimpleTest(SimpleTestCase):
from troggle.core.models_caves import CaveSlug, Cave, CaveAndEntrance, QM, CaveDescription, EntranceSlug, Entrance, Area, SurvexStation from troggle.core.models_caves import CaveSlug, Cave, CaveAndEntrance, QM, CaveDescription, EntranceSlug, Entrance, Area, SurvexStation
from troggle.core.forms import CaveForm, CaveAndEntranceFormSet, VersionControlCommentForm, EntranceForm, EntranceLetterForm from troggle.core.forms import CaveForm, CaveAndEntranceFormSet, VersionControlCommentForm, EntranceForm, EntranceLetterForm
from troggle.helper import login_required_if_public from troggle.helper import login_required_if_public
from django.contrib.auth.decorators import login_required
from django.conf import settings
def test_import_parses_mix(self): def test_import_parses_mix(self):
from troggle.parsers.logbooks import GetCaveLookup from troggle.parsers.logbooks import GetCaveLookup
import troggle.settings import troggle.settings
@ -71,6 +74,68 @@ class SimpleTest(SimpleTestCase):
from django.http import HttpResponse from django.http import HttpResponse
from django.urls import reverse from django.urls import reverse
#class SimplePageTest(unittest.TestCase):
class PageTests(TestCase):
@classmethod
def setUpTestData(cls):
# Set up data for the whole TestCase
#cls.foo = Foo.objects.create(bar="Test")
# Some test using self.foo in tests below..
# read in some SQL ?
pass
def setUp(self):
# Every test needs a client.
self.client = Client()
def test_page_admin(self):
# Issue a GET request.
response = self.client.get('/admin/login/')
content = response.content.decode()
self.assertEqual(response.status_code, 200)
h1 = re.search(r'<h1 id="site-name">Troggle administration</h1>', content)
def test_page_folk(self):
# This page is separately generated, so it has the full data content
response = self.client.get('/folk/')
content = response.content.decode()
self.assertEqual(response.status_code, 200)
# Check that the rendered context has the correct title
phrase = re.search(r'active contribution to the expedition', content)
phrase = re.search(r'Naomi Griffiths', content)
phrase = re.search(r'Gail Smith', content)
phrase = re.search(r'Phil Wigglesworth', content)
phrase = re.search(r'A more obscure record of longest gap between expos has', content)
def test_page_expofile(self):
# Flat file tests.
response = self.client.get('/expofiles/documents/surveying/tunnel-loefflerCP35-only.pdf')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 2299270)
response = self.client.get('/expofiles/writeups/1982/logbook1982.pdf')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 12915413)
def test_page_ss(self):
# this gets an empty page as the database has not been loaded
response = self.client.get('/survey_scans/')
self.assertEqual(response.status_code, 200)
#response = self.client.get('/survey_scans/1991surveybook/page0002.png')
#response = self.client.get('/survey_scans/1991surveybook/')
#content = response.content.decode()
#print(content)
#png93 = re.search(r'/page0093.png">page0093.png</a></td>', content)
def test_page_tunnel(self):
# this fails as the database has not been loaded so there is no Tunnel file
#response = self.client.get('/tunneldataraw/107/107sketch-v2.xml')
response = self.client.get('/tunneldataraw/')
self.assertEqual(response.status_code, 200)
__test__ = {"doctest": """ __test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2. Another way to test that 1 + 1 is equal to 2.

View File

@ -1,6 +1,9 @@
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.conf import settings from django.conf import settings
"""This enforces the login requirement for non-public pages using
the decorator mechanism.
https://www.fullstackpython.com/django-contrib-auth-decorators-login-required-examples.html
"""
class login_required_if_public(object): class login_required_if_public(object):

View File

@ -42,7 +42,7 @@ MEDIA_URL = '/site_media/'
#STATIC_ROOT removed after merging content into MEDIA_ROOT. See urls.py & core/views_surveys.py #STATIC_ROOT removed after merging content into MEDIA_ROOT. See urls.py & core/views_surveys.py
PUBLIC_SITE = False PUBLIC_SITE = True
DEBUG = True DEBUG = True
# executables: # executables: