OnroerendErfgoed /
oe_geoutils
| 1 | # -*- coding: utf-8 -*- |
||
| 2 | import os |
||
| 3 | import unittest |
||
| 4 | import json |
||
| 5 | |||
| 6 | from paste.deploy import appconfig |
||
| 7 | from webtest import TestApp |
||
| 8 | from pyramid import testing |
||
| 9 | import responses |
||
| 10 | |||
| 11 | try: |
||
| 12 | from testdata import test_json_outside_flanders, test_json_intersects_flanders |
||
| 13 | except: |
||
| 14 | from tests.testdata import test_json_outside_flanders, test_json_intersects_flanders |
||
| 15 | |||
| 16 | try: |
||
| 17 | from __init__ import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, get_provincie_results, get_gemeente_results |
||
| 18 | except: |
||
| 19 | from tests import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, get_provincie_results, get_gemeente_results |
||
| 20 | |||
| 21 | from oe_geoutils import main |
||
| 22 | try: |
||
| 23 | from tests import testdata |
||
| 24 | except: |
||
| 25 | import testdata |
||
| 26 | from oe_geoutils.views.exceptions import internal_server_error |
||
| 27 | import pytest |
||
| 28 | |||
| 29 | integration = pytest.mark.skipif( |
||
| 30 | not pytest.config.getoption("--integration"), |
||
| 31 | reason="need --integration option to run" |
||
| 32 | ) |
||
| 33 | |||
| 34 | here = os.path.dirname(__file__) |
||
| 35 | |||
| 36 | test_geom = json.dumps({ |
||
| 37 | "type": "MultiPolygon", |
||
| 38 | "coordinates": [[[[172933.6922879719058983, 174851.14960918109863997], |
||
| 39 | [172930.21180502674542367, 174832.7836931711062789], |
||
| 40 | [172920.64762709615752101, 174848.13247794657945633], |
||
| 41 | [172933.6922879719058983, 174851.14960918109863997]]]], |
||
| 42 | "crs": { |
||
| 43 | "type": "name", |
||
| 44 | "properties": { |
||
| 45 | "name": "urn:ogc:def:crs:EPSG::31370" |
||
| 46 | } |
||
| 47 | } |
||
| 48 | }) |
||
| 49 | |||
| 50 | |||
| 51 | class FunctionalTests(unittest.TestCase): |
||
| 52 | def _get_default_headers(self): |
||
| 53 | return {'Accept': 'application/json'} |
||
| 54 | |||
| 55 | @classmethod |
||
| 56 | def setUpClass(cls): |
||
| 57 | cls.settings = appconfig('config:' + os.path.join(here, 'test.ini')) |
||
| 58 | |||
| 59 | def setUp(self): |
||
| 60 | self.app = main({}, **self.settings) |
||
| 61 | self.testapp = TestApp(self.app) |
||
| 62 | responses.add(responses.POST, "https://test-geo.onroerenderfgoed.be/zoekdiensten/administratievegrenzen") |
||
| 63 | |||
| 64 | def tearDown(self): |
||
| 65 | self.testapp.reset() |
||
| 66 | |||
| 67 | @integration |
||
| 68 | def test_get_nearest_address(self): |
||
| 69 | res = self.testapp.post('/nearest_address', test_geom) |
||
| 70 | self.assertEqual('200 OK', res.status) |
||
| 71 | |||
| 72 | @responses.activate |
||
| 73 | def test_get_nearest_address_mock(self): |
||
| 74 | responses.add( |
||
| 75 | responses.GET, |
||
| 76 | 'https://loc.geopunt.be/geolocation/Location', |
||
| 77 | body='{"LocationResult":[{"ID":201984,"FormattedAddress":"Fonteinstraat 75, 3000 Leuven","Location":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0},"LocationType":"crab_huisnummer_afgeleidVanGebouw","BoundingBox":{"LowerLeft":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0},"UpperRight":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0}}}]}', |
||
| 78 | status=200) |
||
| 79 | res = self.testapp.post('/nearest_address', test_geom) |
||
| 80 | self.assertEqual('200 OK', res.status) |
||
| 81 | |||
| 82 | def test_get_nearest_address_outside_Flanders(self): |
||
| 83 | res = self.testapp.post('/nearest_address', json.dumps(test_json_outside_flanders), expect_errors=True) |
||
| 84 | self.assertEqual('400 Bad Request', res.status) |
||
| 85 | |||
| 86 | @integration |
||
| 87 | def test_get_nearest_address_not_found(self): |
||
| 88 | res = self.testapp.post('/nearest_address', json.dumps(test_json_intersects_flanders), expect_errors=True) |
||
| 89 | self.assertEqual('200 OK', res.status) |
||
| 90 | |||
| 91 | @responses.activate |
||
| 92 | def test_get_nearest_address_not_found_mock(self): |
||
| 93 | responses.add( |
||
| 94 | responses.GET, |
||
| 95 | 'https://loc.geopunt.be/geolocation/Location', |
||
| 96 | body='{"LocationResult":[{"ID":1031530,"FormattedAddress":"Linkebeekstraat 35, 1180 Ukkel","Location":{"Lat_WGS84":50.779580783177835,"Lon_WGS84":4.327986356112306,"X_Lambert72":147125.0,"Y_Lambert72":163234.0},"LocationType":"urbis_huisnummer","BoundingBox":{"LowerLeft":{"Lat_WGS84":50.779580783177835,"Lon_WGS84":4.327986356112306,"X_Lambert72":147125.0,"Y_Lambert72":163234.0},"UpperRight":{"Lat_WGS84":50.779580783177835,"Lon_WGS84":4.327986356112306,"X_Lambert72":147125.0,"Y_Lambert72":163234.0}}}]}', |
||
| 97 | status=200 |
||
| 98 | ) |
||
| 99 | res = self.testapp.post('/nearest_address', json.dumps(test_json_intersects_flanders), expect_errors=True) |
||
| 100 | self.assertEqual('200 OK', res.status) |
||
| 101 | |||
| 102 | def test_check_in_flanders(self): |
||
| 103 | res = self.testapp.post('/check_in_flanders', test_geom) |
||
| 104 | self.assertEqual('200 OK', res.status) |
||
| 105 | |||
| 106 | def test_check_in_flanders_tidal_zone(self): |
||
| 107 | res = self.testapp.post('/check_in_flanders?check_getijdezone=1', |
||
| 108 | json.dumps(testdata.test_json_tidal_zone_flanders)) |
||
| 109 | self.assertEqual('200 OK', res.status) |
||
| 110 | self.assertEqual({'IntersectFlanders': True}, res.json_body) |
||
| 111 | res = self.testapp.post('/check_in_flanders?check_getijdezone=0', |
||
| 112 | json.dumps(testdata.test_json_tidal_zone_flanders)) |
||
| 113 | self.assertEqual('200 OK', res.status) |
||
| 114 | self.assertEqual({'IntersectFlanders': False}, res.json_body) |
||
| 115 | res = self.testapp.post('/check_in_flanders?check_getijdezone=test', |
||
| 116 | json.dumps(testdata.test_json_tidal_zone_flanders), expect_errors=True) |
||
| 117 | self.assertEqual('400 Bad Request', res.status) |
||
| 118 | |||
| 119 | def test_check_within_flanders(self): |
||
| 120 | res = self.testapp.post('/check_within_flanders', test_geom) |
||
| 121 | self.assertEqual('200 OK', res.status) |
||
| 122 | |||
| 123 | def test_check_in_flanders_no_json_body(self): |
||
| 124 | res = self.testapp.post('/check_in_flanders', expect_errors=True) |
||
| 125 | self.assertEqual('400 Bad Request', res.status) |
||
| 126 | |||
| 127 | def test_check_in_flanders_validation_failure(self): |
||
| 128 | res = self.testapp.post('/check_in_flanders', '{}', expect_errors=True) |
||
| 129 | self.assertEqual('400 Bad Request', res.status) |
||
| 130 | |||
| 131 | def test_check_in_flanders_invalid_url(self): |
||
| 132 | res = self.testapp.post('/test', '{}', expect_errors=True) |
||
| 133 | self.assertEqual('404 Not Found', res.status) |
||
| 134 | |||
| 135 | def test_internal_server_error(self): |
||
| 136 | a = Exception() |
||
| 137 | internal_server_error(a, testing.DummyRequest()) |
||
| 138 | |||
| 139 | @responses.activate |
||
| 140 | def test_gemeente(self): |
||
| 141 | res = self.testapp.post('/gemeente', test_geom) |
||
| 142 | self.assertEqual('200 OK', res.status) |
||
| 143 | View Code Duplication | print(res.text) |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 144 | |||
| 145 | @responses.activate |
||
| 146 | def test_gemeente(self): |
||
| 147 | responses.add(responses.POST, 'http://geozoekdienst.en', body=json.dumps(get_gemeente_results)) |
||
| 148 | res = self.testapp.post('/gemeente', test_geom) |
||
| 149 | self.assertEqual('200 OK', res.status) |
||
| 150 | print(res.text) |
||
| 151 | |||
| 152 | @responses.activate |
||
| 153 | def test_provincie(self): |
||
| 154 | responses.add(responses.POST, 'http://geozoekdienst.en', body=json.dumps(get_provincie_results)) |
||
| 155 | res = self.testapp.post('/provincie', test_geom) |
||
| 156 | self.assertEqual('200 OK', res.status) |
||
| 157 | print(res.text) |
||
| 158 | |||
| 159 | @responses.activate |
||
| 160 | def test_check_erfgoedgemeente(self): |
||
| 161 | contour = { |
||
| 162 | "coordinates": [[[[172933.6922879719, 174851.1496091811], [172930.21180502675, 174832.7836931711], |
||
| 163 | [172920.64762709616, 174848.13247794658], [172933.6922879719, 174851.1496091811]]]], |
||
| 164 | "type": "MultiPolygon", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}} |
||
| 165 | } |
||
| 166 | responses.add( |
||
| 167 | responses.POST, |
||
| 168 | 'http://geozoekdienst.en', |
||
| 169 | body='[{"naam": "Leuven", "type": "gemeente", "id": "24062"}]', status=200) |
||
| 170 | res = self.testapp.post('/check_in_erfgoedgemeente', params=json.dumps(contour), |
||
| 171 | headers={'Accept': 'application/json', 'Content-Type': 'application/json'}) |
||
| 172 | self.assertIn('ok', res.json["status"]) |
||
| 173 | |||
| 174 | View Code Duplication | @responses.activate |
|
|
0 ignored issues
–
show
|
|||
| 175 | def test_check_erfgoedgemeente_full_overlap(self): |
||
| 176 | responses.add( |
||
| 177 | responses.POST, |
||
| 178 | 'http://geozoekdienst.en', |
||
| 179 | body='[{"naam": "Koksijde", "type": "gemeente", "id": "38014"}]', status=200) |
||
| 180 | contour = { |
||
| 181 | "coordinates": [[[[172933.6922879719, 174851.1496091811], [172930.21180502675, 174832.7836931711], |
||
| 182 | [172920.64762709616, 174848.13247794658], [172933.6922879719, 174851.1496091811]]]], |
||
| 183 | "type": "MultiPolygon", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}} |
||
| 184 | } |
||
| 185 | res = self.testapp.post('/check_in_erfgoedgemeente', params=json.dumps(contour), |
||
| 186 | headers={'Accept': 'application/json', 'Content-Type': 'application/json'}) |
||
| 187 | self.assertIn('error', res.json["status"]) |
||
| 188 | self.assertIn('Gelieve de melding in te dienen bij deze gemeente', res.json["message"]) |
||
| 189 | |||
| 190 | @responses.activate |
||
| 191 | def test_check_erfgoedgemeente_partial_overlap(self): |
||
| 192 | responses.add( |
||
| 193 | responses.POST, |
||
| 194 | 'http://geozoekdienst.en', |
||
| 195 | body='[{"naam": "Koksijde", "type": "gemeente", "id": "38014"}, ' |
||
| 196 | '{"naam": "Nieuwpoort", "type": "gemeente", "id": "38016"}]', status=200) |
||
| 197 | contour = { |
||
| 198 | "coordinates": [[[[172933.6922879719, 174851.1496091811], [172930.21180502675, 174832.7836931711], |
||
| 199 | [172920.64762709616, 174848.13247794658], [172933.6922879719, 174851.1496091811]]]], |
||
| 200 | "type": "MultiPolygon", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}} |
||
| 201 | } |
||
| 202 | res = self.testapp.post('/check_in_erfgoedgemeente', params=json.dumps(contour), |
||
| 203 | headers={'Accept': 'application/json', 'Content-Type': 'application/json'}) |
||
| 204 | self.assertIn('warn', res.json["status"]) |
||
| 205 | self.assertIn('Gelieve de melding vooronderzoek eveneens in te dienen bij deze gemeente', res.json['message']) |
||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 |