Skip to content

Commit

Permalink
Refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koenedaele committed May 25, 2016
1 parent 82d3fb2 commit 7c86e19
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 171 deletions.
35 changes: 21 additions & 14 deletions pyramid_skosprovider/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@ def concept_adapter(obj, request):
:rtype: :class:`dict`
'''
p = request.skos_registry.get_provider(obj.concept_scheme.uri)
language = request.params.get('language', request.locale_name)
label = obj.label(language)
return {
'id': obj.id,
'type': 'concept',
'uri': obj.uri,
'label': obj.label().label if obj.label() else None,
'label': label.label if 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': _map_relations(obj.narrower, p),
'broader': _map_relations(obj.broader, p),
'related': _map_relations(obj.related, p),
'member_of': _map_relations(obj.member_of, p),
'subordinate_arrays': _map_relations(obj.subordinate_arrays, p),
'narrower': _map_relations(obj.narrower, p, language),
'broader': _map_relations(obj.broader, p, language),
'related': _map_relations(obj.related, p, language),
'member_of': _map_relations(obj.member_of, p, language),
'subordinate_arrays': _map_relations(obj.subordinate_arrays, p, language),
'matches': obj.matches
}

Expand All @@ -56,37 +58,40 @@ def collection_adapter(obj, request):
:rtype: :class:`dict`
'''
p = request.skos_registry.get_provider(obj.concept_scheme.uri)
language = request.params.get('language', request.locale_name)
label = obj.label(language)
return {
'id': obj.id,
'type': 'collection',
'uri': obj.uri,
'label': obj.label().label if obj.label() else None,
'label': label.label if 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': _map_relations(obj.members, p),
'member_of': _map_relations(obj.member_of, p),
'superordinates': _map_relations(obj.superordinates, p),
'members': _map_relations(obj.members, p, language),
'member_of': _map_relations(obj.member_of, p, language),
'superordinates': _map_relations(obj.superordinates, p, language)
}


def _map_relations(relations, p):
def _map_relations(relations, p, language='any'):
'''
:param: :class:`list` relations: Relations to be mapped. These are
concept or collection id's.
:param: :class:`skosprovider.providers.VocabularyProvider` p: Provider
to look up id's.
:param string language: Language to render the relations' labels in
:rtype: :class:`list`
'''
ret = []
for r in relations:
c = p.get_by_id(r)
if c:
ret.append(_map_relation(c))
ret.append(_map_relation(c, language))
else:
log.warning(
'A relation references a concept or collection %d in provider %s that can not be found. Please check the integrity of your data.' %
Expand All @@ -95,18 +100,20 @@ def _map_relations(relations, p):
return ret


def _map_relation(c):
def _map_relation(c, language='any'):
"""
Map related concept or collection, leaving out the relations.
:param c: the concept or collection to map
:param string language: Language to render the relation's label in
:rtype: :class:`dict`
"""
label = c.label(language)
return {
'id': c.id,
'type': c.type,
'uri': c.uri,
'label': c.label().label if c.label() else None
'label': label.label if label else None
}


Expand Down
17 changes: 14 additions & 3 deletions tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def test_concept_adapter(self):
concept_scheme=trees.concept_scheme,
matches=larch['matches']
)
concept = concept_adapter(c, Mock())
request = testing.DummyRequest()
m = Mock()
request.skos_registry = m
concept = concept_adapter(c, request)
self.assertIsInstance(concept, dict)
self.assertEqual(concept['id'], 1)
self.assertIn('uri', concept)
Expand All @@ -72,7 +75,10 @@ def test_collection_adapter(self):
members=species['members'],
concept_scheme=trees.concept_scheme
)
collection = collection_adapter(c, Mock())
request = testing.DummyRequest()
m = Mock()
request.skos_registry = m
collection = collection_adapter(c, request)
self.assertIsInstance(collection, dict)
self.assertEqual(collection['id'], 3)
self.assertIsInstance(collection['label'], text_type)
Expand Down Expand Up @@ -107,7 +113,12 @@ def test_json_concept(self):
matches=larch['matches']
)
r = json_renderer({})
jsonstring = r(c, Mock())
request = testing.DummyRequest()
m = Mock()
request.skos_registry = m
sys = {}
sys['request'] = request
jsonstring = r(c, sys)
concept = json.loads(jsonstring)
self.assertIsInstance(concept, dict)
self.assertEqual(concept['id'], 1)
Expand Down
173 changes: 19 additions & 154 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,157 +122,22 @@ class TestRenderers(unittest.TestCase):
Maintain tests until 0.8.0 when they will be removed.
'''

def _assert_is_labels(self, labels):
self.assertIsInstance(labels, list)
for l in labels:
self.assertIsInstance(l, Label)

def test_concept_adapter(self):
from pyramid_skosprovider.utils import concept_adapter
c = Concept(
id=larch['id'],
labels=larch['labels'],
notes=larch['notes'],
sources=larch['sources'],
concept_scheme=trees.concept_scheme,
matches=larch['matches']
)
concept = concept_adapter(c, Mock())
self.assertIsInstance(concept, dict)
self.assertEqual(concept['id'], 1)
self.assertIn('uri', concept)
self.assertIsInstance(concept['label'], text_type)
self.assertIn(concept['type'], 'concept')
self.assertEqual(len(concept['labels']), 2)
self._assert_is_labels(concept['labels'])
assert 'matches' in concept
assert 0 == len(concept['matches']['broad'])
assert 0 == len(concept['matches']['narrow'])
assert 0 == len(concept['matches']['related'])
assert 0 == len(concept['matches']['exact'])
assert 1 == len(concept['matches']['close'])
assert 'subordinate_arrays' in concept
assert 0 == len(concept['subordinate_arrays'])
assert 'sources' in concept
assert 1 == len(concept['sources'])

def test_collection_adapter(self):
from pyramid_skosprovider.utils import collection_adapter
c = Collection(
id=species['id'],
labels=species['labels'],
members=species['members'],
concept_scheme=trees.concept_scheme
)
collection = collection_adapter(c, Mock())
self.assertIsInstance(collection, dict)
self.assertEqual(collection['id'], 3)
self.assertIsInstance(collection['label'], text_type)
self.assertIn('label', collection)
self.assertIn('uri', collection)
self.assertEqual(collection['type'], 'collection')
self.assertEqual(len(collection['labels']), 2)
self._assert_is_labels(collection['labels'])
self.assertIn('notes', collection)
assert not 'matches' in collection
assert 'superordinates' in collection
assert 0 == len(collection['superordinates'])
assert 0 == len(collection['sources'])

def test_source_adapter(self):
from pyramid_skosprovider.utils import source_adapter
s = Source('<em>My citation</em>', 'HTML')
source = source_adapter(s, Mock())
assert isinstance(source, dict)
assert 'citation' in source
assert 'markup' in source
assert source['markup'] == 'HTML'

def test_json_concept(self):
from pyramid_skosprovider.utils import json_renderer
c = Concept(
id=larch['id'],
uri=larch['uri'],
labels=larch['labels'],
notes=larch['notes'],
concept_scheme=trees.concept_scheme,
matches=larch['matches']
)
r = json_renderer({})
jsonstring = r(c, Mock())
concept = json.loads(jsonstring)
self.assertIsInstance(concept, dict)
self.assertEqual(concept['id'], 1)
self.assertEqual(concept['uri'], 'http://python.com/trees/larch')
self.assertIsInstance(concept['label'], text_type)
self.assertEqual(concept['type'], 'concept')
self.assertIsInstance(concept['labels'], list)
self.assertEqual(len(concept['labels']), 2)
for l in concept['labels']:
self.assertIsInstance(l, dict)
self.assertIn('label', l)
self.assertIn('type', l)
self.assertIn('language', l)
self.assertIsInstance(concept['notes'], list)
self.assertEqual(len(concept['notes']), 1)
for n in concept['notes']:
self.assertIsInstance(n, dict)
self.assertIn('note', n)
self.assertIn('type', n)
self.assertIn('language', n)
self.assertIn('markup', n)
self.assertIsInstance(concept['broader'], list)
self.assertIsInstance(concept['related'], list)
self.assertIsInstance(concept['narrower'], list)
assert 'matches' in concept
for mt in ['broad', 'narrow', 'related', 'close', 'exact']:
assert mt in concept['matches']
assert list == type(concept['matches'][mt])
assert 1 == len(concept['matches']['close'])

def test_json_collection(self):
from pyramid_skosprovider.utils import json_renderer
c = Collection(
id=species['id'],
uri=species['uri'],
labels=species['labels'],
notes=species['notes'],
members=[larch['id']],
concept_scheme=trees.concept_scheme
)
r = json_renderer({})
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)
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']:
assert 'label' in l
assert 'type' in l
assert 'language' in l
assert len(coll['notes']) == 1
for n in coll['notes']:
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
def test_import_concept_adapter(self):
from pyramid_skosprovider.utils import concept_adapter as cau
from pyramid_skosprovider.renderers import concept_adapter as car
assert cau == car

def test_import_collection_adapter(self):
from pyramid_skosprovider.utils import collection_adapter as cau
from pyramid_skosprovider.renderers import collection_adapter as car
assert cau == car

def test_import_source_adapter(self):
from pyramid_skosprovider.utils import source_adapter as sau
from pyramid_skosprovider.renderers import source_adapter as sar
assert sau == sar

def test_import_json_renderer(self):
from pyramid_skosprovider.utils import json_renderer as jru
from pyramid_skosprovider.renderers import json_renderer as jrr
assert jru == jrr

0 comments on commit 7c86e19

Please sign in to comment.