Skip to content

Commit

Permalink
Test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RishiDiwanTT committed Aug 24, 2023
1 parent 33f8532 commit 39ec230
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
5 changes: 4 additions & 1 deletion api/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ def search(self, lane_identifier, feed_class=OPDSAcquisitionFeed):
# Run a search.
annotator = self.manager.annotator(lane, facets)
info = OpenSearchDocument.search_info(lane)
return feed_class.search(
response = feed_class.search(
_db=self._db,
title=info["name"],
url=make_url(),
Expand All @@ -1171,6 +1171,9 @@ def search(self, lane_identifier, feed_class=OPDSAcquisitionFeed):
pagination=pagination,
facets=facets,
)
if isinstance(response, ProblemDetail):
return response
return response.as_response(requested_content_type=flask.request.content_type)

def _qa_feed(
self, feed_factory, feed_title, controller_name, facet_class, worklist_factory
Expand Down
4 changes: 1 addition & 3 deletions core/feed_protocol/acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from core.opds import UnfulfillableWork
from core.problem_details import INVALID_INPUT
from core.util.datetime_helpers import utc_now
from core.util.flask_util import OPDSFeedResponse
from core.util.opds_writer import AtomFeed, OPDSMessage

if TYPE_CHECKING:
Expand Down Expand Up @@ -787,8 +786,7 @@ def make_link(ep):
# technically searching the this lane; you are searching the
# library's entire collection, using _some_ of the constraints
# imposed by this lane (notably language and audience).

return OPDSFeedResponse(response=feed.serialize(), **response_kwargs)
return feed

@classmethod
def from_query(cls, query, _db, feed_name, url, pagination, url_fn, annotator):
Expand Down
7 changes: 4 additions & 3 deletions core/feed_protocol/opds.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ def __init__(self, title, url, precomposed_entries=None) -> None:
self._feed = FeedData()

def generate_feed(self, work_entries):
pass
raise NotImplementedError()

def serialize(self):
return self._serializer.serialize_feed(self._feed)
def serialize(self, requested_content_type=None):
serializer = get_serializer(requested_content_type)
return serializer.serialize_feed(self._feed)

def add_link(self, href, rel=None, **kwargs):
self._feed.add_link(href, rel=rel, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions tests/api/feed_protocol/equivalence/test_feed_equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_page_feed_with_loan_annotator(
work1.active_license_pool(library).loan_to(patron)

with app.test_request_context("/"):
new_feed = OPDSAcquisitionFeed.active_loans_for(None, patron)
new_feed = OPDSAcquisitionFeed.active_loans_for(None, patron).as_response()
old_feed = OldLibraryLoanAndHoldAnnotator.active_loans_for(None, patron)

assert_equal_xmls(str(old_feed), str(new_feed))
Expand Down Expand Up @@ -189,7 +189,7 @@ def test_search_feed(self, annotator_fixture: LibraryAnnotatorFixture):
Pagination.default(),
Facets.default(library),
new_annotator,
)
).as_response()

old_annotator = OldLibraryAnnotator(None, lane, library)
old_feed = AcquisitionFeed.search(
Expand Down
33 changes: 10 additions & 23 deletions tests/api/feed_protocol/test_library_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,25 +901,12 @@ def test_active_loan_feed(
patron=patron,
)

response = OPDSAcquisitionFeed.active_loans_for(None, patron, annotator)
response = OPDSAcquisitionFeed.active_loans_for(
None, patron, annotator
).as_response()

# The feed is private and should not be cached.
assert isinstance(response, OPDSFeedResponse)
assert 0 == response.max_age
assert True == response.private

# Instead, the Last-Modified header is set to the last time
# we successfully brought the patron's bookshelf in sync with
# the vendor APIs.
#
# (The timestamps aren't exactly the same because
# last_loan_activity_sync is tracked at the millisecond level
# and Last-Modified is tracked at the second level.)

assert response.last_modified is not None
assert (
patron.last_loan_activity_sync - response.last_modified
).total_seconds() < 1

# No entries in the feed...
raw = str(response)
Expand Down Expand Up @@ -1007,7 +994,7 @@ def test_active_loan_feed(
annotator_fixture.db.default_library(),
patron=patron,
),
)
).as_response()
raw = str(feed_obj)
feed = feedparser.parse(raw)

Expand Down Expand Up @@ -1051,7 +1038,7 @@ def test_loan_feed_includes_patron(
LibraryLoanAndHoldAnnotator(
None, None, annotator_fixture.db.default_library(), patron
),
)
).as_response()
raw = str(feed_obj)
feed_details = feedparser.parse(raw)["feed"]

Expand All @@ -1069,7 +1056,7 @@ def test_loans_feed_includes_annotations_link(
self, annotator_fixture: LibraryAnnotatorFixture
):
patron = annotator_fixture.db.patron()
feed_obj = OPDSAcquisitionFeed.active_loans_for(None, patron)
feed_obj = OPDSAcquisitionFeed.active_loans_for(None, patron).as_response()
raw = str(feed_obj)
feed = feedparser.parse(raw)["feed"]
links = feed["links"]
Expand Down Expand Up @@ -1098,7 +1085,7 @@ def test_active_loan_feed_ignores_inconsistent_local_data(
hold.license_pool = None

# We can still get a feed...
feed_obj = OPDSAcquisitionFeed.active_loans_for(None, patron)
feed_obj = OPDSAcquisitionFeed.active_loans_for(None, patron).as_response()

# ...but it's empty.
assert "<entry>" not in str(feed_obj)
Expand Down Expand Up @@ -1162,7 +1149,7 @@ def test_loans_feed_includes_fulfill_links(
feed_obj = OPDSAcquisitionFeed.active_loans_for(
None,
patron,
)
).as_response()
raw = str(feed_obj)

entries = feedparser.parse(raw)["entries"]
Expand All @@ -1187,7 +1174,7 @@ def test_loans_feed_includes_fulfill_links(
library = annotator_fixture.db.default_library()
settings = library_fixture.settings(library)
settings.hidden_content_types = [mech1.delivery_mechanism.content_type]
OPDSAcquisitionFeed.active_loans_for(None, patron)
OPDSAcquisitionFeed.active_loans_for(None, patron).as_response()
assert {
mech2.delivery_mechanism.drm_scheme_media_type,
OPDSFeed.ENTRY_TYPE,
Expand All @@ -1198,7 +1185,7 @@ def test_loans_feed_includes_fulfill_links(
# and the streaming mechanism.
loan.fulfillment = mech1

feed_obj = OPDSAcquisitionFeed.active_loans_for(None, patron)
feed_obj = OPDSAcquisitionFeed.active_loans_for(None, patron).as_response()
raw = str(feed_obj)

entries = feedparser.parse(raw)["entries"]
Expand Down
4 changes: 3 additions & 1 deletion tests/api/test_controller_opdsfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,9 @@ class Mock:
@classmethod
def search(cls, **kwargs):
self.called_with = kwargs
return "An OPDS feed"
resp = MagicMock()
resp.as_response.return_value = "An OPDS feed"
return resp

with circulation_fixture.request_context_with_library(
"/?q=t&size=99&after=22&media=Music"
Expand Down

0 comments on commit 39ec230

Please sign in to comment.