made tests robust against changing order

This commit is contained in:
Oliver Zander 2025-10-17 10:28:08 +02:00
parent b963db5731
commit ad3ab2e993
1 changed files with 16 additions and 6 deletions

View File

@ -11,6 +11,15 @@ class ModelTestCase(TestCase):
def setUpTestData(cls): def setUpTestData(cls):
cls.account = Account.objects.create(code='1234', description='blabla') cls.account = Account.objects.create(code='1234', description='blabla')
def assertLen(self, value, length, msg=None):
self.assertEqual(len(value), length, msg)
def assertStartsWith(self, value, start, msg=None):
self.assertTrue(f'{value}'.startswith(f'{start}'), msg)
def assertEndsWith(self, value, end, msg=None):
self.assertTrue(f'{value}'.endswith(f'{end}'), msg)
def test_set_granted(self): def test_set_granted(self):
""" test if the model function set_granted() works as intended """ """ test if the model function set_granted() works as intended """
obj = HonoraryCertificate.objects.create(realname='hurzel', email='hurzel@web.de') obj = HonoraryCertificate.objects.create(realname='hurzel', email='hurzel@web.de')
@ -72,17 +81,18 @@ class ModelTestCase(TestCase):
acc = self.account acc = self.account
startdate = date(2022, 1, 1) startdate = date(2022, 1, 1)
obj = Project.objects.create(account=acc, name='testproject', start=startdate) obj = Project.objects.create(account=acc, name='testproject', start=startdate)
self.assertEqual(obj.pid, "123400000001") self.assertLen(obj.pid, len(acc.code) + 8)
self.assertEqual(obj.account.code, "1234") self.assertStartsWith(obj.pid, acc.code)
self.assertEndsWith(obj.pid, obj.id)
obj2 = Project.objects.create(account=acc, name='testproject2', start=startdate) obj2 = Project.objects.create(account=acc, name='testproject2', start=startdate)
self.assertEqual(obj2.pid, "123400000002") self.assertEndsWith(obj2.pid, obj.id + 1)
olddate = date(2021, 12, 31) olddate = date(2021, 12, 31)
obj4 = Project.objects.create(account=acc, name='testproject2', start=olddate) Project.objects.create(account=acc, name='testproject2', start=olddate)
obj3 = Project.objects.create(account=acc, name='testproject2', start=startdate) obj4 = Project.objects.create(account=acc, name='testproject2', start=startdate)
self.assertEqual(obj3.pid, "123400000004") self.assertEndsWith(obj4.pid, obj.id + 3)
def test_literature(self): def test_literature(self):
obj = Literature.objects.create(cost='100', notes='jolo', selfbuy_give_data=False) obj = Literature.objects.create(cost='100', notes='jolo', selfbuy_give_data=False)