Completed
Push — master ( d6f19a...70f8b2 )
by Koen
01:57
created

TestGemeente   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 62
Duplicated Lines 0 %
Metric Value
dl 0
loc 62
rs 10
wmc 23
1
# -*- coding: utf-8 -*-
2
3
try:
4
    import unittest2 as unittest
5
except ImportError:  # pragma NO COVER
6
    import unittest  # noqa
7
import pytest
8
9
from crabpy.client import (
10
    capakey_factory
11
)
12
13
from crabpy.gateway.exception import (
14
    GatewayResourceNotFoundException
15
)
16
17
from crabpy.gateway.capakey import (
18
    CapakeyGateway,
19
    Gemeente,
20
    Afdeling,
21
    Sectie,
22
    Perceel
23
)
24
25
from tests import (
26
    run_capakey_integration_tests,
27
    config
28
)
29
30
@pytest.mark.skipif(
31
    not pytest.config.getoption('--capakey-integration'),
32
    reason = 'No CAPAKEY Integration tests required'
33
)
34
class TestCapakeyGateway:
35
36
    def test_list_gemeenten(self, capakey_gateway):
37
        res = capakey_gateway.list_gemeenten()
38
        assert isinstance(res, list)
39
40
    def test_list_gemeenten_invalid_auth(self):
41
        capakey_gateway_client = capakey_factory(
42
            user='USER',
43
            password='PASSWORD'
44
        )
45
        capakey_gateway = CapakeyGateway(
46
            capakey_gateway_client
47
        )
48
        from crabpy.gateway.exception import GatewayAuthenticationException
49
        with pytest.raises(GatewayAuthenticationException):
50
            capakey_gateway.list_gemeenten()
51
52
    def test_get_gemeente_by_id(self, capakey_gateway):
53
        res = capakey_gateway.get_gemeente_by_id(44021)
54
        assert isinstance(res, Gemeente)
55
        assert res.id == 44021
56
57
    def test_get_gemeente_by_invalid_id(self, capakey_gateway):
58
        with pytest.raises(GatewayResourceNotFoundException):
59
            capakey_gateway.get_gemeente_by_id('gent')
60
61
    def test_list_afdelingen(self, capakey_gateway):
62
        res = capakey_gateway.list_kadastrale_afdelingen()
63
        assert isinstance(res, list)
64
        assert len(res) > 300
65
66
    def test_list_afdelingen_by_gemeente(self, capakey_gateway):
67
        g = capakey_gateway.get_gemeente_by_id(44021)
68
        res = capakey_gateway.list_kadastrale_afdelingen_by_gemeente(g)
69
        assert isinstance(res, list)
70
        assert len(res) > 0
71
        assert len(res) < 40
72
73
    def test_list_afdelingen_by_unexisting_gemeente(self, capakey_gateway):
74
        g = Gemeente(99999)
75
        res = capakey_gateway.list_kadastrale_afdelingen_by_gemeente(g)
76
        assert isinstance(res, list)
77
        assert len(res) == 0
78
79
    def test_list_afdelingen_by_gemeente_id(self, capakey_gateway):
80
        res = capakey_gateway.list_kadastrale_afdelingen_by_gemeente(44021)
81
        assert isinstance(res, list)
82
        assert len(res) > 0
83
        assert len(res) < 40
84
85
    def test_get_kadastrale_afdeling_by_id(self, capakey_gateway):
86
        res = capakey_gateway.get_kadastrale_afdeling_by_id(44021)
87
        assert isinstance(res, Afdeling)
88
        assert res.id == 44021
89
        assert isinstance(res.gemeente, Gemeente)
90
        assert res.gemeente.id == 44021
91
92
    def test_get_kadastrale_afdeling_by_unexisting_id(self, capakey_gateway):
93
        with pytest.raises(GatewayResourceNotFoundException):
94
            capakey_gateway.get_kadastrale_afdeling_by_id(44000)
95
96
    def test_list_secties_by_afdeling(self, capakey_gateway):
97
        a = capakey_gateway.get_kadastrale_afdeling_by_id(44021)
98
        res = capakey_gateway.list_secties_by_afdeling(a)
99
        assert isinstance(res, list)
100
        assert len(res) ==  1
101
102
    def test_list_secties_by_unexisting_afdeling(self, capakey_gateway):
103
        a = Afdeling(99000)
104
        res = capakey_gateway.list_secties_by_afdeling(a)
105
        assert isinstance(res, list)
106
        assert len(res) ==  0
107
108
    def test_list_secties_by_afdeling_id(self, capakey_gateway):
109
        res = capakey_gateway.list_secties_by_afdeling(44021)
110
        assert isinstance(res, list)
111
        assert len(res) == 1
112
113
    def test_get_sectie_by_id_and_afdeling(self, capakey_gateway):
114
        a = capakey_gateway.get_kadastrale_afdeling_by_id(44021)
115
        res = capakey_gateway.get_sectie_by_id_and_afdeling('A', a)
116
        assert isinstance(res, Sectie)
117
        assert res.id == 'A'
118
        assert res.afdeling.id == 44021
119
120
    def test_get_sectie_by_unexisting_id_and_afdeling(self, capakey_gateway):
121
        a = capakey_gateway.get_kadastrale_afdeling_by_id(44021)
122
        with pytest.raises(GatewayResourceNotFoundException):
123
            capakey_gateway.get_sectie_by_id_and_afdeling('Z', a)
124
125
    def test_list_percelen_by_sectie(self, capakey_gateway):
126
        s = capakey_gateway.get_sectie_by_id_and_afdeling('A', 44021)
127
        res = capakey_gateway.list_percelen_by_sectie(s)
128
        assert isinstance(res, list)
129
        assert len(res) > 0
130
131
    def test_list_percelen_by_unexisting_sectie(self, capakey_gateway):
132
        s = Sectie('Z', Afdeling(99000))
133
        res = capakey_gateway.list_percelen_by_sectie(s)
134
        assert isinstance(res, list)
135
        assert len(res) ==  0
136
137
    def test_get_perceel_by_id_and_sectie(self, capakey_gateway):
138
        s = capakey_gateway.get_sectie_by_id_and_afdeling('A', 44021)
139
        percelen = capakey_gateway.list_percelen_by_sectie(s)
140
        perc = percelen[0]
141
        res = capakey_gateway.get_perceel_by_id_and_sectie(perc.id, s)
142
        assert isinstance(res, Perceel)
143
        assert res.sectie.id == 'A'
144
        assert res.sectie.afdeling.id == 44021
145
146
    def test_get_perceel_by_unexisting_id_and_sectie(self, capakey_gateway):
147
        s = capakey_gateway.get_sectie_by_id_and_afdeling('A', 44021)
148
        percelen = capakey_gateway.list_percelen_by_sectie(s)
149
        perc = percelen[0]
150
        with pytest.raises(GatewayResourceNotFoundException):
151
            capakey_gateway.get_perceel_by_id_and_sectie(-1, s)
152
153
    def test_get_perceel_by_capakey(self, capakey_gateway):
154
        s = capakey_gateway.get_sectie_by_id_and_afdeling('A', 44021)
155
        percelen = capakey_gateway.list_percelen_by_sectie(s)
156
        perc = percelen[0]
157
        res = capakey_gateway.get_perceel_by_capakey(perc.capakey)
158
        assert isinstance(res, Perceel)
159
        assert res.sectie.id == 'A'
160
        assert res.sectie.afdeling.id == 44021
161
162
    def test_get_perceel_by_unexisting_capakey(self, capakey_gateway):
163
        with pytest.raises(GatewayResourceNotFoundException):
164
            capakey_gateway.get_perceel_by_capakey('0000000')
165
166
    def test_get_perceel_by_percid(self, capakey_gateway):
167
        s = capakey_gateway.get_sectie_by_id_and_afdeling('A', 44021)
168
        percelen = capakey_gateway.list_percelen_by_sectie(s)
169
        perc = percelen[0]
170
        res = capakey_gateway.get_perceel_by_percid(perc.percid)
171
        assert isinstance(res, Perceel)
172
        assert res.sectie.id == 'A'
173
        assert res.sectie.afdeling.id == 44021
174
175
    def test_get_perceel_by_unexisting_percid(self, capakey_gateway):
176
        with pytest.raises(GatewayResourceNotFoundException):
177
            capakey_gateway.get_perceel_by_percid('0000/0000')
178
179
180
class TestGemeente:
181
182
    def test_fully_initialised(self):
183
        g = Gemeente(
184
            44021,
185
            'Gent',
186
            (104154.2225, 197300.703),
187
            (94653.453, 185680.984, 113654.992, 208920.422)
188
        )
189
        assert (g.id, 44021)
190
        assert (g.naam, 'Gent')
191
        assert (g.centroid, (104154.2225, 197300.703))
192
        assert (
193
            g.bounding_box,
194
            (94653.453, 185680.984, 113654.992, 208920.422)
195
        )
196
        assert ('Gent (44021)', str(g))
197
        assert ("Gemeente(44021, 'Gent')", repr(g))
198
199
    def test_str_and_repr_dont_lazy_load(self):
200
        g = Gemeente(44021, 'Gent')
201
        assert ('Gent (44021)', str(g))
202
        assert ("Gemeente(44021, 'Gent')", repr(g))
203
204
    def test_check_gateway_not_set(self):
205
        g = Gemeente(44021, 'Gent')
206
        with pytest.raises(RuntimeError):
207
            g.check_gateway()
208
209
    @pytest.mark.skipif(
210
        not pytest.config.getoption('--capakey-integration'),
211
        reason = 'No CAPAKEY Integration tests required'
212
    )
213
    def test_lazy_load(self, capakey_gateway):
214
        g = Gemeente(44021, 'Gent', gateway=capakey_gateway)
215
        g.clear_gateway()
216
        with pytest.raises(RuntimeError):
217
            g.check_gateway()
218
219
    @pytest.mark.skipif(
220
        not pytest.config.getoption('--capakey-integration'),
221
        reason = 'No CAPAKEY Integration tests required'
222
    )
223
    def test_lazy_load(self, capakey_gateway):
224
        g = Gemeente(44021, 'Gent')
225
        g.set_gateway(capakey_gateway)
226
        assert g.id == 44021
227
        assert g.naam == 'Gent'
228
        assert not g.centroid == None
229
        assert not g.bounding_box == None
230
231
    @pytest.mark.skipif(
232
        not pytest.config.getoption('--capakey-integration'),
233
        reason = 'No CAPAKEY Integration tests required'
234
    )
235
    def test_afdelingen(self, capakey_gateway):
236
        g = Gemeente(44021, 'Gent')
237
        g.set_gateway(capakey_gateway)
238
        afdelingen = g.afdelingen
239
        assert isinstance(afdelingen, list)
240
        assert len(afdelingen) > 0
241
        assert len(afdelingen) < 40
242
243
244
class TestAfdeling:
245
246
    def test_fully_initialised(self):
247
        a = Afdeling(
248
            44021,
249
            'GENT  1 AFD',
250
            Gemeente(44021, 'Gent'),
251
            (104893.06375, 196022.244094),
252
            (104002.076625, 194168.3415, 105784.050875, 197876.146688)
253
        )
254
        assert a.id == 44021
255
        assert a.naam == 'GENT  1 AFD'
256
        assert a.centroid == (104893.06375, 196022.244094)
257
        assert a.bounding_box == (104002.076625, 194168.3415, 105784.050875, 197876.146688)
258
        assert 'GENT  1 AFD (44021)' == str(a)
259
        assert "Afdeling(44021, 'GENT  1 AFD')" == repr(a)
260
261
    def test_partially_initialised(self):
262
        a = Afdeling(
263
            44021,
264
            'GENT  1 AFD',
265
            Gemeente(44021, 'Gent'),
266
        )
267
        assert a.id ==44021
268
        assert a.naam == 'GENT  1 AFD'
269
        assert 'GENT  1 AFD (44021)' == str(a)
270
        assert "Afdeling(44021, 'GENT  1 AFD')" == repr(a)
271
272
    def test_to_string_not_fully_initialised(self):
273
        a = Afdeling(
274
            44021
275
        )
276
        assert 'Afdeling 44021' == str(a)
277
278
    def test_check_gateway_not_set(self):
279
        a = Afdeling(44021)
280
        with pytest.raises(RuntimeError):
281
            a.check_gateway()
282
283
    @pytest.mark.skipif(
284
        not pytest.config.getoption('--capakey-integration'),
285
        reason = 'No CAPAKEY Integration tests required'
286
    )
287
    def test_lazy_load(self, capakey_gateway):
288
        a = Afdeling(44021)
289
        a.set_gateway(capakey_gateway)
290
        assert a.id == 44021
291
        assert a.naam == 'GENT  1 AFD'
292
        assert not a.centroid == None
293
        assert not a.bounding_box == None
294
295
    @pytest.mark.skipif(
296
        not pytest.config.getoption('--capakey-integration'),
297
        reason = 'No CAPAKEY Integration tests required'
298
    )
299
    def test_secties(self, capakey_gateway):
300
        a = Afdeling(44021)
301
        a.set_gateway(capakey_gateway)
302
        secties = a.secties
303
        assert isinstance(secties, list)
304
        assert len(secties) == 1
305
306
307
class TestSectie:
308
309
    def test_fully_initialised(self):
310
        s = Sectie(
311
            'A',
312
            Afdeling(44021, 'Gent  1 AFD'),
313
            (104893.06375, 196022.244094),
314
            (104002.076625, 194168.3415, 105784.050875, 197876.146688)
315
        )
316
        assert s.id == 'A'
317
        assert s.centroid == (104893.06375, 196022.244094)
318
        assert s.bounding_box == (104002.076625, 194168.3415, 105784.050875, 197876.146688)
319
        assert ('Gent  1 AFD (44021), Sectie A', str(s))
320
        assert "Sectie('A', Afdeling(44021, 'Gent  1 AFD'))" == repr(s)
321
322
    def test_check_gateway_not_set(self):
323
        s = Sectie('A', Afdeling(44021))
324
        with pytest.raises(RuntimeError):
325
            s.check_gateway()
326
327
    def test_clear_gateway(self, capakey_gateway):
328
        s = Sectie('A', Afdeling(44021))
329
        s.set_gateway(capakey_gateway)
330
        s.check_gateway()
331
        s.clear_gateway()
332
        with pytest.raises(RuntimeError):
333
            s.check_gateway()
334
335
    @pytest.mark.skipif(
336
        not pytest.config.getoption('--capakey-integration'),
337
        reason = 'No CAPAKEY Integration tests required'
338
    )
339
    def test_lazy_load(self, capakey_gateway):
340
        s = Sectie(
341
            'A',
342
            Afdeling(44021)
343
        )
344
        s.set_gateway(capakey_gateway)
345
        assert s.id == 'A'
346
        assert s.afdeling.id == 44021
347
        assert not s.centroid == None
348
        assert not s.bounding_box == None
349
350
    @pytest.mark.skipif(
351
        not pytest.config.getoption('--capakey-integration'),
352
        reason = 'No CAPAKEY Integration tests required'
353
    )
354
    def test_percelen(self, capakey_gateway):
355
        s = Sectie(
356
            'A',
357
            Afdeling(44021)
358
        )
359
        s.set_gateway(capakey_gateway)
360
        percelen = s.percelen
361
        assert isinstance(percelen, list)
362
        assert len(percelen) > 0
363
364
365
class TestPerceel:
366
367
    def test_fully_initialised(self):
368
        p = Perceel(
369
            '1154/02C000', Sectie('A', Afdeling(46013)),
370
            '40613A1154/02C000', '40613_A_1154_C_000_02',
371
            'capaty', 'cashkey',
372
            (104893.06375, 196022.244094),
373
            (104002.076625, 194168.3415, 105784.050875, 197876.146688)
374
        )
375
        assert p.id == ('1154/02C000')
376
        assert p.sectie.id == 'A'
377
        assert p.capatype == 'capaty'
378
        assert p.cashkey == 'cashkey'
379
        assert p.centroid == (104893.06375, 196022.244094)
380
        assert p.bounding_box == (104002.076625, 194168.3415, 105784.050875, 197876.146688)
381
        assert p.capakey == str(p)
382
        assert "Perceel('1154/02C000', Sectie('A', Afdeling(46013)), '40613A1154/02C000', '40613_A_1154_C_000_02')" == repr(p)
383
384
    def test_check_gateway_not_set(self):
385
        p = Perceel(
386
            '1154/02C000', Sectie('A', Afdeling(46013)),
387
            '40613A1154/02C000', '40613_A_1154_C_000_02'
388
        )
389
        with pytest.raises(RuntimeError):
390
            p.check_gateway()
391
392
    def test_clear_gateway(self, capakey_gateway):
393
        p = Perceel(
394
            '1154/02C000', Sectie('A', Afdeling(46013)),
395
            '40613A1154/02C000', '40613_A_1154_C_000_02'
396
        )
397
        p.set_gateway(capakey_gateway)
398
        p.check_gateway()
399
        p.sectie.check_gateway()
400
        p.clear_gateway()
401
        with pytest.raises(RuntimeError):
402
            p.sectie.check_gateway()
403
            p.check_gateway()
404
405
406
    @pytest.mark.skipif(
407
        not pytest.config.getoption('--capakey-integration'),
408
        reason = 'No CAPAKEY Integration tests required'
409
    )
410
    def test_lazy_load(self, capakey_gateway):
411
        p = Perceel(
412
            '1154/02C000', Sectie('A', Afdeling(46013)),
413
            '46013A1154/02C000', '46013_A_1154_C_000_02',
414
            gateway=capakey_gateway
415
        )
416
        assert p.id == '1154/02C000'
417
        assert p.sectie.id == 'A'
418
        assert p.sectie.afdeling.id == 46013
419
        assert not p.capatype == None
420
        assert not p.cashkey == None
421
        assert not p.centroid == None
422
        assert not p.bounding_box == None
423
424
    def test_parse_capakey(self):
425
        p = Perceel(
426
            '1154/02C000', Sectie('A', Afdeling(46013)),
427
            '46013A1154/02C000', '46013_A_1154_C_000_02'
428
        )
429
        assert p.grondnummer == '1154'
430
        assert p.bisnummer == '02'
431
        assert p.exponent == 'C'
432
        assert p.macht == '000'
433
434
    def test_parse_capakey_other_sectie(self):
435
        p = Perceel(
436
            '1154/02C000', Sectie('F', Afdeling(46013)),
437
            '46013F1154/02C000', '46013_F_1154_C_000_02'
438
        )
439
        assert p.grondnummer == '1154'
440
        assert p.bisnummer == '02'
441
        assert p.exponent == 'C'
442
        assert p.macht =='000'
443
444
    def test_parse_invalid_capakey(self):
445
        with pytest.raises(ValueError):
446
            Perceel(
447
                '1154/02C000', Sectie('A', Afdeling(46013)),
448
                '46013_A_1154_C_000_02',
449
                '46013A1154/02C000',
450
            )
451