From ed548736e067924c677094903422feccf003dc65 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Thu, 7 Sep 2023 10:48:11 +0200 Subject: [PATCH] Unit test intervention/konova * adds unit test for intervention app * adds unit test for konova app --- intervention/tests/unit/test_models.py | 50 +++++ konova/tests/test_views.py | 15 +- konova/tests/unit/test_forms.py | 289 +++++++++++++++++++++++++ 3 files changed, 353 insertions(+), 1 deletion(-) create mode 100644 intervention/tests/unit/test_models.py create mode 100644 konova/tests/unit/test_forms.py diff --git a/intervention/tests/unit/test_models.py b/intervention/tests/unit/test_models.py new file mode 100644 index 00000000..acc7da2a --- /dev/null +++ b/intervention/tests/unit/test_models.py @@ -0,0 +1,50 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: ksp-servicestelle@sgdnord.rlp.de +Created on: 07.09.23 + +""" +from django.core.exceptions import ObjectDoesNotExist +from django.utils.timezone import now + +from intervention.models import RevocationDocument, Revocation +from konova.tests.test_views import BaseTestCase + + +class RevocationDocumentTestCase(BaseTestCase): + def setUp(self) -> None: + super().setUp() + self.revocation = Revocation.objects.get_or_create( + date=now().date(), + comment="Test", + legal=self.intervention.legal + )[0] + self.doc = self.create_dummy_document( + RevocationDocument, + instance=self.revocation + ) + + def test_intervention_property(self): + self.assertEqual( + self.doc.intervention, + self.doc.instance.legal.intervention + ) + self.assertEqual( + self.doc.intervention, + self.intervention + ) + + def test_delete(self): + revoc_docs, other_intervention_docs = self.intervention.get_documents() + self.assertIn(self.doc, revoc_docs) + + try: + self.doc.delete() + self.doc.refresh_from_db() + self.fail("Should not be fetchable anymore!") + except ObjectDoesNotExist: + pass + + revoc_docs, other_intervention_docs = self.intervention.get_documents() + self.assertEqual(revoc_docs.count(), 0) diff --git a/konova/tests/test_views.py b/konova/tests/test_views.py index 1ad02f13..81736c82 100644 --- a/konova/tests/test_views.py +++ b/konova/tests/test_views.py @@ -8,7 +8,7 @@ Created on: 26.10.21 import datetime import json -from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID +from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, CODELIST_REGISTRATION_OFFICE_ID from ema.models import Ema from konova.sub_settings.lanis_settings import DEFAULT_SRID_RLP from user.models import User, Team @@ -420,6 +420,19 @@ class BaseTestCase(TestCase): codelist.codes.add(code) return code + def get_registration_office_code(self): + """ Returns a dummy KonovaCode as conservation office code + + Returns: + + """ + codelist = KonovaCodeList.objects.get_or_create( + id=CODELIST_REGISTRATION_OFFICE_ID + )[0] + code = KonovaCode.objects.get(id=3) + codelist.codes.add(code) + return code + def fill_out_ema(self, ema): """ Adds all required (dummy) data to an Ema diff --git a/konova/tests/unit/test_forms.py b/konova/tests/unit/test_forms.py new file mode 100644 index 00000000..d13ec8e1 --- /dev/null +++ b/konova/tests/unit/test_forms.py @@ -0,0 +1,289 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: ksp-servicestelle@sgdnord.rlp.de +Created on: 07.09.23 + +""" +import mimetypes + +from django.core.exceptions import ObjectDoesNotExist +from django.test import RequestFactory +from django.utils.translation import gettext_lazy as _ +from django.core.files.uploadedfile import SimpleUploadedFile +from django.utils.timezone import now + +from compensation.forms.modals.document import NewEcoAccountDocumentModalForm, NewCompensationDocumentModalForm +from compensation.models import Payment +from ema.forms import NewEmaDocumentModalForm +from intervention.forms.modals.document import NewInterventionDocumentModalForm +from intervention.models import InterventionDocument +from konova.forms.modals import EditDocumentModalForm, NewDocumentModalForm, RecordModalForm, RemoveModalForm, \ + RemoveDeadlineModalForm +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 DOCUMENT_EDITED, DEADLINE_REMOVED +from user.models import UserAction + + +class NewDocumentModalFormTestCase(BaseTestCase): + def setUp(self) -> None: + super().setUp() + self.request = RequestFactory().request() + self.request.user = self.superuser + self.dummy_file = SimpleUploadedFile( + "some_file.pdf", + b"Some conent in this file", + mimetypes.types_map[".pdf"] + ) + dummy_file_dict = { + "file": self.dummy_file + } + self.data = { + "title": generate_random_string(length=5, use_letters_lc=True), + "creation_date": now().date(), + "comment": generate_random_string(length=50, use_letters_uc=True), + } + self.forms = [ + NewInterventionDocumentModalForm(self.data, dummy_file_dict, request=self.request, instance=self.intervention), + NewCompensationDocumentModalForm(self.data, dummy_file_dict, request=self.request, instance=self.compensation), + NewEmaDocumentModalForm(self.data, dummy_file_dict, request=self.request, instance=self.ema), + NewEcoAccountDocumentModalForm(self.data, dummy_file_dict, request=self.request, instance=self.eco_account), + ] + + def test_init(self): + for form in self.forms: + self.assertEqual(form.form_title, str(_("Add new document"))) + self.assertEqual(form.form_caption, str(_(""))) + self.assertEqual( + form.form_attrs, + { + "enctype": "multipart/form-data" + } + ) + self.assertEqual(form.request, self.request) + self.assertEqual(form.user, self.superuser) + + try: + NewDocumentModalForm(request=self.request, instance=self.intervention) + self.fail("Base form NewDocumentModalForm should not be creatable") + except NotImplementedError: + pass + + def test_is_valid(self): + for form in self.forms: + self.assertTrue( + form.is_valid(), msg=form.errors + ) + + def test_save(self): + for form in self.forms: + form.is_valid() + obj = form.save() + self.assertEqual(obj.created.action, UserAction.CREATED) + self.assertEqual(obj.created.user, self.superuser) + self.assertEqual(obj.title, self.data["title"]) + self.assertEqual(obj.date_of_creation, self.data["creation_date"]) + self.assertEqual(obj.comment, self.data["comment"]) + self.assertIsNotNone(obj.file) + + last_log = obj.instance.log.first() + self.assertEqual(last_log.action, UserAction.EDITED) + self.assertEqual(last_log.user, self.request.user) + self.assertEqual(last_log.comment, str(_("Added document"))) + self.assertEqual(obj.instance.modified, last_log) + + +class EditDocumentModalFormTestCase(NewDocumentModalFormTestCase): + def setUp(self) -> None: + super().setUp() + dummy_file_dict = { + "file": self.dummy_file + } + self.doc = self.create_dummy_document( + InterventionDocument, + instance=self.intervention + ) + self.form = EditDocumentModalForm( + self.data, + dummy_file_dict, + request=self.request, + instance=self.intervention, + document=self.doc + ) + + def test_init(self): + self.assertEqual(self.form.form_title, str(_("Edit document"))) + self.assertEqual(self.form.document, self.doc) + self.assertEqual(self.form.request, self.request) + self.assertEqual(self.form.user, self.request.user) + self.assertEqual(self.form.fields["title"].initial, self.doc.title) + self.assertEqual(self.form.fields["comment"].initial, self.doc.comment) + self.assertEqual(self.form.fields["creation_date"].initial, self.doc.date_of_creation) + self.assertEqual(self.form.fields["file"].initial, self.doc.file) + + def test_save(self): + self.assertTrue(self.form.is_valid(), msg=self.form.errors) + obj = self.form.save() + self.assertEqual(obj.title, self.data["title"]) + self.assertEqual(obj.comment, self.data["comment"]) + self.assertEqual(obj.date_of_creation, self.data["creation_date"]) + + last_log = obj.instance.log.first() + self.assertEqual(last_log.action, UserAction.EDITED) + self.assertEqual(last_log.user, self.request.user) + self.assertEqual(last_log.comment, DOCUMENT_EDITED) + + +class RecordModalFormTestCase(BaseTestCase): + def setUp(self) -> None: + super().setUp() + self.request = RequestFactory().request() + self.request.user = self.user + + self.fill_out_compensation(self.compensation) + + def test_init(self): + form = RecordModalForm( + request=self.request, + instance=self.intervention + ) + self.assertEqual(form.form_title, str(_("Record data"))) + self.assertEqual(form.form_caption, str( + _("I, {} {}, confirm that all necessary control steps have been performed by myself.").format( + self.user.first_name, + self.user.last_name + ) + )) + + self.intervention.set_recorded(self.user) + self.intervention.refresh_from_db() + form = RecordModalForm( + request=self.request, + instance=self.intervention + ) + self.assertEqual(form.form_title, str(_("Unrecord data"))) + self.assertEqual(form.fields["confirm"].label, str(_("Confirm unrecord"))) + self.assertEqual(form.form_caption, str( + _("I, {} {}, confirm that this data must be unrecorded.").format( + self.user.first_name, + self.user.last_name + ))) + + def test_is_valid(self): + data = { + "confirm": True + } + form = RecordModalForm(data, request=self.request, instance=self.intervention) + self.assertFalse(self.intervention.is_recorded) + self.assertFalse(form.is_valid(), msg=form.errors) # intervention not complete + + self.intervention = self.fill_out_intervention(self.intervention) + form = RecordModalForm(data, request=self.request, instance=self.intervention) + self.assertTrue(form.is_valid(), msg=form.errors) + + def test_save(self): + data = { + "confirm": True + } + self.intervention = self.fill_out_intervention(self.intervention) + form = RecordModalForm(data, request=self.request, instance=self.intervention) + self.assertTrue(form.is_valid(), msg=form.errors) + + form.save() + self.assertEqual(self.intervention.recorded.action, UserAction.RECORDED) + self.assertEqual(self.intervention.recorded.user, self.request.user) + self.assertEqual(self.intervention.recorded, self.intervention.log.first()) + + +class RemoveModalFormTestCase(BaseTestCase): + def setUp(self) -> None: + super().setUp() + self.request = RequestFactory().request() + self.request.user = self.user + + def test_init(self): + form = RemoveModalForm(request=self.request, instance=self.intervention) + self.assertEqual(form.form_title, str(_("Remove"))) + self.assertEqual(form.form_caption, str(_("Are you sure?"))) + self.assertEqual(form.template, "modal/modal_form.html") + self.assertEqual(form.request, self.request) + self.assertEqual(form.user, self.request.user) + + def test_save(self): + data = { + "confirm": True, + } + form = RemoveModalForm( + data, + request=self.request, + instance=self.intervention + ) + self.assertTrue(form.is_valid(), msg=form.errors) + + form.save() + self.assertEqual(self.intervention.deleted.action, UserAction.DELETED) + self.assertEqual(self.intervention.deleted.user, self.request.user) + + payment = Payment.objects.create( + amount=1.0, + intervention=self.intervention + ) + form = RemoveModalForm( + data, + request=self.request, + instance=payment + ) + self.assertTrue(form.is_valid(), msg=form.errors) + form.save() + try: + payment.refresh_from_db() + self.fail("Payment still exists") + except ObjectDoesNotExist: + pass + + +class RemoveDeadlineTestCase(BaseTestCase): + def setUp(self) -> None: + super().setUp() + request = RequestFactory().request() + request.user = self.user + self.request = request + + def test_init(self): + form = RemoveDeadlineModalForm( + request=self.request, + instance=self.compensation, + deadline=self.finished_deadline + ) + self.assertEqual(form.form_title, str(_("Remove"))) + self.assertEqual(form.form_caption, str(_("Are you sure?"))) + self.assertEqual(form.template, "modal/modal_form.html") + self.assertEqual(form.request, self.request) + self.assertEqual(form.user, self.request.user) + + def test_save(self): + self.compensation.deadlines.add(self.finished_deadline) + data = { + "confirm": True + } + form = RemoveDeadlineModalForm( + data, + request=self.request, + instance=self.compensation, + deadline=self.finished_deadline + ) + self.assertTrue(form.is_valid(), msg=form.errors) + form.save() + + try: + self.finished_deadline.refresh_from_db() + self.fail("Deadline still exists") + except ObjectDoesNotExist: + pass + + last_log = self.compensation.log.first() + self.assertEqual(last_log.action, UserAction.EDITED) + self.assertEqual(last_log.user, self.request.user) + self.assertEqual(last_log.comment, DEADLINE_REMOVED)