Spaces:
Build error
Build error
File size: 5,446 Bytes
9626844 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
#!/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()) |