Skip to content

Commit

Permalink
Merge b8ca914 into d061a60
Browse files Browse the repository at this point in the history
  • Loading branch information
koenedaele committed Nov 1, 2019
2 parents d061a60 + b8ca914 commit 9dc4fd4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
28 changes: 21 additions & 7 deletions skosprovider_atramhasis/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'''

import requests
from requests.exceptions import ConnectionError
from requests.exceptions import ConnectionError, Timeout

import logging
from skosprovider.skos import ConceptScheme, Label, Note, dict_to_source
Expand Down Expand Up @@ -66,7 +66,9 @@ def _get_concept_scheme(self):
request = self.base_url + '/conceptschemes/' + self.scheme_id
response = self._request(request, {'Accept': 'application/json'}, dict())
if response.status_code == 404:
raise RuntimeError("Conceptscheme not found for scheme_id: %s" % self.scheme_id)
raise ProviderUnavailableException(
"Conceptscheme %s not found. Check your configuration." % request
)
cs = response.json()
return ConceptScheme(
cs['uri'],
Expand Down Expand Up @@ -291,15 +293,27 @@ def expand(self, id):
return False
return response.json()

@staticmethod
def _get_sort_params(**kwargs):
return {'sort': kwargs.get('sort', None)} or {}
def _get_sort_params(self, **kwargs):
if 'sort' in kwargs:
sort = kwargs.get('sort')
sort_order = kwargs.get('sort_order', 'asc')
if kwargs.get('sort_order', 'asc') == 'desc':
sort = '-' + sort
return {'sort': sort}
return {}

def _request(self, request, headers=None, params=None):
try:
res = self.session.get(request, headers=headers, params=params)
except ConnectionError as e:
raise ProviderUnavailableException("Request could not be executed - Request: %s" % (request,))
except ConnectionError:
raise ProviderUnavailableException("Request could not be executed \
due to connection issues- Request: %s" % (request,))
except Timeout: # pragma: no cover
raise ProviderUnavailableException("Request could not be executed \
due to timeout - Request: %s" % (request,))
if res.status_code >= 500:
raise ProviderUnavailableException("Request could not be executed \
due to server issues - Request: %s" % (request,))
if not res.encoding:
res.encoding = 'utf-8'
return res
6 changes: 6 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def init_responses():
status=200,
content_type='application/json; charset=UTF-8')

responses.add(responses.GET, 'http://localhost/conceptschemes/STYLES/c/?type=collection&language=en&sort=id',
match_querystring=True,
body='[{"label": "Stijlen en culturen", "type": "collection", "id": 0, "uri": "urn:x-vioe:styles:0"}, {"label": "stijlen", "type": "collection", "id": 60, "uri": "urn:x-vioe:styles:60"}, {"label": "culturen", "type": "collection", "id": 61, "uri": "urn:x-vioe:styles:61"}, {"label": "culturen uit de steentijd", "type": "collection", "id": 62, "uri": "urn:x-vioe:styles:62"}, {"label": "culturen uit de metaaltijden", "type": "collection", "id": 63, "uri": "urn:x-vioe:styles:63"}]',
status=200,
content_type='application/json; charset=UTF-8')

responses.add(responses.GET, 'http://localhost/conceptschemes/STYLES/c/?type=collection&language=en&sort=-id',
match_querystring=True,
body='[{"label": "culturen uit de metaaltijden", "type": "collection", "id": 63, "uri": "urn:x-vioe:styles:63"}, {"label": "culturen uit de steentijd", "type": "collection", "id": 62, "uri": "urn:x-vioe:styles:62"}, {"label": "culturen", "type": "collection", "id": 61, "uri": "urn:x-vioe:styles:61"}, {"label": "stijlen", "type": "collection", "id": 60, "uri": "urn:x-vioe:styles:60"}, {"label": "Stijlen en culturen", "type": "collection", "id": 0, "uri": "urn:x-vioe:styles:0"}]',
Expand Down
46 changes: 33 additions & 13 deletions tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def test_base_url_not_available(self):

@responses.activate
def test_scheme_id_not_available(self):
with pytest.raises(RuntimeError):
with pytest.raises(ProviderUnavailableException):
cs = AtramhasisProvider(
{'id': 'Atramhasis'},
base_url='http://localhost',
Expand Down Expand Up @@ -395,15 +395,24 @@ def test_find_with_collection_members(self):
@responses.activate
def test_find_collections(self):
r = AtramhasisProvider({'id': 'Atramhasis'}, base_url='http://localhost', scheme_id='STYLES').find(
{'type': 'collection'}, sort='-id')
assert len(r) > 0
for res in r:
assert res['type'] == 'collection'
{'type': 'collection'}, sort='id')
assert len(r) == 5
assert all([res['type'] =='collection' for res in r])
assert [0, 60, 61, 62, 63] == [res['id'] for res in r]

@responses.activate
def test_find_collections_sort_desc(self):
r = AtramhasisProvider({'id': 'Atramhasis'}, base_url='http://localhost', scheme_id='STYLES').find(
{'type': 'collection'}, sort='id', sort_order='desc')
assert len(r) == 5
assert all([res['type'] =='collection' for res in r])
assert [63, 62, 61, 60, 0] == [res['id'] for res in r]

@responses.activate
def test_find_all_concepts_collections(self):
r = AtramhasisProvider({'id': 'Atramhasis'}, base_url='http://localhost', scheme_id='MATERIALS').find(
{'type': 'all'})
r = AtramhasisProvider({'id': 'Atramhasis'}, base_url='http://localhost', scheme_id='MATERIALS').find({
'type': 'all'
})
assert len(r) > 0
for res in r:
assert res['type'] in ['collection', 'concept']
Expand Down Expand Up @@ -472,12 +481,20 @@ def test_find_match_no_uri(self):
}
})

@responses.activate
def test_expandi_not_found(self):
all_children = AtramhasisProvider(
{'id': 'Atramhasis'},
base_url='http://localhost', scheme_id='TREES'
).expand(100)
assert not all_children

@responses.activate
def test_expand(self):
all_childeren = AtramhasisProvider({'id': 'Atramhasis'}, base_url='http://localhost',
all_children = AtramhasisProvider({'id': 'Atramhasis'}, base_url='http://localhost',
scheme_id='STYLES').expand(1)
assert len(all_childeren) > 0
assert '1' in all_childeren
assert len(all_children) > 0
assert '1' in all_children

@responses.activate
def test_expand(self):
Expand All @@ -489,12 +506,15 @@ def test_expand(self):

@responses.activate
def test_expand_invalid(self):
all_childeren_invalid = AtramhasisProvider({'id': 'Atramhasis'}, base_url='http://localhost',
scheme_id='STYLES').expand('invalid')
assert not all_childeren_invalid
with pytest.raises(ProviderUnavailableException) as e:
AtramhasisProvider(
{'id': 'Atramhasis'},
base_url='http://localhost', scheme_id='STYLES'
).expand('invalid')

@responses.activate
def test_request_encoding(self):
provider = AtramhasisProvider({'id': 'Atramhasis'}, base_url='http://localhost', scheme_id='STYLES')
response = provider._request("http://localhost/no_encoding")
assert response.encoding == "utf-8"

0 comments on commit 9dc4fd4

Please sign in to comment.