#31 API GET Tests

* adds tests for api GET method
* fixes bug where non existing geometry could not be serialized properly
This commit is contained in:
mpeltriaux 2022-01-28 12:30:09 +01:00
parent df309c6124
commit 24b8c12505
3 changed files with 169 additions and 1 deletions

View File

@ -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
"""

View File

@ -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)

View File

@ -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:
"""
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,