๐Ÿ” Code Extractor

function main_v41

Maturity: 44

Orchestrates and executes a suite of ODBC connectivity tests for SQL Server, providing formatted output and a summary of test results.

File:
/tf/active/vicechatdev/full_smartstat/test_odbc.py
Lines:
69 - 104
Complexity:
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

  • pyodbc
  • sqlalchemy

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_odbc_driver 74.6% similar

    Tests the availability of ODBC drivers on the system, specifically checking for SQL Server drivers and returning a boolean indicating whether SQL Server drivers are found.

    From: /tf/active/vicechatdev/full_smartstat/test_odbc.py
  • function test_pyodbc_import 68.9% similar

    A diagnostic function that tests whether the pyodbc library can be imported and displays its version information and ODBC compatibility details.

    From: /tf/active/vicechatdev/full_smartstat/test_odbc.py
  • function test_connection_string 66.5% similar

    Tests the format and creation of a SQL Server connection string using SQLAlchemy with ODBC Driver 18, without establishing an actual database connection.

    From: /tf/active/vicechatdev/full_smartstat/test_odbc.py
  • function main_v25 65.1% similar

    Orchestrates and executes a comprehensive test suite for the Vice AI Data Analysis Integration, running multiple test functions, creating test datasets, and providing detailed pass/fail reporting.

    From: /tf/active/vicechatdev/vice_ai/test_integration.py
  • function main_v42 62.6% similar

    Orchestrates and executes a test suite for an email forwarder service, running multiple test functions sequentially and reporting results.

    From: /tf/active/vicechatdev/email-forwarder/test_service.py
โ† Back to Browse