diff --git a/compensation/forms/eco_account.py b/compensation/forms/eco_account.py index f37098f9..360dd343 100644 --- a/compensation/forms/eco_account.py +++ b/compensation/forms/eco_account.py @@ -128,6 +128,8 @@ class NewEcoAccountForm(AbstractCompensationForm, CompensationResponsibleFormMix # Add the log entry to the main objects log list acc.log.add(action) + + acc.update_deductable_rest() return acc @@ -210,6 +212,8 @@ class EditEcoAccountForm(NewEcoAccountForm): # Add the log entry to the main objects log list self.instance.log.add(action) + + self.instance.update_deductable_rest() return self.instance diff --git a/compensation/tests/ecoaccount/test_workflow.py b/compensation/tests/ecoaccount/test_workflow.py index c7efb05a..ef65edaa 100644 --- a/compensation/tests/ecoaccount/test_workflow.py +++ b/compensation/tests/ecoaccount/test_workflow.py @@ -47,7 +47,7 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): "identifier": test_id, "title": test_title, "geom": geom_json, - "deductable_surface": test_deductable_surface, + "surface": test_deductable_surface, "conservation_office": test_conservation_office.id } self.client_user.post(new_url, post_data) @@ -61,6 +61,8 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): self.assertEqual(acc.identifier, test_id) self.assertEqual(acc.title, test_title) + self.assertEqual(acc.deductable_surface, test_deductable_surface) + self.assertEqual(acc.deductable_rest, test_deductable_surface) self.assert_equal_geometries(acc.geometry.geom, test_geom) self.assertEqual(acc.log.count(), 1) @@ -84,7 +86,7 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): 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 + test_deductable_surface = self.eco_account.deductable_surface + 100 check_on_elements = { self.eco_account.title: new_title, @@ -110,6 +112,7 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): self.eco_account.title: new_title, self.eco_account.identifier: new_identifier, self.eco_account.deductable_surface: test_deductable_surface, + self.eco_account.deductable_rest: test_deductable_surface, self.eco_account.comment: new_comment, } @@ -194,14 +197,14 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): # Perform request --> expect to fail self.client_user.post(deduct_url, post_data) - # Expect that no deduction has been created + # Expect that no deduction has been created since the eco account is not recorded, yet self.assertEqual(0, self.eco_account.deductions.count()) self.assertEqual(0, self.intervention.deductions.count()) self.assertEqual(pre_deduction_acc_log_count, 0) self.assertEqual(pre_deduction_int_log_count, 0) # Now mock the eco account as it would be recorded (with invalid data) - # Make sure the deductible surface is high enough for the request + # Make sure the deductible surface is valid for the request self.eco_account.set_recorded(self.superuser) self.eco_account.refresh_from_db() self.eco_account.deductable_surface = test_surface + 1.00 @@ -216,10 +219,12 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): self.client_user.post(deduct_url, post_data) # Expect that the deduction has been created + self.eco_account.refresh_from_db() 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(self.eco_account.deductable_rest, self.eco_account.deductable_surface - deduction.surface) self.assertEqual(deduction.account, self.eco_account) self.assertEqual(deduction.intervention, self.intervention) @@ -262,6 +267,7 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): self.eco_account.refresh_from_db() deduction.refresh_from_db() + self.assertEqual(self.eco_account.deductable_rest, self.eco_account.deductable_surface - deduction.surface) self.assertEqual(num_deductions_intervention, self.intervention.deductions.count()) self.assertEqual(num_deductions_account, self.eco_account.deductions.count()) self.assertEqual(deduction.surface, test_surface) @@ -275,6 +281,7 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): def test_remove_deduction(self): intervention = self.deduction.intervention account = self.deduction.account + deducted_surface = self.deduction.surface # Prepare url and form data to be posted new_url = reverse("compensation:acc:remove-deduction", args=(account.id, self.deduction.id)) @@ -287,6 +294,7 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): pre_edit_intervention_log_count = intervention.log.count() pre_edit_account_log_count = account.log.count() + pre_edit_account_rest = account.deductable_rest num_deductions_intervention = intervention.deductions.count() num_deductions_account = account.deductions.count() @@ -297,6 +305,7 @@ class EcoAccountWorkflowTestCase(BaseWorkflowTestCase): self.assertEqual(num_deductions_intervention - 1, intervention.deductions.count()) self.assertEqual(num_deductions_account - 1, account.deductions.count()) + self.assertEqual(account.deductable_rest, pre_edit_account_rest + deducted_surface) # Expect logs to be set self.assertEqual(pre_edit_intervention_log_count + 1, intervention.log.count()) diff --git a/konova/tests/test_views.py b/konova/tests/test_views.py index 32ab5986..84a4238b 100644 --- a/konova/tests/test_views.py +++ b/konova/tests/test_views.py @@ -187,6 +187,7 @@ class BaseTestCase(TestCase): eco_account = EcoAccount.objects.create( identifier="TEST", title="Test_title", + deductable_surface=500, legal=lega_data, responsible=responsible_data, created=action,