Completed
Push — master ( 8a1e4c...0fd553 )
by
unknown
26s
created

GeoUtilTests.test_closed_crab_location_2_mock()   A

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
1
# -*- coding: utf-8 -*-
2
import unittest
3
import responses
4
import json
5
6
try:
7
    from unittest.mock import Mock, patch, MagicMock
8
except:
9
    from mock import Mock, patch, MagicMock
10
11
from oe_geoutils.utils import (
12
    convert_geojson_to_wktelement,
13
    get_srid_from_geojson,
14
    convert_wktelement_to_geojson,
15
    get_centroid_xy,
16
    nearest_location,
17
    check_in_flanders,
18
    check_within_flanders,
19
    AdminGrenzenClient,
20
    remove_dupl_values,
21
    remove_dupl_coords,
22
    provincie_niscode)
23
24
try:
25
    from __init__ import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, \
26
        get_gemeente_results, get_provincie_results
27
except:
28
    from tests import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, \
29
        get_gemeente_results, get_provincie_results
30
31
try:
32
    from tests import testdata
33
except:
34
    import testdata
35
import pytest
36
37
38
integration = pytest.mark.skipif(
39
    not pytest.config.getoption("--integration"),
40
    reason="need --integration option to run"
41
)
42
43
niscode_url = 'https://test-geo.onroerenderfgoed.be/zoekdiensten/administratievegrenzen'
44
45
46
class GeoUtilTests(unittest.TestCase):
47
    geojson_valid = {
48
        "type": "MultiPolygon",
49
        "coordinates": [[[[152184.01399999947, 212331.8648750011], [152185.94512499947, 212318.6137500011],
50
                          [152186.13837499946, 212318.6326250011], [152186.86699999947, 212313.9570000011],
51
                          [152186.91462499945, 212313.65187500112], [152192.45099999948, 212314.2943750011],
52
                          [152190.69212499948, 212319.2656250011], [152199.58799999946, 212319.5248750011],
53
                          [152197.85312499947, 212327.9388750011], [152197.57199999946, 212327.8978750011],
54
                          [152197.08099999945, 212333.2668750011], [152184.01399999947, 212331.8648750011]]]],
55
        "crs": {
56
            "type": "name",
57
            "properties": {
58
                "name": "urn:ogc:def:crs:EPSG::31370"
59
            }
60
        }
61
    }
62
63
    def test_geojson_none(self):
64
        self.assertEqual(None, get_srid_from_geojson(None))
65
66
    def test_geojson_value_error(self):
67
        geojson = {"type": "MultiPolygon",
68
                   "coordinates": [[]],
69
                   "crs": {
70
                       "type": "wrong value",
71
                       "properties": {
72
                           "name": "urn:ogc:def:crs:EPSG::31370"
73
                       }
74
                   }}
75
76
        self.assertRaises(ValueError, convert_geojson_to_wktelement, geojson)
77
78
    def test_conversions(self):
79
        self.assertIsNone(convert_wktelement_to_geojson(None))
80
        test_wktelement = convert_geojson_to_wktelement(testdata.test_geojson_valid)
81
        test_geojson_converted = convert_wktelement_to_geojson(test_wktelement)
82
        self.assertEqual(testdata.test_geojson_valid['type'], test_geojson_converted['type'])
83
        self.assertEqual(len(testdata.test_geojson_valid['coordinates']), len(test_geojson_converted['coordinates']))
84
        self.assertEqual(testdata.test_geojson_valid['crs']['type'], test_geojson_converted['crs']['type'])
85
        self.assertEqual(testdata.test_geojson_valid['crs']['properties']['name'],
86
                         test_geojson_converted['crs']['properties']['name'])
87
88
    def test_wktelement_attribute_error(self):
89
        wktelement = "string"
90
        self.assertRaises(AssertionError, convert_wktelement_to_geojson, wktelement)
91
92
    def test_get_centroid_polygon(self):
93
        self.assertEqual('172928.1839066983,174844.0219267663', get_centroid_xy(testdata.test_geojson_valid_polygon))
94
95
    def test_get_centroid(self):
96
        self.assertEqual('172928.1839066983,174844.0219267663', get_centroid_xy(testdata.test_geojson_valid))
97
98
    def test_get_centroid_2(self):
99
        self.assertEqual('152191.3046633389,212324.6399979071', get_centroid_xy(testdata.test_geojson_mulipolygon))
100
101
    @integration
102
    def test_closed_crab_location(self):
103
        closed_adres = {'omschrijving_straat': u'Fonteinstraat, 75', 'huisnummer': u'75', 'straat': u'Fonteinstraat',
104
                        'postcode': u'3000', 'gemeente': u'Leuven', 'land': 'BE'}
105
        self.assertDictEqual(closed_adres, nearest_location(testdata.test_geojson_valid))
106
107
    @responses.activate
108
    def test_closed_crab_location_mock(self):
109
        responses.add(
110
            responses.GET,
111
            'https://loc.geopunt.be/geolocation/Location',
112
            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}}}]}',
113
            status=200)
114
        closed_adres = {'omschrijving_straat': u'Fonteinstraat, 75', 'huisnummer': u'75', 'straat': u'Fonteinstraat',
115
                        'postcode': u'3000', 'gemeente': u'Leuven', 'land': 'BE'}
116
        self.assertDictEqual(closed_adres, nearest_location(testdata.test_geojson_valid))
117
118
    @integration
119
    def test_closed_crab_location_2(self):
120
        closed_adres = {'straat': 'Krijgsbaan', 'land': 'BE', 'postcode': '2100',
121
                         'omschrijving_straat': 'Krijgsbaan, 150', 'huisnummer': '150', 'gemeente': 'Antwerpen'}
122
        self.assertDictEqual(closed_adres, nearest_location(
123
            {"type": "MultiPolygon",
124
             "coordinates": [[[[158788, 211982], [158789, 211982], [158789, 211983], [158788, 211982]]]],
125
             "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}}))
126
127
    @responses.activate
128
    def test_closed_crab_location_2_mock(self):
129
        responses.add(
130
            responses.GET,
131
            'https://loc.geopunt.be/geolocation/Location',
132
            body='{"LocationResult":[{"ID":3884086,"FormattedAddress":"Krijgsbaan 150, 2100 Antwerpen (2100)","Location":{"Lat_WGS84":51.217719624709794,"Lon_WGS84":4.49453430920374,"X_Lambert72":158788.0,"Y_Lambert72":211982.0},"LocationType":"crab_huisnummer_afgeleidVanPerceelGrb","BoundingBox":{"LowerLeft":{"Lat_WGS84":51.217719624709794,"Lon_WGS84":4.49453430920374,"X_Lambert72":158788.0,"Y_Lambert72":211982.0},"UpperRight":{"Lat_WGS84":51.217719624709794,"Lon_WGS84":4.49453430920374,"X_Lambert72":158788.0,"Y_Lambert72":211982.0}}}]}',
133
            status=200)
134
        closed_adres = {'straat': 'Krijgsbaan', 'land': 'BE', 'postcode': '2100',
135
                         'omschrijving_straat': 'Krijgsbaan, 150', 'huisnummer': '150', 'gemeente': 'Antwerpen'}
136
        self.assertDictEqual(closed_adres, nearest_location(
137
            {"type": "MultiPolygon",
138
             "coordinates": [[[[158788, 211982], [158789, 211982], [158789, 211983], [158788, 211982]]]],
139
             "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}}))
140
141
    @integration
142
    def test_closed_crab_location_none(self):
143
        self.assertIsNone(nearest_location(testdata.test_json_intersects_flanders))
144
145
    @responses.activate
146
    def test_closed_crab_location_none_mock(self):
147
        responses.add(
148
            responses.GET,
149
            'https://loc.geopunt.be/geolocation/Location',
150
            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}}}]}',
151
            status=200)
152
        self.assertIsNone(nearest_location(testdata.test_json_intersects_flanders))
153
154
    @integration
155
    def test_closed_crab_location_False(self):
156
        self.assertFalse(nearest_location(testdata.test_json_outside_flanders))
157
158
    @responses.activate
159
    def test_closed_crab_location_False_mock(self):
160
        responses.add(
161
            responses.GET,
162
            'https://loc.geopunt.be/geolocation/Location',
163
            body='{"LocationResult":[{"ID":1206833,"FormattedAddress":"Opperstraat 108, 1050 Elsene","Location":{"Lat_WGS84":50.830774347520865,"Lon_WGS84":4.362151535734923,"X_Lambert72":149535.0,"Y_Lambert72":168928.0},"LocationType":"urbis_huisnummer","BoundingBox":{"LowerLeft":{"Lat_WGS84":50.830774347520865,"Lon_WGS84":4.362151535734923,"X_Lambert72":149535.0,"Y_Lambert72":168928.0},"UpperRight":{"Lat_WGS84":50.830774347520865,"Lon_WGS84":4.362151535734923,"X_Lambert72":149535.0,"Y_Lambert72":168928.0}}}]}',
164
            status=200)
165
        self.assertFalse(nearest_location(testdata.test_json_outside_flanders))
166
167 View Code Duplication
    @integration
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
168
    def test_closesr_crab_location_crabpy_gateway(self):
169
        n75 = Mock()
170
        n75.id = 102
171
        n75.huisnummer = "75"
172
        fonteinstraat = Mock()
173
        fonteinstraat.id = 101
174
        fonteinstraat.label = "Fonteinstraat"
175
        fonteinstraat.huisnummers = [n75]
176
        leuven = Mock()
177
        leuven.id = 100
178
        leuven.naam = "Leuven"
179
        leuven.straten = [fonteinstraat]
180
        gemeenten = [leuven]
181
        crabpy_gateway = Mock()
182
        crabpy_gateway.list_gemeenten = Mock(return_value=gemeenten)
183
        closed_adres = {'omschrijving_straat': u'Fonteinstraat, 75', 'huisnummer': u'75', 'huisnummer_id': 102,
184
                        'straat': u'Fonteinstraat', 'straat_id': 101,
185
                        'postcode': u'3000', 'gemeente': u'Leuven', 'gemeente_id': 100, 'land': 'BE'}
186
        self.assertDictEqual(closed_adres, nearest_location(testdata.test_geojson_valid, crabpy_gateway))
187
188 View Code Duplication
    @responses.activate
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
189
    def test_closesr_crab_location_crabpy_gateway_mock(self):
190
        responses.add(
191
            responses.GET,
192
            'https://loc.geopunt.be/geolocation/Location',
193
            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}}}]}',
194
            status=200)
195
        n75 = Mock()
196
        n75.id = 102
197
        n75.huisnummer = "75"
198
        fonteinstraat = Mock()
199
        fonteinstraat.id = 101
200
        fonteinstraat.label = "Fonteinstraat"
201
        fonteinstraat.huisnummers = [n75]
202
        leuven = Mock()
203
        leuven.id = 100
204
        leuven.naam = "Leuven"
205
        leuven.straten = [fonteinstraat]
206
        gemeenten = [leuven]
207
        crabpy_gateway = Mock()
208
        crabpy_gateway.list_gemeenten = Mock(return_value=gemeenten)
209
        closed_adres = {'omschrijving_straat': u'Fonteinstraat, 75', 'huisnummer': u'75', 'huisnummer_id': 102,
210
                        'straat': u'Fonteinstraat', 'straat_id': 101,
211
                        'postcode': u'3000', 'gemeente': u'Leuven', 'gemeente_id': 100, 'land': 'BE'}
212
        self.assertDictEqual(closed_adres, nearest_location(testdata.test_geojson_valid, crabpy_gateway))
213
214
    def test_check_in_flanders(self):
215
        self.assertTrue(check_in_flanders(testdata.test_geojson_valid))
216
        self.assertTrue(check_in_flanders(testdata.test_json_intersects_flanders))
217
        self.assertFalse(check_in_flanders(testdata.test_json_outside_flanders))
218
219
    def test_check_within_flanders(self):
220
        self.assertTrue(check_within_flanders(testdata.test_geojson_valid))
221
        self.assertFalse(check_within_flanders(testdata.test_json_intersects_flanders))
222
        self.assertFalse(check_within_flanders(testdata.test_json_outside_flanders))
223
224
    def test_convert_geojson_to_wktelement_none(self):
225
        self.assertIsNone(convert_geojson_to_wktelement(None))
226
227
    @responses.activate
228
    def test_admin_grenzen_client(self):
229
        base_url = mock_geozoekdiensten_response()
230
        gemeenten = AdminGrenzenClient(base_url).get_gemeenten(self.geojson_valid)
231
        self.assertIsInstance(gemeenten, list)
232
        self.assertGreater(len(gemeenten), 0)
233
234
    @responses.activate
235
    def test_admin_grenzen_client_outside_flanders(self):
236
        base_url = mock_geozoekdiensten_response()
237
        gemeenten = AdminGrenzenClient(base_url).get_gemeenten(testdata.test_json_outside_flanders)
238
        self.assertIsInstance(gemeenten, list)
239
        self.assertEqual(len(gemeenten), 0)
240
241
    @responses.activate
242
    def test_admin_grenzen_client_raise_service_error(self):
243
        base_url = mock_geozoekdiensten_response(response_status=500)
244
        with self.assertRaises(Exception) as ex:
245
            AdminGrenzenClient(base_url).get_gemeenten(self.geojson_valid)
246
247
    @responses.activate
248
    def test_get_gemeente_2(self):
249
        base_url = mock_geozoekdiensten_get_gemeente_response(len_results=2)
250
        gemeente = AdminGrenzenClient(base_url).get_gemeente(testdata.test_geojson_mulipolygon)
251
        self.assertDictEqual({'naam': 'Antwerpen', 'niscode': '11002'}, gemeente)
252
253
    @responses.activate
254
    def test_get_gemeente_1(self):
255
        base_url = mock_geozoekdiensten_get_gemeente_response(len_results=1)
256
        gemeente = AdminGrenzenClient(base_url).get_gemeente(testdata.test_geojson_mulipolygon)
257
        self.assertDictEqual({'naam': 'gemeente', 'niscode': 'niscode'}, gemeente)
258
259
    @responses.activate
260
    def test_get_gemeente_0(self):
261
        base_url = mock_geozoekdiensten_get_gemeente_response(len_results=0)
262
        gemeente = AdminGrenzenClient(base_url).get_gemeente(testdata.test_geojson_mulipolygon)
263
        self.assertIsNone(gemeente)
264
265
    @responses.activate
266
    def test_get_provincie(self):
267
        base_url = 'http://geozoekdienst.en/provincies'
268
        responses.add(responses.POST, base_url, body=json.dumps(get_provincie_results))
269
        provincie = AdminGrenzenClient(base_url).get_provincie(testdata.test_geojson_mulipolygon)
270
        self.assertDictEqual({'naam': 'Antwerpen', 'niscode': '10000'}, provincie)
271
272
    @responses.activate
273
    def test_get_provincies(self):
274
        base_url = 'http://geozoekdienst.en/provincies'
275
        responses.add(responses.POST, base_url, body=json.dumps(get_provincie_results))
276
        provincies = AdminGrenzenClient(base_url).get_provincies(testdata.test_geojson_mulipolygon)
277
        self.assertListEqual(['Antwerpen', 'Vlaams Brabant'], provincies)
278
279
    def test_remove_dupl_values_simple(self):
280
        a = [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 1]
281
        remove_dupl_values(a)
282
        self.assertEqual([1, 2, 3, 4, 5, 1], a)
283
284
    def test_remove_dupl_values(self):
285
        a = [[94826.49908124168, 193977.986615351],
286
             [94826.49908124168, 193976.986615351],
287
             [94826.49908124168, 193976.986615351],
288
             [94820.99908124168, 193974.486615351],
289
             [94824.99908124168, 193967.986615351],
290
             [94830.99908124168, 193972.986615351],
291
             [94826.49908124168, 193977.986615351]]
292
        remove_dupl_values(a)
293
        self.assertEqual(
294
            [[94826.49908124168, 193977.986615351],
295
             [94826.49908124168, 193976.986615351],
296
             [94820.99908124168, 193974.486615351],
297
             [94824.99908124168, 193967.986615351],
298
             [94830.99908124168, 193972.986615351],
299
             [94826.49908124168, 193977.986615351]], a)
300
301
    def test_remove_dupl_coords(self):
302
        a = {
303
            "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}},
304
            "type": "MultiPolygon",
305
            "coordinates": [
306
                [
307
                    [
308
                        [94826.49908124168, 193977.986615351],
309
                        [94826.49908124168, 193976.986615351],
310
                        [94826.49908124168, 193976.986615351],
311
                        [94820.99908124168, 193974.486615351],
312
                        [94824.99908124168, 193967.986615351],
313
                        [94830.99908124168, 193972.986615351],
314
                        [94826.49908124168, 193977.986615351]
315
                    ]
316
                ]
317
            ]
318
        }
319
        remove_dupl_coords(a["coordinates"])
320
        self.assertEqual(
321
            [[[[94826.49908124168, 193977.986615351],
322
               [94826.49908124168, 193976.986615351],
323
               [94820.99908124168, 193974.486615351],
324
               [94824.99908124168, 193967.986615351],
325
               [94830.99908124168, 193972.986615351],
326
               [94826.49908124168, 193977.986615351]]]], a["coordinates"])
327
328
    def test_provincie_niscode(self):
329
        pniscode = provincie_niscode(12021)
330
        self.assertEqual(10000, pniscode)
331
332
    def test_provincie_niscode_vlb(self):
333
        pniscode = provincie_niscode(24062)
334
        self.assertEqual(20001, pniscode)
335