class TestSQLAlchemyProviderExpandVisitNoVisitation(DBTestCase):

    def setUp(self):
        Base.metadata.create_all(self.engine)
        self.session = self.session_maker()
        Initialiser(self.session).init_all()
        self.visitationprovider=SQLAlchemyProvider(
            {'id': 'SOORTEN', 'conceptscheme_id': 1},
            self.session_maker,
            expand_strategy='visit'
        )

    def tearDown(self):
        self.session.rollback()
        self.session.close_all()
        Base.metadata.drop_all(self.engine)

    def test_expand_concept(self):
        ids = self.visitationprovider.expand(1)
        assert not ids

    def test_expand_collection_visit(self):
        ids = self.visitationprovider.expand(2)
        assert not ids

    def test_expand_concept_without_narrower_visit(self):
        ids = self.visitationprovider.expand(3)
        assert not ids

    def test_expand_unexisting_visit(self):
        ids = self.visitationprovider.expand(404)
        assert not ids
Example #2
0
class TestSQLAlchemyProviderExpandVisitNoVisitation(DBTestCase):
    def setUp(self):
        Base.metadata.create_all(self.engine)
        self.session = self.session_maker()
        Initialiser(self.session).init_all()
        self.visitationprovider = SQLAlchemyProvider(
            {
                'id': 'SOORTEN',
                'conceptscheme_id': 1
            },
            self.session_maker,
            expand_strategy='visit')

    def tearDown(self):
        self.session.rollback()
        self.session.close_all()
        Base.metadata.drop_all(self.engine)

    def test_expand_concept(self):
        ids = self.visitationprovider.expand(1)
        assert not ids

    def test_expand_collection_visit(self):
        ids = self.visitationprovider.expand(2)
        assert not ids

    def test_expand_concept_without_narrower_visit(self):
        ids = self.visitationprovider.expand(3)
        assert not ids

    def test_expand_unexisting_visit(self):
        ids = self.visitationprovider.expand(404)
        assert not ids
Example #3
0
 def setUp(self):
     Base.metadata.create_all(self.engine)
     self.session = self.session_maker()
     Initialiser(self.session).init_all()
     self.visitationprovider = SQLAlchemyProvider(
         {
             'id': 'SOORTEN',
             'conceptscheme_id': 1
         },
         self.session_maker,
         expand_strategy='visit')
Example #4
0
 def setUp(self):
     Base.metadata.create_all(self.engine)
     self.session = self.session_maker()
     Initialiser(self.session).init_all()
     create_data(self.session)
     self.provider = SQLAlchemyProvider(
         {
             'id': 'SOORTEN',
             'conceptscheme_id': 1
         },
         self.session_maker,
         uri_generator=UriPatternGenerator('urn:x-skosprovider-sa:test:%s'))
 def test_gen_uri(self):
     from skosprovider_sqlalchemy.models import Concept, ConceptScheme
     from skosprovider.uri import UriPatternGenerator
     # Set up provider
     provider = SQLAlchemyProvider(
         {'id': 'SOORTEN', 'conceptscheme_id': 99},
         self.session_maker,
         uri_generator=UriPatternGenerator('http://id.example.com/trees/%s')
     )
     c1 = Concept(concept_id=1, conceptscheme=ConceptScheme(id=99, uri='http://id.example.com/trees'))
     session = self.session_maker()
     session.add(c1)
     session.commit()
     assert c1.uri is None
     c2 = provider.get_by_id(1)
     assert c2.uri == 'http://id.example.com/trees/1'
Example #6
0
def includeme(config):
    TERMEN = SQLAlchemyProvider({
        'id': 'TERMEN',
        'conceptscheme_id': 1
    }, config.registry.dbmaker)
    skosregis = config.get_skos_registry()
    skosregis.register_provider(TERMEN)
Example #7
0
 def test_set_invalid_expand_strategy(self):
     with pytest.raises(ValueError):
         SQLAlchemyProvider({
             'id': 'SOORTEN',
             'conceptscheme_id': 1
         },
                            self.session_maker,
                            expand_strategy='invalid')
 def test_override_expand_strategy(self):
     # Set up provider
     self.provider = SQLAlchemyProvider(
         {'id': 'SOORTEN', 'conceptscheme_id': 1},
         self.session,
         expand_strategy='visit'
     )
     self.assertEqual('visit', self.provider.expand_strategy)
Example #9
0
 def test_internal_providers(self):
     self.dummy.provider = SQLAlchemyProvider(metadata={
         'id': 'Test',
         'conceptscheme_id': 1
     },
                                              session_maker=None)
     self.dummy.internal_providers('ok')
     self.assertEqual(self.dummy.dummy, 'ok')
Example #10
0
 def _check_menu(self, uri_pattern=None):
     if not uri_pattern:
         uri_pattern = 'urn:x-skosprovider:menu:%s'
     sql_prov = SQLAlchemyProvider({
         'id': 'MENU',
         'conceptscheme_id': 1
     }, self.session_maker)
     self.assertEqual(11, len(sql_prov.get_all()))
     eb = sql_prov.get_by_id(1)
     self.assertIsInstance(eb, Concept)
     self.assertEqual(1, eb.id)
     self.assertEqual(uri_pattern % '1', eb.uri)
     self.assertEqual('Egg and Bacon', eb.label().label)
     self.assertEqual('prefLabel', eb.label().type)
     self.assertEqual([], eb.notes)
     eb = sql_prov.get_by_uri(uri_pattern % '3')
     self.assertIsInstance(eb, Concept)
     self.assertEqual(3, eb.id)
     self.assertEqual(uri_pattern % '3', eb.uri)
     spam = sql_prov.find({'label': 'Spam'})
     self.assertEqual(8, len(spam))
     eb = sql_prov.get_by_id(11)
     self.assertIsInstance(eb, Concept)
     self.assertEqual(11, eb.id)
     self.assertEqual('Lobster Thermidor', eb.label().label)
     self.assertIsInstance(eb.notes[0], Note)
     self.assertIn('Mornay', eb.notes[0].note)
     self.assertEqual('note', eb.notes[0].type)
Example #11
0
 def test_override_expand_strategy(self):
     # Set up provider
     provider = SQLAlchemyProvider({
         'id': 'SOORTEN',
         'conceptscheme_id': 1
     },
                                   self.session_maker,
                                   expand_strategy='visit')
     assert 'visit' == provider.expand_strategy
 def setUp(self):
     Base.metadata.create_all(self.engine)
     self.session = self.session_maker()
     Initialiser(self.session).init_all()
     self.visitationprovider=SQLAlchemyProvider(
         {'id': 'SOORTEN', 'conceptscheme_id': 1},
         self.session_maker,
         expand_strategy='visit'
     )
 def setUp(self):
     Base.metadata.create_all(self.engine)
     self.session = self.session_maker()
     Initialiser(self.session).init_all()
     create_data(self.session)
     self.provider = SQLAlchemyProvider(
         {'id': 'SOORTEN', 'conceptscheme_id': 1},
         self.session_maker,
         uri_generator=UriPatternGenerator('urn:x-skosprovider-sa:test:%s')
     )
Example #14
0
 def test_external_providers(self):
     self.dummy.provider = SQLAlchemyProvider(metadata={
         'id': 'Test',
         'conceptscheme_id': 1,
         'subject': ['external']
     },
                                              session_maker=None)
     self.assertRaises(HTTPMethodNotAllowed, self.dummy.internal_providers,
                       'ok')
     self.assertIsNone(self.dummy.dummy)
Example #15
0
 def test_gen_uri(self):
     from skosprovider_sqlalchemy.models import Concept, ConceptScheme
     from skosprovider.uri import UriPatternGenerator
     # Set up provider
     provider = SQLAlchemyProvider({
         'id': 'SOORTEN',
         'conceptscheme_id': 99
     },
                                   self.session_maker,
                                   uri_generator=UriPatternGenerator(
                                       'http://id.example.com/trees/%s'))
     c1 = Concept(concept_id=1,
                  conceptscheme=ConceptScheme(
                      id=99, uri='http://id.example.com/trees'))
     session = self.session_maker()
     session.add(c1)
     session.commit()
     assert c1.uri is None
     c2 = provider.get_by_id(1)
     assert c2.uri == 'http://id.example.com/trees/1'
Example #16
0
 def test_set_uri_generator(self):
     from skosprovider.uri import UriPatternGenerator
     # Set up provider
     provider = SQLAlchemyProvider({
         'id': 'SOORTEN',
         'conceptscheme_id': 1
     },
                                   self.session_maker,
                                   uri_generator=UriPatternGenerator(
                                       'http://id.example.com/trees/%s'))
     assert 'http://id.example.com/trees/1' == provider.uri_generator.generate(
         id=1)
Example #17
0
    def _check_trees(self, conceptscheme_label):
        sql_prov = SQLAlchemyProvider({
            'id': 'TREES',
            'conceptscheme_id': 1
        }, self.session_maker)
        dump = dict_dumper(sql_prov)

        self.assertEqual(conceptscheme_label,
                         sql_prov.concept_scheme.labels[0].label)
        obj_1 = [
            item for item in dump if item['uri'] == 'http://id.trees.org/2'
        ][0]
        self.assertEqual(obj_1['broader'], [])
        self.assertEqual(obj_1['id'], 2)
        self.assertEqual(obj_1['member_of'], [3])
        self.assertEqual(obj_1['narrower'], [])
        label_en = [
            label for label in obj_1['labels'] if label['language'] == 'en'
        ][0]
        self.assertDictEqual(label_en, {
            'label': 'The Chestnut',
            'language': 'en',
            'type': 'prefLabel'
        })
        label_nl = [
            label for label in obj_1['labels'] if label['language'] == 'nl'
        ][0]
        self.assertDictEqual(label_nl, {
            'label': 'De Paardekastanje',
            'language': 'nl',
            'type': 'altLabel'
        })
        label_fr = [
            label for label in obj_1['labels'] if label['language'] == 'fr'
        ][0]
        self.assertDictEqual(label_fr, {
            'label': u'la châtaigne',
            'language': 'fr',
            'type': 'altLabel'
        })
        self.assertDictEqual(
            obj_1['notes'][0], {
                'language': 'en',
                'note': 'A different type of tree.',
                'type': 'definition',
                'markup': None
            })
    def setUp(self):
        connection = self.engine.connect()
        self.trans = connection.begin()

        # Setting up SQLAlchemy
        from skosprovider_sqlalchemy.models import Base
        Base.metadata.bind = engine
        sm = sessionmaker(bind=engine)
        self.session = sm()

        # Set up testdata
        self._create_test_data()

        # Set up provider
        self.provider = SQLAlchemyProvider(
            {'id': 'SOORTEN', 'conceptscheme_id': 1},
            self.session,
        )
Example #19
0
def get_internal_providers(config):
    ret = []
    engine = create_engine(config.get_settings()['sqlalchemy.url'], echo=True)
    engine.connect()
    result = engine.execute('SELECT * from conceptscheme')
    for row in result:
        scheme_db_id = row[0]
        scheme_uri = row[1]
        scheme_id = scheme_uri.split('/')[-1]
        scheme = SQLAlchemyProvider(
            {
                'id': scheme_id,
                'conceptscheme_id': scheme_db_id
            },
            config.registry.dbmaker,
            uri_generator=UriPatternGenerator(f'{scheme_uri}/%s'))
        ret.append(scheme)
    engine.dispose()
    return ret
 def _check_menu(self):
     sql_prov = SQLAlchemyProvider({'id': 'MENU', 'conceptscheme_id': 1}, self.session_maker)
     self.assertEqual(11, len(sql_prov.get_all()))
     eb = sql_prov.get_by_id(1)
     self.assertIsInstance(eb, Concept)
     self.assertEqual(1, eb.id)
     self.assertEqual('urn:x-skosprovider:menu:1', eb.uri)
     self.assertEqual('Egg and Bacon', eb.label().label)
     self.assertEqual('prefLabel', eb.label().type)
     self.assertEqual([], eb.notes)
     eb = sql_prov.get_by_uri('urn:x-skosprovider:menu:3')
     self.assertIsInstance(eb, Concept)
     self.assertEqual(3, eb.id)
     self.assertEqual('urn:x-skosprovider:menu:3', eb.uri)
     spam = sql_prov.find({'label': 'Spam'})
     self.assertEqual(8, len(spam))
     eb = sql_prov.get_by_id(11)
     self.assertIsInstance(eb, Concept)
     self.assertEqual(11, eb.id)
     self.assertEqual('Lobster Thermidor', eb.label().label)
     self.assertIsInstance(eb.notes[0], Note)
     self.assertIn('Mornay', eb.notes[0].note)
     self.assertEqual('note', eb.notes[0].type)
Example #21
0
    def setUp(self):
        self.config = Configurator(settings=settings)
        self.config.add_route('login', '/auth/login')
        self.config.add_route('logout', '/auth/logout')
        includeme(self.config)
        self.config.add_static_view('atramhasis/static', 'atramhasis:static')

        Base.metadata.drop_all(self.engine)
        Base.metadata.create_all(self.engine)

        Base.metadata.bind = self.engine

        self.config.registry.dbmaker = self.session_maker
        self.config.add_request_method(db, reify=True)

        with transaction.manager:
            local_session = self.session_maker()
            import_provider(
                trees, ConceptScheme(id=1, uri='urn:x-skosprovider:trees'),
                local_session)
            import_provider(materials,
                            ConceptScheme(id=4, uri='urn:x-vioe:materials'),
                            local_session)
            import_provider(geo, ConceptScheme(id=2), local_session)
            local_session.add(ConceptScheme(id=3))
            local_session.add(LabelType('hiddenLabel', 'A hidden label.'))
            local_session.add(LabelType('altLabel', 'An alternative label.'))
            local_session.add(LabelType('prefLabel', 'A preferred label.'))
            local_session.add(Language('nl', 'Dutch'))
            local_session.add(Language('en', 'English'))

            local_session.add(MatchType('broadMatch', ''))
            local_session.add(MatchType('closeMatch', ''))
            local_session.add(MatchType('exactMatch', ''))
            local_session.add(MatchType('narrowMatch', ''))
            local_session.add(MatchType('relatedMatch', ''))
            local_session.add(Language(id='de', name='test'))

        TREES = SQLAlchemyProvider({
            'id': 'TREES',
            'conceptscheme_id': 1
        }, self.config.registry.dbmaker)

        GEO = SQLAlchemyProvider({
            'id': 'GEOGRAPHY',
            'conceptscheme_id': 2
        }, self.config.registry.dbmaker)

        STYLES = SQLAlchemyProvider({
            'id': 'STYLES',
            'conceptscheme_id': 3
        }, self.config.registry.dbmaker)

        MATERIALS = SQLAlchemyProvider(
            {
                'id': 'MATERIALS',
                'conceptscheme_id': 4
            },
            self.config.registry.dbmaker,
            uri_generator=UriPatternGenerator('urn:x-vioe:materials:%s'))

        self.config.add_subscriber(self.mock_event_handler,
                                   ProtectedResourceEvent)
        self.config.add_subscriber(
            self.mock_event_handler_provider_unavailable,
            ProtectedResourceEvent)

        skosregis = self.config.get_skos_registry()
        skosregis.register_provider(TREES)
        skosregis.register_provider(GEO)
        skosregis.register_provider(STYLES)
        skosregis.register_provider(MATERIALS)
        skosregis.register_provider(TEST)

        self.app = self.config.make_wsgi_app()
        self.testapp = TestApp(self.app)
Example #22
0
def includeme(config):  # pragma: no cover
    TREES = SQLAlchemyProvider({
        'id': 'TREES',
        'conceptscheme_id': 1
    }, config.registry.dbmaker)

    GEO = SQLAlchemyProvider({
        'id': 'GEOGRAPHY',
        'conceptscheme_id': 2
    }, config.registry.dbmaker)

    STYLES = SQLAlchemyProvider({
        'id': 'STYLES',
        'conceptscheme_id': 3
    }, config.registry.dbmaker)

    MATERIALS = SQLAlchemyProvider(
        {
            'id': 'MATERIALS',
            'conceptscheme_id': 4
        },
        config.registry.dbmaker,
        uri_generator=UriPatternGenerator('urn:x-vioe:materials:%s'))

    # use 'subject': ['external'] for read only external providers (only available in REST service)

    AAT = AATProvider({'id': 'AAT', 'subject': ['external']}, )

    TGN = TGNProvider({'id': 'TGN', 'subject': ['external']})

    EH_PERIOD = HeritagedataProvider(
        {
            'id': 'EH_PERIOD',
            'subject': ['external']
        },
        scheme_uri='http://purl.org/heritagedata/schemes/eh_period')

    EH_MONUMENT_TYPE = HeritagedataProvider(
        {
            'id': 'EH_MONUMENT_TYPE',
            'subject': ['external']
        },
        scheme_uri='http://purl.org/heritagedata/schemes/eh_tmt2')

    EH_MATERIALS = HeritagedataProvider(
        {
            'id': 'EH_MATERIALS',
            'subject': ['external']
        },
        scheme_uri='http://purl.org/heritagedata/schemes/eh_tbm')

    skosregis = config.get_skos_registry()
    skosregis.register_provider(TREES)
    skosregis.register_provider(GEO)
    skosregis.register_provider(STYLES)
    skosregis.register_provider(MATERIALS)
    skosregis.register_provider(AAT)
    skosregis.register_provider(TGN)
    skosregis.register_provider(EH_PERIOD)
    skosregis.register_provider(EH_MONUMENT_TYPE)
    skosregis.register_provider(EH_MATERIALS)
class SQLAlchemyProviderExpandVisitNoVisitationTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.engine = engine

    def setUp(self):
        connection = self.engine.connect()
        self.trans = connection.begin()

        # Setting up SQLAlchemy
        from skosprovider_sqlalchemy.models import Base
        Base.metadata.bind = engine
        sm = sessionmaker(bind=engine)
        self.session = sm()

        # Set up testdata
        self._create_test_data()

        # Set up provider
        self.provider = SQLAlchemyProvider(
            {'id': 'SOORTEN', 'conceptscheme_id': 2},
            self.session,
            expand_strategy='visit'
        )

    def tearDown(self):
        self.session.close()
        self.trans.rollback()

    def _create_test_data(self):
        from ..models import (
            Concept,
            ConceptScheme,
            Collection,
            Label
        )
        cs = ConceptScheme(
            id=2
        )
        self.session.add(cs)
        con = Concept(
            id=1,
            concept_id=1,
            conceptscheme=cs
        )
        self.session.add(con)
        l = Label('Churches', 'prefLabel', 'en')
        con.labels.append(l)
        l = Label('Kerken', 'prefLabel', 'nl')
        con.labels.append(l)
        col = Collection(
            id=2,
            concept_id=2,
            conceptscheme=cs
        )
        l = Label('Churches by function', 'prefLabel', 'en')
        col.labels.append(l)
        col.members.add(con)
        self.session.add(col)
        chap = Concept(
            id=3,
            concept_id=3,
            conceptscheme=cs
        )
        l = Label('Chapels', 'prefLabel', 'en')
        chap.labels.append(l)
        self.session.add(chap)
        chap.related_concepts.add(con)
        cath = Concept(
            id=4,
            concept_id=4,
            conceptscheme=cs
        )
        l = Label('Cathedrals', 'prefLabel', 'en')
        cath.labels.append(l)
        self.session.add(cath)
        cath.broader_concepts.add(con)

    def test_expand_concept(self):
        ids = self.provider.expand_concept(1)
        self.assertFalse(ids)

    def test_expand_collection_visit(self):
        ids = self.provider.expand(2)
        self.assertFalse(ids)

    def test_expand_concept_without_narrower_visit(self):
        ids = self.provider.expand(3)
        self.assertFalse(ids)

    def test_expand_unexisting_visit(self):
        ids = self.provider.expand(404)
        self.assertFalse(ids)
class SQLAlchemyProviderTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.engine = engine

    def setUp(self):
        connection = self.engine.connect()
        self.trans = connection.begin()

        # Setting up SQLAlchemy
        from skosprovider_sqlalchemy.models import Base
        Base.metadata.bind = engine
        sm = sessionmaker(bind=engine)
        self.session = sm()

        # Set up testdata
        self._create_test_data()

        # Set up provider
        self.provider = SQLAlchemyProvider(
            {'id': 'SOORTEN', 'conceptscheme_id': 1},
            self.session,
        )

    def tearDown(self):
        self.trans.rollback()
        self.session.close()

    def _create_test_data(self):
        from ..models import (
            Concept,
            ConceptScheme,
            Collection,
            Label
        )
        cs = ConceptScheme(
            id=1
        )
        self.session.add(cs)
        con = Concept(
            id=1,
            concept_id=1,
            conceptscheme=cs
        )
        self.session.add(con)
        l = Label('Churches', 'prefLabel', 'en')
        con.labels.append(l)
        l = Label('Kerken', 'prefLabel', 'nl')
        con.labels.append(l)
        col = Collection(
            id=2,
            concept_id=2,
            conceptscheme=cs
        )
        l = Label('Churches by function', 'prefLabel', 'en')
        col.labels.append(l)
        col.members.add(con)
        self.session.add(col)
        chap = Concept(
            id=3,
            concept_id=3,
            conceptscheme=cs
        )
        l = Label('Chapels', 'prefLabel', 'en')
        chap.labels.append(l)
        self.session.add(chap)
        chap.related_concepts.add(con)
        cath = Concept(
            id=4,
            concept_id=4,
            conceptscheme=cs
        )
        l = Label('Cathedrals', 'prefLabel', 'en')
        cath.labels.append(l)
        self.session.add(cath)
        cath.broader_concepts.add(con)
        self.session.flush()

    def test_default_recurse_strategy(self):
        self.assertEqual('recurse', self.provider.expand_strategy)

    def test_override_expand_strategy(self):
        # Set up provider
        self.provider = SQLAlchemyProvider(
            {'id': 'SOORTEN', 'conceptscheme_id': 1},
            self.session,
            expand_strategy='visit'
        )
        self.assertEqual('visit', self.provider.expand_strategy)

    def test_set_invalid_expand_strategy(self):
        self.assertRaises(
            ValueError,
            self.provider.__init__,
            {'id': 'SOORTEN', 'conceptscheme_id': 1},
            self.session,
            expand_strategy='invalid'
        )

    def test_get_vocabulary_id(self):
        self.assertEquals('SOORTEN', self.provider.get_vocabulary_id())

    def test_get_concept_by_id(self):
        from skosprovider.skos import Concept
        con = self.provider.get_by_id(1)
        self.assertIsInstance(con, Concept)
        self.assertEqual(1, con.id)
        self.assertEqual([3], con.related)
        self.assertEqual([4], con.narrower)

    def test_get_unexisting_by_id(self):
        con = self.provider.get_by_id(404)
        self.assertFalse(con)

    def test_get_collection_by_id(self):
        from skosprovider.skos import Collection
        col = self.provider.get_by_id(2)
        self.assertIsInstance(col, Collection)
        self.assertEquals(2, col.id)

    def test_get_all(self):
        all = self.provider.get_all()
        self.assertEquals(4, len(all))
        self.assertIn({'id': 1, 'label': 'Churches'}, all)
        self.assertIn({'id': 2, 'label': 'Churches by function'}, all)
        self.assertIn({'id': 3, 'label': 'Chapels'}, all)
        self.assertIn({'id': 4, 'label': 'Cathedrals'}, all)

    def test_find_all(self):
        all = self.provider.find({})
        self.assertEquals(4, len(all))
        self.assertIn({'id': 1, 'label': 'Churches'}, all)
        self.assertIn({'id': 2, 'label': 'Churches by function'}, all)
        self.assertIn({'id': 3, 'label': 'Chapels'}, all)
        self.assertIn({'id': 4, 'label': 'Cathedrals'}, all)

    def test_find_type_all(self):
        all = self.provider.find({'type': 'all'})
        self.assertEquals(4, len(all))
        self.assertIn({'id': 1, 'label': 'Churches'}, all)
        self.assertIn({'id': 2, 'label': 'Churches by function'}, all)
        self.assertIn({'id': 3, 'label': 'Chapels'}, all)
        self.assertIn({'id': 4, 'label': 'Cathedrals'}, all)

    def test_find_type_concept(self):
        all = self.provider.find({'type': 'concept'})
        self.assertEquals(3, len(all))
        self.assertIn({'id': 1, 'label': 'Churches'}, all)
        self.assertIn({'id': 3, 'label': 'Chapels'}, all)
        self.assertIn({'id': 4, 'label': 'Cathedrals'}, all)

    def test_find_type_collection(self):
        all = self.provider.find({'type': 'collection'})
        self.assertEquals(1, len(all))
        self.assertIn({'id': 2, 'label': 'Churches by function'}, all)

    def test_find_label_kerken(self):
        all = self.provider.find({'label': 'kerken'})
        self.assertEquals(1, len(all))
        self.assertIn({'id': 1, 'label': 'Churches'}, all)

    def test_find_label_churches_type_concept(self):
        all = self.provider.find({'label': 'churches', 'type': 'concept'})
        self.assertEquals(1, len(all))
        self.assertIn({'id': 1, 'label': 'Churches'}, all)

    def test_find_collection_unexisting(self):
        self.assertRaises(
            ValueError,
            self.provider.find,
            {'collection': {'id': 404}}
        )

    def test_find_collection_2_no_depth(self):
        all = self.provider.find({'collection': {'id': 2}})
        self.assertEquals(1, len(all))
        self.assertIn({'id': 1, 'label': 'Churches'}, all)

    def test_expand_concept(self):
        ids = self.provider.expand_concept(1)
        self.assertEquals([1, 4], ids)

    def test_expand_collection(self):
        ids = self.provider.expand(2)
        self.assertEquals([1, 4], ids)

    def test_expand_concept_without_narrower(self):
        ids = self.provider.expand(3)
        self.assertEquals([3], ids)

    def test_expand_unexisting(self):
        ids = self.provider.expand(404)
        self.assertFalse(ids)
class TestSQLAlchemyProvider(DBTestCase):

    def setUp(self):
        Base.metadata.create_all(self.engine)
        self.session = self.session_maker()
        Initialiser(self.session).init_all()
        create_data(self.session)
        self.provider = SQLAlchemyProvider(
            {'id': 'SOORTEN', 'conceptscheme_id': 1},
            self.session_maker,
            uri_generator=UriPatternGenerator('urn:x-skosprovider-sa:test:%s')
        )

    def tearDown(self):
        self.session.rollback()
        self.session.close_all()
        Base.metadata.drop_all(self.engine)

    def test_default_recurse_strategy(self):
        assert 'recurse' == self.provider.expand_strategy

    def test_override_expand_strategy(self):
        # Set up provider
        provider = SQLAlchemyProvider(
            {'id': 'SOORTEN', 'conceptscheme_id': 1},
            self.session_maker,
            expand_strategy='visit'
        )
        assert 'visit' == provider.expand_strategy

    def test_set_invalid_expand_strategy(self):
        with pytest.raises(ValueError):
            SQLAlchemyProvider(
                {'id': 'SOORTEN', 'conceptscheme_id': 1},
                self.session_maker,
                expand_strategy='invalid'
            )

    def test_provider_without_cs_id(self):
        with pytest.raises(ValueError):
            SQLAlchemyProvider(
                {'id': 'SOORTEN'},
                self.session_maker
            )

    def test_get_vocabulary_id(self):
        assert 'SOORTEN' == self.provider.get_vocabulary_id()

    def test_set_uri_generator(self):
        from skosprovider.uri import UriPatternGenerator
        # Set up provider
        provider = SQLAlchemyProvider(
            {'id': 'SOORTEN', 'conceptscheme_id': 1},
            self.session_maker,
            uri_generator=UriPatternGenerator('http://id.example.com/trees/%s')
        )
        assert 'http://id.example.com/trees/1' == provider.uri_generator.generate(id=1)

    def test_gen_uri(self):
        from skosprovider_sqlalchemy.models import Concept, ConceptScheme
        from skosprovider.uri import UriPatternGenerator
        # Set up provider
        provider = SQLAlchemyProvider(
            {'id': 'SOORTEN', 'conceptscheme_id': 99},
            self.session_maker,
            uri_generator=UriPatternGenerator('http://id.example.com/trees/%s')
        )
        c1 = Concept(concept_id=1, conceptscheme=ConceptScheme(id=99, uri='http://id.example.com/trees'))
        session = self.session_maker()
        session.add(c1)
        session.commit()
        assert c1.uri is None
        c2 = provider.get_by_id(1)
        assert c2.uri == 'http://id.example.com/trees/1'

    def test_concept_scheme(self):
        from skosprovider.skos import (
            ConceptScheme
        )
        cs = self.provider.concept_scheme
        assert isinstance(cs, ConceptScheme)
        assert 'urn:x-skosprovider:test' == cs.uri
        assert 2 == len(cs.languages)
        assert 'en' in cs.languages

    def test_get_concept_by_id(self):
        from skosprovider.skos import Concept

        con = self.provider.get_by_id(1)
        assert isinstance(con, Concept)
        assert 1 == con.id
        assert [3] == con.related
        assert [4] == con.narrower
        assert [2] == con.subordinate_arrays

    def test_concept_has_concept_scheme(self):
        from skosprovider.skos import (
            ConceptScheme
        )
        con = self.provider.get_by_id(1)
        assert isinstance(con.concept_scheme, ConceptScheme)
        assert 'urn:x-skosprovider:test' == con.concept_scheme.uri

    def test_get_concept_by_id_string(self):
        from skosprovider.skos import Concept

        con = self.provider.get_by_id('1')
        assert isinstance(con, Concept)
        assert 1 == con.id
        assert [3] == con.related
        assert [4] == con.narrower
        assert [2] == con.subordinate_arrays

    def test_get_unexisting_by_id(self):
        con = self.provider.get_by_id(404)
        assert not con

    def test_get_concept_by_uri(self):
        cona = self.provider.get_by_id(1)
        conb = self.provider.get_by_uri('urn:x-skosprovider:test:1')
        assert cona.id == conb.id
        assert cona.uri == conb.uri

    def test_get_unexisting_by_uri(self):
        con = self.provider.get_by_uri('urn:x-skosprovider:test:404')
        assert not con

    def test_concept_has_correct_note(self):
        from skosprovider.skos import Note

        cath = self.provider.get_by_id(4)
        assert len(cath.notes) == 1
        assert isinstance(cath.notes[0], Note)

    def test_concept_has_matches(self):
        cath = self.provider.get_by_id(4)
        assert len(cath.matches.keys()) == 5
        assert len(cath.matches['close']) == 1
        assert cath.matches['close'][0] == 'http://vocab.getty.edu/aat/300007501'

    def test_get_collection_by_id(self):
        from skosprovider.skos import Collection

        col = self.provider.get_by_id(2)
        assert isinstance(col, Collection)
        assert 2 == col.id
        assert [4] == col.members
        assert [1] == col.superordinates

    def test_collection_has_no_matches(self):
        col = self.provider.get_by_id(2)
        assert not hasattr(col, 'matches')

    def test_get_collection_by_uri(self):
        from skosprovider.skos import Collection

        cola = self.provider.get_by_id(2)
        colb = self.provider.get_by_uri('urn:x-skosprovider:test:2')
        assert isinstance(colb, Collection)
        assert cola.id == colb.id
        assert cola.uri == colb.uri

    def test_get_all(self):
        all = self.provider.get_all()
        assert len(all) == 5
        assert {
                   'id': 1,
                   'uri': 'urn:x-skosprovider:test:1',
                   'type': 'concept',
                   'label': 'Churches'
               } in all

        assert {
                   'id': 2,
                   'uri': 'urn:x-skosprovider:test:2',
                   'type': 'collection',
                   'label': 'Churches by function'
               } in all

        assert {
                   'id': 3,
                   'uri': 'urn:x-skosprovider:test:3',
                   'type': 'concept',
                   'label': 'Chapels'
               } in all

        assert {
                   'id': 4,
                   'uri': 'urn:x-skosprovider:test:4',
                   'type': 'concept',
                   'label': 'Cathedrals'
               } in all

        assert {
                   'id': 5,
                   'uri': 'urn:x-skosprovider:test:5',
                   'type': 'concept',
                   'label': 'Boomkapellen'
               } in all

    def test_get_all_sorted_id_desc(self):
        all = self.provider.get_all(sort='id', sort_order='desc')
        assert len(all) == 5
        assert [5, 4, 3, 2, 1] == [c['id'] for c in all]

    def test_get_all_sorted_label(self):
        all = self.provider.get_all(sort='label')
        assert len(all) == 5
        assert [
            'Boomkapellen', 'Cathedrals',
            'Chapels', 'Churches',
            'Churches by function'
        ] == [c['label'] for c in all]

    def test_get_all_sorted_sortlabel_desc(self):
        all = self.provider.get_all(sort='sortlabel', sort_order='desc')
        assert len(all) == 5
        assert [
            'Churches',
            'Chapels',
            'Cathedrals',
            'Boomkapellen',
            'Churches by function',
        ] == [c['label'] for c in all]

    def test_get_top_concepts(self):
        all = self.provider.get_top_concepts()
        assert len(all) == 2

        assert {
                   'id': 1,
                   'uri': 'urn:x-skosprovider:test:1',
                   'type': 'concept',
                   'label': 'Churches'
               } in all

        assert {
                   'id': 3,
                   'uri': 'urn:x-skosprovider:test:3',
                   'type': 'concept',
                   'label': 'Chapels'
               } in all

    def test_get_top_concepts_sort_uri_desc(self):
        all = self.provider.get_top_concepts(sort='uri', sort_order='desc')
        assert len(all) == 2

        assert [
            'urn:x-skosprovider:test:3',
            'urn:x-skosprovider:test:1',
        ] == [c['uri'] for c in all]

    def test_get_top_display(self):
        all = self.provider.get_top_display()
        assert len(all) == 2
        assert {
                   'id': 3,
                   'uri': 'urn:x-skosprovider:test:3',
                   'type': 'concept',
                   'label': 'Chapels'
               } in all

        assert {
                   'id': 1,
                   'uri': 'urn:x-skosprovider:test:1',
                   'type': 'concept',
                   'label': 'Churches'
               } in all

    def test_get_top_display_british_sort_label_desc(self):
        all = self.provider.get_top_display(language='en-GB', sort='label', sort_order='desc')
        assert len(all) == 2

        assert [
            'Churches',
            'Chapels'
        ] == [c['label'] for c in all]

    def test_get_children_display_unexisting(self):
        children = self.provider.get_children_display(700)
        assert not children

    def test_get_children_display_collection(self):
        children = self.provider.get_children_display(2)
        assert len(children) == 1
        assert {
                   'id': 4,
                   'uri': 'urn:x-skosprovider:test:4',
                   'type': 'concept',
                   'label': 'Cathedrals'
               } in children

    def test_get_children_display_collection_sort_id(self):
        children = self.provider.get_children_display(2, sort='id')
        assert len(children) == 1
        assert {
                   'id': 4,
                   'uri': 'urn:x-skosprovider:test:4',
                   'type': 'concept',
                   'label': 'Cathedrals'
               } in children

    def test_get_children_display_concept_with_narrower_collection(self):
        children = self.provider.get_children_display(1)
        assert len(children) == 1
        assert {
                   'id': 2,
                   'uri': 'urn:x-skosprovider:test:2',
                   'type': 'collection',
                   'label': 'Churches by function'
               } in children

    def test_get_children_display_concept_with_narrower_concept(self):
        children = self.provider.get_children_display(3)
        assert len(children) == 1
        assert {
                   'id': 5,
                   'uri': 'urn:x-skosprovider:test:5',
                   'type': 'concept',
                   'label': 'Boomkapellen'
               } in children

    def test_get_children_display_concept_with_no_narrower(self):
        children = self.provider.get_children_display(4)
        assert len(children) == 0

    def test_find_all(self):
        all = self.provider.find({})
        assert len(all) == 5

    def test_find_type_all(self):
        all = self.provider.find({'type': 'all'})
        assert len(all) == 5

    def test_find_type_concept(self):
        all = self.provider.find({'type': 'concept'})
        assert len(all) == 4
        assert {
                   'id': 2,
                   'uri': 'urn:x-skosprovider:test:2',
                   'type': 'collection',
                   'label': 'Churches by function'
               } not in all

    def test_find_type_concept_sorted_uri_desc(self):
        all = self.provider.find({'type': 'concept'}, sort='uri', sort_order='desc')
        assert len(all) == 4
        assert [
            'urn:x-skosprovider:test:5', 
            'urn:x-skosprovider:test:4', 
            'urn:x-skosprovider:test:3', 
            'urn:x-skosprovider:test:1', 
            ] == [c['uri'] for c in all]

    def test_find_type_collection(self):
        all = self.provider.find({'type': 'collection'})
        assert len(all) == 1
        assert {
                   'id': 2,
                   'uri': 'urn:x-skosprovider:test:2',
                   'type': 'collection',
                   'label': 'Churches by function'
               } in all

    def test_find_label_kerken(self):
        all = self.provider.find({'label': 'kerken'})
        assert len(all) == 1
        assert {
                   'id': 1,
                   'uri': 'urn:x-skosprovider:test:1',
                   'type': 'concept',
                   'label': 'Churches'
               } in all

    def test_find_label_churches_type_concept(self):
        all = self.provider.find({'label': 'churches', 'type': 'concept'})
        assert len(all) == 1
        assert {
                   'id': 1,
                   'uri': 'urn:x-skosprovider:test:1',
                   'type': 'concept',
                   'label': 'Churches'
               } in all

    def test_find_collection_unexisting(self):
        with pytest.raises(ValueError):
            self.provider.find({'collection': {'id': 404}})

    def test_find_collection_2_no_depth(self):
        all = self.provider.find({'collection': {'id': 2}})
        assert len(all) == 1
        assert {
                   'id': 4,
                   'uri': 'urn:x-skosprovider:test:4',
                   'type': 'concept',
                   'label': 'Cathedrals'
               } in all

    def test_expand_concept(self):
        ids = self.provider.expand(1)
        assert [1, 4] == ids

    def test_expand_collection(self):
        ids = self.provider.expand(2)
        assert [4] == ids

    def test_expand_concept_without_narrower(self):
        ids = self.provider.expand(5)
        assert [5] == ids

    def test_expand_unexisting(self):
        ids = self.provider.expand(404)
        assert not ids
Example #26
0
class TestSQLAlchemyProvider(DBTestCase):
    def setUp(self):
        Base.metadata.create_all(self.engine)
        self.session = self.session_maker()
        Initialiser(self.session).init_all()
        create_data(self.session)
        self.provider = SQLAlchemyProvider(
            {
                'id': 'SOORTEN',
                'conceptscheme_id': 1
            },
            self.session_maker,
            uri_generator=UriPatternGenerator('urn:x-skosprovider-sa:test:%s'))

    def tearDown(self):
        self.session.rollback()
        self.session.close_all()
        Base.metadata.drop_all(self.engine)

    def test_default_recurse_strategy(self):
        assert 'recurse' == self.provider.expand_strategy

    def test_override_expand_strategy(self):
        # Set up provider
        provider = SQLAlchemyProvider({
            'id': 'SOORTEN',
            'conceptscheme_id': 1
        },
                                      self.session_maker,
                                      expand_strategy='visit')
        assert 'visit' == provider.expand_strategy

    def test_set_invalid_expand_strategy(self):
        with pytest.raises(ValueError):
            SQLAlchemyProvider({
                'id': 'SOORTEN',
                'conceptscheme_id': 1
            },
                               self.session_maker,
                               expand_strategy='invalid')

    def test_provider_without_cs_id(self):
        with pytest.raises(ValueError):
            SQLAlchemyProvider({'id': 'SOORTEN'}, self.session_maker)

    def test_get_vocabulary_id(self):
        assert 'SOORTEN' == self.provider.get_vocabulary_id()

    def test_set_uri_generator(self):
        from skosprovider.uri import UriPatternGenerator
        # Set up provider
        provider = SQLAlchemyProvider({
            'id': 'SOORTEN',
            'conceptscheme_id': 1
        },
                                      self.session_maker,
                                      uri_generator=UriPatternGenerator(
                                          'http://id.example.com/trees/%s'))
        assert 'http://id.example.com/trees/1' == provider.uri_generator.generate(
            id=1)

    def test_gen_uri(self):
        from skosprovider_sqlalchemy.models import Concept, ConceptScheme
        from skosprovider.uri import UriPatternGenerator
        # Set up provider
        provider = SQLAlchemyProvider({
            'id': 'SOORTEN',
            'conceptscheme_id': 99
        },
                                      self.session_maker,
                                      uri_generator=UriPatternGenerator(
                                          'http://id.example.com/trees/%s'))
        c1 = Concept(concept_id=1,
                     conceptscheme=ConceptScheme(
                         id=99, uri='http://id.example.com/trees'))
        session = self.session_maker()
        session.add(c1)
        session.commit()
        assert c1.uri is None
        c2 = provider.get_by_id(1)
        assert c2.uri == 'http://id.example.com/trees/1'

    def test_concept_scheme(self):
        from skosprovider.skos import (ConceptScheme)
        cs = self.provider.concept_scheme
        assert isinstance(cs, ConceptScheme)
        assert 'urn:x-skosprovider:test' == cs.uri
        assert 2 == len(cs.languages)
        assert 'en' in cs.languages

    def test_get_concept_by_id(self):
        from skosprovider.skos import Concept

        con = self.provider.get_by_id(1)
        assert isinstance(con, Concept)
        assert 1 == con.id
        assert [3] == con.related
        assert [4] == con.narrower
        assert [2] == con.subordinate_arrays

    def test_concept_has_concept_scheme(self):
        from skosprovider.skos import (ConceptScheme)
        con = self.provider.get_by_id(1)
        assert isinstance(con.concept_scheme, ConceptScheme)
        assert 'urn:x-skosprovider:test' == con.concept_scheme.uri

    def test_get_concept_by_id_string(self):
        from skosprovider.skos import Concept

        con = self.provider.get_by_id('1')
        assert isinstance(con, Concept)
        assert 1 == con.id
        assert [3] == con.related
        assert [4] == con.narrower
        assert [2] == con.subordinate_arrays

    def test_get_unexisting_by_id(self):
        con = self.provider.get_by_id(404)
        assert not con

    def test_get_concept_by_uri(self):
        cona = self.provider.get_by_id(1)
        conb = self.provider.get_by_uri('urn:x-skosprovider:test:1')
        assert cona.id == conb.id
        assert cona.uri == conb.uri

    def test_get_unexisting_by_uri(self):
        con = self.provider.get_by_uri('urn:x-skosprovider:test:404')
        assert not con

    def test_concept_has_correct_note(self):
        from skosprovider.skos import Note

        cath = self.provider.get_by_id(4)
        assert len(cath.notes) == 1
        assert isinstance(cath.notes[0], Note)

    def test_concept_has_matches(self):
        cath = self.provider.get_by_id(4)
        assert len(cath.matches.keys()) == 5
        assert len(cath.matches['close']) == 1
        assert cath.matches['close'][
            0] == 'http://vocab.getty.edu/aat/300007501'

    def test_get_collection_by_id(self):
        from skosprovider.skos import Collection

        col = self.provider.get_by_id(2)
        assert isinstance(col, Collection)
        assert 2 == col.id
        assert [4] == col.members
        assert [1] == col.superordinates

    def test_collection_has_no_matches(self):
        col = self.provider.get_by_id(2)
        assert not hasattr(col, 'matches')

    def test_get_collection_by_uri(self):
        from skosprovider.skos import Collection

        cola = self.provider.get_by_id(2)
        colb = self.provider.get_by_uri('urn:x-skosprovider:test:2')
        assert isinstance(colb, Collection)
        assert cola.id == colb.id
        assert cola.uri == colb.uri

    def test_get_all(self):
        all = self.provider.get_all()
        assert len(all) == 5
        assert {
            'id': 1,
            'uri': 'urn:x-skosprovider:test:1',
            'type': 'concept',
            'label': 'Churches'
        } in all

        assert {
            'id': 2,
            'uri': 'urn:x-skosprovider:test:2',
            'type': 'collection',
            'label': 'Churches by function'
        } in all

        assert {
            'id': 3,
            'uri': 'urn:x-skosprovider:test:3',
            'type': 'concept',
            'label': 'Chapels'
        } in all

        assert {
            'id': 4,
            'uri': 'urn:x-skosprovider:test:4',
            'type': 'concept',
            'label': 'Cathedrals'
        } in all

        assert {
            'id': 5,
            'uri': 'urn:x-skosprovider:test:5',
            'type': 'concept',
            'label': 'Boomkapellen'
        } in all

    def test_get_all_sorted_id_desc(self):
        all = self.provider.get_all(sort='id', sort_order='desc')
        assert len(all) == 5
        assert [5, 4, 3, 2, 1] == [c['id'] for c in all]

    def test_get_all_sorted_label(self):
        all = self.provider.get_all(sort='label')
        assert len(all) == 5
        assert [
            'Boomkapellen', 'Cathedrals', 'Chapels', 'Churches',
            'Churches by function'
        ] == [c['label'] for c in all]

    def test_get_all_sorted_sortlabel_desc(self):
        all = self.provider.get_all(sort='sortlabel', sort_order='desc')
        assert len(all) == 5
        assert [
            'Churches',
            'Chapels',
            'Cathedrals',
            'Boomkapellen',
            'Churches by function',
        ] == [c['label'] for c in all]

    def test_get_top_concepts(self):
        all = self.provider.get_top_concepts()
        assert len(all) == 2

        assert {
            'id': 1,
            'uri': 'urn:x-skosprovider:test:1',
            'type': 'concept',
            'label': 'Churches'
        } in all

        assert {
            'id': 3,
            'uri': 'urn:x-skosprovider:test:3',
            'type': 'concept',
            'label': 'Chapels'
        } in all

    def test_get_top_concepts_sort_uri_desc(self):
        all = self.provider.get_top_concepts(sort='uri', sort_order='desc')
        assert len(all) == 2

        assert [
            'urn:x-skosprovider:test:3',
            'urn:x-skosprovider:test:1',
        ] == [c['uri'] for c in all]

    def test_get_top_display(self):
        all = self.provider.get_top_display()
        assert len(all) == 2
        assert {
            'id': 3,
            'uri': 'urn:x-skosprovider:test:3',
            'type': 'concept',
            'label': 'Chapels'
        } in all

        assert {
            'id': 1,
            'uri': 'urn:x-skosprovider:test:1',
            'type': 'concept',
            'label': 'Churches'
        } in all

    def test_get_top_display_british_sort_label_desc(self):
        all = self.provider.get_top_display(language='en-GB',
                                            sort='label',
                                            sort_order='desc')
        assert len(all) == 2

        assert ['Churches', 'Chapels'] == [c['label'] for c in all]

    def test_get_children_display_unexisting(self):
        children = self.provider.get_children_display(700)
        assert not children

    def test_get_children_display_collection(self):
        children = self.provider.get_children_display(2)
        assert len(children) == 1
        assert {
            'id': 4,
            'uri': 'urn:x-skosprovider:test:4',
            'type': 'concept',
            'label': 'Cathedrals'
        } in children

    def test_get_children_display_collection_sort_id(self):
        children = self.provider.get_children_display(2, sort='id')
        assert len(children) == 1
        assert {
            'id': 4,
            'uri': 'urn:x-skosprovider:test:4',
            'type': 'concept',
            'label': 'Cathedrals'
        } in children

    def test_get_children_display_concept_with_narrower_collection(self):
        children = self.provider.get_children_display(1)
        assert len(children) == 1
        assert {
            'id': 2,
            'uri': 'urn:x-skosprovider:test:2',
            'type': 'collection',
            'label': 'Churches by function'
        } in children

    def test_get_children_display_concept_with_narrower_concept(self):
        children = self.provider.get_children_display(3)
        assert len(children) == 1
        assert {
            'id': 5,
            'uri': 'urn:x-skosprovider:test:5',
            'type': 'concept',
            'label': 'Boomkapellen'
        } in children

    def test_get_children_display_concept_with_no_narrower(self):
        children = self.provider.get_children_display(4)
        assert len(children) == 0

    def test_find_all(self):
        all = self.provider.find({})
        assert len(all) == 5

    def test_find_type_all(self):
        all = self.provider.find({'type': 'all'})
        assert len(all) == 5

    def test_find_type_concept(self):
        all = self.provider.find({'type': 'concept'})
        assert len(all) == 4
        assert {
            'id': 2,
            'uri': 'urn:x-skosprovider:test:2',
            'type': 'collection',
            'label': 'Churches by function'
        } not in all

    def test_find_type_concept_sorted_uri_desc(self):
        all = self.provider.find({'type': 'concept'},
                                 sort='uri',
                                 sort_order='desc')
        assert len(all) == 4
        assert [
            'urn:x-skosprovider:test:5',
            'urn:x-skosprovider:test:4',
            'urn:x-skosprovider:test:3',
            'urn:x-skosprovider:test:1',
        ] == [c['uri'] for c in all]

    def test_find_type_collection(self):
        all = self.provider.find({'type': 'collection'})
        assert len(all) == 1
        assert {
            'id': 2,
            'uri': 'urn:x-skosprovider:test:2',
            'type': 'collection',
            'label': 'Churches by function'
        } in all

    def test_find_label_kerken(self):
        all = self.provider.find({'label': 'kerken'})
        assert len(all) == 1
        assert {
            'id': 1,
            'uri': 'urn:x-skosprovider:test:1',
            'type': 'concept',
            'label': 'Churches'
        } in all

    def test_find_label_churches_type_concept(self):
        all = self.provider.find({'label': 'churches', 'type': 'concept'})
        assert len(all) == 1
        assert {
            'id': 1,
            'uri': 'urn:x-skosprovider:test:1',
            'type': 'concept',
            'label': 'Churches'
        } in all

    def test_find_collection_unexisting(self):
        with pytest.raises(ValueError):
            self.provider.find({'collection': {'id': 404}})

    def test_find_collection_2_no_depth(self):
        all = self.provider.find({'collection': {'id': 2}})
        assert len(all) == 1
        assert {
            'id': 4,
            'uri': 'urn:x-skosprovider:test:4',
            'type': 'concept',
            'label': 'Cathedrals'
        } in all

    def test_expand_concept(self):
        ids = self.provider.expand(1)
        assert [1, 4] == ids

    def test_expand_collection(self):
        ids = self.provider.expand(2)
        assert [4] == ids

    def test_expand_concept_without_narrower(self):
        ids = self.provider.expand(5)
        assert [5] == ids

    def test_expand_unexisting(self):
        ids = self.provider.expand(404)
        assert not ids
Example #27
0
 def test_provider_without_cs_id(self):
     with pytest.raises(ValueError):
         SQLAlchemyProvider({'id': 'SOORTEN'}, self.session_maker)
Example #28
0
def includeme(config):   # pragma: no cover
    dataseturigenerator = UriPatternGenerator('https://id.erfgoed.net/datasets/thesauri/%s')

    TREES = SQLAlchemyProvider(
        {'id': 'TREES', 'conceptscheme_id': 1},
        config.registry.dbmaker
    )

    GEO = SQLAlchemyProvider(
        {'id': 'GEOGRAPHY', 'conceptscheme_id': 2},
        config.registry.dbmaker
    )

    STYLES = SQLAlchemyProvider(
        {
            'id': 'STYLES',
            'conceptscheme_id': 3,
            'dataset': {
                'uri': dataseturigenerator.generate(id='stijlen_en_culturen'),
                'publisher': ['https://id.erfgoed.net/actoren/501'],
                'created': [date(2008,2,14)],
                'language': ['nl-BE'],
                'license': [
                    'https://creativecommons.org/licenses/by/4.0/',
                    'http://data.vlaanderen.be/doc/licentie/modellicentie-gratis-hergebruik/v1.0'
                ]
            }

        },
        config.registry.dbmaker,
        uri_generator=UriPatternGenerator('https://id.erfgoed.net/thesauri/stijlen_en_culturen/%s')
    )

    MATERIALS = SQLAlchemyProvider(
        {
            'id': 'MATERIALS',
            'conceptscheme_id': 4,
            'dataset': {
                'uri': dataseturigenerator.generate(id='materialen'),
                'publisher': ['https://id.erfgoed.net/actoren/501'],
                'created': [date(2011,3,16)],
                'language': ['nl-BE'],
                'license': [
                    'https://creativecommons.org/licenses/by/4.0/',
                    'http://data.vlaanderen.be/doc/licentie/modellicentie-gratis-hergebruik/v1.0'
                ]
            }
        },
        config.registry.dbmaker,
        uri_generator=UriPatternGenerator('https://id.erfgoed.net/thesauri/materialen/%s')
    )

    EVENTTYPES = SQLAlchemyProvider(
        {
            'id': 'EVENTTYPE',
            'conceptscheme_id': 5,
            'dataset': {
                'uri': dataseturigenerator.generate(id='gebeurtenistypes'),
                'publisher': ['https://id.erfgoed.net/actoren/501'],
                'created': [date(2010,8,13)],
                'language': ['nl-BE'],
                'license': [
                    'https://creativecommons.org/licenses/by/4.0/',
                    'http://data.vlaanderen.be/doc/licentie/modellicentie-gratis-hergebruik/v1.0'
                ]
            }
        },
        config.registry.dbmaker,
        uri_generator=UriPatternGenerator('https://id.erfgoed.net/thesauri/gebeurtenistypes/%s')
    )

    HERITAGETYPES = SQLAlchemyProvider(
        {
            'id': 'HERITAGETYPE',
            'conceptscheme_id': 6,
            'dataset': {
                'uri': dataseturigenerator.generate(id='erfgoedtypes'),
                'publisher': ['https://id.erfgoed.net/actoren/501'],
                'created': [date(2008,2,14)],
                'language': ['nl-BE'],
                'license': [
                    'https://creativecommons.org/licenses/by/4.0/',
                    'http://data.vlaanderen.be/doc/licentie/modellicentie-gratis-hergebruik/v1.0'
                ]
            }
        },
        config.registry.dbmaker,
        uri_generator=UriPatternGenerator('https://id.erfgoed.net/thesauri/erfgoedtypes/%s')
    )

    PERIODS = SQLAlchemyProvider(
        {
            'id': 'PERIOD',
            'conceptscheme_id': 7,
            'dataset': {
                'uri': dataseturigenerator.generate(id='dateringen'),
                'publisher': ['https://id.erfgoed.net/actoren/501'],
                'created': [date(2008,2,14)],
                'language': ['nl-BE'],
                'license': [
                    'https://creativecommons.org/licenses/by/4.0/',
                    'http://data.vlaanderen.be/doc/licentie/modellicentie-gratis-hergebruik/v1.0'
                ]
            }
        },
        config.registry.dbmaker,
        uri_generator=UriPatternGenerator('https://id.erfgoed.net/thesauri/dateringen/%s')
    )

    SPECIES = SQLAlchemyProvider(
        {
            'id': 'SPECIES',
            'conceptscheme_id': 8,
            'dataset': {
                'uri': dataseturigenerator.generate(id='soorten'),
                'publisher': ['https://id.erfgoed.net/actoren/501'],
                'created': [date(2011,5,23)],
                'language': ['nl-BE', 'la'],
                'license': [
                    'https://creativecommons.org/licenses/by/4.0/',
                    'http://data.vlaanderen.be/doc/licentie/modellicentie-gratis-hergebruik/v1.0'
                ]
            }
        },
        config.registry.dbmaker,
        uri_generator=UriPatternGenerator('https://id.erfgoed.net/thesauri/soorten/%s')
    )

    # use 'subject': ['external'] for read only external providers (only available in REST service)

    getty_session = CacheControl(requests.Session(), heuristic=ExpiresAfter(weeks=1))

    AAT = AATProvider(
        {'id': 'AAT', 'subject': ['external']},
        session=getty_session
    )

    TGN = TGNProvider(
        {'id': 'TGN', 'subject': ['external']},
        session=getty_session
    )

    eh_session = CacheControl(requests.Session(), heuristic=ExpiresAfter(weeks=1))

    EH_PERIOD = HeritagedataProvider(
        {'id': 'EH_PERIOD', 'subject': ['external']},
        scheme_uri='http://purl.org/heritagedata/schemes/eh_period',
        session=eh_session
    )

    EH_MONUMENT_TYPE = HeritagedataProvider(
        {'id': 'EH_MONUMENT_TYPE', 'subject': ['external']},
        scheme_uri='http://purl.org/heritagedata/schemes/eh_tmt2',
        session=eh_session
    )

    EH_MATERIALS = HeritagedataProvider(
        {'id': 'EH_MATERIALS', 'subject': ['external']},
        scheme_uri='http://purl.org/heritagedata/schemes/eh_tbm',
        session=eh_session
    )

    skosregis = config.get_skos_registry()
    skosregis.register_provider(TREES)
    skosregis.register_provider(GEO)
    skosregis.register_provider(STYLES)
    skosregis.register_provider(MATERIALS)
    skosregis.register_provider(EVENTTYPES)
    skosregis.register_provider(HERITAGETYPES)
    skosregis.register_provider(PERIODS)
    skosregis.register_provider(SPECIES)
    skosregis.register_provider(AAT)
    skosregis.register_provider(TGN)
    skosregis.register_provider(EH_PERIOD)
    skosregis.register_provider(EH_MONUMENT_TYPE)
    skosregis.register_provider(EH_MATERIALS)
 def test_get_by_id(self):
     provider = SQLAlchemyProvider({'id': 'SOORTEN', 'conceptscheme_id': 1},
                                   self.session_maker)
     self.assertIsNotNone(provider)
     concept = provider.get_by_id(10)
     self.assertIsNotNone(concept)