Exemple #1
0
 def test_collection_adapter(self):
     from pyramid_skosprovider.renderers import collection_adapter
     c = Collection(id=species['id'],
                    labels=species['labels'],
                    members=species['members'],
                    concept_scheme=trees.concept_scheme)
     request = testing.DummyRequest()
     m = Mock()
     request.skos_registry = m
     request.locale_name = 'nl'
     collection = collection_adapter(c, request)
     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']), 3)
     self._assert_is_labels(collection['labels'])
     self.assertIn('notes', collection)
     assert collection['infer_concept_relations'] is True
     assert 'matches' not in collection
     assert 'superordinates' in collection
     assert 0 == len(collection['superordinates'])
     assert 0 == len(collection['sources'])
     assert collection['label'] == 'Bomen per soort'
Exemple #2
0
 def testLabel(self):
     labels = self._get_labels()
     coll = Collection(350, labels=labels)
     self.assertEqual(label(labels), coll.label())
     self.assertEqual(label(labels, 'nl'), coll.label('nl'))
     self.assertEqual(label(labels, 'en'), coll.label('en'))
     self.assertEqual(label(labels, None), coll.label(None))
 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=species['members'],
         concept_scheme=trees.concept_scheme
     )
     r = json_renderer({})
     jsonstring = r(c, {})
     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)
     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)
     for n in coll['notes']:
         self.assertIsInstance(n, dict)
         self.assertIn('note', n)
         self.assertIn('type', n)
         self.assertIn('language', n)
     assert 'matches' not in coll
Exemple #4
0
 def testEmptyMembers(self):
     labels = self._get_labels()
     coll = Collection(
         350,
         labels=labels,
         members=[]
     )
     self.assertEqual([], coll.members)
Exemple #5
0
 def testMembers(self):
     labels = self._get_labels()
     coll = Collection(
         id=350,
         labels=labels,
         members=[1, 2]
     )
     self.assertTrue(set([1, 2]), set(coll.members))
Exemple #6
0
def from_thing(thing):
    """
        Map a :class:`skosprovider_sqlalchemy.models.Thing` to a
        :class:`skosprovider.skos.Concept` or
        a :class:`skosprovider.skos.Collection`, depending on the type.

        :param skosprovider_sqlalchemy.models.Thing thing: Thing to map.
        :rtype: :class:`~skosprovider.skos.Concept` or
            :class:`~skosprovider.skos.Collection`.
        """
    if thing.type and thing.type == 'collection':
        return Collection(
            id=thing.concept_id,
            uri=thing.uri,
            concept_scheme=ConceptScheme(thing.conceptscheme.uri),
            labels=[
                Label(l.label, l.labeltype_id, l.language_id)
                for l in thing.labels
            ],
            sources=[
                Source(s.citation)
                for s in thing.sources
            ],
            members=[member.concept_id for member in thing.members] if hasattr(thing, 'members') else [],
            member_of=[c.concept_id for c in thing.member_of],
            superordinates=[broader_concept.concept_id for broader_concept in thing.broader_concepts]
        )
    else:
        matches = {}
        for m in thing.matches:
            key = m.matchtype.name[:m.matchtype.name.find('Match')]
            if key not in matches:
                matches[key] = []
            matches[key].append(m.uri)
        return Concept(
            id=thing.concept_id,
            uri=thing.uri,
            concept_scheme=ConceptScheme(thing.conceptscheme.uri),
            labels=[
                Label(l.label, l.labeltype_id, l.language_id)
                for l in thing.labels
            ],
            notes=[
                Note(n.note, n.notetype_id, n.language_id)
                for n in thing.notes
            ],
            sources=[
                Source(s.citation)
                for s in thing.sources
            ],
            broader=[c.concept_id for c in thing.broader_concepts],
            narrower=[c.concept_id for c in thing.narrower_concepts],
            related=[c.concept_id for c in thing.related_concepts],
            member_of=[c.concept_id for c in thing.member_of],
            subordinate_arrays=[narrower_collection.concept_id for narrower_collection in thing.narrower_collections],
            matches=matches,
        )
Exemple #7
0
 def testSortkey(self):
     labels = self._get_labels()
     sl = Label('allereerste', type='sortLabel', language='nl-BE')
     labels.append(sl)
     coll = Collection(350, labels=labels)
     self.assertEqual('allereerste', coll._sortkey('sortlabel'))
     self.assertEqual('allereerste', coll._sortkey('sortlabel', 'nl'))
     self.assertEqual('allereerste', coll._sortkey('sortlabel', 'en'))
     self.assertEqual('deelgemeenten', coll._sortkey('label', 'nl'))
     self.assertEqual('', coll._sortkey('uri'))
Exemple #8
0
    def _from_graph(self):
        clist = []
        for sub, pred, obj in self.graph.triples(
            (None, RDF.type, SKOS.Concept)):
            uri = self.to_text(sub)
            matches = {}
            for k in Concept.matchtypes:
                matches[k] = self._create_from_subject_predicate(
                    sub, URIRef(SKOS + k + 'Match'))
            con = Concept(
                id=self._get_id_for_subject(sub, uri),
                uri=uri,
                concept_scheme=self.concept_scheme,
                labels=self._create_from_subject_typelist(
                    sub, Label.valid_types),
                notes=self._create_from_subject_typelist(
                    sub, Note.valid_types),
                sources=self._create_sources(sub),
                broader=self._create_from_subject_predicate(sub, SKOS.broader),
                narrower=self._create_from_subject_predicate(
                    sub, SKOS.narrower),
                related=self._create_from_subject_predicate(sub, SKOS.related),
                member_of=[],
                subordinate_arrays=self._create_from_subject_predicate(
                    sub, SKOS_THES.subordinateArray),
                matches=matches)
            clist.append(con)

        for sub, pred, obj in self.graph.triples(
            (None, RDF.type, SKOS.Collection)):
            uri = self.to_text(sub)
            col = Collection(
                id=self._get_id_for_subject(sub, uri),
                uri=uri,
                concept_scheme=self.concept_scheme,
                labels=self._create_from_subject_typelist(
                    sub, Label.valid_types),
                notes=self._create_from_subject_typelist(
                    sub, (Note.valid_types)),
                sources=self._create_sources(sub),
                members=self._create_from_subject_predicate(sub, SKOS.member),
                member_of=[],
                superordinates=self._create_from_subject_predicate(
                    sub, SKOS_THES.superOrdinate))
            clist.append(col)
        self._fill_member_of(clist)
        return clist
Exemple #9
0
 def test_json_collection(self):
     from pyramid_skosprovider.renderers 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']) == 3
     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_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, {})
     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'])
Exemple #11
0
 def testSource(self):
     coll = Collection(id=1, sources=[{'citation': 'My citation'}])
     self.assertEqual(1, len(coll.sources))
     self.assertIsInstance(coll.sources[0], Source)
     self.assertEqual('My citation', coll.sources[0].citation)
Exemple #12
0
 def testMemberOf(self):
     coll = Collection(id=1, member_of=[350])
     self.assertTrue(set([350]), set(coll.member_of))
Exemple #13
0
 def testUri(self):
     c = Collection(350, uri='urn:x-skosprovider:gemeenten:350')
     self.assertEqual(350, c.id)
     self.assertEqual('urn:x-skosprovider:gemeenten:350', c.uri)
Exemple #14
0
 def testId(self):
     coll = Collection(350)
     self.assertEqual(350, coll.id)
Exemple #15
0
 def testRepr(self):
     c = Collection(1)
     self.assertEquals("Collection('1')", c.__repr__())
    def _from_thing(self, thing):
        '''
        Load one concept or collection from the database.

        :param :class:`skosprovider_sqlalchemy.models.Thing` thing: Thing
            to load.
        '''
        if thing.type and thing.type == 'collection':
            return Collection(
                id=thing.concept_id,
                uri=thing.uri
                if thing.uri is not None else self.uri_generator.generate(
                    type='collection', id=thing.concept_id),
                concept_scheme=self.concept_scheme,
                labels=[
                    Label(l.label, l.labeltype_id, l.language_id)
                    for l in thing.labels
                ],
                notes=[
                    Note(n.note, n.notetype_id, n.language_id, n.markup)
                    for n in thing.notes
                ],
                sources=[Source(s.citation, s.markup) for s in thing.sources],
                members=[member.concept_id for member in thing.members]
                if hasattr(thing, 'members') else [],
                member_of=[
                    member_of.concept_id for member_of in thing.member_of
                ],
                superordinates=[
                    broader_concept.concept_id
                    for broader_concept in thing.broader_concepts
                ])
        else:
            matches = {}
            for m in thing.matches:
                key = m.matchtype.name[:m.matchtype.name.find('Match')]
                if not key in matches:
                    matches[key] = []
                matches[key].append(m.uri)
            return Concept(
                id=thing.concept_id,
                uri=thing.uri
                if thing.uri is not None else self.uri_generator.generate(
                    type='concept', id=thing.concept_id),
                concept_scheme=self.concept_scheme,
                labels=[
                    Label(l.label, l.labeltype_id, l.language_id)
                    for l in thing.labels
                ],
                notes=[
                    Note(n.note, n.notetype_id, n.language_id, n.markup)
                    for n in thing.notes
                ],
                sources=[Source(s.citation, s.markup) for s in thing.sources],
                broader=[c.concept_id for c in thing.broader_concepts],
                narrower=[c.concept_id for c in thing.narrower_concepts],
                related=[c.concept_id for c in thing.related_concepts],
                member_of=[
                    member_of.concept_id for member_of in thing.member_of
                ],
                subordinate_arrays=[
                    narrower_collection.concept_id
                    for narrower_collection in thing.narrower_collections
                ],
                matches=matches)
Exemple #17
0
 def testnferConceptRelations(self):
     coll = Collection(id=1, )
     self.assertTrue(coll.infer_concept_relations)
     coll = Collection(id=1, infer_concept_relations=False)
     self.assertFalse(coll.infer_concept_relations)