Skip to content

Commit 1968a4b

Browse files
authored
Merge pull request #11797 from internetarchive/copilot/fix-fulltext-search-pagination
Fix fulltext search pagination: all pages returned identical results
2 parents 8464674 + cded600 commit 1968a4b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

openlibrary/core/fulltext.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ async def fulltext_search_api(params):
4646
return {'error': 'Error converting search engine data to JSON'}
4747

4848

49-
async def fulltext_search_async(q, page=1, offset=0, limit=100, js=False, facets=False):
49+
async def fulltext_search_async(
50+
q, page=1, offset=None, limit=100, js=False, facets=False
51+
):
5052
if offset is None:
5153
offset = (page - 1) * limit
5254
params = {

openlibrary/tests/core/test_fulltext.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,31 @@ async def test_bad_json(self, httpx_mock, monkeypatch):
3939

4040
response = await fulltext.fulltext_search_api({"q": "hello"})
4141
assert response == {"error": "Error converting search engine data to JSON"}
42+
43+
@pytest.mark.asyncio
44+
@pytest.mark.parametrize(
45+
("page", "limit", "offset_kwarg", "expected_from"),
46+
[
47+
(1, 20, "NOT_PASSED", 0), # offset not passed at all
48+
(1, 20, None, 0), # offset=None explicitly
49+
(10, 20, "NOT_PASSED", 180), # offset not passed, page 10
50+
(5, 20, 100, 100), # explicit offset provided
51+
],
52+
)
53+
async def test_pagination_offset_calculation(
54+
self, httpx_mock, monkeypatch, page, limit, offset_kwarg, expected_from
55+
):
56+
url = "http://mock"
57+
monkeypatch.setattr(
58+
config, "plugin_inside", {"search_endpoint": url}, raising=False
59+
)
60+
httpx_mock.add_response(json={"hits": {"hits": []}})
61+
62+
# Conditionally build kwargs to test "not passed" scenario
63+
kwargs = {"page": page, "limit": limit}
64+
if offset_kwarg != "NOT_PASSED":
65+
kwargs["offset"] = offset_kwarg
66+
67+
await fulltext.fulltext_search_async("test", **kwargs)
68+
request = httpx_mock.get_request()
69+
assert f"from={expected_from}" in request.url.query.decode()

0 commit comments

Comments
 (0)