Skip to content

Commit

Permalink
Align service with Atramhasis.
Browse files Browse the repository at this point in the history
  • Loading branch information
koenedaele committed May 4, 2016
1 parent e5d459d commit d6f4ce9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
36 changes: 26 additions & 10 deletions pyramid_skosprovider/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,24 @@ def concept_adapter(obj, request):
:param skosprovider.skos.Concept obj: The concept to be rendered.
:rtype: :class:`dict`
'''
p = request.skos_registry.get_provider(obj.concept_scheme.uri)
return {
'id': obj.id,
'type': 'concept',
'uri': obj.uri,
'label': obj.label().label,
'label': obj.label().label if obj.label() else None,
'concept_scheme': {
'uri': obj.concept_scheme.uri,
'labels': obj.concept_scheme.labels
},
'labels': obj.labels,
'notes': obj.notes,
'sources': obj.sources,
'narrower': obj.narrower,
'broader': obj.broader,
'related': obj.related,
'member_of': obj.member_of,
'subordinate_arrays': obj.subordinate_arrays,
'narrower': [_map_relation(p.get_by_id(n)) for n in obj.narrower],
'broader': [_map_relation(p.get_by_id(b)) for b in obj.broader],
'related': [_map_relation(p.get_by_id(r)) for r in obj.related],
'member_of': [_map_relation(p.get_by_id(m)) for m in obj.member_of],
'subordinate_arrays': [_map_relation(p.get_by_id(sa)) for sa in obj.subordinate_arrays],
'matches': obj.matches
}

Expand All @@ -77,22 +78,37 @@ def collection_adapter(obj, request):
:param skosprovider.skos.Collection obj: The collection to be rendered.
:rtype: :class:`dict`
'''
p = request.skos_registry.get_provider(obj.concept_scheme.uri)
return {
'id': obj.id,
'type': 'collection',
'uri': obj.uri,
'label': obj.label().label,
'label': obj.label().label if obj.label() else None,
'concept_scheme': {
'uri': obj.concept_scheme.uri,
'labels': obj.concept_scheme.labels
},
'labels': obj.labels,
'notes': obj.notes,
'sources': obj.sources,
'members': obj.members,
'member_of': obj.member_of,
'superordinates': obj.superordinates,
'members': [_map_relation(p.get_by_id(m)) for m in obj.members],

This comment has been minimized.

Copy link
@BartSaelen

BartSaelen May 9, 2016

Contributor

get_by_id can return False (see https://github.com/koenedaele/skosprovider/blob/master/skosprovider/providers.py#L163). This can cause problems in _map_relation, resulting in this kind of errors for example: AttributeError: 'bool' object has no attribute 'id'

'member_of': [_map_relation(p.get_by_id(m)) for m in obj.member_of],
'superordinates': [_map_relation(p.get_by_id(so)) for so in obj.superordinates],
}


def _map_relation(c):
"""
Map related concept or collection, leaving out the relations (to avoid circular dependencies)
:param c: the concept or collection to map
:rtype: :class:`dict`
"""
return {
'id': c.id,
'type': c.type,
'uri': c.uri,
'label': c.label().label if c.label() else None
}


Expand Down
61 changes: 39 additions & 22 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

from __future__ import unicode_literals

from pyramid import testing

from pyramid.compat import (
text_type
)

from .fixtures.data import (
larch,
chestnut,
species,
trees
)
Expand All @@ -21,6 +24,7 @@
import json

import unittest
from mock import Mock


class TestUtils(unittest.TestCase):
Expand Down Expand Up @@ -71,7 +75,7 @@ def test_concept_adapter(self):
concept_scheme=trees.concept_scheme,
matches=larch['matches']
)
concept = concept_adapter(c, {})
concept = concept_adapter(c, Mock())
self.assertIsInstance(concept, dict)
self.assertEqual(concept['id'], 1)
self.assertIn('uri', concept)
Expand All @@ -98,7 +102,7 @@ def test_collection_adapter(self):
members=species['members'],
concept_scheme=trees.concept_scheme
)
collection = collection_adapter(c, {})
collection = collection_adapter(c, Mock())
self.assertIsInstance(collection, dict)
self.assertEqual(collection['id'], 3)
self.assertIsInstance(collection['label'], text_type)
Expand All @@ -124,7 +128,7 @@ def test_json_concept(self):
matches=larch['matches']
)
r = json_renderer({})
jsonstring = r(c, {})
jsonstring = r(c, Mock())
concept = json.loads(jsonstring)
self.assertIsInstance(concept, dict)
self.assertEqual(concept['id'], 1)
Expand Down Expand Up @@ -162,29 +166,42 @@ def test_json_collection(self):
uri=species['uri'],
labels=species['labels'],
notes=species['notes'],
members=species['members'],
members=[larch['id']],
concept_scheme=trees.concept_scheme
)
r = json_renderer({})
jsonstring = r(c, {})
m = Mock()
config = {
'get_provider.return_value.get_by_id.return_value': Concept(id=larch['id'], uri=larch['uri'], labels=larch['labels'])
}
m.configure_mock(**config)
request = testing.DummyRequest()
request.skos_registry = m
sys = {}
sys['request'] = request
jsonstring = r(c, sys)
coll = json.loads(jsonstring)
self.assertIsInstance(coll, dict)
self.assertEqual(coll['id'], 3)
self.assertEqual(coll['uri'], 'http://python.com/trees/species')
self.assertIsInstance(coll['label'], text_type)
self.assertEqual(coll['type'], 'collection')
self.assertIsInstance(coll['labels'], list)
self.assertEqual(len(coll['labels']), 2)
assert isinstance(coll, dict)
assert coll['id'] == 3
assert coll['uri'] == 'http://python.com/trees/species'
assert isinstance(coll['label'], text_type)
assert coll['type'] == 'collection'
assert isinstance(coll['labels'], list)
assert len(coll['labels']) == 2
for l in coll['labels']:
self.assertIsInstance(l, dict)
self.assertIn('label', l)
self.assertIn('type', l)
self.assertIn('language', l)
self.assertEqual(len(coll['notes']), 1)
assert 'label' in l
assert 'type' in l
assert 'language' in l
assert len(coll['notes']) == 1
for n in coll['notes']:
self.assertIsInstance(n, dict)
self.assertIn('note', n)
self.assertIn('type', n)
self.assertIn('language', n)
self.assertIn('markup', n)
assert 'note' in n
assert 'type' in n
assert 'language' in n
assert 'markup' in n
assert len(coll['members']) == 1
m = coll['members'][0]
assert 'id' in m
assert 'uri' in m
assert 'type' in m
assert 'label' in m
assert 'matches' not in coll

0 comments on commit d6f4ce9

Please sign in to comment.