function main_v41
Orchestrates and executes a suite of ODBC connectivity tests for SQL Server, providing formatted output and a summary of test results.
/tf/active/vicechatdev/full_smartstat/test_odbc.py
69 - 104
simple
Purpose
This function serves as the main entry point for testing SQL Server ODBC connectivity. It runs a predefined set of tests (pyodbc import, ODBC driver availability, and connection string validation), collects results, handles exceptions, and provides a comprehensive summary with helpful connection tips. It's designed to validate that the environment is properly configured for SQL Server database connections.
Source Code
def main():
"""Run all tests"""
print("๐งช SmartStat ODBC Connectivity Test")
print("=" * 40)
tests = [
test_pyodbc_import,
test_odbc_driver,
test_connection_string
]
results = []
for test in tests:
try:
result = test()
results.append(result)
except Exception as e:
print(f"โ Test failed with exception: {e}")
results.append(False)
print("\n" + "=" * 40)
print("๐ Test Summary:")
print(f"โ
Passed: {sum(results)}/{len(results)} tests")
if all(results):
print("๐ All tests passed! SQL Server connectivity is ready.")
print("\n๐ก Connection Tips:")
print(" - Use 'ODBC Driver 18 for SQL Server' as driver name")
print(" - For local connections: server='localhost' or server='127.0.0.1'")
print(" - For trusted connections: trusted_connection=yes")
print(" - For SQL Auth: provide username and password")
print(" - You may need TrustServerCertificate=yes for SSL issues")
else:
print("โ ๏ธ Some tests failed. Check the errors above.")
return all(results)
Return Value
Returns a boolean value indicating whether all tests passed. Returns True if all tests in the suite completed successfully, False if any test failed or raised an exception.
Dependencies
pyodbcsqlalchemy
Required Imports
import pyodbc
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import text
Usage Example
# Ensure test functions are defined in the same module
def test_pyodbc_import():
try:
import pyodbc
print("โ
pyodbc imported successfully")
return True
except ImportError:
print("โ pyodbc import failed")
return False
def test_odbc_driver():
drivers = pyodbc.drivers()
if 'ODBC Driver 18 for SQL Server' in drivers:
print("โ
ODBC Driver 18 found")
return True
print("โ ODBC Driver 18 not found")
return False
def test_connection_string():
print("โ
Connection string test passed")
return True
# Run the main test suite
if __name__ == '__main__':
success = main()
exit(0 if success else 1)
Best Practices
- This function expects three test functions to be defined in the same module: test_pyodbc_import, test_odbc_driver, and test_connection_string
- Each test function should return a boolean indicating success (True) or failure (False)
- Test functions should handle their own exceptions and print appropriate messages
- The function is designed to be used as a standalone diagnostic tool, typically called from if __name__ == '__main__' block
- Return value can be used to set exit codes for CI/CD pipelines (0 for success, 1 for failure)
- All test exceptions are caught and treated as test failures, ensuring the suite completes even if individual tests crash
- The function provides user-friendly output with emojis and formatting for better readability in terminal environments
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_odbc_driver 74.6% similar
-
function test_pyodbc_import 68.9% similar
-
function test_connection_string 66.5% similar
-
function main_v25 65.1% similar
-
function main_v42 62.6% similar