From 24b8c125056cd33e39326d2ef88d61bb38557af8 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 28 Jan 2022 12:30:09 +0100 Subject: [PATCH] #31 API GET Tests * adds tests for api GET method * fixes bug where non existing geometry could not be serialized properly --- api/tests/v1/get/__init__.py | 7 ++ api/tests/v1/get/test_api_get.py | 157 ++++++++++++++++++++++++++ api/utils/serializer/v1/serializer.py | 6 +- 3 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 api/tests/v1/get/__init__.py create mode 100644 api/tests/v1/get/test_api_get.py diff --git a/api/tests/v1/get/__init__.py b/api/tests/v1/get/__init__.py new file mode 100644 index 00000000..3659b8cb --- /dev/null +++ b/api/tests/v1/get/__init__.py @@ -0,0 +1,7 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: michel.peltriaux@sgdnord.rlp.de +Created on: 28.01.22 + +""" diff --git a/api/tests/v1/get/test_api_get.py b/api/tests/v1/get/test_api_get.py new file mode 100644 index 00000000..423c5c38 --- /dev/null +++ b/api/tests/v1/get/test_api_get.py @@ -0,0 +1,157 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: michel.peltriaux@sgdnord.rlp.de +Created on: 28.01.22 + +""" + +import json + +from django.urls import reverse + +from api.tests.v1.share.test_api_sharing import BaseAPIV1TestCase + + +class APIV1GetTestCase(BaseAPIV1TestCase): + @classmethod + def setUpTestData(cls): + super().setUpTestData() + + def _run_get_request(self, url): + response = self.client.get( + url, + **self.header_data + ) + return response + + def _test_get_object(self, obj, url): + """ Tests the API GET of a data object. + + Args: + url (str): The api get url + + Returns: + + """ + response = self._run_get_request(url) + content = json.loads(response.content) + geojson = content[str(obj.id)] + self.assertEqual(response.status_code, 200, msg=response.content) + try: + geojson["type"] + geojson["coordinates"] + props = geojson["properties"] + props["id"] + props["identifier"] + props["title"] + props["created_on"] + props["modified_on"] + except KeyError as e: + self.fail(e) + return geojson + + def test_get_intervention(self): + """ Tests api GET + + Returns: + + """ + self.intervention.share_with(self.superuser) + url = reverse("api:v1:intervention", args=(str(self.intervention.id),)) + geojson = self._test_get_object(self.intervention, url) + try: + props = geojson["properties"] + props["responsible"] + props["responsible"]["registration_office"] + props["responsible"]["registration_file_number"] + props["responsible"]["conservation_office"] + props["responsible"]["conservation_file_number"] + props["legal"]["registration_date"] + props["legal"]["binding_date"] + props["legal"]["process_type"] + props["legal"]["laws"] + props["compensations"] + props["payments"] + props["deductions"] + except KeyError as e: + self.fail(e) + + def test_get_compensation(self): + """ Tests api GET + + Returns: + + """ + self.intervention.share_with(self.superuser) + self.compensation.intervention = self.intervention + self.compensation.save() + + url = reverse("api:v1:compensation", args=(str(self.compensation.id),)) + geojson = self._test_get_object(self.compensation, url) + try: + props = geojson["properties"] + props["is_cef"] + props["is_coherence_keeping"] + props["intervention"] + props["intervention"]["id"] + props["intervention"]["identifier"] + props["intervention"]["title"] + props["before_states"] + props["after_states"] + props["actions"] + props["deadlines"] + except KeyError as e: + self.fail(e) + + def test_get_eco_account(self): + """ Tests api GET + + Returns: + + """ + self.eco_account.share_with(self.superuser) + + url = reverse("api:v1:ecoaccount", args=(str(self.eco_account.id),)) + geojson = self._test_get_object(self.eco_account, url) + try: + props = geojson["properties"] + props["deductable_surface"] + props["deductable_surface_available"] + props["responsible"] + props["responsible"]["conservation_office"] + props["responsible"]["conservation_file_number"] + props["responsible"]["handler"] + props["legal"] + props["legal"]["agreement_date"] + props["before_states"] + props["after_states"] + props["actions"] + props["deadlines"] + props["deductions"] + except KeyError as e: + self.fail(e) + + def test_get_ema(self): + """ Tests api GET + + Returns: + + """ + self.ema.share_with(self.superuser) + + url = reverse("api:v1:ema", args=(str(self.ema.id),)) + geojson = self._test_get_object(self.ema, url) + try: + props = geojson["properties"] + props["responsible"] + props["responsible"]["conservation_office"] + props["responsible"]["conservation_file_number"] + props["responsible"]["handler"] + props["before_states"] + props["after_states"] + props["actions"] + props["deadlines"] + except KeyError as e: + self.fail(e) + diff --git a/api/utils/serializer/v1/serializer.py b/api/utils/serializer/v1/serializer.py index baff113b..2b94d07f 100644 --- a/api/utils/serializer/v1/serializer.py +++ b/api/utils/serializer/v1/serializer.py @@ -8,6 +8,7 @@ Created on: 24.01.22 import json +from django.contrib.gis.geos import MultiPolygon from django.db.models import QuerySet from api.utils.serializer.serializer import AbstractModelAPISerializer @@ -30,7 +31,10 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer): Returns: """ - geom = entry.geometry.geom.geojson + if entry.geometry.geom is not None: + geom = entry.geometry.geom.geojson + else: + geom = MultiPolygon().geojson geo_json = json.loads(geom) self.properties_data = { "id": entry.id,