Skip to content

Commit

Permalink
Remove the storage method fetch_ordered_annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Betts committed May 4, 2023
1 parent 0d34323 commit a993b5f
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 67 deletions.
35 changes: 0 additions & 35 deletions h/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,6 @@
_ = i18n.TranslationStringFactory(__package__)


def fetch_ordered_annotations(session, ids, query_processor=None):
"""
Fetch all annotations with the given ids and order them based on the list of ids.
The optional `query_processor` parameter allows for passing in a function
that can change the query before it is run, especially useful for
eager-loading certain data. The function will get the query as an argument
and has to return a query object again.
:param session: the database session
:type session: sqlalchemy.orm.session.Session
:param ids: the list of annotation ids
:type ids: list
:param query_processor: an optional function that takes the query and
returns an updated query
:type query_processor: callable
:returns: the annotation, if found, or None.
:rtype: h.models.Annotation, NoneType
"""
if not ids:
return []

ordering = {x: i for i, x in enumerate(ids)}

query = session.query(models.Annotation).filter(models.Annotation.id.in_(ids))
if query_processor:
query = query_processor(query)

anns = sorted(query, key=lambda a: ordering.get(a.id))
return anns


def create_annotation(request, data):
"""
Create an annotation from already-validated data.
Expand Down
32 changes: 0 additions & 32 deletions tests/h/storage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from h_matchers import Any

from h import storage
from h.models.annotation import Annotation
from h.models.document import Document, DocumentURI
from h.schemas import ValidationError
from h.security import Permission
Expand All @@ -16,37 +15,6 @@
pytestmark = pytest.mark.usefixtures("search_index")


class TestFetchOrderedAnnotations:
def test_it_returns_annotations_for_ids_in_the_same_order(
self, db_session, factories
):
ann_1 = factories.Annotation(userid="luke")
ann_2 = factories.Annotation(userid="luke")

assert [ann_2, ann_1] == storage.fetch_ordered_annotations(
db_session, [ann_2.id, ann_1.id]
)
assert [ann_1, ann_2] == storage.fetch_ordered_annotations(
db_session, [ann_1.id, ann_2.id]
)

def test_it_allows_to_change_the_query(self, db_session, factories):
ann_1 = factories.Annotation(userid="luke")
ann_2 = factories.Annotation(userid="maria")

def only_maria(query):
return query.filter(Annotation.userid == "maria")

assert [ann_2] == storage.fetch_ordered_annotations(
db_session, [ann_2.id, ann_1.id], query_processor=only_maria
)

def test_it_handles_empty_ids(self):
results = storage.fetch_ordered_annotations(sentinel.db_session, ids=[])

assert results == []


class TestExpandURI:
@pytest.mark.parametrize(
"normalized,expected_uris",
Expand Down

0 comments on commit a993b5f

Please sign in to comment.