Skip to content

Commit

Permalink
Fix linking errors. Fixes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
koenedaele committed Sep 21, 2016
1 parent d50874e commit 403ec33
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 20 deletions.
62 changes: 42 additions & 20 deletions skosprovider_sqlalchemy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
Source as SourceModel
)

from sqlalchemy.orm.exc import NoResultFound


def import_provider(provider, conceptscheme, session):
'''
Expand Down Expand Up @@ -85,36 +87,56 @@ def import_provider(provider, conceptscheme, session):
.one()
if len(c.narrower) > 0:
for nc in c.narrower:
nc = session.query(ConceptModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(nc)) \
.one()
cm.narrower_concepts.add(nc)
try:
nc = session.query(ConceptModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(nc)) \
.one()
cm.narrower_concepts.add(nc)
except NoResultFound:
log.warning(
'Tried to add a relation %s narrower %s, but target \
does not exist. Relation will be lost.' % (c.id, nc))
if len(c.subordinate_arrays) > 0:
for sa in c.subordinate_arrays:
sa = session.query(CollectionModel) \
.filter(CollectionModel.conceptscheme_id == conceptscheme.id) \
.filter(CollectionModel.concept_id == int(sa)) \
.one()
cm.narrower_collections.add(sa)
try:
sa = session.query(CollectionModel) \
.filter(CollectionModel.conceptscheme_id == conceptscheme.id) \
.filter(CollectionModel.concept_id == int(sa)) \
.one()
cm.narrower_collections.add(sa)
except NoResultFound:
log.warning(
'Tried to add a relation %s subordinate array %s, but target \
does not exist. Relation will be lost.' % (c.id, sa))
if len(c.related) > 0:
for rc in c.related:
rc = session.query(ConceptModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(rc)) \
.one()
cm.related_concepts.add(rc)
try:
rc = session.query(ConceptModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(rc)) \
.one()
cm.related_concepts.add(rc)
except NoResultFound:
log.warning(
'Tried to add a relation %s related %s, but target \
does not exist. Relation will be lost.' % (c.id, rc))
elif isinstance(c, Collection) and len(c.members) > 0:
cm = session.query(CollectionModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(c.id)) \
.one()
for mc in c.members:
mc = session.query(ThingModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(mc)) \
.one()
cm.members.add(mc)
try:
mc = session.query(ThingModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(mc)) \
.one()
cm.members.add(mc)
except NoResultFound:
log.warning(
'Tried to add a relation %s member %s, but target \
does not exist. Relation will be lost.' % (c.id, mc))


def _check_language(language_tag, session):
Expand Down
49 changes: 49 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,41 @@ def _get_buildings():
return buildings


def _get_materials():
from skosprovider.providers import DictionaryProvider

materials = DictionaryProvider(
{'id': 'MATERIALS'},
[
{
'id': '1',
'labels': [
{
'type': 'prefLabel',
'language': 'en',
'label': 'Cardboard'
}
],
'narrower': [2],
'related': [3],
'subordinate_arrays': [56]
}, {
'id': '789',
'type': 'collection',
'labels': [
{
'type': 'prefLabel',
'language': 'en',
'label': 'Wood by Tree'
}
],
'members': [654]
}
]
)
return materials


def _get_heritage_types():
import json

Expand Down Expand Up @@ -406,6 +441,20 @@ def test_event_types(self):
.one()
assert 3 == len(archeologische_opgravingen.narrower_collections)

def test_materials(self):
from skosprovider_sqlalchemy.models import (
Thing as ThingModel,
)

materialsprovider = _get_materials()
cs = self._get_cs()
self.session.add(cs)
import_provider(materialsprovider, cs, self.session)
materials = self.session.query(ThingModel) \
.filter(ThingModel.conceptscheme == cs) \
.all()
assert 2 == len(materials)


class TestVisitationCalculator(DBTestCase):

Expand Down

0 comments on commit 403ec33

Please sign in to comment.