Skip to content

Commit af4d6ce

Browse files
committed
refactor: simplify search endpoint test calls by introducing and using a new helper function.
1 parent b8ab1bd commit af4d6ce

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

openlibrary/tests/fastapi/test_search.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,30 @@ def mock_work_search():
3333
yield mock
3434

3535

36+
def search(client, **params):
37+
"""Helper function to make search requests with query parameters.
38+
39+
This helper provides a cleaner interface for making search requests
40+
by accepting keyword arguments directly instead of building URL strings.
41+
"""
42+
from urllib.parse import urlencode
43+
44+
query_string = urlencode(params)
45+
return client.get(f'/search.json?{query_string}')
46+
47+
3648
class TestSearchEndpoint:
3749
"""Tests for the /search.json endpoint."""
3850

3951
def test_search_uses_query_param(self, client, mock_work_search):
4052
"""Test that the search endpoint uses the 'q' query parameter."""
41-
# Mock the async function
4253
mock_work_search.return_value = {
4354
'numFound': 1,
4455
'start': 0,
4556
'docs': [{'key': '/works/OL1W', 'title': 'The Lord of the Rings'}],
4657
}
4758

48-
response = client.get('/search.json?q=lord+of+the+rings')
59+
response = search(client, q='lord of the rings')
4960

5061
assert response.status_code == 200
5162
data = response.json()
@@ -71,9 +82,7 @@ def test_search_uses_query_alias_param(self, client, mock_work_search):
7182

7283
# The 'query' param should accept a full JSON-encoded Solr query
7384
query_dict = {'q': 'test', 'author': 'Tolkien'}
74-
query_str = json.dumps(query_dict)
75-
76-
response = client.get(f'/search.json?query={query_str}')
85+
response = search(client, query=json.dumps(query_dict))
7786

7887
assert response.status_code == 200
7988

@@ -84,41 +93,41 @@ def test_search_uses_query_alias_param(self, client, mock_work_search):
8493
assert query_arg == query_dict
8594

8695
@pytest.mark.parametrize(
87-
('query', 'expected_kwargs'),
96+
('params', 'expected_kwargs'),
8897
[
8998
(
90-
"q=test&page=3&limit=10",
91-
{"page": 3, "limit": 10, "offset": None},
99+
{'q': 'test', 'page': 3, 'limit': 10},
100+
{'page': 3, 'limit': 10, 'offset': None},
92101
),
93102
(
94-
"q=test&offset=50&limit=25",
95-
{"offset": 50, "limit": 25, "page": None},
103+
{'q': 'test', 'offset': 50, 'limit': 25},
104+
{'offset': 50, 'limit': 25, 'page': None},
96105
),
97106
(
98-
"q=test&page=5&offset=30&limit=10",
99-
{"offset": 30, "limit": 10, "page": None},
107+
{'q': 'test', 'page': 5, 'offset': 30, 'limit': 10},
108+
{'offset': 30, 'limit': 10, 'page': None},
100109
),
101110
(
102-
"q=test",
103-
{"limit": 100, "page": 1, "offset": None},
111+
{'q': 'test'},
112+
{'limit': 100, 'page': 1, 'offset': None},
104113
),
105114
],
106115
)
107116
def test_pagination_variants(
108-
self, client, mock_work_search, query, expected_kwargs
117+
self, client, mock_work_search, params, expected_kwargs
109118
):
110119
"""Test pagination behavior for various query parameter combinations.
111120
112121
The mock is configured to return a generic successful response; the focus is on
113122
verifying that ``work_search_async`` receives the correct pagination arguments.
114123
"""
115124
mock_work_search.return_value = {
116-
"numFound": 10,
117-
"start": 0,
118-
"docs": [],
125+
'numFound': 10,
126+
'start': 0,
127+
'docs': [],
119128
}
120129

121-
response = client.get(f"/search.json?{query}")
130+
response = search(client, **params)
122131
assert response.status_code == 200
123132

124133
mock_work_search.assert_called_once()
@@ -134,7 +143,7 @@ def test_response_includes_metadata(self, client, mock_work_search):
134143
'docs': [{'key': '/works/OL5W', 'title': 'Test'}],
135144
}
136145

137-
response = client.get('/search.json?q=test&offset=10')
146+
response = search(client, q='test', offset=10)
138147

139148
assert response.status_code == 200
140149
data = response.json()
@@ -162,8 +171,7 @@ def test_response_includes_metadata(self, client, mock_work_search):
162171
)
163172
def test_pagination_validation_errors(self, client, mock_work_search, params):
164173
"""Test validation errors for invalid pagination parameters."""
165-
query = '&'.join(f'{k}={v}' for k, v in params.items())
166-
response = client.get(f'/search.json?q=test&{query}')
174+
response = search(client, q='test', **params)
167175

168176
# Should return a validation error
169177
assert response.status_code == 422

0 commit comments

Comments
 (0)