|
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 | | -""" |
7 | 1 | import os |
8 | 2 | import sys |
9 | 3 | import logging |
10 | | -import subprocess |
11 | | -from typing import List, Tuple |
| 4 | +from databricks.sql.client import Connection |
12 | 5 |
|
13 | 6 | logging.basicConfig(level=logging.DEBUG) |
14 | 7 | logger = logging.getLogger(__name__) |
15 | 8 |
|
16 | | -TEST_MODULES = [ |
17 | | - "test_sea_session", |
18 | | - "test_sea_sync_query", |
19 | | - "test_sea_async_query", |
20 | | - "test_sea_metadata", |
21 | | -] |
22 | | - |
23 | | - |
24 | | -def run_test_module(module_name: str) -> bool: |
25 | | - """Run a test module and return success status.""" |
26 | | - module_path = os.path.join( |
27 | | - os.path.dirname(os.path.abspath(__file__)), "tests", f"{module_name}.py" |
28 | | - ) |
29 | | - |
30 | | - # Simply run the module as a script - each module handles its own test execution |
31 | | - result = subprocess.run( |
32 | | - [sys.executable, module_path], capture_output=True, text=True |
33 | | - ) |
34 | | - |
35 | | - # Log the output from the test module |
36 | | - if result.stdout: |
37 | | - for line in result.stdout.strip().split("\n"): |
38 | | - logger.info(line) |
39 | | - |
40 | | - if result.stderr: |
41 | | - for line in result.stderr.strip().split("\n"): |
42 | | - logger.error(line) |
43 | | - |
44 | | - return result.returncode == 0 |
45 | | - |
46 | | - |
47 | | -def run_tests() -> List[Tuple[str, bool]]: |
48 | | - """Run all tests and return results.""" |
49 | | - results = [] |
50 | | - |
51 | | - for module_name in TEST_MODULES: |
52 | | - try: |
53 | | - logger.info(f"\n{'=' * 50}") |
54 | | - logger.info(f"Running test: {module_name}") |
55 | | - logger.info(f"{'-' * 50}") |
56 | | - |
57 | | - success = run_test_module(module_name) |
58 | | - results.append((module_name, success)) |
59 | | - |
60 | | - status = "✅ PASSED" if success else "❌ FAILED" |
61 | | - logger.info(f"Test {module_name}: {status}") |
62 | | - |
63 | | - except Exception as e: |
64 | | - logger.error(f"Error loading or running test {module_name}: {str(e)}") |
65 | | - import traceback |
66 | | - |
67 | | - logger.error(traceback.format_exc()) |
68 | | - results.append((module_name, False)) |
69 | | - |
70 | | - return results |
71 | | - |
72 | | - |
73 | | -def print_summary(results: List[Tuple[str, bool]]) -> None: |
74 | | - """Print a summary of test results.""" |
75 | | - logger.info(f"\n{'=' * 50}") |
76 | | - logger.info("TEST SUMMARY") |
77 | | - logger.info(f"{'-' * 50}") |
78 | | - |
79 | | - passed = sum(1 for _, success in results if success) |
80 | | - total = len(results) |
81 | | - |
82 | | - for module_name, success in results: |
83 | | - status = "✅ PASSED" if success else "❌ FAILED" |
84 | | - logger.info(f"{status} - {module_name}") |
85 | | - |
86 | | - logger.info(f"{'-' * 50}") |
87 | | - logger.info(f"Total: {total} | Passed: {passed} | Failed: {total - passed}") |
88 | | - logger.info(f"{'=' * 50}") |
89 | | - |
90 | | - |
91 | | -if __name__ == "__main__": |
92 | | - # Check if required environment variables are set |
93 | | - required_vars = [ |
94 | | - "DATABRICKS_SERVER_HOSTNAME", |
95 | | - "DATABRICKS_HTTP_PATH", |
96 | | - "DATABRICKS_TOKEN", |
97 | | - ] |
98 | | - missing_vars = [var for var in required_vars if not os.environ.get(var)] |
99 | | - |
100 | | - if missing_vars: |
101 | | - logger.error( |
102 | | - f"Missing required environment variables: {', '.join(missing_vars)}" |
| 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 |
103 | 47 | ) |
104 | | - logger.error("Please set these variables before running the tests.") |
| 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()) |
105 | 61 | sys.exit(1) |
| 62 | + |
| 63 | + logger.info("SEA session test completed successfully") |
106 | 64 |
|
107 | | - # Run all tests |
108 | | - results = run_tests() |
109 | | - |
110 | | - # Print summary |
111 | | - print_summary(results) |
112 | | - |
113 | | - # Exit with appropriate status code |
114 | | - all_passed = all(success for _, success in results) |
115 | | - sys.exit(0 if all_passed else 1) |
| 65 | +if __name__ == "__main__": |
| 66 | + test_sea_session() |
0 commit comments