Skip to content

Commit d87eb8a

Browse files
committed
tests: add tests for output formats (json, md, html)
1 parent f11114e commit d87eb8a

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

tests/test_output_formats.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from unittest.mock import Mock
2+
3+
import pytest
4+
import requests
5+
6+
import serpapi
7+
8+
9+
def response(content_type, content):
10+
response = requests.Response()
11+
response.status_code = 200
12+
response.headers["Content-Type"] = f"{content_type}; charset=utf-8"
13+
response._content = content.encode("utf-8")
14+
return response
15+
16+
17+
def test_search_json_returns_serp_results():
18+
client = serpapi.Client(api_key="test-api-key")
19+
client.session.request = Mock(
20+
return_value=response(
21+
"application/json",
22+
'{"search_metadata": {"id": "search-123"}}',
23+
)
24+
)
25+
26+
result = client.search(engine="google", q="Coffee", output="json")
27+
28+
assert isinstance(result, serpapi.SerpResults)
29+
assert result["search_metadata"]["id"] == "search-123"
30+
assert result.client is client
31+
_, request_kwargs = client.session.request.call_args
32+
assert request_kwargs["params"]["output"] == "json"
33+
34+
35+
@pytest.mark.parametrize(
36+
("output", "content_type"),
37+
[
38+
("html", "text/html"),
39+
("md", "text/markdown"),
40+
],
41+
)
42+
def test_search_text_outputs_return_raw_text(output, content_type):
43+
content = '{"looks": "like JSON"}'
44+
client = serpapi.Client(api_key="test-api-key")
45+
client.session.request = Mock(return_value=response(content_type, content))
46+
47+
result = client.search(engine="google", q="Coffee", output=output)
48+
49+
assert result == content
50+
assert isinstance(result, str)
51+
_, request_kwargs = client.session.request.call_args
52+
assert request_kwargs["params"]["output"] == output

0 commit comments

Comments
 (0)