Skip to content

Commit 1ce9794

Browse files
committed
fix tests
1 parent dc2ae8b commit 1ce9794

5 files changed

Lines changed: 15 additions & 28 deletions

File tree

serpapi/serpapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def search_archive(self, search_id: str, decoder: str = 'json'):
189189
if decoder in self.SUPPORTED_DECODER:
190190
path += decoder
191191
else:
192-
raise SerpApiException('Decoder must be json or html')
192+
raise SerpApiException(f'Invalid decoder: {decoder} must be json or html. ')
193193
return self.start(path, {}, decoder)
194194

195195
def account(self, api_key: str = None):

tests/example_search_google_local_services_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def test_search_google_local_services(self):
1212
'api_key': os.getenv("API_KEY")
1313
})
1414
data = client.search({
15-
'q': 'Electrician',
16-
'data_cid': 'ChIJOwg_06VPwokRYv534QaPC8g',
15+
'q': 'electrician',
16+
'data_cid': '6745062158417646970',
1717
})
1818
self.assertIsNone(data.get('error'))
1919
self.assertIsNotNone(data['local_ads'])

tests/test_location_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def test_get_account(self):
1010
client = serpapi.Client({'api_key': os.getenv('API_KEY')})
1111
locations = client.location({'q':'Austin', 'limit': 3})
1212
self.assertGreater(len(locations), 1)
13+
self.assertLessEqual(len(locations), 3)
1314
self.assertTrue('id' in locations[0])
14-
15-
15+
self.assertTrue('name' in locations[0])
16+
self.assertTrue('canonical_name' in locations[0])

tests/test_search_archive.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ def test_search_archive(self):
2424
self.assertEqual(data_archive['organic_results'][0], data["organic_results"][0])
2525

2626
# fetch results from the archive again (code coverage)
27-
object_archive = client.search_archive(search_id, 'object')
27+
object_archive = client.search_archive(search_id, 'json')
2828
self.assertIsNotNone(object_archive)
29-
self.assertEqual(object_archive.organic_results[0].title, data["organic_results"][0]["title"])
29+
self.assertEqual(object_archive['organic_results'][0]['title'], data["organic_results"][0]["title"])
3030

31+
def test_bad_decoder(self):
32+
client = serpapi.Client({
33+
"engine": "google",
34+
"api_key": os.getenv("API_KEY")
35+
})
3136
with pytest.raises(serpapi.SerpApiException, match=r'Decoder must be json or html'):
32-
client.search_archive(search_id, 'bad')
37+
client.search_archive('007', 'bad')

tests/test_serpapi.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,6 @@ def test_html(self):
3434
})
3535
self.assertRegex(data, r'</html>$')
3636

37-
# test ObjectDecoder
38-
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
39-
def test_object(self):
40-
client = serpapi.Client({
41-
"engine": "google",
42-
"api_key": os.getenv("API_KEY"),
43-
'timeout': 120,
44-
'retries': True
45-
})
46-
data = client.search({
47-
"q": "Coffee",
48-
"location": "Austin,Texas"
49-
}, decoder="object")
50-
self.assertIsInstance(data, object)
51-
self.assertIsNotNone(data.organic_results)
52-
self.assertIsInstance(data.organic_results, list)
53-
self.assertGreater(len(data.organic_results), 3)
54-
self.assertIsInstance(data.organic_results[0].link, str)
55-
5637
def test_invalid_api_key(self):
5738
client = serpapi.Client({
5839
"engine": "google",
@@ -67,7 +48,7 @@ def test_invalid_api_key(self):
6748
def test_invalid_decoder(self):
6849
client = serpapi.Client({
6950
"engine": "google",
70-
"api_key": os.getenv("API_KEY"),
51+
"api_key": os.getenv("API_KEY")
7152
})
7253
mockResponse = MockResponse()
7354
self.assertEqual(mockResponse.status, 200)

0 commit comments

Comments
 (0)