diff --git a/compensation/tests/compensation/unit/test_forms.py b/compensation/tests/compensation/unit/test_forms.py index 8e3eb15c..82fb90e8 100644 --- a/compensation/tests/compensation/unit/test_forms.py +++ b/compensation/tests/compensation/unit/test_forms.py @@ -14,16 +14,19 @@ from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ from codelist.models import KonovaCodeList -from codelist.settings import CODELIST_COMPENSATION_ACTION_ID +from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID from compensation.forms.modals.compensation_action import NewCompensationActionModalForm, \ EditCompensationActionModalForm, RemoveCompensationActionModalForm from compensation.forms.modals.deadline import NewDeadlineModalForm, EditDeadlineModalForm +from compensation.forms.modals.state import NewCompensationStateModalForm, EditCompensationStateModalForm, \ + RemoveCompensationStateModalForm from compensation.models import UnitChoices from konova.models import DeadlineType from konova.tests.test_views import BaseTestCase from konova.utils.generators import generate_random_string from konova.utils.message_templates import COMPENSATION_ACTION_EDITED, ADDED_COMPENSATION_ACTION, \ - COMPENSATION_ACTION_REMOVED, ADDED_DEADLINE, DEADLINE_EDITED + COMPENSATION_ACTION_REMOVED, ADDED_DEADLINE, DEADLINE_EDITED, ADDED_COMPENSATION_STATE, COMPENSATION_STATE_EDITED, \ + COMPENSATION_STATE_REMOVED from user.models import UserAction @@ -247,3 +250,163 @@ class EditDeadlineModalFormTestCase(NewDeadlineModalFormTestCase): self.assertEqual(last_log.comment, DEADLINE_EDITED) self.assertIn(deadline, self.compensation.deadlines.all()) + + +class NewCompensationStateModalFormTestCase(BaseTestCase): + def setUp(self) -> None: + super().setUp() + self.request = RequestFactory().request() + self.request.user = self.superuser + + self.comp_biotope_code = self.create_dummy_codes().first() + self.biotope_codelist = KonovaCodeList.objects.get_or_create( + id=CODELIST_BIOTOPES_ID + )[0] + self.biotope_codelist.codes.add(self.comp_biotope_code) + + def test_init(self): + form = NewCompensationStateModalForm(request=self.request, instance=self.compensation) + + self.assertEqual(form.form_title, str(_("New state"))) + self.assertEqual(form.form_caption, str(_("Insert data for the new state"))) + self.assertEqual(len(form.fields["biotope_type"].choices), 1) + + def test_save(self): + test_surface = 123.45 + data = { + "biotope_type": self.comp_biotope_code.id, + "biotope_extra": [], + "surface": test_surface, + } + self.assertEqual(self.compensation.before_states.count(), 0) + self.assertEqual(self.compensation.after_states.count(), 0) + + form = NewCompensationStateModalForm(data, request=self.request, instance=self.compensation) + + self.assertTrue(form.is_valid(), msg=form.errors) + + is_before_state = True + state = form.save(is_before_state) + + self.assertEqual(self.compensation.before_states.count(), 1) + self.assertEqual(self.compensation.after_states.count(), 0) + self.assertIn(state, self.compensation.before_states.all()) + self.assertEqual(state.biotope_type, self.comp_biotope_code) + self.assertEqual(state.biotope_type_details.count(), 0) + self.assertEqual(float(state.surface), test_surface) + + 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_COMPENSATION_STATE) + + is_before_state = False + state = form.save(is_before_state) + + self.assertEqual(self.compensation.before_states.count(), 1) + self.assertEqual(self.compensation.after_states.count(), 1) + self.assertIn(state, self.compensation.after_states.all()) + self.assertEqual(state.biotope_type, self.comp_biotope_code) + self.assertEqual(state.biotope_type_details.count(), 0) + self.assertEqual(float(state.surface), test_surface) + + 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_COMPENSATION_STATE) + + +class EditCompensationStateModalFormTestCase(NewCompensationStateModalFormTestCase): + def setUp(self) -> None: + super().setUp() + self.comp_state.biotope_type = self.comp_biotope_code + self.comp_state.save() + + self.compensation.after_states.add(self.comp_state) + + def test_init(self): + form = EditCompensationStateModalForm(request=self.request, instance=self.compensation, state=self.comp_state) + + self.assertEqual(form.state, self.comp_state) + self.assertEqual(form.form_title, str(_("Edit state"))) + self.assertEqual(form.fields["biotope_type"].initial, self.comp_state.biotope_type.id) + self.assertTrue( + form.fields["biotope_extra"].initial.difference( + self.comp_state.biotope_type_details.all() + ).count() == 0 + ) + self.assertEqual(form.fields["surface"].initial, self.comp_state.surface) + + def test_save(self): + test_surface = 987.65 + test_code = self.create_dummy_codes().exclude( + id=self.comp_biotope_code.id + ).first() + self.biotope_codelist.codes.add(test_code) + + self.assertEqual(self.compensation.after_states.count(), 1) + self.assertEqual(self.compensation.before_states.count(), 0) + + data = { + "biotope_type": test_code.id, + "biotope_extra": [], + "surface": test_surface, + } + form = EditCompensationStateModalForm( + data, + request=self.request, + instance=self.compensation, + state=self.comp_state + ) + self.assertTrue(form.is_valid(), msg=form.errors) + + is_before_state = False + state = form.save(is_before_state=is_before_state) + self.assertEqual(state.biotope_type, test_code) + self.assertEqual(state.biotope_type_details.count(), 0) + self.assertEqual(float(state.surface), test_surface) + + 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, COMPENSATION_STATE_EDITED) + +class RemoveCompensationStateModalFormTestCase(EditCompensationStateModalFormTestCase): + def setUp(self) -> None: + super().setUp() + + def test_init(self): + form = RemoveCompensationStateModalForm(request=self.request, instance=self.compensation, state=self.comp_state) + + self.assertEqual(form.state, self.comp_state) + + def test_save(self): + data = { + "confirm": True + } + form = RemoveCompensationStateModalForm( + data, + request=self.request, + instance=self.compensation, + state=self.comp_state + ) + self.assertTrue(form.is_valid(), msg=form.errors) + + self.assertIn(self.comp_state, self.compensation.after_states.all()) + self.assertNotIn(self.comp_state, self.compensation.before_states.all()) + + form.save() + + self.assertEqual(self.compensation.after_states.count(), 0) + self.assertEqual(self.compensation.before_states.count(), 0) + try: + self.comp_state.refresh_from_db() + self.fail("Entry should not existing anymore") + except ObjectDoesNotExist: + pass + + 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, COMPENSATION_STATE_REMOVED) +