Skip to content

Commit

Permalink
update CapakeyRestGateway + more tests #45
Browse files Browse the repository at this point in the history
  • Loading branch information
cahytinne committed Mar 30, 2017
1 parent 11706cf commit 754faff
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 55 deletions.
23 changes: 13 additions & 10 deletions crabpy/gateway/capakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,19 +633,17 @@ def list_secties_by_afdeling(self, afdeling):
'''
try:
aid = afdeling.id
gid = afdeling.gemeente.id
except AttributeError:
aid = afdeling
afdeling = self.get_kadastrale_afdeling_by_id(aid)
gid = afdeling.gemeente.id
afdeling.clear_gateway()

def creator():
url = self.base_url + '/municipality/%s/department/%s/section' % (afdeling.gemeente.id, afdeling.id)
url = self.base_url + '/municipality/%s/department/%s/section' % (gid, aid)
h = self.base_headers
p = {
'geometry': 'bbox',
'srs': 31370
}
res = capakey_rest_gateway_request(url, h, p).json()
res = capakey_rest_gateway_request(url, h).json()
return [
Sectie(
r['sectionCode'],
Expand Down Expand Up @@ -731,7 +729,7 @@ def parse_capakey(self, percid):
"Invalid percid %s can't be parsed" % percid
)

def list_percelen_by_sectie(self, sectie, sort=1):
def list_percelen_by_sectie(self, sectie):
'''
List all percelen in a `sectie`.
Expand All @@ -746,7 +744,10 @@ def list_percelen_by_sectie(self, sectie, sort=1):
def creator():
url = self.base_url + '/municipality/%s/department/%s/section/%s/parcel' % (gid, aid, sid)
h = self.base_headers
res = capakey_rest_gateway_request(url, h).json()
p = {
'data': 'cadmap'
}
res = capakey_rest_gateway_request(url, h, p).json()
return [
Perceel(
r['perceelnummer'],
Expand Down Expand Up @@ -781,7 +782,8 @@ def creator():
h = self.base_headers
p = {
'geometry': 'bbox',
'srs': 31370
'srs': 31370,
'data': 'cadmap'
}
res = capakey_rest_gateway_request(url, p, h).json()
return Perceel(
Expand Down Expand Up @@ -814,7 +816,8 @@ def creator():
h = self.base_headers
p = {
'geometry': 'bbox',
'srs': 31370
'srs': 31370,
'data': 'cadmap'
}
res = capakey_rest_gateway_request(url, p, h).json()
return Perceel(
Expand Down
2 changes: 1 addition & 1 deletion pytest_dist.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
addopts = --crab-integration --capakey-integration --capakey-user=<username> --capakey-password=<password>
addopts = --crab-integration --capakey-integration
2 changes: 1 addition & 1 deletion pytest_travis.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
addopts = --crab-integration
addopts = --crab-integration --capakey-integration
19 changes: 12 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@ def pytest_addoption(parser):
help="capakey-integartion: Run CAPAKEY integration tests or not?"
)
parser.addoption(
"--capakey-user",
"--capakey-soap-integration",
action="store_true",
help="capakey-integartion: Run CAPAKEY SOAP integration tests or not?"
)
parser.addoption(
"--capakey-soap-user",
action="store", default="capakey_user",
help="capakey-user: Run CAPAKEY integration tests with this user"
help="capakey-soap-user: Run CAPAKEY integration tests with this user"
)
parser.addoption(
"--capakey-password",
"--capakey-soap-password",
action="store", default="capakey_password",
help="capakey-password: Run CAPAKEY integration tests with this password"
help="capakey-soap-password: Run CAPAKEY integration tests with this password"
)

@pytest.fixture(scope="session")
def capakey(request):
if not request.config.getoption('--capakey-integration'):
if not request.config.getoption('--capakey-soap-integration'):
return None
from crabpy.client import capakey_factory
capakey = capakey_factory(
user=request.config.getoption("--capakey-user"),
password=request.config.getoption("--capakey-password")
user=request.config.getoption("--capakey-soap-user"),
password=request.config.getoption("--capakey-soap-password")
)
return capakey

Expand Down
165 changes: 134 additions & 31 deletions tests/gateway/test_capakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@
)

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


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


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


@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
not pytest.config.getoption('--capakey-soap-integration'),
reason = 'No CAPAKEY Integration tests required'
)
class TestCapakeyGateway:
Expand Down Expand Up @@ -283,29 +291,21 @@ def test_get_perceel_by_percid(self, capakey_rest_gateway):
assert res.sectie.id == 'A'
assert res.sectie.afdeling.id == 44021

@patch('requests.get', MagicMock(side_effect=connection_error))
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()'

@patch('requests.get', MagicMock(side_effect=request_exception))
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:

def test_fully_initialised(self):
Expand Down Expand Up @@ -337,9 +337,19 @@ def test_check_gateway_not_set(self):

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
reason = 'No CAPAKEY Integration tests required'
reason='No CAPAKEY Integration tests required'
)
def test_lazy_load_1(self, capakey_rest_gateway):
g = Gemeente(44021, 'Gent', gateway=capakey_rest_gateway)
g.clear_gateway()
with pytest.raises(RuntimeError):
g.check_gateway()

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-soap-integration'),
reason='No CAPAKEY Integration tests required'
)
def test_lazy_load(self, capakey_gateway):
def test_lazy_load_1_soap(self, capakey_gateway):
g = Gemeente(44021, 'Gent', gateway=capakey_gateway)
g.clear_gateway()
with pytest.raises(RuntimeError):
Expand All @@ -349,7 +359,19 @@ def test_lazy_load(self, capakey_gateway):
not pytest.config.getoption('--capakey-integration'),
reason = 'No CAPAKEY Integration tests required'
)
def test_lazy_load(self, capakey_gateway):
def test_lazy_load(self, capakey_rest_gateway):
g = Gemeente(44021, 'Gent')
g.set_gateway(capakey_rest_gateway)
assert g.id == 44021
assert g.naam == 'Gent'
assert not g.centroid == None
assert not g.bounding_box == None

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-soap-integration'),
reason='No CAPAKEY Integration tests required'
)
def test_lazy_load_soap(self, capakey_gateway):
g = Gemeente(44021, 'Gent')
g.set_gateway(capakey_gateway)
assert g.id == 44021
Expand All @@ -359,9 +381,21 @@ def test_lazy_load(self, capakey_gateway):

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
reason = 'No CAPAKEY Integration tests required'
reason='No CAPAKEY Integration tests required'
)
def test_afdelingen(self, capakey_rest_gateway):
g = Gemeente(44021, 'Gent')
g.set_gateway(capakey_rest_gateway)
afdelingen = g.afdelingen
assert isinstance(afdelingen, list)
assert len(afdelingen) > 0
assert len(afdelingen) < 40

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-soap-integration'),
reason='No CAPAKEY Integration tests required'
)
def test_afdelingen(self, capakey_gateway):
def test_afdelingen_soap(self, capakey_gateway):
g = Gemeente(44021, 'Gent')
g.set_gateway(capakey_gateway)
afdelingen = g.afdelingen
Expand Down Expand Up @@ -411,9 +445,21 @@ def test_check_gateway_not_set(self):

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
reason = 'No CAPAKEY Integration tests required'
reason='No CAPAKEY Integration tests required'
)
def test_lazy_load(self, capakey_gateway):
def test_lazy_load(self, capakey_rest_gateway):
a = Afdeling(44021)
a.set_gateway(capakey_rest_gateway)
assert a.id == 44021
assert a.naam == 'GENT 1 AFD'
assert not a.centroid == None
assert not a.bounding_box == None

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-soap-integration'),
reason='No CAPAKEY Integration tests required'
)
def test_lazy_load_soap(self, capakey_gateway):
a = Afdeling(44021)
a.set_gateway(capakey_gateway)
assert a.id == 44021
Expand All @@ -423,9 +469,20 @@ def test_lazy_load(self, capakey_gateway):

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
reason = 'No CAPAKEY Integration tests required'
reason='No CAPAKEY Integration tests required'
)
def test_secties(self, capakey_rest_gateway):
a = Afdeling(44021)
a.set_gateway(capakey_rest_gateway)
secties = a.secties
assert isinstance(secties, list)
assert len(secties) == 1

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-soap-integration'),
reason='No CAPAKEY Integration tests required'
)
def test_secties(self, capakey_gateway):
def test_secties_soap(self, capakey_gateway):
a = Afdeling(44021)
a.set_gateway(capakey_gateway)
secties = a.secties
Expand Down Expand Up @@ -453,19 +510,34 @@ def test_check_gateway_not_set(self):
with pytest.raises(RuntimeError):
s.check_gateway()

def test_clear_gateway(self, capakey_gateway):
def test_clear_gateway(self, capakey_rest_gateway):
s = Sectie('A', Afdeling(44021))
s.set_gateway(capakey_gateway)
s.set_gateway(capakey_rest_gateway)
s.check_gateway()
s.clear_gateway()
with pytest.raises(RuntimeError):
s.check_gateway()

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
reason = 'No CAPAKEY Integration tests required'
reason='No CAPAKEY Integration tests required'
)
def test_lazy_load(self, capakey_gateway):
def test_lazy_load(self, capakey_rest_gateway):
s = Sectie(
'A',
Afdeling(44021)
)
s.set_gateway(capakey_rest_gateway)
assert s.id == 'A'
assert s.afdeling.id == 44021
assert not s.centroid == None
assert not s.bounding_box == None

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-soap-integration'),
reason='No CAPAKEY Integration tests required'
)
def test_lazy_load_soap(self, capakey_gateway):
s = Sectie(
'A',
Afdeling(44021)
Expand All @@ -478,9 +550,23 @@ def test_lazy_load(self, capakey_gateway):

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
reason = 'No CAPAKEY Integration tests required'
reason='No CAPAKEY Integration tests required'
)
def test_percelen(self, capakey_gateway):
def test_percelen(self, capakey_rest_gateway):
s = Sectie(
'A',
Afdeling(44021)
)
s.set_gateway(capakey_rest_gateway)
percelen = s.percelen
assert isinstance(percelen, list)
assert len(percelen) > 0

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-soap-integration'),
reason='No CAPAKEY Integration tests required'
)
def test_percelen_soap(self, capakey_gateway):
s = Sectie(
'A',
Afdeling(44021)
Expand Down Expand Up @@ -531,12 +617,29 @@ def test_clear_gateway(self, capakey_gateway):
p.sectie.check_gateway()
p.check_gateway()


@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
reason = 'No CAPAKEY Integration tests required'
reason='No CAPAKEY Integration tests required'
)
def test_lazy_load(self, capakey_rest_gateway):
p = Perceel(
'1154/02C000', Sectie('A', Afdeling(46013)),
'46013A1154/02C000', '46013_A_1154_C_000_02',
gateway=capakey_rest_gateway
)
assert p.id == '1154/02C000'
assert p.sectie.id == 'A'
assert p.sectie.afdeling.id == 46013
assert p.capatype == None
assert p.cashkey == None
assert not p.centroid == None
assert not p.bounding_box == None

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-soap-integration'),
reason='No CAPAKEY Integration tests required'
)
def test_lazy_load(self, capakey_gateway):
def test_lazy_load_soap(self, capakey_gateway):
p = Perceel(
'1154/02C000', Sectie('A', Afdeling(46013)),
'46013A1154/02C000', '46013_A_1154_C_000_02',
Expand Down
2 changes: 1 addition & 1 deletion tests/gateway/test_capakey_cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def capakey_gateway(capakey):
return capakey_gateway

@pytest.mark.skipif(
not pytest.config.getoption('--capakey-integration'),
not pytest.config.getoption('--capakey-soap-integration'),
reason = 'No CAPAKEY Integration tests required'
)
class TestCapakeyCachedGateway:
Expand Down
Loading

0 comments on commit 754faff

Please sign in to comment.