#19 Tests
* adds workflow tests for compensation checking and recording * improves related code
This commit is contained in:
135
compensation/tests/test_workflow.py
Normal file
135
compensation/tests/test_workflow.py
Normal file
@@ -0,0 +1,135 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 11.11.21
|
||||
|
||||
"""
|
||||
import datetime
|
||||
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.models import Compensation
|
||||
from konova.settings import DEFAULT_GROUP, ETS_GROUP, ZB_GROUP
|
||||
from konova.tests.test_views import BaseWorkflowTestCase
|
||||
from user.models import UserAction
|
||||
|
||||
|
||||
class CompensationWorkflowTestCase(BaseWorkflowTestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
super().setUpTestData()
|
||||
|
||||
# Give the user shared access to the dummy intervention -> inherits the access to the compensation
|
||||
cls.intervention.users.add(cls.superuser)
|
||||
|
||||
# Make sure the intervention itself would be fine with valid data
|
||||
cls.intervention = cls.fill_out_intervention(cls.intervention)
|
||||
|
||||
# Make sure the compensation is linked to the intervention
|
||||
cls.intervention.compensations.set([cls.compensation])
|
||||
|
||||
def setUp(self) -> None:
|
||||
# Delete all existing compensations, which might be created by tests
|
||||
Compensation.objects.all().delete()
|
||||
|
||||
# Create a fresh dummy (non-valid) compensation before each test
|
||||
self.compensation = self.create_dummy_compensation()
|
||||
|
||||
def test_checkability(self):
|
||||
"""
|
||||
This tests if the checkability of the compensation (which is defined by the linked intervention's checked
|
||||
attribute) is triggered by the quality of it's data (e.g. not all fields filled)
|
||||
|
||||
We expect a compensation, missing required data, linked to an intervention to fail the intervention's quality
|
||||
check performed in the checking action.
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Add proper privilege for the user
|
||||
self.superuser.groups.add(self.groups.get(name=ZB_GROUP))
|
||||
|
||||
# Prepare url and form data
|
||||
url = reverse("intervention:check", args=(self.intervention.id,))
|
||||
post_data = {
|
||||
"checked_intervention": True,
|
||||
"checked_comps": True,
|
||||
}
|
||||
|
||||
# Make sure the intervention is not checked
|
||||
self.assertIsNone(self.intervention.checked)
|
||||
|
||||
# Run the request --> expect fail, since the compensation is not valid, yet
|
||||
self.client_user.post(url, post_data)
|
||||
|
||||
# Check that the intervention is still not recorded
|
||||
self.assertIsNone(self.intervention.checked)
|
||||
|
||||
# Now fill out the data for a compensation
|
||||
self.compensation = self.fill_out_compensation(self.compensation)
|
||||
|
||||
# Rerun the request
|
||||
self.client_user.post(url, post_data)
|
||||
|
||||
# Expect the linked intervention now to be checked
|
||||
# Attention: We can only test the date part of the timestamp,
|
||||
# since the delay in microseconds would lead to fail
|
||||
self.intervention.refresh_from_db()
|
||||
checked = self.intervention.checked
|
||||
self.assertIsNotNone(checked)
|
||||
self.assertEqual(self.superuser, checked.user)
|
||||
self.assertEqual(UserAction.CHECKED, checked.action)
|
||||
self.assertEqual(datetime.date.today(), checked.timestamp.date())
|
||||
|
||||
# Expect the user action to be in the log
|
||||
self.assertIn(checked, self.compensation.log.all())
|
||||
|
||||
def test_recordability(self):
|
||||
"""
|
||||
This tests if the recordability of the compensation (which is defined by the linked intervention's recorded
|
||||
attribute) is triggered by the quality of it's data (e.g. not all fields filled)
|
||||
|
||||
We expect a compensation, missing required data, linked to an intervention to fail the intervention's quality
|
||||
check performed in the recording action.
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Add proper privilege for the user
|
||||
self.superuser.groups.add(self.groups.get(name=ETS_GROUP))
|
||||
|
||||
# Prepare url and form data
|
||||
record_url = reverse("intervention:record", args=(self.intervention.id,))
|
||||
post_data = {
|
||||
"confirm": True,
|
||||
}
|
||||
|
||||
# Make sure the intervention is not recorded
|
||||
self.assertIsNone(self.intervention.recorded)
|
||||
|
||||
# Run the request --> expect fail, since the compensation is not valid, yet
|
||||
self.client_user.post(record_url, post_data)
|
||||
|
||||
# Check that the intervention is still not recorded
|
||||
self.assertIsNone(self.intervention.recorded)
|
||||
|
||||
# Now fill out the data for a compensation
|
||||
self.compensation = self.fill_out_compensation(self.compensation)
|
||||
|
||||
# Rerun the request
|
||||
self.client_user.post(record_url, post_data)
|
||||
|
||||
# Expect the linked intervention now to be recorded
|
||||
# Attention: We can only test the date part of the timestamp,
|
||||
# since the delay in microseconds would lead to fail
|
||||
self.intervention.refresh_from_db()
|
||||
recorded = self.intervention.recorded
|
||||
self.assertIsNotNone(recorded)
|
||||
self.assertEqual(self.superuser, recorded.user)
|
||||
self.assertEqual(UserAction.RECORDED, recorded.action)
|
||||
self.assertEqual(datetime.date.today(), recorded.timestamp.date())
|
||||
|
||||
# Expect the user action to be in the log
|
||||
self.assertIn(recorded, self.compensation.log.all())
|
||||
|
||||
Reference in New Issue
Block a user