more tests for files served from several places

This commit is contained in:
Philip Sargent 2021-03-28 23:48:36 +01:00
parent 623483f3b1
commit c81f17c24b

View File

@ -1,12 +1,20 @@
"""
We are using unittest for troggle.
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Note that the database has not been parsed from the source files when these tests are run,
so any path that relies on data being in the database will fail.
Replace these with more appropriate tests for your application.
The simple redirections to files which exist, e.g. in
/expoweb/
/expofiles/
/expofiles/documents/
etc. will test fine.
But paths like this:
/survey_scans/
which rely on database resolution will fail unless a fixture has been set up for
them.
https://docs.python.org/3.8/library/doctest.html
https://docs.djangoproject.com/en/3.0/topics/testing/tools/
"""
@ -20,6 +28,9 @@ class SimpleTest(SimpleTestCase):
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
def test_test_setting(self):
from django.conf import settings
self.assertEqual(settings.EMAIL_BACKEND, 'django.core.mail.backends.locmem.EmailBackend')
def test_import_TroggleModel(self):
from troggle.core.models import TroggleModel
def test_import_Cave(self):
@ -98,41 +109,63 @@ class PageTests(TestCase):
# Every test needs a client.
self.client = Client()
def test_page_admin(self):
# Issue a GET request.
# see the login page
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_admindocs(self):
# Issue a GET request.
response = self.client.get('/admin/login/models/')
response = self.client.get('/admin/login/models/')
content = response.content.decode()
self.assertEqual(response.status_code, 200)
h1 = re.search(r'<h1>Model documentation</h1>', content)
# database not loaded yet? Or logon-problem?
# def test_page_admindocs_exped(self):
# # Issue a GET request.
# response = self.client.get('/admin/doc/models/core.expedition/')
# content = response.content.decode()
# self.assertEqual(response.status_code, 200)
# h1 = re.search(r'<td>logbookentry_set.all</td>', content)
def test_page_admindocs_exped(self):
# Get redirected to login page
response = self.client.get('/admin/doc/models/core.expedition/')
content = response.content.decode()
self.assertEqual(response.status_code, 302)
h1 = re.search(r'<td>logbookentry_set.all</td>', content)
def test_page_expofiles_dir(self):
# Flat file tests.
response = self.client.get('/expofiles/')
self.assertEqual(response.status_code, 200)
content = response.content.decode()
for ph in [ r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/',
r'<a href="/expofiles/photos">/photos/',
r'<a href="/expofiles/surveyscans">/surveyscans' ]:
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
def test_page_survey_scans_dir(self):
# Flat file tests.
response = self.client.get('/expofiles/surveyscans')
self.assertEqual(response.status_code, 200)
content = response.content.decode()
for ph in [ r'<a href="/expofiles/surveyscans/2004">/2004/',
r'<a href="/expofiles/surveyscans/1989LUSS">/1989LUSS/',
r'<a href="/expofiles/surveyscans/2018">/2018' ]:
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
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()
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)
for ph in [ r'involves some active contribution',
r'Naomi Griffiths',
r'Gail Smith',
r'Phil Wigglesworth',
r'A more obscure record of longest gap between expos has' ]:
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
def test_page_expofile_document(self):
def test_page_expofile_document_loeffler_pdf(self):
# Flat file tests.
response = self.client.get('/expofiles/documents/surveying/tunnel-loefflerCP35-only.pdf')
if response.status_code != 200:
@ -141,6 +174,24 @@ class PageTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 2299270) # fails, but is working manually!
def test_page_expofile_document_rope_pdf(self):
# Flat file tests.
response = self.client.get('/expofiles/documents/rope-age-agm-2019.pdf')
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 76197) # fails, but is working manually!
def test_page_expofile_document_png(self):
# Flat file tests.
response = self.client.get('/expofiles/documents/callout-2012.png')
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 69921) # fails, but is working manually!
def test_page_expofile_writeup(self):
# Flat file tests.
response = self.client.get('/expofiles/writeups/1982/logbook1982.pdf')
@ -150,12 +201,63 @@ class PageTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 12915413) # fails, but is working manually!
def test_page_ss(self):
def test_page_survey_scans_empty(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)
content = response.content.decode()
ph = r'contains the scanned original in-cave survey notes and sketches'
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
def test_page_tunneldataraw_empty(self):
# this gets an empty page as the database has not been loaded
response = self.client.get('/tunneldataraw/')
self.assertEqual(response.status_code, 200)
content = response.content.decode()
ph = r'<h1>Page not found tunneldataraw/</h1>'
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
def test_not_found_survexfile_cave(self):
response = self.client.get('/survexfile/not_a_real_cave_number')
self.assertEqual(response.status_code, 200)
content = response.content.decode()
ph = r'Cave not found in database'
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
# these need a fixture to load the datbase before they will pass
# we also need tests for invalid queries to check that error pages are right
# def test_page_survey_scans_khplan2_png(self):
# # this has an error as the database has not been loaded yet in the tests
# response = self.client.get('/survey_scans/smkhs/khplan2.png')
# if response.status_code != 200:
# self.assertEqual(response.status_code, 302)
# if response.status_code != 302:
# self.assertEqual(response.status_code, 200)
# self.assertEqual(len(response.content), 823304) # fails, but is working manually!
# def test_page_tunneldataraw_107sketch_xml(self):
# # this has an error as the database has not been loaded yet in the tests
# response = self.client.get('/tunneldataraw/107/107sketch-v2.xml')
# if response.status_code != 200:
# self.assertEqual(response.status_code, 302)
# if response.status_code != 302:
# self.assertEqual(response.status_code, 200)
# content = response.content.decode()
# for ph in [ r'tunneldate="2014-08-21 11:34:00"',
# r'<sketchsubset subname="Caves of the Loser Plateau"/>',
# r'sfsketch="ollyjen107drawings',
# r'sfsketch="surveyscans/2014/2014#01',
# r'aa-js-plan.png"' ]:
# phmatch = re.search(ph, content)
# self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
# database not loaded yet:
#response = self.client.get('/survey_scans/1991surveybook/page0002.png')
#response = self.client.get('/survey_scans/1991surveybook/')
@ -164,17 +266,4 @@ class PageTests(TestCase):
#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": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}