Completed
Push — master ( 3a7275...213df2 )
by
unknown
01:12
created

GeoUtilTests.test_get_provincies()   A

Complexity

Conditions 1

Size

Total Lines 6

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 6
rs 9.4285
1
# -*- coding: utf-8 -*-
2
import unittest
3
import responses
4
import json
5
import re
6
7
try:
8
    from unittest.mock import Mock, patch, MagicMock
9
except:
10
    from mock import Mock, patch, MagicMock
11
12
from oe_geoutils.utils import (
13
    convert_geojson_to_wktelement,
14
    get_srid_from_geojson,
15
    convert_wktelement_to_geojson,
16
    get_centroid_xy,
17
    nearest_location,
18
    check_in_flanders,
19
    check_within_flanders,
20
    AdminGrenzenClient
21
)
22
try:
23
    from __init__ import text_
24
except:
25
    from tests import text_
26
27
try:
28
    from tests import testdata
29
except:
30
    import testdata
31
import os
32
33
niscode_url = 'https://test-geo.onroerenderfgoed.be/zoekdiensten/administratievegrenzen'
34
35
with open(os.path.join(os.path.dirname(__file__), 'fixtures/get_gemeente_results.json'), 'rb') as f:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable f does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable __file__ does not seem to be defined.
Loading history...
36
    get_gemeente_results = json.loads(text_(f.read()))
37
with open(os.path.join(os.path.dirname(__file__), 'fixtures/get_provincie_results.json'), 'rb') as f:
38
    get_provincie_results = json.loads(text_(f.read()))
39
40
41
class GeoUtilTests(unittest.TestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unittest does not seem to be defined.
Loading history...
42
    geojson_valid = {
43
        "type": "MultiPolygon",
44
        "coordinates": [[[[152184.01399999947, 212331.8648750011], [152185.94512499947, 212318.6137500011],
45
                          [152186.13837499946, 212318.6326250011], [152186.86699999947, 212313.9570000011],
46
                          [152186.91462499945, 212313.65187500112], [152192.45099999948, 212314.2943750011],
47
                          [152190.69212499948, 212319.2656250011], [152199.58799999946, 212319.5248750011],
48
                          [152197.85312499947, 212327.9388750011], [152197.57199999946, 212327.8978750011],
49
                          [152197.08099999945, 212333.2668750011], [152184.01399999947, 212331.8648750011]]]],
50
        "crs": {
51
            "type": "name",
52
            "properties": {
53
                "name": "urn:ogc:def:crs:EPSG::31370"
54
            }
55
        }
56
    }
57
58
    def test_geojson_none(self):
59
        self.assertEqual(None, get_srid_from_geojson(None))
60
61
    def test_geojson_value_error(self):
62
        geojson = {"type": "MultiPolygon",
63
                   "coordinates": [[]],
64
                   "crs": {
65
                       "type": "wrong value",
66
                       "properties": {
67
                           "name": "urn:ogc:def:crs:EPSG::31370"
68
                       }
69
                   }}
70
71
        self.assertRaises(ValueError, convert_geojson_to_wktelement, geojson)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable convert_geojson_to_wktelement does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable geojson does not seem to be defined.
Loading history...
72
73
    def test_conversions(self):
74
        self.assertIsNone(convert_wktelement_to_geojson(None))
75
        test_wktelement = convert_geojson_to_wktelement(testdata.test_geojson_valid)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
76
        test_geojson_converted = convert_wktelement_to_geojson(test_wktelement)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable test_wktelement does not seem to be defined.
Loading history...
77
        self.assertEqual(testdata.test_geojson_valid['type'], test_geojson_converted['type'])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable test_geojson_converted does not seem to be defined.
Loading history...
78
        self.assertEqual(len(testdata.test_geojson_valid['coordinates']), len(test_geojson_converted['coordinates']))
79
        self.assertEqual(testdata.test_geojson_valid['crs']['type'], test_geojson_converted['crs']['type'])
80
        self.assertEqual(testdata.test_geojson_valid['crs']['properties']['name'],
81
                         test_geojson_converted['crs']['properties']['name'])
82
83
    def test_wktelement_attribute_error(self):
84
        wktelement = "string"
85
        self.assertRaises(AssertionError, convert_wktelement_to_geojson, wktelement)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable AssertionError does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable convert_wktelement_to_geojson does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable wktelement does not seem to be defined.
Loading history...
86
87
    def test_get_centroid_polygon(self):
88
        self.assertEqual('172928.1839066983,174844.0219267663', get_centroid_xy(testdata.test_geojson_valid_polygon))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
89
90
    def test_get_centroid(self):
91
        self.assertEqual('172928.1839066983,174844.0219267663', get_centroid_xy(testdata.test_geojson_valid))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
92
93
    def test_get_centroid_2(self):
94
        self.assertEqual('152191.3046633389,212324.6399979071', get_centroid_xy(testdata.test_geojson_mulipolygon))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
95
96
    def test_closed_crab_location(self):
97
        closed_adres = {'omschrijving_straat': u'Fonteinstraat, 75', 'huisnummer': u'75', 'straat': u'Fonteinstraat',
98
                        'postcode': u'3000', 'gemeente': u'Leuven', 'land': 'BE'}
99
        self.assertDictEqual(closed_adres, nearest_location(testdata.test_geojson_valid))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable closed_adres does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
100
101
    def test_closed_crab_location_none(self):
102
        self.assertIsNone(nearest_location(testdata.test_json_intersects_flanders))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
103
104
    def test_closed_crab_location_False(self):
105
        self.assertFalse(nearest_location(testdata.test_json_outside_flanders))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
106
107
    def test_check_in_flanders(self):
108
        self.assertTrue(check_in_flanders(testdata.test_geojson_valid))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
109
        self.assertTrue(check_in_flanders(testdata.test_json_intersects_flanders))
110
        self.assertFalse(check_in_flanders(testdata.test_json_outside_flanders))
111
112
    def test_check_within_flanders(self):
113
        self.assertTrue(check_within_flanders(testdata.test_geojson_valid))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
114
        self.assertFalse(check_within_flanders(testdata.test_json_intersects_flanders))
115
        self.assertFalse(check_within_flanders(testdata.test_json_outside_flanders))
116
117
    def test_convert_geojson_to_wktelement_none(self):
118
        self.assertIsNone(convert_geojson_to_wktelement(None))
119
120
    def mock_geozoekdiensten_response(self, base_url='http://geozoekdienst.en', response_status=200):
121
        def callback(request):
122
            self.geozoekdienst_request = request
123
            resp_body = [{'naam': 'gemeente'}]
124
            headers = {'content_type': 'application/json'}
125
            return response_status, headers, json.dumps(resp_body)
126
127
        responses.add_callback(
128
            responses.GET,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
129
            re.compile(r'^({0}).+'.format(base_url)),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable base_url does not seem to be defined.
Loading history...
130
            callback=callback)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable callback does not seem to be defined.
Loading history...
131
        return base_url
132
133
    def mock_geozoekdiensten_get_gemeente_response(self, len_results, base_url='http://geozoekdienst.en'):
134
        def callback(request):
135
            self.geozoekdienst_request = request
136
            if len_results == 2:
137
                resp_body = get_gemeente_results
138
            elif len_results == 1:
139
                resp_body = [{'naam': 'gemeente', 'id': 'niscode'}]
140
            else:
141
                resp_body = []
142
            headers = {'content_type': 'application/json'}
143
            return 200, headers, json.dumps(resp_body)
144
145
        responses.add_callback(
146
            responses.GET,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
147
            re.compile(r'^({0}).+'.format(base_url)),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable base_url does not seem to be defined.
Loading history...
148
            callback=callback)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable callback does not seem to be defined.
Loading history...
149
        return base_url
150
151
    @responses.activate
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
152
    def test_admin_grenzen_client(self):
153
        base_url = self.mock_geozoekdiensten_response()
154
        gemeenten = AdminGrenzenClient(base_url).get_gemeenten(self.geojson_valid)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
155
        self.assertIsInstance(gemeenten, list)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable list does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gemeenten does not seem to be defined.
Loading history...
156
        self.assertGreater(len(gemeenten), 0)
157
158
    @responses.activate
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
159
    def test_admin_grenzen_client_outside_flanders(self):
160
        base_url = self.mock_geozoekdiensten_response()
161
        gemeenten = AdminGrenzenClient(base_url).get_gemeenten(testdata.test_json_outside_flanders)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
162
        self.assertIsInstance(gemeenten, list)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable list does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gemeenten does not seem to be defined.
Loading history...
163
        self.assertEqual(len(gemeenten), 0)
164
165
    @responses.activate
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
166
    def test_admin_grenzen_client_raise_service_error(self):
167
        base_url = self.mock_geozoekdiensten_response(response_status=500)
168
        with self.assertRaises(Exception) as ex:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Exception does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable ex does not seem to be defined.
Loading history...
169
            AdminGrenzenClient(base_url).get_gemeenten(self.geojson_valid)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
170
171
    @responses.activate
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
172
    def test_get_gemeente_2(self):
173
        base_url = self.mock_geozoekdiensten_get_gemeente_response(len_results=2)
174
        gemeente = AdminGrenzenClient(base_url).get_gemeente(testdata.test_geojson_mulipolygon)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
175
        self.assertDictEqual({'naam': 'Antwerpen', 'niscode': '11002'}, gemeente)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gemeente does not seem to be defined.
Loading history...
176
177
    @responses.activate
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
178
    def test_get_gemeente_1(self):
179
        base_url = self.mock_geozoekdiensten_get_gemeente_response(len_results=1)
180
        gemeente = AdminGrenzenClient(base_url).get_gemeente(testdata.test_geojson_mulipolygon)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
181
        self.assertDictEqual({'naam': 'gemeente', 'niscode': 'niscode'}, gemeente)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gemeente does not seem to be defined.
Loading history...
182
183
    @responses.activate
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
184
    def test_get_gemeente_0(self):
185
        base_url = self.mock_geozoekdiensten_get_gemeente_response(len_results=0)
186
        gemeente = AdminGrenzenClient(base_url).get_gemeente(testdata.test_geojson_mulipolygon)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
187
        self.assertIsNone(gemeente)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gemeente does not seem to be defined.
Loading history...
188
189
    @responses.activate
190
    def test_get_provincie(self):
191
        base_url = 'http://geozoekdienst.en/provincies'
192
        responses.add(responses.GET, base_url, body=json.dumps(get_provincie_results))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable base_url does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable get_provincie_results does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
193
        provincie = AdminGrenzenClient(base_url).get_provincie(testdata.test_geojson_mulipolygon)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
194
        self.assertDictEqual({'naam': 'Antwerpen', 'niscode': '10000'}, provincie)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable provincie does not seem to be defined.
Loading history...
195
196
    @responses.activate
197
    def test_get_provincies(self):
198
        base_url = 'http://geozoekdienst.en/provincies'
199
        responses.add(responses.GET, base_url, body=json.dumps(get_provincie_results))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable get_provincie_results does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable responses does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable base_url does not seem to be defined.
Loading history...
200
        provincies = AdminGrenzenClient(base_url).get_provincies(testdata.test_geojson_mulipolygon)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable testdata does not seem to be defined.
Loading history...
201
        self.assertListEqual(['Antwerpen', 'Vlaams Brabant'], provincies)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable provincies does not seem to be defined.
Loading history...
202