From 530ceb387668b8fa80f9439117b04769d5932d67 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Thu, 7 Sep 2023 11:07:17 +0200 Subject: [PATCH] Unit test konova base forms * adds unit test for resubmission modal form --- konova/tests/unit/test_forms.py | 80 ++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/konova/tests/unit/test_forms.py b/konova/tests/unit/test_forms.py index d13ec8e1..3cbb4371 100644 --- a/konova/tests/unit/test_forms.py +++ b/konova/tests/unit/test_forms.py @@ -6,6 +6,7 @@ Created on: 07.09.23 """ import mimetypes +from datetime import timedelta from django.core.exceptions import ObjectDoesNotExist from django.test import RequestFactory @@ -19,8 +20,8 @@ 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 + RemoveDeadlineModalForm, ResubmissionModalForm +from konova.models import Resubmission 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 @@ -287,3 +288,78 @@ class RemoveDeadlineTestCase(BaseTestCase): self.assertEqual(last_log.action, UserAction.EDITED) self.assertEqual(last_log.user, self.request.user) self.assertEqual(last_log.comment, DEADLINE_REMOVED) + + +class ResubmissionModalFormTestCase(BaseTestCase): + def setUp(self) -> None: + super().setUp() + self.request = RequestFactory().request() + self.request.user = self.user + + def test_init(self): + # Resubmission nonexistent + form = ResubmissionModalForm(request=self.request, instance=self.intervention) + self.assertEqual(form.form_title, str(_("Resubmission"))) + self.assertEqual(form.form_caption, str(_("Set your resubmission for this entry."))) + self.assertEqual(form.action_url, None) + self.assertIsNotNone(form.resubmission) + + resubmission = Resubmission.objects.create( + user=self.request.user, + resubmit_on=now().date(), + comment=generate_random_string(length=10, use_letters_lc=True) + ) + self.intervention.resubmissions.add(resubmission) + + # Resubmission exists + form = ResubmissionModalForm(request=self.request, instance=self.intervention) + self.assertEqual(form.form_title, str(_("Resubmission"))) + self.assertEqual(form.form_caption, str(_("Set your resubmission for this entry."))) + self.assertEqual(form.action_url, None) + self.assertEqual(form.fields["date"].initial, str(resubmission.resubmit_on)) + self.assertEqual(form.fields["comment"].initial, resubmission.comment) + self.assertEqual(form.resubmission, resubmission) + + def test_is_valid(self): + yesterday = now().date() - timedelta(days=1) + data = { + "date": yesterday, + "comment": "Edited comment" + } + form = ResubmissionModalForm( + data, + request=self.request, + instance=self.intervention + ) + self.assertFalse(form.is_valid(), msg=form.errors) + self.assertTrue(form.has_error("date")) + + tomorrow = yesterday + timedelta(days=2) + data = { + "date": tomorrow, + "comment": "Edited comment" + } + form = ResubmissionModalForm( + data, + request=self.request, + instance=self.intervention + ) + self.assertTrue(form.is_valid(), msg=form.errors) + + def test_save(self): + data = { + "date": now().date() + timedelta(days=1), + "comment": "New comment for new resubmission" + } + form = ResubmissionModalForm( + data, + request=self.request, + instance=self.intervention + ) + self.assertTrue(form.is_valid(), msg=form.errors) + obj = form.save() + self.assertIn(obj, self.intervention.resubmissions.all()) + self.assertEqual(obj.resubmit_on, data["date"]) + self.assertEqual(obj.comment, data["comment"]) + self.assertEqual(obj.resubmission_sent, False) + self.assertEqual(obj.user, self.request.user)