From 5eebd42c3c9d0ec879fa0d82b4611c6c11d37626 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Tue, 8 Feb 2022 11:58:43 +0100 Subject: [PATCH] Further tests ecoaccount * adds ecoaccount workflow tests --- .../tests/ecoaccount/test_workflow.py | 153 +++++++++++++++++- konova/tests/test_views.py | 22 +++ 2 files changed, 172 insertions(+), 3 deletions(-) diff --git a/compensation/tests/ecoaccount/test_workflow.py b/compensation/tests/ecoaccount/test_workflow.py index 92079207..f394ec7c 100644 --- a/compensation/tests/ecoaccount/test_workflow.py +++ b/compensation/tests/ecoaccount/test_workflow.py @@ -5,9 +5,14 @@ Contact: michel.peltriaux@sgdnord.rlp.de Created on: 11.11.21 """ +import datetime + +from django.contrib.gis.geos import MultiPolygon +from django.core.exceptions import ObjectDoesNotExist from django.urls import reverse -from konova.settings import ETS_GROUP +from compensation.models import EcoAccount +from konova.settings import ETS_GROUP, DEFAULT_GROUP from konova.tests.test_views import BaseWorkflowTestCase from user.models import UserAction @@ -17,9 +22,151 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): def setUpTestData(cls): super().setUpTestData() + def setUp(self) -> None: + super().setUp() # Add user to conservation office group and give shared access to the account - cls.superuser.groups.add(cls.groups.get(name=ETS_GROUP)) - cls.eco_account.share_with_list([cls.superuser]) + self.superuser.groups.add(self.groups.get(name=DEFAULT_GROUP)) + self.superuser.groups.add(self.groups.get(name=ETS_GROUP)) + self.eco_account.share_with_list([self.superuser]) + + def test_new(self): + """ Test the creation of an EcoAccount + + Returns: + + """ + # Prepare url and form data to be posted + new_url = reverse("compensation:acc:new") + test_id = self.create_dummy_string() + test_title = self.create_dummy_string() + test_geom = self.create_dummy_geometry() + test_deductable_surface = 1000 + test_conservation_office = self.get_conservation_office_code() + post_data = { + "identifier": test_id, + "title": test_title, + "geom": test_geom.geojson, + "deductable_surface": test_deductable_surface, + "conservation_office": test_conservation_office.id + } + self.client_user.post(new_url, post_data) + + try: + acc = EcoAccount.objects.get( + identifier=test_id + ) + except ObjectDoesNotExist: + self.fail(msg="EcoAccount not created") + + self.assertEqual(acc.identifier, test_id) + self.assertEqual(acc.title, test_title) + self.assert_equal_geometries(acc.geometry.geom, test_geom) + self.assertEqual(acc.log.count(), 1) + + # Expect logs to be set + self.assertEqual(acc.log.count(), 1) + self.assertEqual(acc.log.first().action, UserAction.CREATED) + + def test_edit(self): + """ Checks that the editing of an EcoAccount works + + Returns: + + """ + self.eco_account.share_with(self.superuser) + + url = reverse("compensation:acc:edit", args=(self.eco_account.id,)) + pre_edit_log_count = self.eco_account.log.count() + + new_title = self.create_dummy_string() + new_identifier = self.create_dummy_string() + new_comment = self.create_dummy_string() + new_geometry = MultiPolygon(srid=4326) # Create an empty geometry + test_conservation_office = self.get_conservation_office_code() + test_deductable_surface = 10005 + + check_on_elements = { + self.eco_account.title: new_title, + self.eco_account.identifier: new_identifier, + self.eco_account.comment: new_comment, + self.eco_account.deductable_surface: test_deductable_surface, + } + for k, v in check_on_elements.items(): + self.assertNotEqual(k, v) + + post_data = { + "identifier": new_identifier, + "title": new_title, + "comment": new_comment, + "geom": new_geometry.geojson, + "surface": test_deductable_surface, + "conservation_office": test_conservation_office.id + } + self.client_user.post(url, post_data) + self.eco_account.refresh_from_db() + + check_on_elements = { + self.eco_account.title: new_title, + self.eco_account.identifier: new_identifier, + self.eco_account.deductable_surface: test_deductable_surface, + self.eco_account.comment: new_comment, + } + + for k, v in check_on_elements.items(): + self.assertEqual(k, v) + + self.assert_equal_geometries(self.eco_account.geometry.geom, new_geometry) + + # Expect logs to be set + self.assertEqual(pre_edit_log_count + 1, self.eco_account.log.count()) + self.assertEqual(self.eco_account.log.first().action, UserAction.EDITED) + + def test_recordability(self): + """ + This tests if the recordability of the EcoAccount is triggered by the quality of it's data (e.g. not all fields filled) + + Returns: + + """ + # Add proper privilege for the user + self.eco_account.share_with(self.superuser) + pre_record_log_count = self.eco_account.log.count() + + # Prepare url and form data + record_url = reverse("compensation:acc:record", args=(self.eco_account.id,)) + post_data = { + "confirm": True, + } + self.eco_account.refresh_from_db() + + # Make sure the account is not recorded + self.assertIsNone(self.eco_account.recorded) + + # Run the request --> expect fail, since the account is not valid, yet + self.client_user.post(record_url, post_data) + + # Check that the account is still not recorded + self.assertIsNone(self.eco_account.recorded) + + # Now fill out the data for an ecoaccount + self.eco_account = self.fill_out_eco_account(self.eco_account) + + # Rerun the request + self.client_user.post(record_url, post_data) + + # Expect the EcoAccount 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.eco_account.refresh_from_db() + recorded = self.eco_account.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.eco_account.log.all()) + self.assertEqual(pre_record_log_count + 1, self.eco_account.log.count()) def test_deductability(self): """ diff --git a/konova/tests/test_views.py b/konova/tests/test_views.py index a10acf16..536a7868 100644 --- a/konova/tests/test_views.py +++ b/konova/tests/test_views.py @@ -330,6 +330,27 @@ class BaseTestCase(TestCase): ema.geometry.save() return ema + @classmethod + def fill_out_eco_account(cls, eco_account): + """ Adds all required (dummy) data to an EcoAccount + + Returns: + """ + eco_account.legal.registration_date = "2022-01-01" + eco_account.legal.save() + eco_account.responsible.conservation_office = cls.get_conservation_office_code() + eco_account.responsible.conservation_file_number = "test" + eco_account.responsible.handler = "handler" + eco_account.responsible.save() + eco_account.after_states.add(cls.comp_state) + eco_account.before_states.add(cls.comp_state) + eco_account.actions.add(cls.comp_action) + eco_account.geometry.geom = cls.create_dummy_geometry() + eco_account.geometry.save() + eco_account.deductable_surface = eco_account.get_state_after_surface_sum() + eco_account.save() + return eco_account + def assert_equal_geometries(self, geom1: MultiPolygon, geom2: MultiPolygon): """ Assert for geometries to be equal @@ -534,6 +555,7 @@ class BaseWorkflowTestCase(BaseTestCase): Returns: """ + super().setUp() # Set the default group as only group for the user default_group = self.groups.get(name=DEFAULT_GROUP) self.superuser.groups.set([default_group])