Unit test for compensation forms

* adds unit tests for adding and editing deadline
This commit is contained in:
mpeltriaux 2023-08-21 10:33:05 +02:00
parent b802c02069
commit 147d4938db

View File

@ -5,19 +5,25 @@ Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 21.08.23 Created on: 21.08.23
""" """
from datetime import timedelta
from dateutil.parser import parse
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.test import RequestFactory from django.test import RequestFactory
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from codelist.models import KonovaCodeList from codelist.models import KonovaCodeList
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID from codelist.settings import CODELIST_COMPENSATION_ACTION_ID
from compensation.forms.modals.compensation_action import NewCompensationActionModalForm, \ from compensation.forms.modals.compensation_action import NewCompensationActionModalForm, \
EditCompensationActionModalForm, RemoveCompensationActionModalForm EditCompensationActionModalForm, RemoveCompensationActionModalForm
from compensation.forms.modals.deadline import NewDeadlineModalForm, EditDeadlineModalForm
from compensation.models import UnitChoices from compensation.models import UnitChoices
from konova.models import DeadlineType
from konova.tests.test_views import BaseTestCase from konova.tests.test_views import BaseTestCase
from konova.utils.generators import generate_random_string from konova.utils.generators import generate_random_string
from konova.utils.message_templates import COMPENSATION_ACTION_EDITED, ADDED_COMPENSATION_ACTION, \ from konova.utils.message_templates import COMPENSATION_ACTION_EDITED, ADDED_COMPENSATION_ACTION, \
COMPENSATION_ACTION_REMOVED COMPENSATION_ACTION_REMOVED, ADDED_DEADLINE, DEADLINE_EDITED
from user.models import UserAction from user.models import UserAction
@ -152,3 +158,92 @@ class RemoveCompensationActionModalFormTestCase(EditCompensationActionModalFormT
self.fail(msg="This action should not be fetchable anymore") self.fail(msg="This action should not be fetchable anymore")
except ObjectDoesNotExist: except ObjectDoesNotExist:
pass pass
class NewDeadlineModalFormTestCase(BaseTestCase):
def setUp(self) -> None:
super().setUp()
self.request = RequestFactory().request()
self.request.user = self.superuser
def test_init(self):
form = NewDeadlineModalForm(
request=self.request,
instance=self.compensation
)
self.assertEqual(form.form_title, str(_("New deadline")))
self.assertEqual(form.form_caption, str(_("Insert data for the new deadline")))
def test_save(self):
deadline_type = DeadlineType.MAINTAIN
deadline_date = now().date() + timedelta(days=500)
deadline_comment = generate_random_string(50, use_letters_uc=True, use_letters_lc=True)
data = {
"type": deadline_type,
"date": deadline_date,
"comment": deadline_comment,
}
form = NewDeadlineModalForm(data, request=self.request, instance=self.compensation)
self.assertTrue(form.is_valid())
deadline = form.save()
last_log = self.compensation.log.first()
self.assertEqual(last_log.user, self.superuser)
self.assertEqual(last_log.action, UserAction.EDITED)
self.assertEqual(last_log.comment, ADDED_DEADLINE)
self.assertEqual(deadline.date, deadline_date)
self.assertEqual(deadline.type, deadline_type)
self.assertEqual(deadline.comment, deadline_comment)
self.assertIn(deadline, self.compensation.deadlines.all())
class EditDeadlineModalFormTestCase(NewDeadlineModalFormTestCase):
def setUp(self) -> None:
super().setUp()
self.compensation.deadlines.add(self.finished_deadline)
def test_init(self):
form = EditDeadlineModalForm(
request=self.request,
instance=self.compensation,
deadline=self.finished_deadline
)
self.assertEqual(form.deadline, self.finished_deadline)
self.assertEqual(form.form_title, str(_("Edit deadline")))
self.assertEqual(form.fields["type"].initial, self.finished_deadline.type)
self.assertEqual(form.fields["date"].initial, self.finished_deadline.date)
self.assertEqual(form.fields["comment"].initial, self.finished_deadline.comment)
def test_save(self):
edit_type = DeadlineType.MAINTAIN
edit_date = parse(self.finished_deadline.date).date() - timedelta(days=5)
edit_comment = generate_random_string(length=40, use_letters_lc=True)
data = {
"type": edit_type,
"date": edit_date,
"comment": edit_comment,
}
form = EditDeadlineModalForm(
data,
request=self.request,
instance=self.compensation,
deadline=self.finished_deadline
)
self.assertTrue(form.is_valid())
deadline = form.save()
self.assertEqual(deadline.type, edit_type)
self.assertEqual(deadline.date, edit_date)
self.assertEqual(deadline.comment, edit_comment)
last_log = self.compensation.log.first()
self.assertEqual(last_log.action, UserAction.EDITED)
self.assertEqual(last_log.user, self.superuser)
self.assertEqual(last_log.comment, DEADLINE_EDITED)
self.assertIn(deadline, self.compensation.deadlines.all())