Spaces:
Build error
Build error
#!/usr/bin/env python3 | |
"""Test script for dependency injection and configuration setup.""" | |
import sys | |
import os | |
import logging | |
from pathlib import Path | |
# Add src to path for imports | |
sys.path.insert(0, str(Path(__file__).parent / "src")) | |
from infrastructure.config.container_setup import ( | |
create_configured_container, | |
get_service_registry_info, | |
initialize_global_container, | |
get_global_container, | |
cleanup_global_container | |
) | |
from infrastructure.config.app_config import AppConfig | |
from infrastructure.config.dependency_container import DependencyContainer | |
from application.services.audio_processing_service import AudioProcessingApplicationService | |
from application.services.configuration_service import ConfigurationApplicationService | |
def test_configuration_loading(): | |
"""Test configuration loading from file and environment.""" | |
print("=" * 60) | |
print("Testing Configuration Loading") | |
print("=" * 60) | |
try: | |
# Test default configuration | |
print("\n1. Testing default configuration...") | |
config = AppConfig() | |
print(f"β Default config loaded successfully") | |
print(f" - TTS preferred providers: {config.tts.preferred_providers}") | |
print(f" - STT default model: {config.stt.default_model}") | |
print(f" - Translation provider: {config.translation.default_provider}") | |
print(f" - Temp directory: {config.processing.temp_dir}") | |
print(f" - Log level: {config.logging.level}") | |
# Test configuration from file | |
print("\n2. Testing configuration from file...") | |
config_file = "config.example.json" | |
if os.path.exists(config_file): | |
config_from_file = AppConfig(config_file=config_file) | |
print(f"β Config loaded from {config_file}") | |
print(f" - TTS preferred providers: {config_from_file.tts.preferred_providers}") | |
print(f" - Processing timeout: {config_from_file.processing.processing_timeout_seconds}s") | |
else: | |
print(f"β Config file {config_file} not found, skipping file test") | |
# Test environment variable override | |
print("\n3. Testing environment variable override...") | |
os.environ['TTS_DEFAULT_SPEED'] = '1.5' | |
os.environ['LOG_LEVEL'] = 'DEBUG' | |
config_with_env = AppConfig() | |
print(f"β Config with environment overrides loaded") | |
print(f" - TTS default speed: {config_with_env.tts.default_speed}") | |
print(f" - Log level: {config_with_env.logging.level}") | |
# Clean up environment | |
del os.environ['TTS_DEFAULT_SPEED'] | |
del os.environ['LOG_LEVEL'] | |
return True | |
except Exception as e: | |
print(f"β Configuration loading failed: {e}") | |
return False | |
def test_dependency_container(): | |
"""Test dependency container setup and service resolution.""" | |
print("\n" + "=" * 60) | |
print("Testing Dependency Container") | |
print("=" * 60) | |
try: | |
# Test container creation | |
print("\n1. Testing container creation...") | |
container = create_configured_container() | |
print("β Container created successfully") | |
# Test service registration info | |
print("\n2. Testing service registry...") | |
registry_info = get_service_registry_info(container) | |
print("β Service registry information retrieved") | |
print(f" - Registered services: {len(registry_info['registered_services'])}") | |
for service_name, lifetime in registry_info['registered_services'].items(): | |
print(f" β’ {service_name}: {lifetime}") | |
# Test provider availability | |
print("\n3. Testing provider availability...") | |
provider_info = registry_info['provider_availability'] | |
print(f" - Available TTS providers: {provider_info['tts_providers']}") | |
print(f" - Available STT providers: {provider_info['stt_providers']}") | |
print(f" - Available translation providers: {provider_info['translation_providers']}") | |
# Test service resolution | |
print("\n4. Testing service resolution...") | |
# Resolve configuration | |
config = container.resolve(AppConfig) | |
print("β AppConfig resolved successfully") | |
# Resolve application services | |
audio_service = container.resolve(AudioProcessingApplicationService) | |
print("β AudioProcessingApplicationService resolved successfully") | |
config_service = container.resolve(ConfigurationApplicationService) | |
print("β ConfigurationApplicationService resolved successfully") | |
# Test provider services | |
tts_provider = container.get_tts_provider() | |
print(f"β TTS provider resolved: {tts_provider.__class__.__name__}") | |
stt_provider = container.get_stt_provider() | |
print(f"β STT provider resolved: {stt_provider.__class__.__name__}") | |
translation_provider = container.get_translation_provider() | |
print(f"β Translation provider resolved: {translation_provider.__class__.__name__}") | |
# Test scoped services | |
print("\n5. Testing scoped services...") | |
with container.create_scope() as scope: | |
scoped_audio_service = scope.resolve(AudioProcessingApplicationService) | |
print("β Scoped AudioProcessingApplicationService resolved") | |
# Cleanup | |
container.cleanup() | |
print("β Container cleanup completed") | |
return True | |
except Exception as e: | |
print(f"β Dependency container test failed: {e}") | |
import traceback | |
traceback.print_exc() | |
return False | |
def test_global_container(): | |
"""Test global container management.""" | |
print("\n" + "=" * 60) | |
print("Testing Global Container Management") | |
print("=" * 60) | |
try: | |
# Test global container initialization | |
print("\n1. Testing global container initialization...") | |
global_container = initialize_global_container() | |
print("β Global container initialized") | |
# Test global container access | |
print("\n2. Testing global container access...") | |
retrieved_container = get_global_container() | |
print("β Global container retrieved") | |
assert global_container is retrieved_container, "Global container instances should be the same" | |
print("β Global container instance consistency verified") | |
# Test service resolution through global container | |
print("\n3. Testing service resolution through global container...") | |
config = retrieved_container.resolve(AppConfig) | |
print("β Configuration resolved through global container") | |
# Test global container cleanup | |
print("\n4. Testing global container cleanup...") | |
cleanup_global_container() | |
print("β Global container cleaned up") | |
return True | |
except Exception as e: | |
print(f"β Global container test failed: {e}") | |
return False | |
def test_configuration_service(): | |
"""Test configuration application service.""" | |
print("\n" + "=" * 60) | |
print("Testing Configuration Application Service") | |
print("=" * 60) | |
try: | |
# Create container | |
container = create_configured_container() | |
# Get configuration service | |
print("\n1. Testing configuration service resolution...") | |
config_service = container.resolve(ConfigurationApplicationService) | |
print("β Configuration service resolved") | |
# Test getting current configuration | |
print("\n2. Testing configuration retrieval...") | |
current_config = config_service.get_current_configuration() | |
print("β Current configuration retrieved") | |
print(f" - Configuration sections: {list(current_config.keys())}") | |
# Test TTS configuration | |
print("\n3. Testing TTS configuration...") | |
tts_config = config_service.get_tts_configuration() | |
print(f"β TTS configuration: {len(tts_config)} settings") | |
# Test configuration updates | |
print("\n4. Testing configuration updates...") | |
updated_tts = config_service.update_tts_configuration({ | |
'default_speed': 1.2, | |
'enable_streaming': False | |
}) | |
print("β TTS configuration updated") | |
print(f" - New default speed: {updated_tts['default_speed']}") | |
print(f" - Streaming enabled: {updated_tts['enable_streaming']}") | |
# Test provider availability | |
print("\n5. Testing provider availability check...") | |
availability = config_service.get_provider_availability() | |
print("β Provider availability checked") | |
for category, providers in availability.items(): | |
print(f" - {category.upper()}: {providers}") | |
# Test system info | |
print("\n6. Testing system information...") | |
system_info = config_service.get_system_info() | |
print("β System information retrieved") | |
print(f" - Supported languages: {len(system_info['supported_languages'])}") | |
print(f" - Supported audio formats: {system_info['supported_audio_formats']}") | |
# Test configuration validation | |
print("\n7. Testing configuration validation...") | |
validation_issues = config_service.validate_configuration() | |
print("β Configuration validation completed") | |
total_issues = sum(len(issues) for issues in validation_issues.values()) | |
if total_issues == 0: | |
print(" - No validation issues found") | |
else: | |
print(f" - Found {total_issues} validation issues") | |
for category, issues in validation_issues.items(): | |
if issues: | |
print(f" β’ {category}: {issues}") | |
# Cleanup | |
container.cleanup() | |
return True | |
except Exception as e: | |
print(f"β Configuration service test failed: {e}") | |
import traceback | |
traceback.print_exc() | |
return False | |
def main(): | |
"""Run all tests.""" | |
print("Dependency Injection and Configuration Test Suite") | |
print("=" * 60) | |
# Configure basic logging | |
logging.basicConfig( | |
level=logging.WARNING, # Reduce noise during testing | |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' | |
) | |
tests = [ | |
("Configuration Loading", test_configuration_loading), | |
("Dependency Container", test_dependency_container), | |
("Global Container Management", test_global_container), | |
("Configuration Service", test_configuration_service) | |
] | |
results = [] | |
for test_name, test_func in tests: | |
try: | |
result = test_func() | |
results.append((test_name, result)) | |
except Exception as e: | |
print(f"\nβ {test_name} failed with exception: {e}") | |
results.append((test_name, False)) | |
# Print summary | |
print("\n" + "=" * 60) | |
print("Test Results Summary") | |
print("=" * 60) | |
passed = 0 | |
failed = 0 | |
for test_name, result in results: | |
status = "β PASSED" if result else "β FAILED" | |
print(f"{test_name:<30} {status}") | |
if result: | |
passed += 1 | |
else: | |
failed += 1 | |
print(f"\nTotal: {len(results)} tests, {passed} passed, {failed} failed") | |
if failed == 0: | |
print("\nπ All tests passed!") | |
return 0 | |
else: | |
print(f"\nβ {failed} test(s) failed!") | |
return 1 | |
if __name__ == "__main__": | |
sys.exit(main()) |