""" 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. The simple redirections to files which exist, e.g. in /expoweb/ /photos/ etc. will test fine. But paths like this: /survey_scans/ /caves/ which rely on database resolution will fail unless a fixture has been set up for them. https://docs.djangoproject.com/en/dev/topics/testing/tools/ """ import re import subprocess import unittest from http import HTTPStatus from django.contrib.auth.models import User from django.test import Client, SimpleTestCase, TestCase import troggle.parsers.logbooks as lbp from troggle.core.models.logbooks import LogbookEntry from troggle.core.models.troggle import DataIssue, Expedition, Person, PersonExpedition from troggle.core.utils import current_expo current_year = current_expo() def create_user(name=None, last_name="Caver", is_superuser=False): u = User() u.username = name u.email = f"philip.sargent+{name}@gmail.com" u.first_name, u.last_name = name, last_name u.set_password("secretword") # all test users have same password u.save() return u def create_person(firstname, lastname, nickname=False, vfho=False, exped=None): fullname = f"{firstname} {lastname}" slug=f"{firstname.lower()}-{lastname.lower()}" coUniqueAttribs = {"first_name": firstname, "last_name": (lastname or ""), "slug": slug,} otherAttribs = {"is_vfho": vfho, "fullname": fullname, "nickname": nickname} person = Person.objects.create(**otherAttribs, **coUniqueAttribs) coUniqueAttribs = {"person": person, "expedition": exped} otherAttribs = {} pe = PersonExpedition.objects.create(**otherAttribs, **coUniqueAttribs) return person TEST_YEAR = "1986" lbp.ENTRIES[TEST_YEAR] = 4 # number of entries in the test logbook class ImportTest(TestCase): # see test_logins.py for the tests to check that logged-in users work @classmethod def setUpTestData(cls): import troggle.settings as settings LOGBOOKS_PATH = settings.EXPOWEB / lbp.LOGBOOKS_DIR cls.test_logbook = LOGBOOKS_PATH / TEST_YEAR / lbp.DEFAULT_LOGBOOK_FILE frontmatter_file = LOGBOOKS_PATH / TEST_YEAR / "frontmatter.html" if frontmatter_file.is_file(): frontmatter_file.unlink() # delete if it exists coUniqueAttribs = {"year": TEST_YEAR} otherAttribs = {"name": f"CUCC expo-test {TEST_YEAR}"} cls.test_expo = Expedition.objects.create(**otherAttribs, **coUniqueAttribs) fred = create_person("Fred", "Smartarse", nickname="freddy", exped=cls.test_expo) phil = create_person("Phil", "Tosser", nickname="tosspot", exped=cls.test_expo) dave = create_person("David", "Smartarse", "", exped=cls.test_expo) mike = create_person("Michael", "Wideboy", "WB", vfho=True, exped=cls.test_expo) # NOT created Kurt, as the whole point is that he is a guest. def setUp(self): create_user(name="expo") # needed for current_year() self.user = create_user(name="expotest") self.client = Client() def tearDown(self): User.objects.all().delete() Person.objects.all().delete() PersonExpedition.objects.all().delete() Expedition.objects.all().delete() def test_logbook_exists(self): self.assertTrue(self.test_logbook.is_file()) def test_logbook_parse_issues(self): """This is just testing the db not the web page """ lbp.LoadLogbook(self.test_expo) # i.e. load the 1986 logbook issues = DataIssue.objects.all() messages = [] for i in issues: if i.parser=="logbooks": # f"{self.parser} - {self.message}" messages.append(i.message) print(f"'{i.message}'") expected = [ "! - 1986 No name match for: 'Kurt Keinnamen' in entry", ] not_expected = [ " ! - 1986 EXCEPTION:: 'Dave Smartarse' (Dave Smartarse) in entry tid='1986-07-27a' for this year.", " ! - 1986 Warning: logentry: surface - stupour - no expo member author for entry '1986-07-31a'", " ! - 1986 Warning: logentry: 123 - wave 2 - no expo member author for entry '1986-08-01a'", ] # with open('_test_response.txt', 'w') as f: # for m in messages: # f.write(m) messages_text = ", ".join(messages) for e in expected: phmatch = re.search(e, messages_text) self.assertIsNotNone(phmatch, f"Failed to find expected text: '{e}' in\n{messages_text}") for e in not_expected: phmatch = re.search(e, messages_text) self.assertIsNone(phmatch, f"Found unexpected text: '{e}' in\n{messages_text}") def test_lbe(self): lbp.LoadLogbook(self.test_expo) # i.e. load the 1986 logbook, which has this logbook entry response = self.client.get(f"/logbookentry/1986-07-27/1986-07-27a") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() # with open('_test_response_1986-07-27a.html', 'w') as f: # f.write(content) expected = [ "