|
| 1 | +""" |
| 2 | +Main script to run all SEA connector tests. |
| 3 | +
|
| 4 | +This script runs all the individual test modules and displays |
| 5 | +a summary of test results with visual indicators. |
| 6 | +""" |
1 | 7 | import os
|
2 | 8 | import sys
|
3 | 9 | import logging
|
4 |
| -from databricks.sql.client import Connection |
| 10 | +import subprocess |
| 11 | +from typing import List, Tuple |
5 | 12 |
|
6 | 13 | logging.basicConfig(level=logging.DEBUG)
|
7 | 14 | logger = logging.getLogger(__name__)
|
8 | 15 |
|
9 |
| -def test_sea_session(): |
10 |
| - """ |
11 |
| - Test opening and closing a SEA session using the connector. |
12 |
| - |
13 |
| - This function connects to a Databricks SQL endpoint using the SEA backend, |
14 |
| - opens a session, and then closes it. |
15 |
| - |
16 |
| - Required environment variables: |
17 |
| - - DATABRICKS_SERVER_HOSTNAME: Databricks server hostname |
18 |
| - - DATABRICKS_HTTP_PATH: HTTP path for the SQL endpoint |
19 |
| - - DATABRICKS_TOKEN: Personal access token for authentication |
20 |
| - """ |
21 |
| - |
22 |
| - server_hostname = os.environ.get("DATABRICKS_SERVER_HOSTNAME") |
23 |
| - http_path = os.environ.get("DATABRICKS_HTTP_PATH") |
24 |
| - access_token = os.environ.get("DATABRICKS_TOKEN") |
25 |
| - catalog = os.environ.get("DATABRICKS_CATALOG") |
26 |
| - |
27 |
| - if not all([server_hostname, http_path, access_token]): |
28 |
| - logger.error("Missing required environment variables.") |
29 |
| - logger.error("Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN.") |
30 |
| - sys.exit(1) |
31 |
| - |
32 |
| - logger.info(f"Connecting to {server_hostname}") |
33 |
| - logger.info(f"HTTP Path: {http_path}") |
34 |
| - if catalog: |
35 |
| - logger.info(f"Using catalog: {catalog}") |
36 |
| - |
37 |
| - try: |
38 |
| - logger.info("Creating connection with SEA backend...") |
39 |
| - connection = Connection( |
40 |
| - server_hostname=server_hostname, |
41 |
| - http_path=http_path, |
42 |
| - access_token=access_token, |
43 |
| - catalog=catalog, |
44 |
| - schema="default", |
45 |
| - use_sea=True, |
46 |
| - user_agent_entry="SEA-Test-Client" # add custom user agent |
| 16 | +TEST_MODULES = [ |
| 17 | + "test_sea_session", |
| 18 | + "test_sea_sync_query", |
| 19 | + "test_sea_async_query", |
| 20 | + "test_sea_metadata", |
| 21 | + "test_sea_multi_chunk", |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +def run_test_module(module_name: str) -> bool: |
| 26 | + """Run a test module and return success status.""" |
| 27 | + module_path = os.path.join( |
| 28 | + os.path.dirname(os.path.abspath(__file__)), "tests", f"{module_name}.py" |
| 29 | + ) |
| 30 | + |
| 31 | + # Handle the multi-chunk test which is in the main directory |
| 32 | + if module_name == "test_sea_multi_chunk": |
| 33 | + module_path = os.path.join( |
| 34 | + os.path.dirname(os.path.abspath(__file__)), f"{module_name}.py" |
47 | 35 | )
|
48 |
| - |
49 |
| - logger.info(f"Successfully opened SEA session with ID: {connection.get_session_id_hex()}") |
50 |
| - logger.info(f"backend type: {type(connection.session.backend)}") |
51 |
| - |
52 |
| - # Close the connection |
53 |
| - logger.info("Closing the SEA session...") |
54 |
| - connection.close() |
55 |
| - logger.info("Successfully closed SEA session") |
56 |
| - |
57 |
| - except Exception as e: |
58 |
| - logger.error(f"Error testing SEA session: {str(e)}") |
59 |
| - import traceback |
60 |
| - logger.error(traceback.format_exc()) |
61 |
| - sys.exit(1) |
62 |
| - |
63 |
| - logger.info("SEA session test completed successfully") |
| 36 | + |
| 37 | + # Simply run the module as a script - each module handles its own test execution |
| 38 | + result = subprocess.run( |
| 39 | + [sys.executable, module_path], capture_output=True, text=True |
| 40 | + ) |
| 41 | + |
| 42 | + # Log the output from the test module |
| 43 | + if result.stdout: |
| 44 | + for line in result.stdout.strip().split("\n"): |
| 45 | + logger.info(line) |
| 46 | + |
| 47 | + if result.stderr: |
| 48 | + for line in result.stderr.strip().split("\n"): |
| 49 | + logger.error(line) |
| 50 | + |
| 51 | + return result.returncode == 0 |
| 52 | + |
| 53 | + |
| 54 | +def run_tests() -> List[Tuple[str, bool]]: |
| 55 | + """Run all tests and return results.""" |
| 56 | + results = [] |
| 57 | + |
| 58 | + for module_name in TEST_MODULES: |
| 59 | + try: |
| 60 | + logger.info(f"\n{'=' * 50}") |
| 61 | + logger.info(f"Running test: {module_name}") |
| 62 | + logger.info(f"{'-' * 50}") |
| 63 | + |
| 64 | + success = run_test_module(module_name) |
| 65 | + results.append((module_name, success)) |
| 66 | + |
| 67 | + status = "✅ PASSED" if success else "❌ FAILED" |
| 68 | + logger.info(f"Test {module_name}: {status}") |
| 69 | + |
| 70 | + except Exception as e: |
| 71 | + logger.error(f"Error loading or running test {module_name}: {str(e)}") |
| 72 | + import traceback |
| 73 | + |
| 74 | + logger.error(traceback.format_exc()) |
| 75 | + results.append((module_name, False)) |
| 76 | + |
| 77 | + return results |
| 78 | + |
| 79 | + |
| 80 | +def print_summary(results: List[Tuple[str, bool]]) -> None: |
| 81 | + """Print a summary of test results.""" |
| 82 | + logger.info(f"\n{'=' * 50}") |
| 83 | + logger.info("TEST SUMMARY") |
| 84 | + logger.info(f"{'-' * 50}") |
| 85 | + |
| 86 | + passed = sum(1 for _, success in results if success) |
| 87 | + total = len(results) |
| 88 | + |
| 89 | + for module_name, success in results: |
| 90 | + status = "✅ PASSED" if success else "❌ FAILED" |
| 91 | + logger.info(f"{status} - {module_name}") |
| 92 | + |
| 93 | + logger.info(f"{'-' * 50}") |
| 94 | + logger.info(f"Total: {total} | Passed: {passed} | Failed: {total - passed}") |
| 95 | + logger.info(f"{'=' * 50}") |
| 96 | + |
64 | 97 |
|
65 | 98 | if __name__ == "__main__":
|
66 |
| - test_sea_session() |
| 99 | + # Check if required environment variables are set |
| 100 | + required_vars = [ |
| 101 | + "DATABRICKS_SERVER_HOSTNAME", |
| 102 | + "DATABRICKS_HTTP_PATH", |
| 103 | + "DATABRICKS_TOKEN", |
| 104 | + ] |
| 105 | + missing_vars = [var for var in required_vars if not os.environ.get(var)] |
| 106 | + |
| 107 | + if missing_vars: |
| 108 | + logger.error( |
| 109 | + f"Missing required environment variables: {', '.join(missing_vars)}" |
| 110 | + ) |
| 111 | + logger.error("Please set these variables before running the tests.") |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | + # Run all tests |
| 115 | + results = run_tests() |
| 116 | + |
| 117 | + # Print summary |
| 118 | + print_summary(results) |
| 119 | + |
| 120 | + # Exit with appropriate status code |
| 121 | + all_passed = all(success for _, success in results) |
| 122 | + sys.exit(0 if all_passed else 1) |
0 commit comments