From 02e8d65f023ad888ba280b1fb2fec10b83fdbde3 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Thu, 24 Aug 2023 10:59:32 +0200 Subject: [PATCH] Unit test EMA model * adds unit test for EMA model --- ema/models/ema.py | 9 ++-- ema/settings.py | 4 +- ema/tests/unit/__init__.py | 7 +++ ema/tests/unit/test_models.py | 90 +++++++++++++++++++++++++++++++++++ konova/models/object.py | 6 +-- 5 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 ema/tests/unit/__init__.py create mode 100644 ema/tests/unit/test_models.py diff --git a/ema/models/ema.py b/ema/models/ema.py index a7172da8..8d627587 100644 --- a/ema/models/ema.py +++ b/ema/models/ema.py @@ -122,7 +122,7 @@ class EmaDocument(AbstractDocument): def delete(self, user=None, *args, **kwargs): """ - Custom delete functionality for EcoAccountDocuments. + Custom delete functionality for EmaDocuments. Removes the folder from the file system if there are no further documents for this entry. Args: @@ -139,8 +139,11 @@ class EmaDocument(AbstractDocument): # The only file left for this EMA is the one which is currently processed and will be deleted # Make sure that the compensation folder itself is deleted as well, not only the file # Therefore take the folder path from the file path - folder_path = self.file.path.split("/")[:-1] - folder_path = "/".join(folder_path) + try: + folder_path = self.file.path.split("/")[:-1] + folder_path = "/".join(folder_path) + except ValueError: + folder_path = None if user: self.instance.mark_as_edited(user, edit_comment=DOCUMENT_REMOVED_TEMPLATE.format(self.title)) diff --git a/ema/settings.py b/ema/settings.py index 0c6f5b6b..a6a124c8 100644 --- a/ema/settings.py +++ b/ema/settings.py @@ -6,5 +6,5 @@ Created on: 19.08.21 """ -EMA_ACCOUNT_IDENTIFIER_LENGTH = 6 -EMA_ACCOUNT_IDENTIFIER_TEMPLATE = "EMA-{}" \ No newline at end of file +EMA_IDENTIFIER_LENGTH = 6 +EMA_IDENTIFIER_TEMPLATE = "EMA-{}" \ No newline at end of file diff --git a/ema/tests/unit/__init__.py b/ema/tests/unit/__init__.py new file mode 100644 index 00000000..685f2583 --- /dev/null +++ b/ema/tests/unit/__init__.py @@ -0,0 +1,7 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: ksp-servicestelle@sgdnord.rlp.de +Created on: 24.08.23 + +""" diff --git a/ema/tests/unit/test_models.py b/ema/tests/unit/test_models.py new file mode 100644 index 00000000..b468249d --- /dev/null +++ b/ema/tests/unit/test_models.py @@ -0,0 +1,90 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: ksp-servicestelle@sgdnord.rlp.de +Created on: 24.08.23 + +""" +from django.urls import reverse +from django.utils.timezone import now + +from ema.models import Ema, EmaDocument +from ema.settings import EMA_IDENTIFIER_TEMPLATE +from konova.tests.test_views import BaseTestCase +from konova.utils.message_templates import DOCUMENT_REMOVED_TEMPLATE +from user.models import UserAction + + +class EmaModelTestCase(BaseTestCase): + def setUp(self) -> None: + super().setUp() + + def test_str(self): + self.assertEqual(str(self.ema), f"{self.ema.identifier}") + + def test_save(self): + new_ema = Ema( + title="Test" + ) + self.assertIsNone(new_ema.identifier) + + new_ema.save() + new_ema.refresh_from_db() + + self.assertIsNotNone(new_ema.identifier) + self.assertIn("EMA-", new_ema.identifier) + + def test_is_ready_for_publish(self): + self.assertIsNone(self.ema.recorded) + self.assertFalse(self.ema.is_ready_for_publish()) + + self.ema.set_recorded(self.superuser) + self.ema.refresh_from_db() + self.assertIsNotNone(self.ema.recorded) + self.assertTrue(self.ema.is_ready_for_publish()) + + def test_get_share_link(self): + self.assertEqual( + self.ema.get_share_link(), + reverse("ema:share-token", args=(self.ema.id, self.ema.access_token)) + ) + + def test_get_documents(self): + self.assertEqual(self.ema.get_documents().count(), 0) + + doc = EmaDocument( + instance=self.ema, + date_of_creation=now().date(), + comment="Test", + ) + doc.save() + docs = self.ema.get_documents() + self.assertEqual(docs.count(), 1) + self.assertEqual(docs.first(), doc) + + +class EmaDocumentModelTestCase(BaseTestCase): + def setUp(self) -> None: + super().setUp() + + def test_delete(self): + doc = EmaDocument.objects.create( + date_of_creation=now().date(), + instance=self.ema, + comment="TEST" + ) + self.ema.refresh_from_db() + docs = self.ema.get_documents() + self.assertEqual(docs.count(), 1) + self.assertEqual(docs.first(), doc) + + doc_title = doc.title + doc.delete(user=self.superuser) + last_log = self.ema.log.first() + self.assertEqual(last_log.action, UserAction.EDITED) + self.assertEqual(last_log.user, self.superuser) + self.assertEqual(last_log.comment, DOCUMENT_REMOVED_TEMPLATE.format(doc_title)) + + docs = self.ema.get_documents() + self.assertEqual(docs.count(), 0) + diff --git a/konova/models/object.py b/konova/models/object.py index 5491e54e..4f996f5b 100644 --- a/konova/models/object.py +++ b/konova/models/object.py @@ -25,7 +25,7 @@ from django.utils.timezone import now from django.db import models, transaction from compensation.settings import COMPENSATION_IDENTIFIER_TEMPLATE, COMPENSATION_IDENTIFIER_LENGTH, \ ECO_ACCOUNT_IDENTIFIER_TEMPLATE, ECO_ACCOUNT_IDENTIFIER_LENGTH -from ema.settings import EMA_ACCOUNT_IDENTIFIER_LENGTH, EMA_ACCOUNT_IDENTIFIER_TEMPLATE +from ema.settings import EMA_IDENTIFIER_LENGTH, EMA_IDENTIFIER_TEMPLATE from intervention.settings import INTERVENTION_IDENTIFIER_LENGTH, INTERVENTION_IDENTIFIER_TEMPLATE from konova.utils import generators from konova.utils.generators import generate_random_string @@ -211,8 +211,8 @@ class BaseObject(BaseResource, DeletableObjectMixin): "template": ECO_ACCOUNT_IDENTIFIER_TEMPLATE, }, Ema: { - "length": EMA_ACCOUNT_IDENTIFIER_LENGTH, - "template": EMA_ACCOUNT_IDENTIFIER_TEMPLATE, + "length": EMA_IDENTIFIER_LENGTH, + "template": EMA_IDENTIFIER_TEMPLATE, }, }