foerderbarometer/input/tests/models.py

100 lines
3.8 KiB
Python
Executable File

from datetime import date
from django.test import TestCase
from input.models import HonoraryCertificate, Project, Account, Literature
class ModelTestCase(TestCase):
@classmethod
def setUpTestData(cls):
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):
""" test if the model function set_granted() works as intended """
obj = HonoraryCertificate.objects.create(realname='hurzel', email='hurzel@web.de')
self.assertEqual(obj.granted, None)
HonoraryCertificate.set_granted(obj.pk, True)
obj2 = HonoraryCertificate.objects.get(pk=obj.pk)
self.assertEqual(obj2.granted, True)
def test_project_of_year(self):
""" test if the finance id is resettet ad start of year """
acc = self.account
startdate = date(2022, 1, 1)
obj = Project.objects.create(account=acc, name='testproject', start=startdate)
self.assertEqual(obj.project_of_year, 1)
obj2 = Project.objects.create(account=acc, name='testproject2', start=startdate)
self.assertEqual(obj2.project_of_year, 2)
olddate = date(2021, 12, 31)
obj4 = Project.objects.create(account=acc, name='testproject2', start=olddate)
obj3 = Project.objects.create(account=acc, name='testproject2', start=startdate)
self.assertEqual(obj3.project_of_year, 3)
def test_finance_id(self):
""" test if the finance counting is correct """
acc = self.account
startdate = date(2022, 1, 1)
obj = Project.objects.create(account=acc, name='testproject', start=startdate)
self.assertEqual(obj.finance_id, "1234")
obj2 = Project.objects.create(account=acc, name='testproject2', start=startdate)
self.assertEqual(obj2.finance_id, "1234")
olddate = date(2021, 12, 31)
obj4 = Project.objects.create(account=acc, name='testproject2', start=olddate)
obj3 = Project.objects.create(account=acc, name='testproject2', start=startdate)
self.assertEqual(obj3.finance_id, "1234")
def test_financed_id_for_subaccounts(self):
account = Account.objects.create(code='21111', description='has subaccounts')
obj = Project.objects.create(account=account, name='test', start=date(2025, 1, 1))
self.assertEqual(obj.finance_id, f'{account.code}-001')
def test_finance_id_later(self):
obj = Project.objects.create(name='test', start=date(2025, 1, 1))
self.assertFalse(obj.finance_id)
obj.account = self.account
obj.save()
self.assertTrue(obj.finance_id)
def test_pid(self):
""" test if the pid counting is correct """
acc = self.account
startdate = date(2022, 1, 1)
obj = Project.objects.create(account=acc, name='testproject', start=startdate)
self.assertLen(obj.pid, len(acc.code) + 8)
self.assertStartsWith(obj.pid, acc.code)
self.assertEndsWith(obj.pid, obj.id)
obj2 = Project.objects.create(account=acc, name='testproject2', start=startdate)
self.assertEndsWith(obj2.pid, obj.id + 1)
olddate = date(2021, 12, 31)
Project.objects.create(account=acc, name='testproject2', start=olddate)
obj4 = Project.objects.create(account=acc, name='testproject2', start=startdate)
self.assertEndsWith(obj4.pid, obj.id + 3)
def test_literature(self):
obj = Literature.objects.create(cost='100', notes='jolo', selfbuy_give_data=False)
self.assertEqual(obj.service_id, f'Literature{obj.id}')