#31 API Deductions Tests

* adds tests for deduction API
This commit is contained in:
mpeltriaux 2022-01-28 16:21:23 +01:00
parent c32911755a
commit 1b3adc396f
8 changed files with 124 additions and 10 deletions

View File

@ -0,0 +1,5 @@
{
"eco_account": "CHANGE_BEFORE_RUN!!!",
"surface": 500.0,
"intervention": "CHANGE_BEFORE_RUN!!!"
}

View File

@ -45,7 +45,7 @@ class APIV1CreateTestCase(BaseAPIV1TestCase):
self.assertIsNotNone(content.get("id", None), msg=response.content)
def test_create_intervention(self):
""" Tests api creation of bare minimum interventions
""" Tests api creation
Returns:
@ -57,7 +57,7 @@ class APIV1CreateTestCase(BaseAPIV1TestCase):
self._test_create_object(url, post_body)
def test_create_compensation(self):
""" Tests api creation of bare minimum interventions
""" Tests api creation
Returns:
@ -80,7 +80,7 @@ class APIV1CreateTestCase(BaseAPIV1TestCase):
self._test_create_object(url, post_body)
def test_create_eco_account(self):
""" Tests api creation of bare minimum interventions
""" Tests api creation
Returns:
@ -92,7 +92,7 @@ class APIV1CreateTestCase(BaseAPIV1TestCase):
self._test_create_object(url, post_body)
def test_create_ema(self):
""" Tests api creation of bare minimum interventions
""" Tests api creation
Returns:
@ -103,3 +103,20 @@ class APIV1CreateTestCase(BaseAPIV1TestCase):
post_body = json.load(fp=json_file)
self._test_create_object(url, post_body)
def test_create_deduction(self):
""" Tests api creation
Returns:
"""
self.intervention.share_with(self.superuser)
self.eco_account.share_with(self.superuser)
url = reverse("api:v1:deduction")
json_file_path = "api/tests/v1/create/deduction_create_post_body.json"
with open(json_file_path) as json_file:
post_body = json.load(fp=json_file)
post_body["intervention"] = str(self.intervention.id)
post_body["eco_account"] = str(self.eco_account.id)
self._test_create_object(url, post_body)

View File

@ -8,6 +8,7 @@ Created on: 28.01.22
import json
from django.core.exceptions import ObjectDoesNotExist
from django.urls import reverse
from api.tests.v1.share.test_api_sharing import BaseAPIV1TestCase
@ -93,3 +94,25 @@ class APIV1DeleteTestCase(BaseAPIV1TestCase):
url = reverse("api:v1:ema", args=(str(test_ema.id),))
self._test_delete_object(test_ema, url)
def test_delete_deduction(self):
""" Tests api creation of bare minimum interventions
Returns:
"""
test_deduction = self.create_dummy_deduction()
test_deduction.intervention.share_with(self.superuser)
url = reverse("api:v1:deduction", args=(str(test_deduction.id),))
response = self._run_delete_request(url)
content = json.loads(response.content)
self.assertEqual(response.status_code, 200, msg=response.content)
self.assertTrue(content.get("success", False), msg=response.content)
try:
test_deduction.refresh_from_db()
self.fail("Deduction is not deleted from db!")
except ObjectDoesNotExist:
pass

View File

@ -38,6 +38,9 @@ class APIV1GetTestCase(BaseAPIV1TestCase):
content = json.loads(response.content)
geojson = content[str(obj.id)]
self.assertEqual(response.status_code, 200, msg=response.content)
return geojson
def _assert_geojson_format(self, geojson):
try:
geojson["type"]
geojson["coordinates"]
@ -49,7 +52,6 @@ class APIV1GetTestCase(BaseAPIV1TestCase):
props["modified_on"]
except KeyError as e:
self.fail(e)
return geojson
def test_get_intervention(self):
""" Tests api GET
@ -60,6 +62,7 @@ class APIV1GetTestCase(BaseAPIV1TestCase):
self.intervention.share_with(self.superuser)
url = reverse("api:v1:intervention", args=(str(self.intervention.id),))
geojson = self._test_get_object(self.intervention, url)
self._assert_geojson_format(geojson)
try:
props = geojson["properties"]
props["responsible"]
@ -89,6 +92,7 @@ class APIV1GetTestCase(BaseAPIV1TestCase):
url = reverse("api:v1:compensation", args=(str(self.compensation.id),))
geojson = self._test_get_object(self.compensation, url)
self._assert_geojson_format(geojson)
try:
props = geojson["properties"]
props["is_cef"]
@ -114,6 +118,7 @@ class APIV1GetTestCase(BaseAPIV1TestCase):
url = reverse("api:v1:ecoaccount", args=(str(self.eco_account.id),))
geojson = self._test_get_object(self.eco_account, url)
self._assert_geojson_format(geojson)
try:
props = geojson["properties"]
props["deductable_surface"]
@ -142,6 +147,7 @@ class APIV1GetTestCase(BaseAPIV1TestCase):
url = reverse("api:v1:ema", args=(str(self.ema.id),))
geojson = self._test_get_object(self.ema, url)
self._assert_geojson_format(geojson)
try:
props = geojson["properties"]
props["responsible"]
@ -155,3 +161,27 @@ class APIV1GetTestCase(BaseAPIV1TestCase):
except KeyError as e:
self.fail(e)
def test_get_deduction(self):
""" Tests api GET
Returns:
"""
self.deduction.intervention.share_with(self.superuser)
url = reverse("api:v1:deduction", args=(str(self.deduction.id),))
_json = self._test_get_object(self.deduction, url)
try:
_json["id"]
_json["eco_account"]
_json["eco_account"]["id"]
_json["eco_account"]["identifier"]
_json["eco_account"]["title"]
_json["surface"]
_json["intervention"]
_json["intervention"]["id"]
_json["intervention"]["identifier"]
_json["intervention"]["title"]
except KeyError as e:
self.fail(e)

View File

@ -0,0 +1,5 @@
{
"eco_account": "CHANGE_BEFORE_RUN!!!",
"surface": 523400.0,
"intervention": "CHANGE_BEFORE_RUN!!!"
}

View File

@ -161,3 +161,26 @@ class APIV1UpdateTestCase(BaseAPIV1TestCase):
self.assertEqual(len(put_props["before_states"]), self.ema.before_states.count())
self.assertEqual(len(put_props["after_states"]), self.ema.after_states.count())
self.assertEqual(len(put_props["deadlines"]), self.ema.deadlines.count())
def test_update_deduction(self):
""" Tests api update
Returns:
"""
self.deduction.intervention.share_with(self.superuser)
self.deduction.account.share_with(self.superuser)
url = reverse("api:v1:deduction", args=(str(self.deduction.id),))
json_file_path = "api/tests/v1/update/deduction_update_put_body.json"
with open(json_file_path) as json_file:
put_body = json.load(fp=json_file)
put_body["intervention"] = str(self.deduction.intervention.id)
put_body["eco_account"] = str(self.deduction.account.id)
self._test_update_object(url, put_body)
self.deduction.refresh_from_db()
self.assertEqual(put_body["intervention"], str(self.deduction.intervention.id))
self.assertEqual(put_body["eco_account"], str(self.deduction.account.id))
self.assertEqual(put_body["surface"], self.deduction.surface)

View File

@ -360,19 +360,21 @@ class InterventionWorkflowTestCase(BaseWorkflowTestCase):
self.eco_account.recorded = rec_action
self.eco_account.share_with_list([self.superuser])
self.eco_account.save()
num_all_deducs = EcoAccountDeduction.objects.count()
# Run the request
self.client_user.post(new_url, post_data)
# Expect the deduction to be created, since all constraints are fulfilled
self.assertEqual(1, self.eco_account.deductions.count())
self.assertEqual(1, EcoAccountDeduction.objects.count())
self.assertEqual(num_all_deducs + 1, EcoAccountDeduction.objects.count())
# Make sure the deduction contains the expected data
deduction = EcoAccountDeduction.objects.first()
deduction = EcoAccountDeduction.objects.get(
account=self.eco_account,
intervention=self.intervention
)
self.assertEqual(deduction.surface, test_surface)
self.assertEqual(deduction.intervention, self.intervention)
self.assertEqual(deduction.account, self.eco_account)
# Return deduction for further usage in tests
return deduction

View File

@ -16,7 +16,7 @@ from django.test import TestCase, Client
from django.urls import reverse
from codelist.models import KonovaCode
from compensation.models import Compensation, CompensationState, CompensationAction, EcoAccount
from compensation.models import Compensation, CompensationState, CompensationAction, EcoAccount, EcoAccountDeduction
from intervention.models import Legal, Responsibility, Intervention
from konova.management.commands.setup_data import GROUPS_DATA
from konova.models import Geometry
@ -54,6 +54,7 @@ class BaseTestCase(TestCase):
cls.compensation = cls.create_dummy_compensation()
cls.eco_account = cls.create_dummy_eco_account()
cls.ema = cls.create_dummy_ema()
cls.deduction = cls.create_dummy_deduction()
cls.create_dummy_states()
cls.create_dummy_action()
cls.codes = cls.create_dummy_codes()
@ -194,6 +195,14 @@ class BaseTestCase(TestCase):
)
return ema
@classmethod
def create_dummy_deduction(cls):
return EcoAccountDeduction.objects.create(
account=cls.create_dummy_eco_account(),
intervention=cls.create_dummy_intervention(),
surface=100,
)
@classmethod
def create_dummy_states(cls):
""" Creates an intervention which can be used for tests