1
1
"""
2
2
Main script to run all SEA connector tests.
3
3
4
- This script imports and runs all the individual test modules and displays
4
+ This script runs all the individual test modules and displays
5
5
a summary of test results with visual indicators.
6
6
"""
7
7
import os
8
8
import sys
9
9
import logging
10
- import importlib . util
11
- from typing import Dict , Callable , List , Tuple
10
+ import subprocess
11
+ from typing import List , Tuple
12
12
13
- # Configure logging
14
- logging .basicConfig (level = logging .INFO )
13
+ logging .basicConfig (level = logging .DEBUG )
15
14
logger = logging .getLogger (__name__ )
16
15
17
- # Define test modules and their main test functions
18
16
TEST_MODULES = [
19
17
"test_sea_session" ,
20
18
"test_sea_sync_query" ,
23
21
]
24
22
25
23
26
- def load_test_function (module_name : str ) -> Callable :
27
- """Load a test function from a module ."""
24
+ def run_test_module (module_name : str ) -> bool :
25
+ """Run a test module and return success status ."""
28
26
module_path = os .path .join (
29
27
os .path .dirname (os .path .abspath (__file__ )), "tests" , f"{ module_name } .py"
30
28
)
31
29
32
- spec = importlib .util .spec_from_file_location (module_name , module_path )
33
- module = importlib .util .module_from_spec (spec )
34
- spec .loader .exec_module (module )
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
+ )
35
34
36
- # Get the main test function (assuming it starts with "test_")
37
- for name in dir (module ):
38
- if name .startswith ("test_" ) and callable (getattr (module , name )):
39
- # For sync and async query modules, we want the main function that runs both tests
40
- if name == f"test_sea_{ module_name .replace ('test_sea_' , '' )} _exec" :
41
- return getattr (module , name )
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 )
42
39
43
- # Fallback to the first test function found
44
- for name in dir (module ):
45
- if name .startswith ("test_" ) and callable (getattr (module , name )):
46
- return getattr (module , name )
40
+ if result .stderr :
41
+ for line in result .stderr .strip ().split ("\n " ):
42
+ logger .error (line )
47
43
48
- raise ValueError ( f"No test function found in module { module_name } " )
44
+ return result . returncode == 0
49
45
50
46
51
47
def run_tests () -> List [Tuple [str , bool ]]:
@@ -54,12 +50,11 @@ def run_tests() -> List[Tuple[str, bool]]:
54
50
55
51
for module_name in TEST_MODULES :
56
52
try :
57
- test_func = load_test_function (module_name )
58
53
logger .info (f"\n { '=' * 50 } " )
59
54
logger .info (f"Running test: { module_name } " )
60
55
logger .info (f"{ '-' * 50 } " )
61
56
62
- success = test_func ( )
57
+ success = run_test_module ( module_name )
63
58
results .append ((module_name , success ))
64
59
65
60
status = "✅ PASSED" if success else "❌ FAILED"
@@ -92,29 +87,48 @@ def print_summary(results: List[Tuple[str, bool]]) -> None:
92
87
logger .info (f"Total: { total } | Passed: { passed } | Failed: { total - passed } " )
93
88
logger .info (f"{ '=' * 50 } " )
94
89
95
-
96
- if __name__ == "__main__" :
97
- # Check if required environment variables are set
98
- required_vars = [
99
- "DATABRICKS_SERVER_HOSTNAME" ,
100
- "DATABRICKS_HTTP_PATH" ,
101
- "DATABRICKS_TOKEN" ,
102
- ]
103
- missing_vars = [var for var in required_vars if not os .environ .get (var )]
104
-
105
- if missing_vars :
106
- logger .error (
107
- f"Missing required environment variables: { ', ' .join (missing_vars )} "
90
+ server_hostname = os .environ .get ("DATABRICKS_SERVER_HOSTNAME" )
91
+ http_path = os .environ .get ("DATABRICKS_HTTP_PATH" )
92
+ access_token = os .environ .get ("DATABRICKS_TOKEN" )
93
+ catalog = os .environ .get ("DATABRICKS_CATALOG" )
94
+
95
+ if not all ([server_hostname , http_path , access_token ]):
96
+ logger .error ("Missing required environment variables." )
97
+ logger .error ("Please set DATABRICKS_SERVER_HOSTNAME, DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN." )
98
+ sys .exit (1 )
99
+
100
+ logger .info (f"Connecting to { server_hostname } " )
101
+ logger .info (f"HTTP Path: { http_path } " )
102
+ if catalog :
103
+ logger .info (f"Using catalog: { catalog } " )
104
+
105
+ try :
106
+ logger .info ("Creating connection with SEA backend..." )
107
+ connection = Connection (
108
+ server_hostname = server_hostname ,
109
+ http_path = http_path ,
110
+ access_token = access_token ,
111
+ catalog = catalog ,
112
+ schema = "default" ,
113
+ use_sea = True ,
114
+ user_agent_entry = "SEA-Test-Client" # add custom user agent
108
115
)
109
- logger .error ("Please set these variables before running the tests." )
116
+
117
+ logger .info (f"Successfully opened SEA session with ID: { connection .get_session_id_hex ()} " )
118
+ logger .info (f"backend type: { type (connection .session .backend )} " )
119
+
120
+ # Close the connection
121
+ logger .info ("Closing the SEA session..." )
122
+ connection .close ()
123
+ logger .info ("Successfully closed SEA session" )
124
+
125
+ except Exception as e :
126
+ logger .error (f"Error testing SEA session: { str (e )} " )
127
+ import traceback
128
+ logger .error (traceback .format_exc ())
110
129
sys .exit (1 )
130
+
131
+ logger .info ("SEA session test completed successfully" )
111
132
112
- # Run all tests
113
- results = run_tests ()
114
-
115
- # Print summary
116
- print_summary (results )
117
-
118
- # Exit with appropriate status code
119
- all_passed = all (success for _ , success in results )
120
- sys .exit (0 if all_passed else 1 )
133
+ if __name__ == "__main__" :
134
+ test_sea_session ()
0 commit comments