|
| 1 | +import sys, os, unittest, asyncio |
| 2 | +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 3 | +from pystackql import StackQL |
| 4 | +from .test_params import * |
| 5 | + |
| 6 | +def async_test_decorator(func): |
| 7 | + def wrapper(*args, **kwargs): |
| 8 | + if asyncio.iscoroutinefunction(func): |
| 9 | + return asyncio.run(func(*args, **kwargs)) |
| 10 | + else: |
| 11 | + return func(*args, **kwargs) |
| 12 | + return wrapper |
| 13 | + |
| 14 | +class PyStackQLTestsBase(unittest.TestCase): |
| 15 | + pass |
| 16 | + |
| 17 | +def setUpModule(): |
| 18 | + print("downloading stackql binary...") |
| 19 | + PyStackQLTestsBase.stackql = StackQL() |
| 20 | + print("downloading aws provider for tests...") |
| 21 | + res = PyStackQLTestsBase.stackql.executeStmt(registry_pull_aws_query) |
| 22 | + print(res) |
| 23 | + print("downloading google provider for tests...") |
| 24 | + res = PyStackQLTestsBase.stackql.executeStmt(registry_pull_google_query) |
| 25 | + print(res) |
| 26 | + print("starting stackql server...") |
| 27 | + PyStackQLTestsBase.server_process = subprocess.Popen([PyStackQLTestsBase.stackql.bin_path, "srv", "--pgsrv.address", server_address, "--pgsrv.port", str(server_port)]) |
| 28 | + time.sleep(5) |
| 29 | + |
| 30 | +def tearDownModule(): |
| 31 | + print("stopping stackql server...") |
| 32 | + if PyStackQLTestsBase.server_process: |
| 33 | + PyStackQLTestsBase.server_process.terminate() |
| 34 | + PyStackQLTestsBase.server_process.wait() |
| 35 | + |
| 36 | +class PyStackQLAsyncTests(PyStackQLTestsBase): |
| 37 | + |
| 38 | + @async_test_decorator |
| 39 | + async def test_executeQueriesAsync_server_mode_default_output(self): |
| 40 | + stackql = StackQL(server_mode=True) |
| 41 | + result = await stackql.executeQueriesAsync(async_queries) |
| 42 | + is_valid_result = isinstance(result, list) and all(isinstance(res, dict) for res in result) |
| 43 | + self.assertTrue(is_valid_result, f"Result is not a valid list of dicts: {result}") |
| 44 | + print_test_result(f"[ASYNC] Test executeQueriesAsync in server_mode with default output\nRESULT_COUNT: {len(result)}", is_valid_result, True) |
| 45 | + |
| 46 | + @async_test_decorator |
| 47 | + async def test_executeQueriesAsync_server_mode_pandas_output(self): |
| 48 | + stackql = StackQL(server_mode=True, output='pandas') |
| 49 | + result = await stackql.executeQueriesAsync(async_queries) |
| 50 | + is_valid_dataframe = isinstance(result, pd.DataFrame) and not result.empty |
| 51 | + self.assertTrue(is_valid_dataframe, f"Result is not a valid DataFrame: {result}") |
| 52 | + print_test_result(f"[ASYNC] Test executeQueriesAsync in server_mode with pandas output\nRESULT_COUNT: {len(result)}", is_valid_dataframe, True) |
| 53 | + |
| 54 | +def main(): |
| 55 | + unittest.main(verbosity=0) |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + main() |
0 commit comments