@@ -39,8 +39,10 @@ def test_html(self):
3939 def test_object (self ):
4040 client = serpapi .Client ({
4141 "engine" : "google" ,
42- "api_key" : os .getenv ("API_KEY" )
43- })
42+ "api_key" : os .getenv ("API_KEY" ),
43+ 'timeout' : 120 ,
44+ 'retries' : True
45+ })
4446 data = client .search ({
4547 "q" : "Coffee" ,
4648 "location" : "Austin,Texas"
@@ -62,7 +64,17 @@ def test_invalid_api_key(self):
6264 "location" : "USA" ,
6365 })
6466
65- # TODO file a ticket default search engine is google
67+ def test_invalid_decoder (self ):
68+ client = serpapi .Client ({
69+ "engine" : "google" ,
70+ "api_key" : os .getenv ("API_KEY" ),
71+ })
72+ mockResponse = MockResponse ()
73+ self .assertEqual (mockResponse .status , 200 )
74+ with pytest .raises (serpapi .SerpApiException , match = r'Invalid decoder' ):
75+ client .decode (mockResponse , 'bad' )
76+
77+ @unittest .skipIf ((os .getenv ("API_KEY" ) == None ), "no api_key provided" )
6678 def test_error_missing_engine (self ):
6779 client = serpapi .Client ({
6880 "api_key" : os .getenv ("API_KEY" ),
@@ -71,16 +83,39 @@ def test_error_missing_engine(self):
7183 with pytest .raises (serpapi .SerpApiException , match = r'Unsupported.*search engine.' ):
7284 client .search ({"q" : "Coffee" })
7385
86+ @unittest .skipIf ((os .getenv ("API_KEY" ) == None ), "no api_key provided" )
7487 def test_missing_q (self ):
7588 client = serpapi .Client ({
7689 "api_key" : os .getenv ("API_KEY" )
7790 })
7891 with pytest .raises (serpapi .SerpApiException , match = r'Missing query' ):
7992 client .search ({"engine" : "google" })
8093
94+ @unittest .skipIf ((os .getenv ("API_KEY" ) == None ), "no api_key provided" )
95+ def test_no_parameter (self ):
96+ client = serpapi .Client ()
97+ with pytest .raises (serpapi .SerpApiException , match = r'Missing query' ):
98+ client .search ({"engine" : "google" , 'api_key' : os .getenv ('API_KEY' )})
99+
100+
81101 def debug (self , payload ):
82102 pp = pprint .PrettyPrinter (indent = 2 )
83103 pp .pprint (payload )
84104
105+ # Mock object to enable higher code coverage
106+ #
107+ class MockResponse :
108+ '''Mock HTTP response in order to test serpapi.decode'''
109+ def __init__ (self , status = 200 ):
110+ self .status = 200
111+ self .data = MockString ("{}" )
112+
113+ class MockString :
114+ def __init__ (self , data : str ):
115+ self .data = data
116+
117+ def decode (self , encoding ) -> str :
118+ return self .data
119+
85120if __name__ == '__main__' :
86121 unittest .main ()
0 commit comments