From a5e816c67bb6ac31f9731a0c353b433066817cde Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Thu, 11 Nov 2021 13:35:08 +0100 Subject: [PATCH] #19 Tests * adds workflow tests for eco account deduction creation --- compensation/tests/test_workflow.py | 61 ++++++++++++++++++++++++++++- konova/tests/test_views.py | 10 ++--- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/compensation/tests/test_workflow.py b/compensation/tests/test_workflow.py index bc57d36d..73c728c5 100644 --- a/compensation/tests/test_workflow.py +++ b/compensation/tests/test_workflow.py @@ -10,7 +10,7 @@ import datetime from django.urls import reverse from compensation.models import Compensation -from konova.settings import DEFAULT_GROUP, ETS_GROUP, ZB_GROUP +from konova.settings import ETS_GROUP, ZB_GROUP from konova.tests.test_views import BaseWorkflowTestCase from user.models import UserAction @@ -30,6 +30,7 @@ class CompensationWorkflowTestCase(BaseWorkflowTestCase): cls.intervention.compensations.set([cls.compensation]) def setUp(self) -> None: + super().setUp() # Delete all existing compensations, which might be created by tests Compensation.objects.all().delete() @@ -133,3 +134,61 @@ class CompensationWorkflowTestCase(BaseWorkflowTestCase): # Expect the user action to be in the log self.assertIn(recorded, self.compensation.log.all()) + +class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): + @classmethod + def setUpTestData(cls): + super().setUpTestData() + + # 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.users.set([cls.superuser]) + + def test_deductability(self): + """ + This tests the deductability of an eco account. + + An eco account should only be deductible if it is recorded. + + Returns: + + """ + # Give user shared access to the dummy intervention, which will be needed here + self.intervention.users.add(self.superuser) + + # Prepare data for deduction creation + deduct_url = reverse("compensation:acc-new-deduction", args=(self.eco_account.id,)) + test_surface = 10.00 + post_data = { + "surface": test_surface, + "account": self.id, + "intervention": self.intervention.id, + } + # Perform request --> expect to fail + self.client_user.post(deduct_url, post_data) + + # Expect that no deduction has been created + self.assertEqual(0, self.eco_account.deductions.count()) + self.assertEqual(0, self.intervention.deductions.count()) + + # Now mock the eco account as it would be recorded (with invalid data) + # Make sure the deductible surface is high enough for the request + self.eco_account.toggle_recorded(self.superuser) + self.eco_account.refresh_from_db() + self.eco_account.deductable_surface = test_surface + 1.00 + self.eco_account.save() + self.assertIsNotNone(self.eco_account.recorded) + self.assertGreater(self.eco_account.deductable_surface, test_surface) + + # Rerun the request + self.client_user.post(deduct_url, post_data) + + # Expect that the deduction has been created + self.assertEqual(1, self.eco_account.deductions.count()) + self.assertEqual(1, self.intervention.deductions.count()) + deduction = self.eco_account.deductions.first() + self.assertEqual(deduction.surface, test_surface) + self.assertEqual(deduction.account, self.eco_account) + self.assertEqual(deduction.intervention, self.intervention) + + diff --git a/konova/tests/test_views.py b/konova/tests/test_views.py index a05b5bcc..c79c730f 100644 --- a/konova/tests/test_views.py +++ b/konova/tests/test_views.py @@ -431,11 +431,6 @@ class BaseWorkflowTestCase(BaseTestCase): def setUpTestData(cls): super().setUpTestData() - # Create logged in client and a non-logged in client (anon) - cls.client_user = Client() - 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 @@ -446,6 +441,11 @@ class BaseWorkflowTestCase(BaseTestCase): default_group = self.groups.get(name=DEFAULT_GROUP) self.superuser.groups.set([default_group]) + # Create fresh logged in client and a non-logged in client (anon) for each test + self.client_user = Client() + self.client_user.login(username=self.superuser.username, password=self.superuser_pw) + self.client_anon = Client() + def assert_object_is_deleted(self, obj): """ Provides a quick check whether an object has been removed from the database or not