Skip to content

Commit 9ac0b33

Browse files
Copilotmekarpeles
andcommitted
Fix fulltext search pagination by correcting offset default value
Co-authored-by: mekarpeles <978325+mekarpeles@users.noreply.github.com>
1 parent 9fe774c commit 9ac0b33

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

openlibrary/core/fulltext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ 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(q, page=1, offset=None, limit=100, js=False, facets=False):
5050
if offset is None:
5151
offset = (page - 1) * limit
5252
params = {

openlibrary/tests/core/test_fulltext.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,37 @@ 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+
async def test_pagination_offset_calculation(self, httpx_mock, monkeypatch):
45+
"""Test that page parameter correctly calculates offset for pagination."""
46+
url = "http://mock"
47+
monkeypatch.setattr(
48+
config, "plugin_inside", {"search_endpoint": url}, raising=False
49+
)
50+
51+
# Mock a successful response
52+
httpx_mock.add_response(json={"hits": {"hits": []}})
53+
54+
# Test page 1 should have offset 0
55+
await fulltext.fulltext_search_async("test", page=1, limit=20)
56+
request = httpx_mock.get_request()
57+
assert "from=0" in request.url.query.decode()
58+
59+
# Reset mock
60+
httpx_mock.reset(False)
61+
httpx_mock.add_response(json={"hits": {"hits": []}})
62+
63+
# Test page 10 should have offset 180 (9 * 20)
64+
await fulltext.fulltext_search_async("test", page=10, limit=20)
65+
request = httpx_mock.get_request()
66+
assert "from=180" in request.url.query.decode()
67+
68+
# Reset mock
69+
httpx_mock.reset(False)
70+
httpx_mock.add_response(json={"hits": {"hits": []}})
71+
72+
# Test explicit offset overrides page
73+
await fulltext.fulltext_search_async("test", page=5, offset=100, limit=20)
74+
request = httpx_mock.get_request()
75+
assert "from=100" in request.url.query.decode()

0 commit comments

Comments
 (0)