From 45fcaac47d2fefd5ab764c1be88a896a2ac6be9a Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sun, 12 Mar 2023 16:21:31 +0000 Subject: [PATCH] test revers() function --- core/TESTS/{tests_caves.py => test_caves.py} | 0 .../TESTS/{tests_logins.py => test_logins.py} | 0 core/TESTS/test_urls.py | 149 ++++++++++++++++++ core/TESTS/tests.py | 17 -- url-reverse.txt | 41 +++++ 5 files changed, 190 insertions(+), 17 deletions(-) rename core/TESTS/{tests_caves.py => test_caves.py} (100%) rename core/TESTS/{tests_logins.py => test_logins.py} (100%) create mode 100644 core/TESTS/test_urls.py create mode 100644 url-reverse.txt diff --git a/core/TESTS/tests_caves.py b/core/TESTS/test_caves.py similarity index 100% rename from core/TESTS/tests_caves.py rename to core/TESTS/test_caves.py diff --git a/core/TESTS/tests_logins.py b/core/TESTS/test_logins.py similarity index 100% rename from core/TESTS/tests_logins.py rename to core/TESTS/test_logins.py diff --git a/core/TESTS/test_urls.py b/core/TESTS/test_urls.py new file mode 100644 index 0000000..df34ee6 --- /dev/null +++ b/core/TESTS/test_urls.py @@ -0,0 +1,149 @@ +""" +We are using unittest for troggle. + +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. + +https://docs.djangoproject.com/en/dev/topics/testing/tools/ + +The code {% url THING %} or {% url THING PARAMETER %} appears a hundred times or more in the troggle/templates/ HTML template files. +This is the template synstax for +reverse('THING') +or +reverse('THING', args=[PARAMETER]) + +It is the URLS which take parameters which need understanding and testing. The reverse() which take no +parameters should be fine as this is fundamental Django stuff which will have been tested to death. + +But the reverse() function is purely syntactical, the PARAMETER is just a string which is applied to +the url. So this is not testing anything important really. See the test_url_threed() below. + +1. No tests: No parameters + +{% url "caveindex" %} +{% url "controlpanel" %} +{% url "dataissues" %} +{% url "dwgallfiles" %} +{% url "dwgupload" %} +{% url "eastings" %} +{% url "exportlogbook" %} +{% url "newcave" %} +{% url "notablepersons" %} +{% url "photoupload" %} +{% url "walletedit" %} + +Tests exist: +{% url "stats" %} +{% url "allscans" %} +{% url "survexcaveslist" %} + +2. With parameter + +{% url "caveQMs" "1623-290" %} +{% url "cavewallets" cave_id %} +{% url "dwgfilesingle" drawing.dwgpath %} +{% url "edit_cave" cave.url_parent cave.slug %} +{% url "editentrance" cave.slug ent.entrance.slug %} +{% url "editexpopage" path %} +{% url "err" title %} +{% url "expedition" 2022 %} +{% url "newentrance" cave.slug %} +{% url "survexcavessingle" cavedir %} +{% url "survexcavessingle" cavefiles.0.1 %} +{% url "svx" cavepath %} +{% url "svx" survexfile.path %} +{% url "svxlog" title %} +{% url 'caveQMs' '1623-161' %} +{% url 'image_selector' path %} +{% url 'new_image_form' path %} + +Tests exist: +{% url "threed" title %} +""" + + +todo = """These just do {% url THING %} with no parameter, we also need tests which take a parameter + +- Read all this https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Testing + +- Read all this https://realpython.com/testing-in-django-part-1-best-practices-and-examples/ + +- add 'coverage' to all tests + +- statistics also needs test when we have put data into the database + +""" + +import re +from http import HTTPStatus + +from django.test import Client, TestCase +from django.urls import reverse, path + +# class SimplePageTest(unittest.TestCase): +class URLTests(TestCase): + """These tests may appear to be redundant, but in fact they exercise different bits of code. The urls.py + dispatcher is sending these URLs view via different 'view' handlers, and they all need verifying. + """ + + @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_statistics(self): + response = self.client.get("/statistics") + self.assertEqual(response.status_code, HTTPStatus.OK) + content = response.content.decode() + ph = r"0 expeditions: 0 people, 0 caves and 0 logbook entries." + phmatch = re.search(ph, content) + self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") + + def test_stats(self): + # Needs another test with test data + response = self.client.get("/stats") + self.assertEqual(response.status_code, HTTPStatus.OK) + content = response.content.decode() + # with open('_test_response.html', 'w') as f: + # f.write(content) + ph = r"Total length: 0.0 km adding up the total for each year." + phmatch = re.search(ph, content) + self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") + + def test_url_stats(self): + """Test the {% url "stats" %} reverse resolution + path('statistics', statistics.stats, name="stats"), + path('stats', statistics.stats, name="stats"), + """ + reversed_url = reverse('stats') # NB _ must be written as - if present in name + self.assertEqual(reversed_url, "/stats") + + def test_url_allscans(self): + """Test the {% url "allscans" %} reverse resolution + path('survey_scans/', allscans, name="allscans"), # all the scans in all wallets + """ + reversed_url = reverse('allscans') # NB _ must be written as - if present in name + self.assertEqual(reversed_url, "/survey_scans/") + + def test_url_survexcaveslist(self): + """Test the {% url "allscans" %} reverse resolution + path('survexfile/caves', survex.survexcaveslist, name="survexcaveslist"), + path('survexfile/caves/', survex.survexcaveslist, name="survexcaveslist"), # auto slash not working + """ + reversed_url = reverse('survexcaveslist') # NB _ must be written as - if present in name + self.assertEqual(reversed_url, "/survexfile/caves/") + + def test_url_threed(self): + """Test the {% url "threed" %} reverse resolution + path('survexfile/.3d', survex.threed, name="threed"), + """ + reversed_url = reverse('threed', args=['zilch']) # NB _ must be written as - if present in name + self.assertEqual(reversed_url, "/survexfile/zilch.3d") + diff --git a/core/TESTS/tests.py b/core/TESTS/tests.py index fc6f12b..e5c4349 100644 --- a/core/TESTS/tests.py +++ b/core/TESTS/tests.py @@ -476,23 +476,6 @@ class PageTests(TestCase): self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") - def test_statistics(self): - response = self.client.get("/statistics") - self.assertEqual(response.status_code, HTTPStatus.OK) - content = response.content.decode() - ph = r"0 expeditions: 0 people, 0 caves and 0 logbook entries." - phmatch = re.search(ph, content) - self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") - - def test_stats(self): - # Needs another test with test data - response = self.client.get("/stats") - self.assertEqual(response.status_code, HTTPStatus.OK) - content = response.content.decode() - ph = r"Total length: 0.0 km adding up the total for each year." - phmatch = re.search(ph, content) - self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") - def test_dataissues(self): # Needs another test with test data response = self.client.get("/dataissues") diff --git a/url-reverse.txt b/url-reverse.txt new file mode 100644 index 0000000..18e595c --- /dev/null +++ b/url-reverse.txt @@ -0,0 +1,41 @@ +Create TESTs for all these reverse-match href values. +Philip +2023-03-12 + +1. No parameters + +{% url "walletedit" %} +{% url "allscans" %} +{% url "caveindex" %} +{% url "controlpanel" %} +{% url "dataissues" %} +{% url "dwgallfiles" %} +{% url "dwgupload" %} +{% url "eastings" %} +{% url "exportlogbook" %} +{% url "newcave" %} +{% url "notablepersons" %} +{% url "photoupload" %} +{% url "stats" %} +{% url "survexcaveslist" %} + +2. With parameter + +{% url "caveQMs" "1623-290" %} +{% url "cavewallets" cave_id %} +{% url "dwgfilesingle" drawing.dwgpath %} +{% url "edit_cave" cave.url_parent cave.slug %} +{% url "editentrance" cave.slug ent.entrance.slug %} +{% url "editexpopage" path %} +{% url "err" title %} +{% url "expedition" 2022 %} +{% url "newentrance" cave.slug %} +{% url "survexcavessingle" cavedir %} +{% url "survexcavessingle" cavefiles.0.1 %} +{% url "svx" cavepath %} +{% url "svx" survexfile.path %} +{% url "svxlog" title %} +{% url "threed" title %} +{% url 'caveQMs' '1623-161' %} +{% url 'image_selector' path %} +{% url 'new_image_form' path %}