diff --git a/crabpy_pyramid/tests/test_functional.py b/crabpy_pyramid/tests/test_functional.py index 21c42a2..c4d80d0 100644 --- a/crabpy_pyramid/tests/test_functional.py +++ b/crabpy_pyramid/tests/test_functional.py @@ -5,17 +5,15 @@ .. versionadded:: 0.1.0 ''' import unittest -import json import os import shutil -from paste.deploy.loadwsgi import appconfig - from pyramid import testing from webtest import TestApp from crabpy_pyramid import main + def as_bool(value): ''' Cast a textual value from a config file to a boolean. @@ -24,6 +22,7 @@ def as_bool(value): ''' return value in ['true', 'True', '1', 't', 'T', 'Yes'] + def run_capakey_integration_tests(): from testconfig import config try: @@ -66,6 +65,7 @@ def setUpModule(): True ) + class FunctionalTests(unittest.TestCase): @classmethod def setUpClass(cls): @@ -78,6 +78,7 @@ def setUp(self): def tearDown(self): testing.tearDown() + @unittest.skipUnless( run_capakey_integration_tests(), 'No CAPAKEY Integration tests required' @@ -91,6 +92,10 @@ 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') self.assertEqual('200 OK', res.status) @@ -103,6 +108,10 @@ 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') self.assertEqual('200 OK', res.status) @@ -111,14 +120,26 @@ 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) + 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( run_crab_integration_tests(), @@ -133,6 +154,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', status=404) + self.assertEqual('404 Not Found', res.status) + def test_list_provincies(self): res = self.testapp.get('/crab/gewesten/2/provincies') self.assertEqual('200 OK', res.status) @@ -141,6 +166,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', status=404) + 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) @@ -153,10 +182,18 @@ 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', status=404) + 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', status=404) + self.assertEqual('404 Not Found', res.status) + def test_list_straten(self): res = self.testapp.get('/crab/gemeenten/11001/straten') self.assertEqual('200 OK', res.status) @@ -165,6 +202,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', status=404) + 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) @@ -173,10 +214,18 @@ 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', status=404) + 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', status=404) + 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) @@ -185,6 +234,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', status=404) + 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) @@ -193,10 +246,18 @@ 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/99999999', status=404) + 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', status=404) + self.assertEqual('404 Not Found', res.status) + def test_list_subadressen(self): res = self.testapp.get('/crab/huisnummers/129462/subadressen') self.assertEqual('200 OK', res.status) @@ -205,6 +266,10 @@ 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', status=404) + self.assertEqual('404 Not Found', res.status) + def test_list_postkantons_by_gemeente(self): res = self.testapp.get('/crab/gemeenten/90/postkantons') self.assertEqual('200 OK', res.status) @@ -221,6 +286,10 @@ 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', status=404) + self.assertEqual('404 Not Found', res.status) + def test_list_landen(self): res = self.testapp.get('/crab/landen') self.assertEqual('200 OK', res.status) @@ -231,3 +300,4 @@ def test_get_land_by_id(self): def test_get_land_by_unexisting_id(self): res = self.testapp.get('/crab/landen/MORDOR', status=404) + 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(