-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_search_archive.py
More file actions
37 lines (33 loc) · 1.34 KB
/
test_search_archive.py
File metadata and controls
37 lines (33 loc) · 1.34 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
import unittest
import os
import serpapi
import pytest
class TestSearchArchive(unittest.TestCase):
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
def test_search_archive(self):
client = serpapi.Client({
"engine": "google",
"api_key": os.getenv("API_KEY")
})
data = client.search({
"q": "Coffee",
"location": "Austin,Texas"
})
self.assertEqual(data.get("error"), None)
self.assertIsNotNone(data["organic_results"][0]["title"])
# search unique search identifier
search_id = data['search_metadata']['id']
# fetch results from the archive (free of charge)
data_archive = client.search_archive(search_id)
self.assertEqual(data_archive['organic_results'][0], data["organic_results"][0])
# fetch results from the archive again (code coverage)
object_archive = client.search_archive(search_id, 'json')
self.assertIsNotNone(object_archive)
self.assertEqual(object_archive['organic_results'][0]['title'], data["organic_results"][0]["title"])
def test_bad_decoder(self):
client = serpapi.Client({
"engine": "google",
"api_key": os.getenv("API_KEY")
})
with pytest.raises(serpapi.SerpApiException, match=r'Decoder must be json or html'):
client.search_archive('007', 'bad')