|
| 1 | +import random |
| 2 | +import unittest |
| 3 | +import os |
| 4 | +import pprint |
| 5 | +from serpapi.google_search_results import GoogleSearchResults |
| 6 | + |
| 7 | +class TestSearchApi(unittest.TestCase): |
| 8 | + |
| 9 | +def setUp(self): |
| 10 | +GoogleSearchResults.SERP_API_KEY = os.getenv("API_KEY", "demo") |
| 11 | + |
| 12 | +@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided") |
| 13 | +def test_get_json(self): |
| 14 | +client = GoogleSearchResults({"q": "Coffee", "location": "Austin,Texas"}) |
| 15 | +data = client.get_json() |
| 16 | +self.assertEqual(data["search_metadata"]["status"], "Success") |
| 17 | +self.assertIsNotNone(data["search_metadata"]["google_url"]) |
| 18 | +self.assertIsNotNone(data["search_metadata"]["id"]) |
| 19 | +# pp = pprint.PrettyPrinter(indent=2) |
| 20 | +# pp.pprint(data['local_results']) |
| 21 | +self.assertIsNotNone(data['local_results']['places'][0]) |
| 22 | + |
| 23 | +@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided") |
| 24 | +def test_get_json(self): |
| 25 | +client = GoogleSearchResults({"q": "Coffee", "engine": "google_scholar"}) |
| 26 | +data = client.get_json() |
| 27 | +self.assertIsNotNone(data["organic_results"][0]["title"]) |
| 28 | + |
| 29 | +@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided") |
| 30 | +def test_get_dict(self): |
| 31 | +client = GoogleSearchResults({"q": "Coffee", "location": "Austin,Texas"}) |
| 32 | +data = client.get_dict() |
| 33 | +self.assertIsNotNone(data.get('local_results')) |
| 34 | + |
| 35 | +@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided") |
| 36 | +def test_get_dictionary(self): |
| 37 | +client = GoogleSearchResults({"q": "Coffee", "location": "Austin,Texas"}) |
| 38 | +data = client.get_dictionary() |
| 39 | +self.assertIsNotNone(data.get('local_results')) |
| 40 | + |
| 41 | +@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided") |
| 42 | +def test_get_html(self): |
| 43 | +client = GoogleSearchResults({"q": "Coffee", "location": "Austin,Texas"}) |
| 44 | +data = client.get_html() |
| 45 | +self.assertGreater(len(data), 10) |
| 46 | + |
| 47 | +if __name__ == '__main__': |
| 48 | +unittest.main() |
0 commit comments