11import sys , os , unittest , asyncio , re
2- from unittest .mock import MagicMock
2+ from unittest .mock import MagicMock , patch
33sys .path .append (os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
44from pystackql import StackQL , magic , magics , StackqlMagic , StackqlServerMagic
55from .test_params import *
66
77def pystackql_test_setup (** kwargs ):
88 def decorator (func ):
9- def wrapper (self ):
9+ def wrapper (self , * args ):
1010 try :
1111 del self .stackql
1212 except AttributeError :
1313 pass
1414 self .stackql = StackQL (** kwargs )
15- func (self )
15+ func (self , * args )
1616 return wrapper
1717 return decorator
1818
@@ -201,8 +201,14 @@ def test_11a_execute_with_defaults_null_response(self):
201201 print_test_result (f"Test execute with defaults (empty response)\n RESULT: { result } " , is_valid_empty_resp )
202202
203203 @pystackql_test_setup (output = 'pandas' )
204- def test_12_execute_with_pandas_output (self ):
205- # result = self.stackql.execute(aws_query)
204+ @patch ('pystackql.StackQL.execute' )
205+ def test_12_execute_with_pandas_output (self , mock_execute ):
206+ # mocking the response for pandas DataFrame
207+ mock_execute .return_value = pd .DataFrame ({
208+ 'status' : ['RUNNING' , 'TERMINATED' ],
209+ 'num_instances' : [2 , 1 ]
210+ })
211+
206212 result = self .stackql .execute (google_query )
207213 is_valid_dataframe = isinstance (result , pd .DataFrame )
208214 self .assertTrue (is_valid_dataframe , f"Result is not a valid DataFrame: { result } " )
@@ -221,8 +227,10 @@ def test_12_execute_with_pandas_output(self):
221227 print_test_result (f"Test execute with pandas output\n RESULT COUNT: { len (result )} " , is_valid_dataframe )
222228
223229 @pystackql_test_setup (output = 'csv' )
224- def test_13_execute_with_csv_output (self ):
225- # result = self.stackql.execute(aws_query)
230+ @patch ('pystackql.StackQL.execute' )
231+ def test_13_execute_with_csv_output (self , mock_execute ):
232+ # mocking the response for csv output
233+ mock_execute .return_value = "status,num_instances\n RUNNING,2\n TERMINATED,1\n "
226234 result = self .stackql .execute (google_query )
227235 is_valid_csv = isinstance (result , str ) and result .count ("\n " ) >= 1 and result .count ("," ) >= 1
228236 self .assertTrue (is_valid_csv , f"Result is not a valid CSV: { result } " )
@@ -280,22 +288,34 @@ def test_20a_executeStmt_server_mode_with_pandas_output(self):
280288 print_test_result (f"Test executeStmt in server mode with pandas output\n { result_df } " , is_valid_response , True )
281289
282290 @pystackql_test_setup (server_mode = True )
283- def test_21_execute_server_mode_default_output (self ):
291+ @patch ('pystackql.stackql.StackQL._run_server_query' )
292+ def test_21_execute_server_mode_default_output (self , mock_run_server_query ):
293+ # Mocking the response as a list of dictionaries
294+ mock_result = [
295+ {'status' : 'RUNNING' , 'num_instances' : 2 },
296+ {'status' : 'TERMINATED' , 'num_instances' : 1 }
297+ ]
298+ mock_run_server_query .return_value = mock_result
299+
284300 result = self .stackql .execute (google_query )
285301 is_valid_dict_output = isinstance (result , list ) and all (isinstance (row , dict ) for row in result )
286302 print_test_result (f"""Test execute in server_mode with default output\n RESULT_COUNT: { len (result )} """ , is_valid_dict_output , True )
303+ # Check `_run_server_query` method
304+ mock_run_server_query .assert_called_once_with (google_query )
287305
288306 @pystackql_test_setup (server_mode = True , output = 'pandas' )
289- def test_22_execute_server_mode_pandas_output (self ):
290- # result = self.stackql.execute(aws_query)
307+ @patch ('pystackql.stackql.StackQL._run_server_query' )
308+ def test_22_execute_server_mode_pandas_output (self , mock_run_server_query ):
309+ # Mocking the response for pandas DataFrame
310+ mock_df = pd .DataFrame ({
311+ 'status' : ['RUNNING' , 'TERMINATED' ],
312+ 'num_instances' : [2 , 1 ]
313+ })
314+ mock_run_server_query .return_value = mock_df .to_dict (orient = 'records' )
291315 result = self .stackql .execute (google_query )
292316 is_valid_dataframe = isinstance (result , pd .DataFrame )
293317 self .assertTrue (is_valid_dataframe , f"Result is not a valid DataFrame: { result } " )
294318 # Check datatypes of the columns
295- # expected_dtypes = {
296- # 'instance_type': 'object',
297- # 'num_instances': 'int64'
298- # }
299319 expected_dtypes = {
300320 'status' : 'object' ,
301321 'num_instances' : 'int64'
@@ -304,6 +324,8 @@ def test_22_execute_server_mode_pandas_output(self):
304324 actual_dtype = result [col ].dtype
305325 self .assertEqual (actual_dtype , expected_dtype , f"Column '{ col } ' has dtype '{ actual_dtype } ' but expected '{ expected_dtype } '" )
306326 print_test_result (f"Test execute in server_mode with pandas output\n RESULT COUNT: { len (result )} " , is_valid_dataframe )
327+ # Check `_run_server_query` method
328+ mock_run_server_query .assert_called_once_with (google_query )
307329
308330class MockInteractiveShell :
309331 """A mock class for IPython's InteractiveShell."""
0 commit comments