1- import sys , os , unittest , asyncio
1+ import sys , os , unittest , asyncio , re
22from unittest .mock import MagicMock
33sys .path .append (os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
44from pystackql import StackQL , magic , magics , StackqlMagic , StackqlServerMagic
@@ -155,13 +155,37 @@ def test_09_csv_output_with_header(self):
155155
156156 @pystackql_test_setup ()
157157 def test_10_executeStmt (self ):
158+ okta_result_dict = self .stackql .executeStmt (registry_pull_okta_query )
159+ okta_result = okta_result_dict [0 ]["message" ]
160+ expected_pattern = registry_pull_resp_pattern ("okta" )
161+ self .assertTrue (re .search (expected_pattern , okta_result ), f"Expected pattern not found in result: { okta_result } " )
162+ github_result_dict = self .stackql .executeStmt (registry_pull_github_query )
163+ github_result = github_result_dict [0 ]["message" ]
164+ expected_pattern = registry_pull_resp_pattern ("github" )
165+ self .assertTrue (re .search (expected_pattern , github_result ), f"Expected pattern not found in result: { github_result } " )
166+ print_test_result (f"""Test executeStmt method\n RESULTS:\n { okta_result_dict } \n { github_result_dict } """ , True )
167+
168+ @pystackql_test_setup (output = "csv" )
169+ def test_10a_executeStmt_with_csv_output (self ):
158170 okta_result = self .stackql .executeStmt (registry_pull_okta_query )
159171 expected_pattern = registry_pull_resp_pattern ("okta" )
160172 self .assertTrue (re .search (expected_pattern , okta_result ), f"Expected pattern not found in result: { okta_result } " )
161173 github_result = self .stackql .executeStmt (registry_pull_github_query )
162174 expected_pattern = registry_pull_resp_pattern ("github" )
163175 self .assertTrue (re .search (expected_pattern , github_result ), f"Expected pattern not found in result: { github_result } " )
164- print_test_result (f"""Test executeStmt method\n RESULTS:\n { okta_result } { github_result } """ , True )
176+ print_test_result (f"""Test executeStmt method with csv output\n RESULTS:\n { okta_result } \n { github_result } """ , True )
177+
178+ @pystackql_test_setup (output = "pandas" )
179+ def test_10b_executeStmt_with_pandas_output (self ):
180+ okta_result_df = self .stackql .executeStmt (registry_pull_okta_query )
181+ okta_result = okta_result_df ['message' ].iloc [0 ]
182+ expected_pattern = registry_pull_resp_pattern ("okta" )
183+ self .assertTrue (re .search (expected_pattern , okta_result ), f"Expected pattern not found in result: { okta_result } " )
184+ github_result_df = self .stackql .executeStmt (registry_pull_github_query )
185+ github_result = github_result_df ['message' ].iloc [0 ]
186+ expected_pattern = registry_pull_resp_pattern ("github" )
187+ self .assertTrue (re .search (expected_pattern , github_result ), f"Expected pattern not found in result: { github_result } " )
188+ print_test_result (f"""Test executeStmt method with pandas output\n RESULTS:\n { okta_result_df } \n { github_result_df } """ , True )
165189
166190 @pystackql_test_setup ()
167191 def test_11_execute_with_defaults (self ):
@@ -232,13 +256,16 @@ def test_19_server_mode_connectivity(self):
232256 @pystackql_test_setup (server_mode = True )
233257 def test_20_executeStmt_server_mode (self ):
234258 result = self .stackql .executeStmt (registry_pull_google_query )
235- is_valid_json_string_of_empty_list = False
236- try :
237- parsed_result = json .loads (result )
238- is_valid_json_string_of_empty_list = isinstance (parsed_result , list ) and len (parsed_result ) == 0
239- except json .JSONDecodeError :
240- pass
241- print_test_result ("Test executeStmt in server mode" , is_valid_json_string_of_empty_list , True )
259+ # Checking if the result is a list containing a single dictionary with a key 'message' and value 'OK'
260+ is_valid_response = isinstance (result , list ) and len (result ) == 1 and result [0 ].get ('message' ) == 'OK'
261+ print_test_result (f"Test executeStmt in server mode\n { result } " , is_valid_response , True )
262+
263+ @pystackql_test_setup (server_mode = True , output = 'pandas' )
264+ def test_20a_executeStmt_server_mode_with_pandas_output (self ):
265+ result_df = self .stackql .executeStmt (registry_pull_google_query )
266+ # Verifying if the result is a dataframe with a column 'message' containing the value 'OK' in its first row
267+ is_valid_response = isinstance (result_df , pd .DataFrame ) and 'message' in result_df .columns and result_df ['message' ].iloc [0 ] == 'OK'
268+ print_test_result (f"Test executeStmt in server mode with pandas output\n { result_df } " , is_valid_response , True )
242269
243270 @pystackql_test_setup (server_mode = True )
244271 def test_21_execute_server_mode_default_output (self ):
@@ -288,10 +315,11 @@ def setUp(self):
288315 self .stackql_magic = self .MAGIC_CLASS (shell = self .shell )
289316 self .query = "SELECT 1 as fred"
290317 self .expected_result = pd .DataFrame ({"fred" : [1 ]})
318+ self .statement = "REGISTRY PULL github"
291319
292320 def print_test_result (self , test_name , * checks ):
293321 all_passed = all (checks )
294- print_test_result (f"{ test_name } , server_mode: { self . server_mode } " , all_passed , True , True )
322+ print_test_result (f"{ test_name } " , all_passed , self . server_mode , True )
295323
296324 def run_magic_test (self , line , cell , expect_none = False ):
297325 # Mock the run_query method to return a known DataFrame.
@@ -310,16 +338,52 @@ def run_magic_test(self, line, cell, expect_none=False):
310338
311339 def test_line_magic_query (self ):
312340 checks = self .run_magic_test (line = self .query , cell = None )
313- self .print_test_result ("Line magic test" , * checks )
341+ self .print_test_result ("Line magic query test" , * checks )
314342
315343 def test_cell_magic_query (self ):
316344 checks = self .run_magic_test (line = "" , cell = self .query )
317- self .print_test_result ("Cell magic test" , * checks )
345+ self .print_test_result ("Cell magic query test" , * checks )
318346
319347 def test_cell_magic_query_no_output (self ):
320348 checks = self .run_magic_test (line = "--no-display" , cell = self .query , expect_none = True )
321- self .print_test_result ("Cell magic test (with --no-display)" , * checks )
349+ self .print_test_result ("Cell magic query test (with --no-display)" , * checks )
322350
351+ def run_magic_statement_test (self , line , cell , expect_none = False ):
352+ # Execute the magic with our statement.
353+ result = self .stackql_magic .stackql (line = line , cell = cell )
354+ # Validate the outcome.
355+ checks = []
356+ # Check that the output contains expected content
357+ if expect_none :
358+ checks .append (result is None )
359+ else :
360+ if self .server_mode :
361+ checks .append ("OK" in result ["message" ].iloc [0 ])
362+ else :
363+ pattern = registry_pull_resp_pattern ('github' )
364+ message = result ["message" ].iloc [0 ] if "message" in result .columns else ""
365+ checks .append (bool (re .search (pattern , message )))
366+ # Check dataframe exists and is populated as expected
367+ checks .append ('stackql_df' in self .shell .user_ns )
368+ if self .server_mode :
369+ checks .append ("OK" in self .shell .user_ns ['stackql_df' ]["message" ].iloc [0 ])
370+ else :
371+ pattern = registry_pull_resp_pattern ('github' )
372+ message = self .shell .user_ns ['stackql_df' ]["message" ].iloc [0 ] if 'stackql_df' in self .shell .user_ns else ""
373+ checks .append (bool (re .search (pattern , message )))
374+ return checks , result
375+
376+ def test_line_magic_statement (self ):
377+ checks , result = self .run_magic_statement_test (line = self .statement , cell = None )
378+ self .print_test_result (f"Line magic statement test\n { result } " , * checks )
379+
380+ def test_cell_magic_statement (self ):
381+ checks , result = self .run_magic_statement_test (line = "" , cell = self .statement )
382+ self .print_test_result (f"Cell magic statement test\n { result } " , * checks )
383+
384+ def test_cell_magic_statement_no_output (self ):
385+ checks , result = self .run_magic_statement_test (line = "--no-display" , cell = self .statement , expect_none = True )
386+ self .print_test_result (f"Cell magic statement test (with --no-display)\n { result } " , * checks )
323387
324388class StackQLMagicTests (BaseStackQLMagicTests , unittest .TestCase ):
325389
0 commit comments