#19 Tests
* adds workflow tests for compensation checking and recording * improves related code
This commit is contained in:
@@ -122,11 +122,12 @@ class BaseObject(BaseResource):
|
||||
self.save()
|
||||
|
||||
def add_log_entry(self, action: UserAction, user: User, comment: str):
|
||||
""" Wraps adding of UserActionLogEntry to self.log
|
||||
""" Wraps adding of UserActionLogEntry to log
|
||||
|
||||
Args:
|
||||
action (UserAction): The performed UserAction
|
||||
user (User): Performing user
|
||||
comment (str): The optional comment
|
||||
|
||||
Returns:
|
||||
|
||||
@@ -349,6 +350,7 @@ class RecordableObject(models.Model):
|
||||
self.recorded = None
|
||||
self.save()
|
||||
self.log.add(action)
|
||||
return action
|
||||
|
||||
def set_recorded(self, user: User):
|
||||
""" Perform recording
|
||||
@@ -366,8 +368,9 @@ class RecordableObject(models.Model):
|
||||
self.recorded = action
|
||||
self.save()
|
||||
self.log.add(action)
|
||||
return action
|
||||
|
||||
def toggle_recorded(self, user: User):
|
||||
def toggle_recorded(self, user: User) -> UserActionLogEntry:
|
||||
""" Un/Record intervention
|
||||
|
||||
Args:
|
||||
@@ -377,9 +380,10 @@ class RecordableObject(models.Model):
|
||||
|
||||
"""
|
||||
if not self.recorded:
|
||||
self.set_recorded(user)
|
||||
ret_log_entry = self.set_recorded(user)
|
||||
else:
|
||||
self.set_unrecorded(user)
|
||||
ret_log_entry = self.set_unrecorded(user)
|
||||
return ret_log_entry
|
||||
|
||||
|
||||
class CheckableObject(models.Model):
|
||||
@@ -392,10 +396,11 @@ class CheckableObject(models.Model):
|
||||
help_text="Holds data on user and timestamp of this action",
|
||||
related_name="+"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def set_unchecked(self, user: User):
|
||||
def set_unchecked(self) -> None:
|
||||
""" Perform unrecording
|
||||
|
||||
Args:
|
||||
@@ -403,10 +408,13 @@ class CheckableObject(models.Model):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Do not .delete() the checked attribute! Just set it to None, since a delete() would kill it out of the
|
||||
# log history, which is not what we want!
|
||||
self.checked = None
|
||||
self.save()
|
||||
return None
|
||||
|
||||
def set_checked(self, user: User):
|
||||
def set_checked(self, user: User) -> UserActionLogEntry:
|
||||
""" Perform checking
|
||||
|
||||
Args:
|
||||
@@ -422,8 +430,9 @@ class CheckableObject(models.Model):
|
||||
self.checked = action
|
||||
self.save()
|
||||
self.log.add(action)
|
||||
return action
|
||||
|
||||
def toggle_checked(self, user: User):
|
||||
def toggle_checked(self, user: User) -> UserActionLogEntry:
|
||||
""" Un/Record intervention
|
||||
|
||||
Args:
|
||||
@@ -433,9 +442,10 @@ class CheckableObject(models.Model):
|
||||
|
||||
"""
|
||||
if not self.checked:
|
||||
self.set_checked(user)
|
||||
ret_log_entry = self.set_checked(user)
|
||||
else:
|
||||
self.set_unchecked(user)
|
||||
ret_log_entry = self.set_unchecked()
|
||||
return ret_log_entry
|
||||
|
||||
|
||||
class ShareableObject(models.Model):
|
||||
|
||||
@@ -18,6 +18,7 @@ from compensation.models import Compensation, CompensationState, CompensationAct
|
||||
from intervention.models import LegalData, ResponsibilityData, Intervention
|
||||
from konova.management.commands.setup_data import GROUPS_DATA
|
||||
from konova.models import Geometry
|
||||
from konova.settings import DEFAULT_GROUP
|
||||
from user.models import UserActionLogEntry, UserAction
|
||||
|
||||
|
||||
@@ -49,6 +50,8 @@ class BaseTestCase(TestCase):
|
||||
cls.intervention = cls.create_dummy_intervention()
|
||||
cls.compensation = cls.create_dummy_compensation()
|
||||
cls.eco_account = cls.create_dummy_eco_account()
|
||||
cls.create_dummy_states()
|
||||
cls.create_dummy_action()
|
||||
cls.codes = cls.create_dummy_codes()
|
||||
|
||||
@classmethod
|
||||
@@ -204,7 +207,16 @@ class BaseTestCase(TestCase):
|
||||
return codes
|
||||
|
||||
@staticmethod
|
||||
def fill_out_intervention(intervention: Intervention) -> Intervention:
|
||||
def create_dummy_geometry() -> MultiPolygon:
|
||||
""" Creates some geometry
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return MultiPolygon(Polygon.from_bbox([-4.526367, 18.354526, -1.801758, 20.591652]))
|
||||
|
||||
@classmethod
|
||||
def fill_out_intervention(cls, intervention: Intervention) -> Intervention:
|
||||
""" Adds all required (dummy) data to an intervention
|
||||
|
||||
Args:
|
||||
@@ -224,11 +236,28 @@ class BaseTestCase(TestCase):
|
||||
intervention.legal.process_type = KonovaCode.objects.get(id=3)
|
||||
intervention.legal.save()
|
||||
intervention.legal.laws.set([KonovaCode.objects.get(id=(4))])
|
||||
intervention.geometry.geom = MultiPolygon(Polygon.from_bbox([-4.526367, 18.354526, -1.801758, 20.591652]))
|
||||
intervention.geometry.geom = cls.create_dummy_geometry()
|
||||
intervention.geometry.save()
|
||||
intervention.save()
|
||||
return intervention
|
||||
|
||||
@classmethod
|
||||
def fill_out_compensation(cls, compensation: Compensation) -> Compensation:
|
||||
""" Adds all required (dummy) data to a compensation
|
||||
|
||||
Args:
|
||||
compensation (Compensation): The compensation which shall be filled out
|
||||
|
||||
Returns:
|
||||
compensation (Compensation): The modified compensation
|
||||
"""
|
||||
compensation.after_states.add(cls.comp_state)
|
||||
compensation.before_states.add(cls.comp_state)
|
||||
compensation.actions.add(cls.comp_action)
|
||||
compensation.geometry.geom = cls.create_dummy_geometry()
|
||||
compensation.geometry.save()
|
||||
return compensation
|
||||
|
||||
|
||||
class BaseViewTestCase(BaseTestCase):
|
||||
""" Wraps basic test functionality, reusable for every specialized ViewTestCase
|
||||
@@ -236,6 +265,9 @@ class BaseViewTestCase(BaseTestCase):
|
||||
"""
|
||||
login_url = None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls) -> None:
|
||||
super().setUpTestData()
|
||||
@@ -404,6 +436,16 @@ class BaseWorkflowTestCase(BaseTestCase):
|
||||
cls.client_user.login(username=cls.superuser.username, password=cls.superuser_pw)
|
||||
cls.client_anon = Client()
|
||||
|
||||
def setUp(self) -> None:
|
||||
""" Setup data before each test run
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
# Set the default group as only group for the user
|
||||
default_group = self.groups.get(name=DEFAULT_GROUP)
|
||||
self.superuser.groups.set([default_group])
|
||||
|
||||
def assert_object_is_deleted(self, obj):
|
||||
""" Provides a quick check whether an object has been removed from the database or not
|
||||
|
||||
|
||||
Reference in New Issue
Block a user