HttpCachingFunctionalTests   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A test_list_gewesten() 0 4 1
A test_http_304_res() 0 6 1
1
# -*- coding: utf-8 -*-
2
'''
3
Functional tests.
4
5
.. versionadded:: 0.1.0
6
'''
7
import unittest
8
import os
9
import shutil
10
11
from pyramid import testing
12
from webtest import TestApp
13
14
from crabpy_pyramid import main
15
16
17
def as_bool(value):
18
    '''
19
    Cast a textual value from a config file to a boolean.
20
    Will convert 'true', 'True', '1', 't', 'T' or 'Yes' to `True`. All other
21
    values are considered to be `False`.
22
    '''
23
    return value in ['true', 'True', '1', 't', 'T', 'Yes']
24
25
26
def run_capakey_integration_tests():
27
    from testconfig import config
28
    try:
29
        return as_bool(config['capakey']['run_integration_tests'])
30
    except KeyError:  # pragma NO COVER
31
        return False
32
33
34
def run_crab_integration_tests():
35
    from testconfig import config
36
    try:
37
        return as_bool(config['crab']['run_integration_tests'])
38
    except KeyError:  # pragma NO COVER
39
        return False
40
41
settings = {
42
    'crabpy.cache.file.root': os.path.join(os.path.dirname(__file__), 'dogpile_data'),
43
    'crabpy.capakey.include': True,
44
    'crabpy.capakey.cache_config.permanent.backend': 'dogpile.cache.dbm',
45
    'crabpy.capakey.cache_config.permanent.expiration_time': 604800,
46
    'crabpy.capakey.cache_config.permanent.arguments.filename': os.path.join(os.path.dirname(__file__), 'dogpile_data', 'capakey_permanent.dbm'),
47
    'crabpy.capakey.cache_config.long.backend': 'dogpile.cache.dbm',
48
    'crabpy.capakey.cache_config.long.expiration_time': 86400,
49
    'crabpy.capakey.cache_config.long.arguments.filename': os.path.join(os.path.dirname(__file__), 'dogpile_data', 'capakey_long.dbm'),
50
    'crabpy.capakey.cache_config.short.backend': 'dogpile.cache.dbm',
51
    'crabpy.capakey.cache_config.short.expiration_time': 3600,
52
    'crabpy.capakey.cache_config.short.arguments.filename': os.path.join(os.path.dirname(__file__), 'dogpile_data', 'capakey_short.dbm'),
53
    'crabpy.crab.include': True,
54
    'crabpy.crab.cache_config.permanent.backend': 'dogpile.cache.dbm',
55
    'crabpy.crab.cache_config.permanent.expiration_time': 604800,
56
    'crabpy.crab.cache_config.permanent.arguments.filename': os.path.join(os.path.dirname(__file__), 'dogpile_data', 'crab_permanent.dbm'),
57
    'crabpy.crab.cache_config.long.backend': 'dogpile.cache.dbm',
58
    'crabpy.crab.cache_config.long.expiration_time': 86400,
59
    'crabpy.crab.cache_config.long.arguments.filename': os.path.join(os.path.dirname(__file__), 'dogpile_data', 'crab_long.dbm'),
60
    'crabpy.crab.cache_config.short.backend': 'dogpile.cache.dbm',
61
    'crabpy.crab.cache_config.short.expiration_time': 3600,
62
    'crabpy.crab.cache_config.short.arguments.filename': os.path.join(os.path.dirname(__file__), 'dogpile_data', 'crab_short.dbm'),
63
}
64
65
66
def setUpModule():
67
    shutil.rmtree(
68
        os.path.join(os.path.dirname(__file__), 'dogpile_data'),
69
        True
70
    )
71
72
73
class FunctionalTests(unittest.TestCase):
74
    @classmethod
75
    def setUpClass(cls):
76
        cls.app = main({}, **settings)
77
78
    def setUp(self):
79
        self.testapp = TestApp(self.app)
80
        self.config = testing.setUp()
81
82
    def tearDown(self):
83
        testing.tearDown()
84
85
86
@unittest.skipUnless(
87
    run_capakey_integration_tests(),
88
    'No CAPAKEY Integration tests required'
89
)
90
class CapakeyFunctionalTests(FunctionalTests):
91
    def test_list_gemeenten(self):
92
        res = self.testapp.get('/capakey/gemeenten')
93
        self.assertEqual('200 OK', res.status)
94
95
    def test_get_gemeente_by_id(self):
96
        res = self.testapp.get('/capakey/gemeenten/11001')
97
        self.assertEqual('200 OK', res.status)
98
99
    def test_get_gemeente_by_unexisting_id(self):
100
        res = self.testapp.get('/capakey/gemeenten/1100', status=404)
101
        self.assertEqual('404 Not Found', res.status)
102
103
    def test_list_kadastrale_afdelingen_by_gemeente(self):
104
        res = self.testapp.get('/capakey/gemeenten/11001/afdelingen')
105
        self.assertEqual('200 OK', res.status)
106
107
    def test_list_kadastrale_afdelingen(self):
108
        res = self.testapp.get('/capakey/afdelingen')
109
        self.assertEqual('200 OK', res.status)
110
111
    def test_get_kadastrale_afdeling_by_id(self):
112
        res = self.testapp.get('/capakey/afdelingen/11001')
113
        self.assertEqual('200 OK', res.status)
114
115
    def test_get_kadastrale_afdeling_by_unexisting_id(self):
116
        res = self.testapp.get('/capakey/afdelingen/99999', status=404)
117
        self.assertEqual('404 Not Found', res.status)
118
119
    def test_list_secties_by_afdeling(self):
120
        res = self.testapp.get('/capakey/afdelingen/11001/secties')
121
        self.assertEqual('200 OK', res.status)
122
123
    def test_get_sectie_by_id_and_afdeling(self):
124
        res = self.testapp.get('/capakey/afdelingen/11001/secties/B')
125
        self.assertEqual('200 OK', res.status)
126
127
    def test_get_sectie_by_unexisting_id_and_afdeling(self):
128
        res = self.testapp.get('/capakey/afdelingen/11001/secties/Z', status=404)
129
        self.assertEqual('404 Not Found', res.status)
130
131
    def test_list_percelen_by_sectie(self):
132
        res = self.testapp.get('/capakey/afdelingen/11001/secties/B/percelen')
133
        self.assertEqual('200 OK', res.status)
134
135
    def test_get_perceel_by_sectie_and_id(self):
136
        res = self.testapp.get('/capakey/afdelingen/11001/secties/B/percelen/0001/00S000')
137
        self.assertEqual('200 OK', res.status)
138
139
    def test_get_perceel_by_unexisting_sectie_and_id(self):
140
        res = self.testapp.get('/capakey/afdelingen/11001/secties/B/percelen/0000/00000', status=404)
141
        self.assertEqual('404 Not Found', res.status)
142
143
    def test_get_perceel_by_capakey(self):
144
        res = self.testapp.get('/capakey/percelen/11001B0001/00S000')
145
        self.assertEqual('200 OK', res.status)
146
147
    def test_get_perceel_by_unexisting_capakey(self):
148
        res = self.testapp.get('/capakey/percelen/99009X0009/00X000', status=404)
149
        self.assertEqual('404 Not Found', res.status)
150
151
    def test_get_perceel_by_percid(self):
152
        res = self.testapp.get('/capakey/percelen/11001_B_0001_S_000_00')
153
        self.assertEqual('200 OK', res.status)
154
155
    def test_get_perceel_by_unexisting_percid(self):
156
        res = self.testapp.get('/capakey/percelen/99009_X_0009_X_000_00', status=404)
157
        self.assertEqual('404 Not Found', res.status)
158
159
160
@unittest.skipUnless(
161
    run_crab_integration_tests(),
162
    'No CRAB Integration tests required'
163
)
164
class CrabFunctionalTests(FunctionalTests):
165
    def test_list_gewesten(self):
166
        res = self.testapp.get('/crab/gewesten')
167
        self.assertEqual('200 OK', res.status)
168
169
    def test_get_gewest_by_id(self):
170
        res = self.testapp.get('/crab/gewesten/2')
171
        self.assertEqual('200 OK', res.status)
172
173
    def test_get_gewest_by_unexisting_id(self):
174
        res = self.testapp.get('/crab/gewesten/0', status=404)
175
        self.assertEqual('404 Not Found', res.status)
176
177
    def test_list_provincies(self):
178
        res = self.testapp.get('/crab/gewesten/2/provincies')
179
        self.assertEqual('200 OK', res.status)
180
181
    def test_get_provincie_by_id(self):
182
        res = self.testapp.get('/crab/provincies/10000')
183
        self.assertEqual('200 OK', res.status)
184
185
    def test_get_provincie_by_unexisting_id(self):
186
        res = self.testapp.get('/crab/provincies/00000', status=404)
187
        self.assertEqual('404 Not Found', res.status)
188
189
    def test_list_gemeenten_by_provincie(self):
190
        res = self.testapp.get('/crab/provincies/10000/gemeenten')
191
        self.assertEqual('200 OK', res.status)
192
193
    def test_list_gemeenten_crab(self):
194
        res = self.testapp.get('/crab/gewesten/2/gemeenten')
195
        self.assertEqual('200 OK', res.status)
196
197
    def test_get_gemeente_crab_niscode(self):
198
        res = self.testapp.get('/crab/gemeenten/11001')
199
        self.assertEqual('200 OK', res.status)
200
201
    def test_get_gemeente_crab_unexisting_niscode(self):
202
        res = self.testapp.get('/crab/gemeenten/00000', status=404)
203
        self.assertEqual('404 Not Found', res.status)
204
205
    def test_get_gemeente_crab_id(self):
206
        res = self.testapp.get('/crab/gemeenten/1')
207
        self.assertEqual('200 OK', res.status)
208
209
    def test_get_gemeente_crab_unexisting_id(self):
210
        res = self.testapp.get('/crab/gemeenten/0', status=404)
211
        self.assertEqual('404 Not Found', res.status)
212
213
    def test_list_deelgemeenten(self):
214
        res = self.testapp.get('/crab/gewesten/2/deelgemeenten')
215
        self.assertEqual('200 OK', res.status)
216
217
    def test_list_deelgemeenten_wrong_gewest(self):
218
        res = self.testapp.get('/crab/gewesten/1/deelgemeenten', status=404)
219
        self.assertEqual('404 Not Found', res.status)
220
221
    def test_list_deelgemeenten_by_gemeente(self):
222
        res = self.testapp.get('/crab/gemeenten/11001/deelgemeenten')
223
        self.assertEqual('200 OK', res.status)
224
225
    def test_list_deelgemeenten_by_unexisting_gemeente(self):
226
        res = self.testapp.get('/crab/gemeenten/99999/deelgemeenten', status=404)
227
        self.assertEqual('404 Not Found', res.status)
228
        res = self.testapp.get('/crab/gemeenten/9999/deelgemeenten', status=404)
229
        self.assertEqual('404 Not Found', res.status)
230
231
    def test_get_deelgemeente_by_id(self):
232
        res = self.testapp.get('/crab/deelgemeenten/45062A')
233
        self.assertEqual('200 OK', res.status)
234
235
    def test_list_straten(self):
236
        res = self.testapp.get('/crab/gemeenten/11001/straten')
237
        self.assertEqual('200 OK', res.status)
238
239
    def test_get_straat_by_id(self):
240
        res = self.testapp.get('/crab/straten/1')
241
        self.assertEqual('200 OK', res.status)
242
243
    def test_get_straat_by_unexisting_id(self):
244
        res = self.testapp.get('/crab/straten/0', status=404)
245
        self.assertEqual('404 Not Found', res.status)
246
247
    def test_list_huisnummers(self):
248
        res = self.testapp.get('/crab/straten/1/huisnummers')
249
        self.assertEqual('200 OK', res.status)
250
251
    def test_get_huisnummer_by_straat_and_label(self):
252
        res = self.testapp.get('/crab/straten/1/huisnummers/3')
253
        self.assertEqual('200 OK', res.status)
254
255
    def test_get_huisnummer_by_unexisting_straat_and_label(self):
256
        res = self.testapp.get('/crab/straten/1/huisnummers/0', status=404)
257
        self.assertEqual('404 Not Found', res.status)
258
259
    def test_get_huisnummer_by_id(self):
260
        res = self.testapp.get('/crab/huisnummers/1')
261
        self.assertEqual('200 OK', res.status)
262
263
    def test_get_huisnummer_by_unexisting_id(self):
264
        res = self.testapp.get('/crab/huisnummers/0', status=404)
265
        self.assertEqual('404 Not Found', res.status)
266
267
    def test_list_percelen(self):
268
        res = self.testapp.get('/crab/huisnummers/1/percelen')
269
        self.assertEqual('200 OK', res.status)
270
271
    def test_get_perceel_by_id(self):
272
        res = self.testapp.get('/crab/percelen/31433D0011/00T016')
273
        self.assertEqual('200 OK', res.status)
274
275
    def test_get_perceel_by_unexisting_id(self):
276
        res = self.testapp.get('/crab/percelen/31433D0011/000000', status=404)
277
        self.assertEqual('404 Not Found', res.status)
278
279
    def test_list_huisnummers_by_perceel(self):
280
        res = self.testapp.get('/crab/percelen/31433D0011/00T016/huisnummers')
281
        self.assertEqual('200 OK', res.status)
282
283
    def test_list_huisnummers_by_unexisting_perceel(self):
284
        res = self.testapp.get('/crab/percelen/31433D0011/000000/huisnummers', status=404)
285
        self.assertEqual('404 Not Found', res.status)
286
287
    def test_list_gebouwen(self):
288
        res = self.testapp.get('/crab/huisnummers/1/gebouwen')
289
        self.assertEqual('200 OK', res.status)
290
291
    def test_get_gebouw_by_id(self):
292
        res = self.testapp.get('/crab/gebouwen/1538575')
293
        self.assertEqual('200 OK', res.status)
294
295
    def test_get_gebouw_by_unexisting_id(self):
296
        res = self.testapp.get('/crab/gebouwen/99999999', status=404)
297
        self.assertEqual('404 Not Found', res.status)
298
299
    def test_get_wegobject(self):
300
        res = self.testapp.get('/crab/wegobjecten/53694755')
301
        self.assertEqual('200 OK', res.status)
302
303
    def test_get_unexisting_wegobject(self):
304
        res = self.testapp.get('/crab/wegobjecten/00000000', status=404)
305
        self.assertEqual('404 Not Found', res.status)
306
307
    def test_list_subadressen(self):
308
        res = self.testapp.get('/crab/huisnummers/129462/subadressen')
309
        self.assertEqual('200 OK', res.status)
310
311
    def test_get_subadressen_by_id(self):
312
        res = self.testapp.get('/crab/subadressen/1120934')
313
        self.assertEqual('200 OK', res.status)
314
315
    def test_get_subadressen_by_unexisting_id(self):
316
        res = self.testapp.get('/crab/subadressen/0000000', status=404)
317
        self.assertEqual('404 Not Found', res.status)
318
319
    def test_list_postkantons_by_gemeente(self):
320
        res = self.testapp.get('/crab/gemeenten/90/postkantons')
321
        self.assertEqual('200 OK', res.status)
322
323
    def test_list_adresposities_by_huisnummer(self):
324
        res = self.testapp.get('/crab/huisnummers/145/adresposities')
325
        self.assertEqual('200 OK', res.status)
326
327
    def test_list_adresposities_by_subadres(self):
328
        res = self.testapp.get('/crab/subadressen/145/adresposities')
329
        self.assertEqual('200 OK', res.status)
330
331
    def test_get_adrespositie_by_id(self):
332
        res = self.testapp.get('/crab/adresposities/137')
333
        self.assertEqual('200 OK', res.status)
334
335
    def test_get_adrespositie_by_unexisting_id(self):
336
        res = self.testapp.get('/crab/adresposities/0', status=404)
337
        self.assertEqual('404 Not Found', res.status)
338
339
    def test_list_landen(self):
340
        res = self.testapp.get('/crab/landen')
341
        self.assertEqual('200 OK', res.status)
342
343
    def test_get_land_by_id(self):
344
        res = self.testapp.get('/crab/landen/BE')
345
        self.assertEqual('200 OK', res.status)
346
347
    def test_get_land_by_unexisting_id(self):
348
        res = self.testapp.get('/crab/landen/MORDOR', status=404)
349
        self.assertEqual('404 Not Found', res.status)
350
351
@unittest.skipUnless(
352
    run_crab_integration_tests(),
353
    'No CRAB Integration tests required'
354
)
355
class HttpCachingFunctionalTests(FunctionalTests):
356
    def test_list_gewesten(self):
357
        res = self.testapp.get('/crab/gewesten')
358
        self.assertEqual('200 OK', res.status)
359
        self.assertIn('ETag', res.headers)
360
361
    def test_http_304_res(self):
362
        res = self.testapp.get('/crab/gewesten')
363
        self.assertEqual('200 OK', res.status)
364
        etag = res.headers['Etag']
365
        res2 = self.testapp.get('/crab/gewesten', headers={'If-None-Match': etag})
366
        self.assertEqual('304 Not Modified', res2.status)
367