🔍 Code Extractor

function test_imports

Maturity: 42

A diagnostic function that tests the availability and correct import of all critical project modules including configuration, logging utilities, and email forwarding components.

File:
/tf/active/vicechatdev/email-forwarder/test_imports.py
Lines:
11 - 50
Complexity:
simple

Purpose

This function serves as a health check for the project's module structure and dependencies. It systematically attempts to import each required module (config.settings, utils.logger, forwarder components) and reports success or failure for each import. This is useful for troubleshooting deployment issues, verifying project structure integrity, and ensuring all dependencies are properly installed before running the main application.

Source Code

def test_imports():
    """Test all imports work correctly."""
    print("Testing imports...")
    
    try:
        import config.settings as settings
        print("✓ Config settings imported successfully")
    except Exception as e:
        print(f"✗ Failed to import config.settings: {e}")
        return False
    
    try:
        from utils.logger import setup_logging
        print("✓ Logger utilities imported successfully")
    except Exception as e:
        print(f"✗ Failed to import logger: {e}")
        return False
    
    try:
        from forwarder.o365_client import O365Client
        print("✓ O365Client imported successfully")
    except Exception as e:
        print(f"✗ Failed to import O365Client: {e}")
        return False
    
    try:
        from forwarder.email_handler import EmailHandler
        print("✓ EmailHandler imported successfully")
    except Exception as e:
        print(f"✗ Failed to import EmailHandler: {e}")
        return False
    
    try:
        from forwarder.smtp_server import SMTPServer
        print("✓ SMTPServer imported successfully")
    except Exception as e:
        print(f"✗ Failed to import SMTPServer: {e}")
        return False
    
    return True

Return Value

Returns a boolean value: True if all imports succeed, False if any import fails. The function also prints status messages to stdout for each import attempt, prefixed with '✓' for success or '✗' for failure, along with error details when imports fail.

Dependencies

  • config.settings
  • utils.logger
  • forwarder.o365_client
  • forwarder.email_handler
  • forwarder.smtp_server

Conditional/Optional Imports

These imports are only needed under specific conditions:

import config.settings as settings

Condition: tested during function execution to verify config module availability

Required (conditional)
from utils.logger import setup_logging

Condition: tested during function execution to verify logger utilities availability

Required (conditional)
from forwarder.o365_client import O365Client

Condition: tested during function execution to verify O365 client availability

Required (conditional)
from forwarder.email_handler import EmailHandler

Condition: tested during function execution to verify email handler availability

Required (conditional)
from forwarder.smtp_server import SMTPServer

Condition: tested during function execution to verify SMTP server availability

Required (conditional)

Usage Example

# Run the import test to verify all modules are available
result = test_imports()

if result:
    print("All imports successful, application is ready to run")
else:
    print("Import failures detected, check error messages above")
    sys.exit(1)

# Typical usage in a startup script or test suite:
if __name__ == '__main__':
    if not test_imports():
        print("Failed to import required modules. Please check your installation.")
        sys.exit(1)
    print("All checks passed. Starting application...")

Best Practices

  • Run this function during application startup or as part of deployment verification to catch import issues early
  • Use the return value to conditionally proceed with application initialization or exit gracefully
  • Monitor the printed output for detailed error messages when debugging import failures
  • This function only tests if modules can be imported, not if they are properly configured or functional
  • Consider running this as part of CI/CD pipeline to verify package structure before deployment
  • The function catches all exceptions during imports, making it safe to run even with missing dependencies

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_configuration 63.4% similar

    A test function that validates configuration settings by importing and calling the Config.validate_config() method, printing the result and returning a boolean status.

    From: /tf/active/vicechatdev/SPFCsync/test_connections.py
  • function test_models_integration 60.5% similar

    Integration test function that validates the import and instantiation of data models including SectionType, TextSection, DataAnalysisSession, AnalysisStatus, DataSource, and DataSourceType.

    From: /tf/active/vicechatdev/vice_ai/test_integration.py
  • function test_config_loading 60.1% similar

    A test function that validates configuration loading by instantiating a Config object and verifying access to key configuration parameters across FileCloud, LLM, and output settings.

    From: /tf/active/vicechatdev/contract_validity_analyzer/test_implementation.py
  • function test_pyodbc_import 58.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 check_dependencies_v1 58.8% similar

    Validates the presence of required Python packages by attempting to import them and returns a list of any missing dependencies.

    From: /tf/active/vicechatdev/email-forwarder/run_service.py
← Back to Browse