diff options
Diffstat (limited to 'core/TESTS/test_parsers.py')
-rw-r--r-- | core/TESTS/test_parsers.py | 62 |
1 files changed, 38 insertions, 24 deletions
diff --git a/core/TESTS/test_parsers.py b/core/TESTS/test_parsers.py index 1c7b929..aaccff3 100644 --- a/core/TESTS/test_parsers.py +++ b/core/TESTS/test_parsers.py @@ -23,33 +23,45 @@ import unittest from http import HTTPStatus from django.test import Client, SimpleTestCase, TestCase +from django.contrib.auth.models import User from troggle.core.models.logbooks import LogbookEntry from troggle.core.models.troggle import Expedition, DataIssue, Person, PersonExpedition +from troggle.core.utils import current_expo import troggle.parsers.logbooks as lbp + +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 - fixtures = ["auth_users"] # contains user 'expotest' with a hash => password = 'secretword' @classmethod - def setUpTestData(cls): - def make_person(firstname, lastname, nickname=False, vfho=False): - 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": cls.test_expo} - otherAttribs = {} - pe = PersonExpedition.objects.create(**otherAttribs, **coUniqueAttribs) - - return person - + def setUpTestData(cls): import troggle.settings as settings LOGBOOKS_PATH = settings.EXPOWEB / lbp.LOGBOOKS_DIR @@ -63,21 +75,23 @@ class ImportTest(TestCase): otherAttribs = {"name": f"CUCC expo-test {TEST_YEAR}"} cls.test_expo = Expedition.objects.create(**otherAttribs, **coUniqueAttribs) - fred = make_person("Fred", "Smartarse", nickname="freddy") - phil = make_person("Phil", "Tosser", nickname="tosspot") - dave = make_person("David", "Smartarse", "") - mike = make_person("Michael", "Wideboy", "WB", vfho=True) + 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): - from django.contrib.auth.models import User - - self.user = User.objects.get(username="expotest") # has password 'secretword' from fixture + create_user(name="expo") # needed for current_year() + self.user = create_user(name="expotest") self.client = Client() def tearDown(self): - pass + 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()) @@ -184,7 +198,7 @@ class ImportTest(TestCase): response = self.client.get(f"/aliases/{TEST_YEAR}") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() - # with open('_test_response.html', 'w') as f: + # with open('_test_responsealiases.html', 'w') as f: # f.write(content) ph = f"'fsmartarse'" phmatch = re.search(ph, content) |