function test_connection_string
Tests the format and creation of a SQL Server connection string using SQLAlchemy with ODBC Driver 18, without establishing an actual database connection.
/tf/active/vicechatdev/full_smartstat/test_odbc.py
31 - 50
simple
Purpose
This function validates that a SQL Server connection string is properly formatted and that a SQLAlchemy engine can be created with the specified configuration. It uses a mock strategy to avoid actual database connectivity, making it useful for testing connection string syntax, driver availability checks, and SQLAlchemy configuration validation in development or CI/CD environments where database access may not be available.
Source Code
def test_connection_string():
"""Test connection string format"""
print("\nš Testing connection string format...")
try:
# Test connection string format (without actually connecting)
driver = "{ODBC Driver 18 for SQL Server}"
conn_str = f"mssql+pyodbc://localhost/testdb?driver={driver}&trusted_connection=yes"
print(f"ā
Connection string format: {conn_str}")
# Test engine creation (without connecting)
engine = create_engine(conn_str, strategy='mock', executor=lambda sql, *_: None)
print("ā
SQLAlchemy engine creation successful")
return True
except Exception as e:
print(f"ā Error with connection string: {e}")
return False
Return Value
Returns a boolean value: True if the connection string format is valid and the SQLAlchemy engine can be created successfully, False if any exception occurs during the process. The function also prints status messages to stdout indicating success or failure at each step.
Dependencies
pyodbcsqlalchemy
Required Imports
import pyodbc
from sqlalchemy import create_engine
Usage Example
# Import required modules
from sqlalchemy import create_engine
import pyodbc
# Call the test function
result = test_connection_string()
if result:
print("Connection string test passed")
else:
print("Connection string test failed")
# Expected output:
# š Testing connection string format...
# ā
Connection string format: mssql+pyodbc://localhost/testdb?driver={ODBC Driver 18 for SQL Server}&trusted_connection=yes
# ā
SQLAlchemy engine creation successful
Best Practices
- This function is designed for testing purposes only and should not be used for actual database connections
- The mock strategy prevents actual database connectivity, making it safe for CI/CD pipelines
- Ensure ODBC Driver 18 for SQL Server is installed before running this test
- The function uses Windows Authentication (trusted_connection=yes), which requires Windows environment
- Consider wrapping this in a test suite or using it as part of environment validation scripts
- The function prints output directly; consider capturing stdout if using in automated testing frameworks
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_odbc_driver 68.1% similar
-
function main_v41 66.5% similar
-
class ConnectionConfig 59.1% similar
-
function test_connection 57.3% similar
-
function test_pyodbc_import 54.0% similar