From 5dc1b11ca1332e2d4c326d15bd0d27781c4908b7 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 28 Jan 2022 13:31:44 +0100 Subject: [PATCH] #31 API PUT Tests * adds tests for PUT method in api --- api/tests/v1/update/__init__.py | 7 + .../update/compensation_update_put_body.json | 61 +++++++ .../v1/update/ecoaccount_update_put_body.json | 70 ++++++++ api/tests/v1/update/ema_update_put_body.json | 63 +++++++ .../update/intervention_update_put_body.json | 61 +++++++ api/tests/v1/update/test_api_update.py | 163 ++++++++++++++++++ 6 files changed, 425 insertions(+) create mode 100644 api/tests/v1/update/__init__.py create mode 100644 api/tests/v1/update/compensation_update_put_body.json create mode 100644 api/tests/v1/update/ecoaccount_update_put_body.json create mode 100644 api/tests/v1/update/ema_update_put_body.json create mode 100644 api/tests/v1/update/intervention_update_put_body.json create mode 100644 api/tests/v1/update/test_api_update.py diff --git a/api/tests/v1/update/__init__.py b/api/tests/v1/update/__init__.py new file mode 100644 index 00000000..3659b8cb --- /dev/null +++ b/api/tests/v1/update/__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/update/compensation_update_put_body.json b/api/tests/v1/update/compensation_update_put_body.json new file mode 100644 index 00000000..57ad5ed4 --- /dev/null +++ b/api/tests/v1/update/compensation_update_put_body.json @@ -0,0 +1,61 @@ +{ + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [ + 7.845568656921382, + 50.79829702304368 + ], + [ + 7.837371826171871, + 50.80155187891526 + ], + [ + 7.835698127746578, + 50.805267562209806 + ], + [ + 7.841062545776364, + 50.806623577403386 + ], + [ + 7.848916053771969, + 50.808359219420474 + ], + [ + 7.855696678161618, + 50.807057493952975 + ], + [ + 7.854666709899899, + 50.80423696434001 + ], + [ + 7.850461006164548, + 50.80217570040005 + ], + [ + 7.845568656921382, + 50.79829702304368 + ] + ] + ] + ], + "properties": { + "title": "TEST_compensation_CHANGED", + "is_cef": true, + "is_coherence_keeping": true, + "intervention": "CHANGE_BEFORE_RUN!!!", + "before_states": [], + "after_states": [], + "actions": [], + "deadlines": [ + { + "type": "finished", + "date": "2022-01-31", + "comment": "TEST_CHANGED" + } + ] + } +} \ No newline at end of file diff --git a/api/tests/v1/update/ecoaccount_update_put_body.json b/api/tests/v1/update/ecoaccount_update_put_body.json new file mode 100644 index 00000000..7b62a127 --- /dev/null +++ b/api/tests/v1/update/ecoaccount_update_put_body.json @@ -0,0 +1,70 @@ +{ + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [ + 7.845568656921382, + 50.79829702304368 + ], + [ + 7.837371826171871, + 50.80155187891526 + ], + [ + 7.835698127746578, + 50.805267562209806 + ], + [ + 7.841062545776364, + 50.806623577403386 + ], + [ + 7.848916053771969, + 50.808359219420474 + ], + [ + 7.855696678161618, + 50.807057493952975 + ], + [ + 7.854666709899899, + 50.80423696434001 + ], + [ + 7.850461006164548, + 50.80217570040005 + ], + [ + 7.845568656921382, + 50.79829702304368 + ] + ] + ] + ], + "properties": { + "title": "TEST_account_CHANGED", + "deductable_surface": "100000.0", + "responsible": { + "conservation_office": null, + "conservation_file_number": "123-TEST", + "handler": "TEST_HANDLER_CHANGED" + }, + "legal": { + "agreement_date": "2022-01-11" + }, + "before_states": [ + ], + "after_states": [ + ], + "actions": [ + ], + "deadlines": [ + { + "type": "finished", + "date": "2022-01-31", + "comment": "TEST_CHANGED" + } + ] + } +} \ No newline at end of file diff --git a/api/tests/v1/update/ema_update_put_body.json b/api/tests/v1/update/ema_update_put_body.json new file mode 100644 index 00000000..933c4fc3 --- /dev/null +++ b/api/tests/v1/update/ema_update_put_body.json @@ -0,0 +1,63 @@ +{ + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [ + 7.845568656921382, + 50.79829702304368 + ], + [ + 7.837371826171871, + 50.80155187891526 + ], + [ + 7.835698127746578, + 50.805267562209806 + ], + [ + 7.841062545776364, + 50.806623577403386 + ], + [ + 7.848916053771969, + 50.808359219420474 + ], + [ + 7.855696678161618, + 50.807057493952975 + ], + [ + 7.854666709899899, + 50.80423696434001 + ], + [ + 7.850461006164548, + 50.80217570040005 + ], + [ + 7.845568656921382, + 50.79829702304368 + ] + ] + ] + ], + "properties": { + "title": "TEST_EMA_CHANGED", + "responsible": { + "conservation_office": null, + "conservation_file_number": "TEST_CHANGED", + "handler": "TEST_HANDLER_CHANGED" + }, + "before_states": [], + "after_states": [], + "actions": [], + "deadlines": [ + { + "type": "finished", + "date": "2022-01-31", + "comment": "TEST_CHANGED" + } + ] + } +} \ No newline at end of file diff --git a/api/tests/v1/update/intervention_update_put_body.json b/api/tests/v1/update/intervention_update_put_body.json new file mode 100644 index 00000000..0376424a --- /dev/null +++ b/api/tests/v1/update/intervention_update_put_body.json @@ -0,0 +1,61 @@ +{ + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [ + 7.845568656921382, + 50.79829702304368 + ], + [ + 7.837371826171871, + 50.80155187891526 + ], + [ + 7.835698127746578, + 50.805267562209806 + ], + [ + 7.841062545776364, + 50.806623577403386 + ], + [ + 7.848916053771969, + 50.808359219420474 + ], + [ + 7.855696678161618, + 50.807057493952975 + ], + [ + 7.854666709899899, + 50.80423696434001 + ], + [ + 7.850461006164548, + 50.80217570040005 + ], + [ + 7.845568656921382, + 50.79829702304368 + ] + ] + ] + ], + "properties": { + "title": "Test_intervention_CHANGED", + "responsible": { + "registration_office": null, + "registration_file_number": "CHANGED", + "conservation_office": null, + "conservation_file_number": "CHANGED", + "handler": null + }, + "legal": { + "registration_date": "2022-02-01", + "binding_date": "2022-02-01", + "process_type": null, + "laws": [] + } + } +} \ No newline at end of file diff --git a/api/tests/v1/update/test_api_update.py b/api/tests/v1/update/test_api_update.py new file mode 100644 index 00000000..e2194d40 --- /dev/null +++ b/api/tests/v1/update/test_api_update.py @@ -0,0 +1,163 @@ +""" +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.contrib.gis import geos +from django.urls import reverse + +from api.tests.v1.share.test_api_sharing import BaseAPIV1TestCase + + +class APIV1UpdateTestCase(BaseAPIV1TestCase): + @classmethod + def setUpTestData(cls): + super().setUpTestData() + + def _run_update_request(self, url, data): + data = json.dumps(data) + response = self.client.put( + url, + data=data, + content_type="application/json", + **self.header_data + ) + return response + + def _test_update_object(self, url, put_body): + """ Tests the API update of a data object. + + Put body data stored in a local json file + + Args: + url (str): The api creation url + put_body (dict): The put body content as dict + + Returns: + + """ + response = self._run_update_request(url, put_body) + self.assertEqual(response.status_code, 200, msg=response.content) + content = json.loads(response.content) + self.assertIsNotNone(content.get("id", None), msg=response.content) + + def test_update_intervention(self): + """ Tests api update + + Returns: + + """ + self.intervention.share_with(self.superuser) + modified_on = self.intervention.modified + url = reverse("api:v1:intervention", args=(str(self.intervention.id),)) + json_file_path = "api/tests/v1/update/intervention_update_put_body.json" + with open(json_file_path) as json_file: + put_body = json.load(fp=json_file) + self._test_update_object(url, put_body) + self.intervention.refresh_from_db() + + put_props = put_body["properties"] + put_geom = geos.fromstr(json.dumps(put_body)) + self.assertEqual(put_geom, self.intervention.geometry.geom) + self.assertEqual(put_props["title"], self.intervention.title) + self.assertNotEqual(modified_on, self.intervention.modified) + self.assertEqual(put_props["responsible"]["registration_file_number"], self.intervention.responsible.registration_file_number) + self.assertEqual(put_props["responsible"]["conservation_file_number"], self.intervention.responsible.conservation_file_number) + self.assertEqual(put_props["legal"]["registration_date"], str(self.intervention.legal.registration_date)) + self.assertEqual(put_props["legal"]["binding_date"], str(self.intervention.legal.binding_date)) + + def test_update_compensation(self): + """ Tests api update + + Returns: + + """ + self.compensation.intervention = self.intervention + self.compensation.save() + self.intervention.share_with(self.superuser) + + modified_on = self.compensation.modified + url = reverse("api:v1:compensation", args=(str(self.compensation.id),)) + json_file_path = "api/tests/v1/update/compensation_update_put_body.json" + with open(json_file_path) as json_file: + put_body = json.load(fp=json_file) + put_body["properties"]["intervention"] = str(self.intervention.id) + self._test_update_object(url, put_body) + self.compensation.refresh_from_db() + + put_props = put_body["properties"] + put_geom = geos.fromstr(json.dumps(put_body)) + self.assertEqual(put_geom, self.compensation.geometry.geom) + self.assertEqual(put_props["title"], self.compensation.title) + self.assertNotEqual(modified_on, self.compensation.modified) + self.assertEqual(put_props["is_cef"], self.compensation.is_cef) + self.assertEqual(put_props["is_coherence_keeping"], self.compensation.is_coherence_keeping) + self.assertEqual(len(put_props["actions"]), self.compensation.actions.count()) + self.assertEqual(len(put_props["before_states"]), self.compensation.before_states.count()) + self.assertEqual(len(put_props["after_states"]), self.compensation.after_states.count()) + self.assertEqual(len(put_props["deadlines"]), self.compensation.deadlines.count()) + + def test_update_ecoaccount(self): + """ Tests api update + + Returns: + + """ + self.eco_account.share_with(self.superuser) + + modified_on = self.eco_account.modified + url = reverse("api:v1:ecoaccount", args=(str(self.eco_account.id),)) + json_file_path = "api/tests/v1/update/ecoaccount_update_put_body.json" + with open(json_file_path) as json_file: + put_body = json.load(fp=json_file) + self._test_update_object(url, put_body) + self.eco_account.refresh_from_db() + + put_props = put_body["properties"] + put_geom = geos.fromstr(json.dumps(put_body)) + self.assertEqual(put_geom, self.eco_account.geometry.geom) + self.assertEqual(put_props["title"], self.eco_account.title) + self.assertNotEqual(modified_on, self.eco_account.modified) + self.assertEqual(put_props["deductable_surface"], str(self.eco_account.deductable_surface)) + self.assertEqual(put_props["responsible"]["conservation_office"], self.eco_account.responsible.conservation_office) + self.assertEqual(put_props["responsible"]["conservation_file_number"], self.eco_account.responsible.conservation_file_number) + self.assertEqual(put_props["responsible"]["handler"], self.eco_account.responsible.handler) + self.assertEqual(put_props["legal"]["agreement_date"], str(self.eco_account.legal.registration_date)) + self.assertEqual(len(put_props["actions"]), self.eco_account.actions.count()) + self.assertEqual(len(put_props["before_states"]), self.eco_account.before_states.count()) + self.assertEqual(len(put_props["after_states"]), self.eco_account.after_states.count()) + self.assertEqual(len(put_props["deadlines"]), self.eco_account.deadlines.count()) + + def test_update_ema(self): + """ Tests api update + + Returns: + + """ + self.ema.share_with(self.superuser) + + modified_on = self.ema.modified + url = reverse("api:v1:ema", args=(str(self.ema.id),)) + json_file_path = "api/tests/v1/update/ema_update_put_body.json" + with open(json_file_path) as json_file: + put_body = json.load(fp=json_file) + self._test_update_object(url, put_body) + self.ema.refresh_from_db() + + put_props = put_body["properties"] + put_geom = geos.fromstr(json.dumps(put_body)) + self.assertEqual(put_geom, self.ema.geometry.geom) + self.assertEqual(put_props["title"], self.ema.title) + self.assertNotEqual(modified_on, self.ema.modified) + self.assertEqual(put_props["responsible"]["conservation_office"], self.ema.responsible.conservation_office) + self.assertEqual(put_props["responsible"]["conservation_file_number"], self.ema.responsible.conservation_file_number) + self.assertEqual(put_props["responsible"]["handler"], self.ema.responsible.handler) + self.assertEqual(len(put_props["actions"]), self.ema.actions.count()) + 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())