Пример #1
0
 def testLabel(self):
     labels = self._get_labels()
     c = Concept(1, labels=labels)
     self.assertEqual(label(labels), c.label())
     self.assertEqual(label(labels, 'nl'), c.label('nl'))
     self.assertEqual(label(labels, 'en'), c.label('en'))
     self.assertEqual(label(labels, None), c.label(None))
Пример #2
0
 def testIter(self):
     c = Concept(1)
     keys = [
         'id', 'uri', 'labels', 'notes', 'broader', 'narrower', 'related'
     ]
     for k in c.keys():
         self.assertIn(k, keys)
Пример #3
0
 def testSortKey(self):
     labels = self._get_labels()
     sl = Label('allereerste', type='sortLabel', language='nl-BE')
     labels.append(sl)
     c = Concept(1, labels=labels)
     self.assertEqual('allereerste', c._sortkey('sortlabel'))
     self.assertEqual('allereerste', c._sortkey('sortlabel', 'nl'))
     self.assertEqual('knocke-heyst', c._sortkey('sortlabel', 'en'))
     self.assertEqual('', c._sortkey('uri'))
Пример #4
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
Пример #5
0
 def test_json_concept(self):
     from pyramid_skosprovider.renderers 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({})
     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)
     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']), 3)
     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'])
Пример #6
0
def things_from_graph(graph, concept_scheme):
    '''
    Read concepts and collections from a graph.

    :param rdflib.Graph graph: Graph to read from.
    :param skosprovider.skos.ConceptScheme concept_scheme: Conceptscheme the
        concepts and collections belong to.
    :rtype: :class:`list`
    '''
    clist = []
    valid_label_types = Label.valid_types[:]
    valid_label_types.remove('sortLabel')
    for sub, pred, obj in graph.triples((None, RDF.type, SKOS.Concept)):
        uri = str(sub)
        con = Concept(
            id=_split_uri(uri, 1),
            uri=uri,
            concept_scheme=concept_scheme,
            labels=_create_from_subject_typelist(graph, sub,
                                                 valid_label_types),
            notes=_create_from_subject_typelist(graph, sub, Note.valid_types),
            broader=_create_from_subject_predicate(graph, sub, SKOS.broader),
            narrower=_create_from_subject_predicate(graph, sub, SKOS.narrower),
            related=_create_from_subject_predicate(graph, sub, SKOS.related),
            subordinate_arrays=[])
        clist.append(con)

        # at this moment, Heritagedata does not support SKOS.Collection
    # for sub, pred, obj in graph.triples((None, RDF.type, SKOS.Collection)):
    # uri = str(sub)
    #     col = Collection(_split_uri(uri, 1), uri=uri)
    #     col.members = _create_from_subject_predicate(sub, SKOS.member)
    #     col.labels = _create_from_subject_typelist(sub, Label.valid_types)
    #     col.notes = _create_from_subject_typelist(sub, Note.valid_types)
    #     clist.append(col)

    return clist
Пример #7
0
 def testSource(self):
     c = Concept(id=1, sources=[{'citation': 'My citation'}])
     self.assertEqual(1, len(c.sources))
     self.assertIsInstance(c.sources[0], Source)
     self.assertEqual('My citation', c.sources[0].citation)
Пример #8
0
 def testMemberOf(self):
     c = Concept(1, uri='urn:x-skosprovider:gemeenten:1', member_of=[15])
     self.assertEqual(set([15]), set(c.member_of))
Пример #9
0
 def testUri(self):
     c = Concept(1, uri='urn:x-skosprovider:gemeenten:1')
     self.assertEqual(1, c.id)
     self.assertEqual('urn:x-skosprovider:gemeenten:1', c.uri)
Пример #10
0
 def testRepr(self):
     c = Concept(1)
     self.assertEquals("Concept('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)
Пример #12
0
 def testRepr(self):
     c = Concept(1)
     self.assertEqual("Concept('1')", c.__repr__())