Skip to content

Commit

Permalink
test capakey_rest_gateway_request exceptions #45
Browse files Browse the repository at this point in the history
  • Loading branch information
cahytinne committed Mar 27, 2017
1 parent c22ccb1 commit ed7b17d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
8 changes: 4 additions & 4 deletions crabpy/gateway/capakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,26 +434,26 @@ def creator():
return perceel


def capakey_rest_gateway_request(url, headers = {}, params = {}):
def capakey_rest_gateway_request(url, headers={}, params={}):
try:
res = requests.get(url, headers=headers, params=params)
res.raise_for_status()
return res
except requests.ConnectionError as ce:
raise GatewayRuntimeException(
'Could not execute request due to connection problems:\n%s' % ce.message,
'Could not execute request due to connection problems:\n%s' % repr(ce),
ce
)
except requests.HTTPError as he:
raise GatewayResourceNotFoundException()
except requests.RequestException as re:
raise GatewayRuntimeException(
'Could not execute request due to:\n%s' % re.message,
'Could not execute request due to:\n%s' % repr(re),
re
)


class CapakeyRestGateway(CapakeyGateway):
class CapakeyRestGateway(object):
'''
A REST gateway to the capakey webservice.
'''
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pytest==2.8.5
pytest-cov==2.2.0
coveralls==1.1
flake8==2.5.4
mock==2.0.0

# Update data script
requests==2.9.1
33 changes: 32 additions & 1 deletion tests/gateway/test_capakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@
Gemeente,
Afdeling,
Sectie,
Perceel
Perceel,
capakey_rest_gateway_request,
GatewayRuntimeException
)

import requests

from tests import (
run_capakey_integration_tests,
config
)

try:
from unittest.mock import MagicMock
except:
from mock import MagicMock


@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
Expand Down Expand Up @@ -274,6 +283,28 @@ def test_get_perceel_by_percid(self, capakey_rest_gateway):
assert res.sectie.id == 'A'
assert res.sectie.afdeling.id == 44021

def test_requests_connection(self):
requests.get = MagicMock(side_effect=connection_error)
with pytest.raises(GatewayRuntimeException) as cm:
capakey_rest_gateway_request('url')
exception = cm.value.message
assert exception == 'Could not execute request due to connection problems:\nConnectionError()'

def test_requests_request_exception(self):
requests.get = MagicMock(side_effect=request_exception)
with pytest.raises(GatewayRuntimeException) as cm:
capakey_rest_gateway_request('url')
exception = cm.value.message
assert exception == 'Could not execute request due to:\nRequestException()'


def connection_error(url, headers={}, params={}):
raise requests.exceptions.ConnectionError


def request_exception(url, headers={}, params={}):
raise requests.exceptions.RequestException


class TestGemeente:

Expand Down

0 comments on commit ed7b17d

Please sign in to comment.