Skip to content

Commit 0f006d6

Browse files
committed
fix: Fix test codes and remove test.env
1 parent f0417d3 commit 0f006d6

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

run_tests

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/bash
2-
. tests/creds/env_vars/test.env
3-
python3 -m tests.pystackql_tests
2+
python3 -m tests.pystackql_tests

tests/pystackql_tests.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import sys, os, unittest, asyncio, re
2-
from unittest.mock import MagicMock
2+
from unittest.mock import MagicMock, patch
33
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
44
from pystackql import StackQL, magic, magics, StackqlMagic, StackqlServerMagic
55
from .test_params import *
66

77
def 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)\nRESULT: {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\nRESULT 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\nRUNNING,2\nTERMINATED,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\nRESULT_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\nRESULT COUNT: {len(result)}", is_valid_dataframe)
327+
# Check `_run_server_query` method
328+
mock_run_server_query.assert_called_once_with(google_query)
307329

308330
class MockInteractiveShell:
309331
"""A mock class for IPython's InteractiveShell."""

tests/test_params.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import json, os, sys, platform, re, time, unittest, subprocess
1+
import json, sys, platform, re, time, unittest, subprocess
22
import pandas as pd
33
from termcolor import colored
44
import unittest.mock as mock
@@ -33,11 +33,14 @@ def get_custom_download_dir(platform_name):
3333
def registry_pull_resp_pattern(provider):
3434
return r"%s provider, version 'v\d+\.\d+\.\d+' successfully installed\s*" % provider
3535

36+
test_gcp_project_id = "test-gcp-project"
37+
test_gcp_zone = "australia-southeast2-a"
38+
3639
google_query = f"""
3740
SELECT status, count(*) as num_instances
3841
FROM google.compute.instances
39-
WHERE project = '{os.environ['GCP_PROJECT']}'
40-
AND zone = '{os.environ['GCP_ZONE']}'
42+
WHERE project = '{test_gcp_project_id}'
43+
AND zone = '{test_gcp_zone}'
4144
GROUP BY status
4245
"""
4346

@@ -50,15 +53,15 @@ def registry_pull_resp_pattern(provider):
5053
GROUP BY instance_type
5154
"""
5255

53-
regions = os.environ.get('AWS_REGIONS').split(',')
56+
test_aws_regions = ["ap-southeast-2", "ap-southeast-4"]
5457

5558
async_queries = [
5659
f"""
5760
SELECT region, COUNT(*) as num_functions
5861
FROM aws.lambda.functions
5962
WHERE region = '{region}'
6063
"""
61-
for region in regions
64+
for region in test_aws_regions
6265
]
6366

6467
def print_test_result(test_name, condition=True, server_mode=False, is_ipython=False):
@@ -73,4 +76,4 @@ def print_test_result(test_name, condition=True, server_mode=False, is_ipython=F
7376
headers.append(test_name)
7477
message = " ".join(headers)
7578

76-
print("\n" + message)
79+
print("\n" + message)

0 commit comments

Comments
 (0)