Spaces:
Build error
Build error
| #!/usr/bin/env python3 | |
| """Test script to verify the translation service migration.""" | |
| import sys | |
| import os | |
| # Add src to path | |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src')) | |
| def test_translation_provider_creation(): | |
| """Test that we can create translation providers.""" | |
| print("Testing translation provider creation...") | |
| try: | |
| from infrastructure.translation.provider_factory import ( | |
| TranslationProviderFactory, | |
| TranslationProviderType | |
| ) | |
| factory = TranslationProviderFactory() | |
| # Test getting available providers | |
| available = factory.get_available_providers() | |
| print(f"Available providers: {[p.value for p in available]}") | |
| # Test provider info | |
| all_info = factory.get_all_providers_info() | |
| print(f"Provider info: {all_info}") | |
| # Test creating NLLB provider (may fail if transformers not installed) | |
| try: | |
| provider = factory.create_provider(TranslationProviderType.NLLB) | |
| print(f"Created provider: {provider.provider_name}") | |
| print(f"Provider available: {provider.is_available()}") | |
| # Test supported languages | |
| supported = provider.get_supported_languages() | |
| print(f"Supported language pairs: {len(supported)} source languages") | |
| if 'en' in supported: | |
| print(f"English can translate to: {len(supported['en'])} languages") | |
| except Exception as e: | |
| print(f"Could not create NLLB provider (expected if transformers not installed): {e}") | |
| print("β Translation provider creation test passed") | |
| return True | |
| except Exception as e: | |
| print(f"β Translation provider creation test failed: {e}") | |
| return False | |
| def test_domain_models(): | |
| """Test that domain models work correctly.""" | |
| print("\nTesting domain models...") | |
| try: | |
| from domain.models.text_content import TextContent | |
| from domain.models.translation_request import TranslationRequest | |
| # Test TextContent creation | |
| text_content = TextContent( | |
| text="Hello, world!", | |
| language="en", | |
| encoding="utf-8" | |
| ) | |
| print(f"Created TextContent: {text_content.text} ({text_content.language})") | |
| # Test TranslationRequest creation | |
| translation_request = TranslationRequest( | |
| source_text=text_content, | |
| target_language="zh" | |
| ) | |
| print(f"Created TranslationRequest: {translation_request.effective_source_language} -> {translation_request.target_language}") | |
| print("β Domain models test passed") | |
| return True | |
| except Exception as e: | |
| print(f"β Domain models test failed: {e}") | |
| return False | |
| def test_base_class_functionality(): | |
| """Test the base class functionality.""" | |
| print("\nTesting base class functionality...") | |
| try: | |
| from infrastructure.base.translation_provider_base import TranslationProviderBase | |
| from domain.models.text_content import TextContent | |
| from domain.models.translation_request import TranslationRequest | |
| # Create a mock provider for testing | |
| class MockTranslationProvider(TranslationProviderBase): | |
| def __init__(self): | |
| super().__init__("MockProvider", {"en": ["zh", "es", "fr"]}) | |
| def _translate_chunk(self, text: str, source_language: str, target_language: str) -> str: | |
| return f"[TRANSLATED:{source_language}->{target_language}]{text}" | |
| def is_available(self) -> bool: | |
| return True | |
| def get_supported_languages(self) -> dict: | |
| return self.supported_languages | |
| # Test the mock provider | |
| provider = MockTranslationProvider() | |
| # Test text chunking | |
| long_text = "This is a test sentence. " * 50 # Create long text | |
| chunks = provider._chunk_text(long_text) | |
| print(f"Text chunked into {len(chunks)} pieces") | |
| # Test translation request | |
| text_content = TextContent(text="Hello world", language="en") | |
| request = TranslationRequest(source_text=text_content, target_language="zh") | |
| result = provider.translate(request) | |
| print(f"Translation result: {result.text}") | |
| print("β Base class functionality test passed") | |
| return True | |
| except Exception as e: | |
| print(f"β Base class functionality test failed: {e}") | |
| return False | |
| def main(): | |
| """Run all tests.""" | |
| print("Running translation service migration tests...\n") | |
| tests = [ | |
| test_domain_models, | |
| test_translation_provider_creation, | |
| test_base_class_functionality | |
| ] | |
| passed = 0 | |
| total = len(tests) | |
| for test in tests: | |
| if test(): | |
| passed += 1 | |
| print(f"\n{'='*50}") | |
| print(f"Test Results: {passed}/{total} tests passed") | |
| if passed == total: | |
| print("π All tests passed! Translation service migration is working correctly.") | |
| return 0 | |
| else: | |
| print("β Some tests failed. Please check the implementation.") | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) |