Unit test compensation models
* adds unit tests for compensation models * removes duplicated unit tests
This commit is contained in:
parent
777b7a929d
commit
4392401f27
@ -399,7 +399,7 @@ class Compensation(AbstractCompensation, CEFMixin, CoherenceMixin, PikMixin):
|
||||
Returns:
|
||||
users (QuerySet)
|
||||
"""
|
||||
return self.intervention.users.all()
|
||||
return self.intervention.shared_users
|
||||
|
||||
@property
|
||||
def shared_teams(self) -> QuerySet:
|
||||
@ -408,7 +408,7 @@ class Compensation(AbstractCompensation, CEFMixin, CoherenceMixin, PikMixin):
|
||||
Returns:
|
||||
users (QuerySet)
|
||||
"""
|
||||
return self.intervention.teams.all()
|
||||
return self.intervention.shared_teams
|
||||
|
||||
def get_documents(self) -> QuerySet:
|
||||
""" Getter for all documents of a compensation
|
||||
@ -513,8 +513,11 @@ class CompensationDocument(AbstractDocument):
|
||||
# The only file left for this compensation 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))
|
||||
|
@ -240,8 +240,11 @@ class EcoAccountDocument(AbstractDocument):
|
||||
# The only file left for this eco account 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))
|
||||
|
@ -5,27 +5,21 @@ Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 21.08.23
|
||||
|
||||
"""
|
||||
from datetime import timedelta
|
||||
|
||||
from dateutil.parser import parse
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.test import RequestFactory
|
||||
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, 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, ADDED_COMPENSATION_STATE, COMPENSATION_STATE_EDITED, \
|
||||
COMPENSATION_ACTION_REMOVED, ADDED_COMPENSATION_STATE, COMPENSATION_STATE_EDITED, \
|
||||
COMPENSATION_STATE_REMOVED
|
||||
from user.models import UserAction
|
||||
|
||||
@ -163,95 +157,6 @@ class RemoveCompensationActionModalFormTestCase(EditCompensationActionModalFormT
|
||||
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())
|
||||
|
||||
|
||||
class NewCompensationStateModalFormTestCase(BaseTestCase):
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
@ -371,6 +276,7 @@ class EditCompensationStateModalFormTestCase(NewCompensationStateModalFormTestCa
|
||||
self.assertEqual(last_log.user, self.superuser)
|
||||
self.assertEqual(last_log.comment, COMPENSATION_STATE_EDITED)
|
||||
|
||||
|
||||
class RemoveCompensationStateModalFormTestCase(EditCompensationStateModalFormTestCase):
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
|
201
compensation/tests/compensation/unit/test_models.py
Normal file
201
compensation/tests/compensation/unit/test_models.py
Normal file
@ -0,0 +1,201 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 30.08.23
|
||||
|
||||
"""
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.test import RequestFactory
|
||||
from django.utils.timezone import now
|
||||
|
||||
from compensation.forms.modals.deadline import NewDeadlineModalForm
|
||||
from compensation.models import CompensationDocument
|
||||
from konova.forms.modals import RemoveDeadlineModalForm
|
||||
from konova.models import DeadlineType
|
||||
from konova.tests.test_views import BaseTestCase
|
||||
from konova.utils.message_templates import DEADLINE_REMOVED, DOCUMENT_REMOVED_TEMPLATE, COMPENSATION_REMOVED_TEMPLATE, \
|
||||
DEADLINE_ADDED
|
||||
from user.models import UserAction, Team
|
||||
|
||||
|
||||
class AbstractCompensationModelTestCase(BaseTestCase):
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.request = RequestFactory().request()
|
||||
self.request.user = self.superuser
|
||||
|
||||
def test_remove_deadline(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)
|
||||
self.assertIn(self.finished_deadline, self.compensation.deadlines.all())
|
||||
form.save()
|
||||
|
||||
last_log = self.compensation.log.first()
|
||||
self.assertEqual(last_log.user, self.request.user)
|
||||
self.assertEqual(last_log.action, UserAction.EDITED)
|
||||
self.assertEqual(last_log.comment, DEADLINE_REMOVED)
|
||||
|
||||
self.assertNotIn(self.finished_deadline, self.compensation.deadlines.all())
|
||||
try:
|
||||
self.finished_deadline.refresh_from_db()
|
||||
self.fail("Deadline should not exist anymore after removing from abstract compensation")
|
||||
except ObjectDoesNotExist:
|
||||
pass
|
||||
|
||||
def test_add_deadline(self):
|
||||
request = RequestFactory().request()
|
||||
request.user = self.superuser
|
||||
|
||||
data = {
|
||||
"type": DeadlineType.MAINTAIN,
|
||||
"date": now().date(),
|
||||
"comment": "TestDeadline"
|
||||
}
|
||||
form = NewDeadlineModalForm(
|
||||
data,
|
||||
request=self.request,
|
||||
instance=self.compensation,
|
||||
)
|
||||
self.assertTrue(form.is_valid(), msg=form.errors)
|
||||
deadline = self.compensation.add_deadline(form)
|
||||
self.assertEqual(deadline.date, data["date"])
|
||||
self.assertEqual(deadline.type, data["type"])
|
||||
self.assertEqual(deadline.comment, data["comment"])
|
||||
self.assertEqual(deadline.created.action, UserAction.CREATED)
|
||||
self.assertEqual(deadline.created.user, self.superuser)
|
||||
self.assertEqual(deadline.created.comment, None)
|
||||
self.assertIn(deadline, self.compensation.deadlines.all())
|
||||
|
||||
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_ADDED)
|
||||
|
||||
|
||||
class CompensationTestCase(BaseTestCase):
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
|
||||
def test_str(self):
|
||||
self.assertEqual(str(self.compensation), self.compensation.identifier)
|
||||
|
||||
def test_save(self):
|
||||
old_identifier = self.compensation.identifier
|
||||
self.compensation.identifier = None
|
||||
self.compensation.save()
|
||||
self.assertIsNotNone(self.compensation.identifier)
|
||||
self.assertNotEqual(old_identifier, self.compensation.identifier)
|
||||
|
||||
def test_share_with_user(self):
|
||||
self.assertNotIn(self.user, self.compensation.shared_users)
|
||||
self.compensation.share_with_user(self.user)
|
||||
self.assertIn(self.user, self.compensation.shared_users)
|
||||
|
||||
def test_share_with_user_list(self):
|
||||
user_list = [
|
||||
self.user
|
||||
]
|
||||
self.assertNotIn(self.user, self.compensation.shared_users)
|
||||
self.compensation.share_with_user_list(user_list)
|
||||
self.assertIn(self.user, self.compensation.shared_users)
|
||||
user_list = [
|
||||
self.superuser
|
||||
]
|
||||
self.assertNotIn(self.superuser, self.compensation.shared_users)
|
||||
self.compensation.share_with_user_list(user_list)
|
||||
self.assertIn(self.superuser, self.compensation.shared_users)
|
||||
self.assertNotIn(self.user, self.compensation.shared_users)
|
||||
|
||||
def test_share_with_team(self):
|
||||
self.assertNotIn(self.team, self.compensation.shared_teams)
|
||||
self.compensation.share_with_team(self.team)
|
||||
self.assertIn(self.team, self.compensation.shared_teams)
|
||||
|
||||
def test_share_with_team_list(self):
|
||||
self.compensation.share_with_team(self.team)
|
||||
self.assertIn(self.team, self.compensation.shared_teams)
|
||||
other_team = Team.objects.create(
|
||||
name="NewTeam"
|
||||
)
|
||||
team_list = [
|
||||
other_team
|
||||
]
|
||||
self.compensation.share_with_team_list(team_list)
|
||||
self.assertIn(other_team, self.compensation.shared_teams)
|
||||
self.assertNotIn(self.team, self.compensation.shared_teams)
|
||||
|
||||
def test_shared_users(self):
|
||||
intervention = self.compensation.intervention
|
||||
diff = self.compensation.shared_users.difference(intervention.shared_users)
|
||||
self.assertEqual(diff.count(), 0)
|
||||
|
||||
self.compensation.share_with_user(self.superuser)
|
||||
diff = self.compensation.shared_users.difference(intervention.shared_users)
|
||||
self.assertEqual(diff.count(), 0)
|
||||
|
||||
def test_shared_teams(self):
|
||||
intervention = self.compensation.intervention
|
||||
diff = self.compensation.shared_users.difference(intervention.shared_users)
|
||||
self.assertEqual(diff.count(), 0)
|
||||
|
||||
self.compensation.share_with_user(self.superuser)
|
||||
diff = self.compensation.shared_users.difference(intervention.shared_users)
|
||||
self.assertEqual(diff.count(), 0)
|
||||
|
||||
def test_get_documents(self):
|
||||
doc = self.create_dummy_document(CompensationDocument, self.compensation)
|
||||
docs = self.compensation.get_documents()
|
||||
self.assertIn(doc, docs)
|
||||
|
||||
def test_mark_as_deleted(self):
|
||||
self.assertIsNone(self.compensation.deleted)
|
||||
self.compensation.mark_as_deleted(self.superuser, send_mail=False)
|
||||
|
||||
comp_deleted = self.compensation.deleted
|
||||
self.assertIsNotNone(comp_deleted)
|
||||
self.assertEqual(comp_deleted.action, UserAction.DELETED)
|
||||
self.assertEqual(comp_deleted.user, self.superuser)
|
||||
self.assertEqual(comp_deleted.comment, None)
|
||||
|
||||
intervention_last_log = self.compensation.intervention.log.first()
|
||||
self.assertEqual(intervention_last_log.action, UserAction.EDITED)
|
||||
self.assertEqual(intervention_last_log.user, self.superuser)
|
||||
self.assertEqual(
|
||||
intervention_last_log.comment,
|
||||
COMPENSATION_REMOVED_TEMPLATE.format(
|
||||
self.compensation.identifier
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class CompensationDocumentTestCase(BaseTestCase):
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.doc = self.create_dummy_document(CompensationDocument, self.compensation)
|
||||
|
||||
def test_delete(self):
|
||||
doc_title = self.doc.title
|
||||
self.assertIn(self.doc, self.compensation.get_documents())
|
||||
self.doc.delete(self.superuser)
|
||||
self.assertNotIn(self.doc, self.compensation.get_documents())
|
||||
try:
|
||||
self.doc.refresh_from_db()
|
||||
self.fail("Document should not be fetchable 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, DOCUMENT_REMOVED_TEMPLATE.format(doc_title))
|
@ -419,8 +419,11 @@ class InterventionDocument(AbstractDocument):
|
||||
# The only file left for this intervention is the one which is currently processed and will be deleted
|
||||
# Make sure that the intervention 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))
|
||||
|
Loading…
Reference in New Issue
Block a user