Skip to content

Commit

Permalink
Fix querying a collection with depth all. Closes #76 (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
koenedaele authored Jul 29, 2020
1 parent 11e42ad commit 021b4eb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
now be passed a :class:`sqlachemy.orm.session.Session` at startup, or a
callable that returns such a session. (#64)
* Improved performance of getting the concept_scheme by caching it. (#71)
* Make querying a collection with depth=all possible. Before the provider would
only provide the direct members of a collection. (#76)
* Drop support for Python 3.4. Add support for Python 3.7. This
is also the last version to support Python 2. (#62)

Expand Down
9 changes: 5 additions & 4 deletions skosprovider_sqlalchemy/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,11 @@ def find(self, query, **kwargs):
raise ValueError(
'You are searching for items in an unexisting collection.'
)
q = q.filter(
model.member_of.any(Thing.concept_id == coll.id)
)

if 'depth' in query['collection'] and query['collection']['depth'] == 'all':
members = self.expand(coll.id)
else:
members = coll.members
q = q.filter(model.concept_id.in_(members))
all = q.all()
sort = self._get_sort(**kwargs)
sort_order = self._get_sort_order(**kwargs)
Expand Down
46 changes: 44 additions & 2 deletions tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,50 @@ 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}})
def test_find_collection_2_depth_default_members(self):
nodepth = self.provider.find({'collection': {'id': 2}})
depth = self.provider.find({
'collection': {
'id': 2,
'depth': 'members'
}
})
assert len(depth) == len(nodepth)

def test_find_collection_2_depth_all(self):
all = self.provider.find({
'collection': {
'id': 2,
'depth': 'all'
}
})
assert len(all) == 3
assert {
'id': 4,
'uri': 'urn:x-skosprovider:test:4',
'type': 'concept',
'label': 'Cathedrals'
} in all
assert {
'id': 6,
'uri': 'urn:x-skosprovider:test:6',
'type': 'concept',
'label': 'Parochiekerken'
} in all
assert {
'id': 7,
'uri': 'urn:x-skosprovider:test:7',
'type': 'concept',
'label': 'Hulpkerken'
} in all

def test_find_collection_2_depth_members(self):
all = self.provider.find({
'collection': {
'id': 2,
'depth': 'members'
}
})
assert len(all) == 2
assert {
'id': 4,
Expand Down

0 comments on commit 021b4eb

Please sign in to comment.