-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_serpapi.py
More file actions
102 lines (88 loc) · 2.8 KB
/
test_serpapi.py
File metadata and controls
102 lines (88 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import unittest
import os
import pprint
import serpapi
import pytest
# This test shows how to extends serpapi.Client
# without using client engine wrapper.
#
class TestSerpApi(unittest.TestCase):
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
def test_search(self):
client = serpapi.Client({
"engine": "google",
"api_key": os.getenv("API_KEY")
})
data = client.search({
"q": "Coffee",
"location": "Austin,Texas"
})
assert data.get("error") == None
self.assertIsNotNone(data["organic_results"][0]["title"])
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
def test_html(self):
client = serpapi.Client({
"engine": "google",
"api_key": os.getenv("API_KEY")
})
data = client.html({
"q": "Coffee",
"location": "Austin,Texas",
})
self.assertRegex(data, r'</html>$')
def test_invalid_api_key(self):
client = serpapi.Client({
"engine": "google",
"api_key": "invalid_api_key"
})
with pytest.raises(serpapi.SerpApiException, match=r'Invalid API key'):
client.search({
"q": "Coffee",
"location": "USA",
})
def test_invalid_decoder(self):
client = serpapi.Client({
"engine": "google",
"api_key": os.getenv("API_KEY")
})
mockResponse = MockResponse()
self.assertEqual(mockResponse.status, 200)
with pytest.raises(serpapi.SerpApiException, match=r'Invalid decoder'):
client.decode(mockResponse, 'bad')
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
def test_error_missing_engine(self):
client = serpapi.Client({
"api_key": os.getenv("API_KEY"),
"engine": ""
})
with pytest.raises(serpapi.SerpApiException, match=r'Unsupported.*search engine.'):
client.search({"q": "Coffee"})
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
def test_missing_q(self):
client = serpapi.Client({
"api_key": os.getenv("API_KEY")
})
with pytest.raises(serpapi.SerpApiException, match=r'Missing query'):
client.search({"engine": "google"})
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
def test_no_parameter(self):
client = serpapi.Client()
with pytest.raises(serpapi.SerpApiException, match=r'Missing query'):
client.search({"engine": "google", 'api_key': os.getenv('API_KEY')})
def debug(self, payload):
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(payload)
# Mock object to enable higher code coverage
#
class MockResponse:
'''Mock HTTP response in order to test serpapi.decode'''
def __init__(self, status=200):
self.status = 200
self.data = MockString("{}")
class MockString:
def __init__(self, data: str):
self.data = data
def decode(self, encoding) -> str:
return self.data
if __name__ == '__main__':
unittest.main()