From 80358645410c8360d43d1939f1a35516b0cc5a65 Mon Sep 17 00:00:00 2001 From: TalissaJoly Date: Thu, 21 May 2015 09:36:53 +0200 Subject: [PATCH 1/3] handling unexisting objects crab refs #25 --- crabpy_pyramid/tests/test_functional.py | 61 ++++++++++++++++++++++- crabpy_pyramid/views/crab.py | 64 +++++++++++++++++++------ 2 files changed, 109 insertions(+), 16 deletions(-) diff --git a/crabpy_pyramid/tests/test_functional.py b/crabpy_pyramid/tests/test_functional.py index 55d2e33..b90d08d 100644 --- a/crabpy_pyramid/tests/test_functional.py +++ b/crabpy_pyramid/tests/test_functional.py @@ -16,9 +16,16 @@ from crabpy_pyramid import main +def as_bool(value): + ''' + Cast a textual value from a config file to a boolean. + Will convert 'true', 'True', '1', 't', 'T' or 'Yes' to `True`. All other + values are considered to be `False`. + ''' + return value in ['true', 'True', '1', 't', 'T', 'Yes'] + def run_capakey_integration_tests(): from testconfig import config - from crabpy.tests import as_bool try: return as_bool(config['capakey']['run_integration_tests']) except KeyError: # pragma NO COVER @@ -27,7 +34,6 @@ def run_capakey_integration_tests(): def run_crab_integration_tests(): from testconfig import config - from crabpy.tests import as_bool try: return as_bool(config['crab']['run_integration_tests']) except KeyError: # pragma NO COVER @@ -127,6 +133,10 @@ def test_get_gewest_by_id(self): res = self.testapp.get('/crab/gewesten/2') self.assertEqual('200 OK', res.status) + def test_get_gewest_by_unexisting_id(self): + res = self.testapp.get('/crab/gewesten/0') + self.assertEqual('404 NotFound', res.status) + def test_list_provincies(self): res = self.testapp.get('/crab/gewesten/2/provincies') self.assertEqual('200 OK', res.status) @@ -135,6 +145,10 @@ def test_get_provincie_by_id(self): res = self.testapp.get('/crab/provincies/10000') self.assertEqual('200 OK', res.status) + def test_get_provincie_by_unexisting_id(self): + res = self.testapp.get('/crab/provincies/00000') + self.assertEqual('404 Not Found', res.status) + def test_list_gemeenten_by_provincie(self): res = self.testapp.get('/crab/provincies/10000/gemeenten') self.assertEqual('200 OK', res.status) @@ -147,9 +161,17 @@ def test_get_gemeente_crab_niscode(self): res = self.testapp.get('/crab/gemeenten/11001') self.assertEqual('200 OK', res.status) + def test_get_gemeente_crab_unexisting_niscode(self): + res = self.testapp.get('/crab/gemeenten/00000') + self.assertEqual('404 Not Found', res.status) + def test_get_gemeente_crab_id(self): res = self.testapp.get('/crab/gemeenten/1') self.assertEqual('200 OK', res.status) + + def test_get_gemeente_crab_unexisting_id(self): + res = self.testapp.get('/crab/gemeenten/0') + self.assertEqual('404 Not Found', res.status) def test_list_straten(self): res = self.testapp.get('/crab/gemeenten/11001/straten') @@ -159,6 +181,10 @@ def test_get_straat_by_id(self): res = self.testapp.get('/crab/straten/1') self.assertEqual('200 OK', res.status) + def test_get_straat_by_unexisting_id(self): + res = self.testapp.get('/crab/straten/0') + self.assertEqual('404 Not Found', res.status) + def test_list_huisnummers(self): res = self.testapp.get('/crab/straten/1/huisnummers') self.assertEqual('200 OK', res.status) @@ -166,11 +192,19 @@ def test_list_huisnummers(self): def test_get_huisnummer_by_straat_and_label(self): res = self.testapp.get('/crab/straten/1/huisnummers/3') self.assertEqual('200 OK', res.status) + + def test_get_huisnummer_by_unexisting_straat_and_label(self): + res = self.testapp.get('crab/straten/1/huisnummers/0') + self.assertEqual('404 Not Found', res.status) def test_get_huisnummer_by_id(self): res = self.testapp.get('/crab/huisnummers/1') self.assertEqual('200 OK', res.status) + def test_get_huisnummer_by_unexisting_id(self): + res = self.testapp.get('/crab/huisnummers/0') + self.assertEqual('404 Not Found', res.status) + def test_list_percelen(self): res = self.testapp.get('/crab/huisnummers/1/percelen') self.assertEqual('200 OK', res.status) @@ -179,6 +213,10 @@ def test_get_perceel_by_id(self): res = self.testapp.get('/crab/percelen/31433D0011/00T016') self.assertEqual('200 OK', res.status) + def test_get_perceel_by_unexisting_id(self): + res = self.testapp.get('crab/percelen/31433D0011/000000') + self.assertEqual('404 Not Found', res.status) + def test_list_gebouwen(self): res = self.testapp.get('/crab/huisnummers/1/gebouwen') self.assertEqual('200 OK', res.status) @@ -187,10 +225,17 @@ def test_get_gebouw_by_id(self): res = self.testapp.get('/crab/gebouwen/1538575') self.assertEqual('200 OK', res.status) + def test_get_gebouw_by_unexisting_id(self): + res = self.testapp.get('/crab/gebouwen/0') + self.assertEqual('404 Not Found', res.status) + def test_get_wegobject(self): res = self.testapp.get('/crab/wegobjecten/53694755') self.assertEqual('200 OK', res.status) + def test_get_unexisting_wegobject(self): + res = self.testapp.get('/crab/wegobjecten/00000000') + def test_list_subadressen(self): res = self.testapp.get('/crab/huisnummers/129462/subadressen') self.assertEqual('200 OK', res.status) @@ -199,6 +244,9 @@ def test_get_subadressen_by_id(self): res = self.testapp.get('/crab/subadressen/1120934') self.assertEqual('200 OK', res.status) + def test_get_subadressen_by_unexisting_id(self): + res = self.testapp.get('/crab/subadressen/0000000') + def test_list_postkantons_by_gemeente(self): res = self.testapp.get('/crab/gemeenten/90/postkantons') self.assertEqual('200 OK', res.status) @@ -215,9 +263,18 @@ def test_get_adrespositie_by_id(self): res = self.testapp.get('/crab/adresposities/137') self.assertEqual('200 OK', res.status) + def test_get_adrespositie_by_unexisting_id(self): + res = self.testapp.get('/crab/adresposities/0') + self.assertEqual('404 Not Found', res.status) + def test_list_landen(self): res = self.testapp.get('/crab/landen') self.assertEqual('200 OK', res.status) def test_get_land_by_id(self): res = self.testapp.get('/crab/landen/BE') + self.assertEqual('200 OK', res.status) + + def test_get_land_by_unexisting_id(self): + res = self.testapp.get('crab/landen/BDH') + self.assertEqual('404 Not Found', res.status) diff --git a/crabpy_pyramid/views/crab.py b/crabpy_pyramid/views/crab.py index 638ef60..4d53c54 100644 --- a/crabpy_pyramid/views/crab.py +++ b/crabpy_pyramid/views/crab.py @@ -8,6 +8,8 @@ from crabpy_pyramid.utils import range_return import pycountry +from crabpy.gateway.exception import GatewayResourceNotFoundException + from pyramid.httpexceptions import HTTPNotFound @view_config( @@ -26,7 +28,10 @@ def list_gewesten(request): def get_gewest_by_id(request): Gateway = request.crab_gateway() gewest_id = int(request.matchdict.get('gewest_id')) - return Gateway.get_gewest_by_id(gewest_id) + try: + return Gateway.get_gewest_by_id(gewest_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( route_name='list_provincies', @@ -45,8 +50,10 @@ def list_provincies(request): def get_provincie(request): Gateway = request.crab_gateway() provincie_id = int(request.matchdict.get('provincie_id')) - provincie = Gateway.get_provincie_by_id(provincie_id) - return provincie + try: + return Gateway.get_provincie_by_id(provincie_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( route_name='list_gemeenten_by_provincie', @@ -79,9 +86,15 @@ def get_gemeente_crab(request): Gateway = request.crab_gateway() gemeente_id = request.matchdict.get('gemeente_id') if len(gemeente_id) == 5: - return Gateway.get_gemeente_by_niscode(gemeente_id) + try: + return Gateway.get_gemeente_by_niscode(gemeente_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() else: - return Gateway.get_gemeente_by_id(gemeente_id) + try: + return Gateway.get_gemeente_by_id(gemeente_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( route_name='list_straten', @@ -102,7 +115,10 @@ def list_straten(request): def get_straat_by_id(request): Gateway = request.crab_gateway() straat_id = request.matchdict.get('straat_id') - return Gateway.get_straat_by_id(straat_id) + try: + return Gateway.get_straat_by_id(straat_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( route_name='list_huisnummers', @@ -122,7 +138,10 @@ def get_huisnummer_by_straat_and_label(request): Gateway = request.crab_gateway() straat_id = request.matchdict.get('straat_id') huisnummer = request.matchdict.get('huisnummer_label') - return Gateway.get_huisnummer_by_nummer_and_straat(huisnummer, straat_id) + try: + return Gateway.get_huisnummer_by_nummer_and_straat(huisnummer, straat_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( @@ -132,7 +151,10 @@ def get_huisnummer_by_straat_and_label(request): def get_huisnummer_by_id(request): Gateway = request.crab_gateway() huisnummer_id = request.matchdict.get('huisnummer_id') - return Gateway.get_huisnummer_by_id(huisnummer_id) + try: + return Gateway.get_huisnummer_by_id(huisnummer_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( route_name='list_percelen', @@ -151,7 +173,10 @@ def list_percelen(request): def get_perceel_by_id(request): Gateway = request.crab_gateway() perceel_id = request.matchdict.get('perceel_id1')+'/'+request.matchdict.get('perceel_id2') - return Gateway.get_perceel_by_id(perceel_id) + try: + return Gateway.get_perceel_by_id(perceel_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( route_name='list_gebouwen', @@ -170,7 +195,10 @@ def list_gebouwen(request): def get_gebouw_by_id(request): Gateway = request.crab_gateway() gebouw_id = request.matchdict.get('gebouw_id') - return Gateway.get_gebouw_by_id(gebouw_id) + try: + return Gateway.get_gebouw_by_id(gebouw_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( route_name='get_wegobject', @@ -179,8 +207,10 @@ def get_gebouw_by_id(request): def get_wegobject(request): Gateway = request.crab_gateway() wegobject_id = request.matchdict.get('wegobject_id') - return Gateway.get_wegobject_by_id(wegobject_id) - + try: + return Gateway.get_wegobject_by_id(wegobject_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( route_name='list_subadressen', @@ -199,7 +229,10 @@ def list_subadressen(request): def get_subadres_by_id(request): Gateway = request.crab_gateway() subadres_id = request.matchdict.get('subadres_id') - return Gateway.get_subadres_by_id(subadres_id) + try: + return Gateway.get_subadres_by_id(subadres_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( @@ -242,7 +275,10 @@ def list_adresposities_by_subadres(request): def get_adrespositie_by_id(request): Gateway = request.crab_gateway() adrespositie_id = request.matchdict.get('adrespositie_id') - return Gateway.get_adrespositie_by_id(adrespositie_id) + try: + return Gateway.get_adrespositie_by_id(adrespositie_id) + except GatewayResourceNotFoundException: + return HTTPNotFound() @view_config( From 57cb48fc81a03bc771cc84c1fab3993283c3f659 Mon Sep 17 00:00:00 2001 From: TalissaJoly Date: Thu, 21 May 2015 10:50:12 +0200 Subject: [PATCH 2/3] handling unexisting objects refs #25 --- crabpy_pyramid/tests/test_functional.py | 29 +++++++++++++------------ 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/crabpy_pyramid/tests/test_functional.py b/crabpy_pyramid/tests/test_functional.py index b90d08d..9b01ecb 100644 --- a/crabpy_pyramid/tests/test_functional.py +++ b/crabpy_pyramid/tests/test_functional.py @@ -134,8 +134,8 @@ def test_get_gewest_by_id(self): self.assertEqual('200 OK', res.status) def test_get_gewest_by_unexisting_id(self): - res = self.testapp.get('/crab/gewesten/0') - self.assertEqual('404 NotFound', res.status) + res = self.testapp.get('/crab/gewesten/0', status=404) + self.assertEqual('404 Not Found', res.status) def test_list_provincies(self): res = self.testapp.get('/crab/gewesten/2/provincies') @@ -146,7 +146,7 @@ def test_get_provincie_by_id(self): self.assertEqual('200 OK', res.status) def test_get_provincie_by_unexisting_id(self): - res = self.testapp.get('/crab/provincies/00000') + res = self.testapp.get('/crab/provincies/00000', status=404) self.assertEqual('404 Not Found', res.status) def test_list_gemeenten_by_provincie(self): @@ -162,7 +162,7 @@ def test_get_gemeente_crab_niscode(self): self.assertEqual('200 OK', res.status) def test_get_gemeente_crab_unexisting_niscode(self): - res = self.testapp.get('/crab/gemeenten/00000') + res = self.testapp.get('/crab/gemeenten/00000', status=404) self.assertEqual('404 Not Found', res.status) def test_get_gemeente_crab_id(self): @@ -170,7 +170,7 @@ def test_get_gemeente_crab_id(self): self.assertEqual('200 OK', res.status) def test_get_gemeente_crab_unexisting_id(self): - res = self.testapp.get('/crab/gemeenten/0') + res = self.testapp.get('/crab/gemeenten/0', status=404) self.assertEqual('404 Not Found', res.status) def test_list_straten(self): @@ -182,7 +182,7 @@ def test_get_straat_by_id(self): self.assertEqual('200 OK', res.status) def test_get_straat_by_unexisting_id(self): - res = self.testapp.get('/crab/straten/0') + res = self.testapp.get('/crab/straten/0', status=404) self.assertEqual('404 Not Found', res.status) def test_list_huisnummers(self): @@ -194,7 +194,7 @@ def test_get_huisnummer_by_straat_and_label(self): self.assertEqual('200 OK', res.status) def test_get_huisnummer_by_unexisting_straat_and_label(self): - res = self.testapp.get('crab/straten/1/huisnummers/0') + res = self.testapp.get('/crab/straten/1/huisnummers/0', status=404) self.assertEqual('404 Not Found', res.status) def test_get_huisnummer_by_id(self): @@ -202,7 +202,7 @@ def test_get_huisnummer_by_id(self): self.assertEqual('200 OK', res.status) def test_get_huisnummer_by_unexisting_id(self): - res = self.testapp.get('/crab/huisnummers/0') + res = self.testapp.get('/crab/huisnummers/0', status=404) self.assertEqual('404 Not Found', res.status) def test_list_percelen(self): @@ -214,7 +214,7 @@ def test_get_perceel_by_id(self): self.assertEqual('200 OK', res.status) def test_get_perceel_by_unexisting_id(self): - res = self.testapp.get('crab/percelen/31433D0011/000000') + res = self.testapp.get('/crab/percelen/31433D0011/000000', status=404) self.assertEqual('404 Not Found', res.status) def test_list_gebouwen(self): @@ -226,7 +226,7 @@ def test_get_gebouw_by_id(self): self.assertEqual('200 OK', res.status) def test_get_gebouw_by_unexisting_id(self): - res = self.testapp.get('/crab/gebouwen/0') + res = self.testapp.get('/crab/gebouwen/99999999', status=404) self.assertEqual('404 Not Found', res.status) def test_get_wegobject(self): @@ -234,7 +234,8 @@ def test_get_wegobject(self): self.assertEqual('200 OK', res.status) def test_get_unexisting_wegobject(self): - res = self.testapp.get('/crab/wegobjecten/00000000') + res = self.testapp.get('/crab/wegobjecten/00000000', status=404) + self.assertEqual('404 Not Found', res.status) def test_list_subadressen(self): res = self.testapp.get('/crab/huisnummers/129462/subadressen') @@ -245,7 +246,7 @@ def test_get_subadressen_by_id(self): self.assertEqual('200 OK', res.status) def test_get_subadressen_by_unexisting_id(self): - res = self.testapp.get('/crab/subadressen/0000000') + res = self.testapp.get('/crab/subadressen/0000000', status=404) def test_list_postkantons_by_gemeente(self): res = self.testapp.get('/crab/gemeenten/90/postkantons') @@ -264,7 +265,7 @@ def test_get_adrespositie_by_id(self): self.assertEqual('200 OK', res.status) def test_get_adrespositie_by_unexisting_id(self): - res = self.testapp.get('/crab/adresposities/0') + res = self.testapp.get('/crab/adresposities/0', status=404) self.assertEqual('404 Not Found', res.status) def test_list_landen(self): @@ -276,5 +277,5 @@ def test_get_land_by_id(self): self.assertEqual('200 OK', res.status) def test_get_land_by_unexisting_id(self): - res = self.testapp.get('crab/landen/BDH') + res = self.testapp.get('/crab/landen/BDH', status=404) self.assertEqual('404 Not Found', res.status) From 259b214fb74dd265bbcc61427279a5bc20973b1e Mon Sep 17 00:00:00 2001 From: TalissaJoly Date: Wed, 27 May 2015 11:18:00 +0200 Subject: [PATCH 3/3] added tests handling unexisting objects capakey refs #25 --- crabpy_pyramid/tests/test_functional.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crabpy_pyramid/tests/test_functional.py b/crabpy_pyramid/tests/test_functional.py index 9b01ecb..5ff6e4d 100644 --- a/crabpy_pyramid/tests/test_functional.py +++ b/crabpy_pyramid/tests/test_functional.py @@ -90,6 +90,10 @@ def test_list_gemeenten(self): def test_get_gemeente_by_id(self): res = self.testapp.get('/capakey/gemeenten/11001') self.assertEqual('200 OK', res.status) + + def test_get_gemeente_by_unexisting_id(self): + res = self.testapp.get('/capakey/gemeenten/1100', status=404) + self.assertEqual('404 Not Found', res.status) def test_list_kadastrale_afdelingen_by_gemeente(self): res = self.testapp.get('/capakey/gemeenten/11001/afdelingen') @@ -102,6 +106,10 @@ def test_list_secties_by_afdeling(self): def test_get_sectie_by_id_and_afdeling(self): res = self.testapp.get('/capakey/afdelingen/11001/secties/B') self.assertEqual('200 OK', res.status) + + def test_get_sectie_by_unexisting_id_and_afdeling(self): + res = self.testapp.get('/capakey/afdelingen/11001/secties/Z', status=404) + self.assertEqual('404 Not Found', res.status) def test_list_percelen_by_sectie(self): res = self.testapp.get('/capakey/afdelingen/11001/secties/B/percelen') @@ -110,14 +118,26 @@ def test_list_percelen_by_sectie(self): def test_get_perceel_by_sectie_and_id(self): res = self.testapp.get('/capakey/afdelingen/11001/secties/B/percelen/0001/00S000') self.assertEqual('200 OK', res.status) + + def test_get_perceel_by_unexisting_sectie_and_id(self): + res = self.testapp.get('/capakey/afdelingen/11001/secties/B/percelen/0000/00000', status=404) + self.assertEqual('404 Not Found', res.status) def test_get_perceel_by_capakey(self): res = self.testapp.get('/capakey/percelen/11001B0001/00S000') self.assertEqual('200 OK', res.status) + + def test_get_perceel_by_unexisting_capakey(self): + res = self.testapp.get('/capakey/percelen/00000000000/000000', status=404) + self.assertEqual('404 Not Found', res.status) def test_get_perceel_by_percid(self): res = self.testapp.get('/capakey/percelen/11001_B_0001_S_000_00') - self.assertEqual('200 OK', res.status) + self.assertEqual('200 OK', res.status) + + def test_get_perceel_by_unexisting_percid(self): + res = self.testapp.get('/capakey/percelen/00000_0_0000_0_000_00', status=404) + self.assertEqual('404 Not Found', res.status) @unittest.skipUnless(