diff --git a/compensation/tests/test_workflow.py b/compensation/tests/test_workflow.py index d8e83147..3c34bf49 100644 --- a/compensation/tests/test_workflow.py +++ b/compensation/tests/test_workflow.py @@ -37,6 +37,63 @@ class CompensationWorkflowTestCase(BaseWorkflowTestCase): # Create a fresh dummy (non-valid) compensation before each test self.compensation = self.create_dummy_compensation() + def test_new(self): + """ Test the creation of a compensation + + Returns: + + """ + # Prepare url and form data to be posted + new_url = reverse("compensation:new") + test_id = self.create_dummy_string() + test_title = self.create_dummy_string() + test_geom = self.create_dummy_geometry() + post_data = { + "identifier": test_id, + "title": test_title, + "geom": test_geom.geojson, + "intervention": self.intervention.id, + } + + # Preserve the current number of intervention's compensations + num_compensations = self.intervention.compensations.count() + self.client_user.post(new_url, post_data) + + self.intervention.refresh_from_db() + self.assertEqual(num_compensations + 1, self.intervention.compensations.count()) + new_compensation = self.intervention.compensations.get(identifier=test_id) + self.assertEqual(new_compensation.identifier, test_id) + self.assertEqual(new_compensation.title, test_title) + self.assert_equal_geometries(new_compensation.geometry.geom, test_geom) + + def test_new_from_intervention(self): + """ Test the creation of a compensation from a given intervention + + Returns: + + """ + # Prepare url and form data to be posted + new_url = reverse("compensation:new", args=(self.intervention.id,)) + test_id = self.create_dummy_string() + test_title = self.create_dummy_string() + test_geom = self.create_dummy_geometry() + post_data = { + "identifier": test_id, + "title": test_title, + "geom": test_geom.geojson, + } + + # Preserve the current number of intervention's compensations + num_compensations = self.intervention.compensations.count() + self.client_user.post(new_url, post_data) + + self.intervention.refresh_from_db() + self.assertEqual(num_compensations + 1, self.intervention.compensations.count()) + new_compensation = self.intervention.compensations.get(identifier=test_id) + self.assertEqual(new_compensation.identifier, test_id) + self.assertEqual(new_compensation.title, test_title) + self.assert_equal_geometries(new_compensation.geometry.geom, test_geom) + def test_checkability(self): """ This tests if the checkability of the compensation (which is defined by the linked intervention's checked diff --git a/intervention/forms/forms.py b/intervention/forms/forms.py index d1c40435..f7453ccd 100644 --- a/intervention/forms/forms.py +++ b/intervention/forms/forms.py @@ -356,7 +356,7 @@ class EditInterventionForm(NewInterventionForm): self.instance.save() # Uncheck and unrecord intervention due to changed data - self.instance.set_unchecked(user) + self.instance.set_unchecked() self.instance.set_unrecorded(user) return self.instance diff --git a/intervention/tests/test_workflow.py b/intervention/tests/test_workflow.py index d0f3a83b..f6f56e34 100644 --- a/intervention/tests/test_workflow.py +++ b/intervention/tests/test_workflow.py @@ -39,7 +39,9 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase): """ # Define the intervention identifier for easier handling on the next lines - test_id = "Test_IDENTIFIER" + test_id = self.create_dummy_string() + test_title = self.create_dummy_string() + test_geom = self.create_dummy_geometry() new_url = reverse("intervention:new", args=()) @@ -52,8 +54,8 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase): # User creates a new intervention with bare minimum content, using the proper url and post data post_data = { "identifier": test_id, - "title": "Test_TITLE", - "geometry": "", + "title": test_title, + "geom": test_geom.geojson, } response = self.client_user.post( new_url, @@ -66,6 +68,8 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase): identifier=test_id ) self.assertEqual(obj.identifier, test_id) + self.assertEqual(obj.title, test_title) + self.assert_equal_geometries(obj.geometry.geom, test_geom) except ObjectDoesNotExist: # Fail if there is no such object self.fail() @@ -210,7 +214,7 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase): # Create form data to be sent to the url test_amount = 10.00 test_due = "2021-01-01" - test_comment = "test_comment" + test_comment = self.create_dummy_string() post_data = { "amount": test_amount, "due": test_due, diff --git a/konova/tests/test_views.py b/konova/tests/test_views.py index c79c730f..e7f466e7 100644 --- a/konova/tests/test_views.py +++ b/konova/tests/test_views.py @@ -19,6 +19,7 @@ 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 konova.utils.generators import generate_random_string from user.models import UserActionLogEntry, UserAction @@ -79,6 +80,15 @@ class BaseTestCase(TestCase): ) cls.groups = Group.objects.all() + @staticmethod + def create_dummy_string(prefix: str = ""): + """ Create + + Returns: + + """ + return f"{prefix}{generate_random_string(3, True)}" + @classmethod def create_dummy_intervention(cls): """ Creates an intervention which can be used for tests @@ -213,7 +223,10 @@ class BaseTestCase(TestCase): Returns: """ - return MultiPolygon(Polygon.from_bbox([-4.526367, 18.354526, -1.801758, 20.591652])) + polygon = Polygon.from_bbox((7.157593, 49.882247, 7.816772, 50.266521)) + polygon.srid = 4326 + polygon = polygon.transform(3857, clone=True) + return MultiPolygon(polygon, srid=3857) # 3857 is the default srid used for MultiPolygonField in the form @classmethod def fill_out_intervention(cls, intervention: Intervention) -> Intervention: @@ -258,6 +271,27 @@ class BaseTestCase(TestCase): compensation.geometry.save() return compensation + def assert_equal_geometries(self, geom1: MultiPolygon, geom2: MultiPolygon): + """ Assert for geometries to be equal + + Transforms the geometries to matching srids before checking + + Args: + geom1 (MultiPolygon): A geometry + geom2 (MultiPolygon): A geometry + + Returns: + + """ + if geom1.srid != geom2.srid: + # Due to prior possible transformation of any of these geometries, we need to make sure there exists a + # transformation from one coordinate system into the other, which is valid + geom1_t = geom1.transform(geom2.srid, clone=True) + geom2_t = geom2.transform(geom1.srid, clone=True) + self.assertTrue(geom1_t.equals(geom2) or geom2_t.equals(geom1)) + else: + self.assertTrue(geom1.equals(geom2)) + class BaseViewTestCase(BaseTestCase): """ Wraps basic test functionality, reusable for every specialized ViewTestCase