Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Script di test per l'API di traduzione locale. | |
| Questo script testa tutti gli endpoint principali del servizio. | |
| """ | |
| import requests | |
| import json | |
| import time | |
| # Configurazione | |
| BASE_URL = "http://localhost:8000" | |
| def test_health_check(): | |
| """Testa l'endpoint di health check.""" | |
| print("π₯ Test Health Check...") | |
| try: | |
| response = requests.get(f"{BASE_URL}/") | |
| print(f"Status: {response.status_code}") | |
| print(f"Response: {response.json()}") | |
| print("β Health check OK\n") | |
| return True | |
| except Exception as e: | |
| print(f"β Health check failed: {e}\n") | |
| return False | |
| def test_get_languages(): | |
| """Testa l'endpoint per ottenere le lingue supportate.""" | |
| print("π Test Lingue Supportate...") | |
| try: | |
| response = requests.get(f"{BASE_URL}/languages") | |
| data = response.json() | |
| languages = data["supported_languages"] | |
| print(f"Status: {response.status_code}") | |
| print(f"Lingue supportate ({len(languages)}): {languages[:10]}...") | |
| print("β Lingue supportate OK\n") | |
| return True | |
| except Exception as e: | |
| print(f"β Test lingue failed: {e}\n") | |
| return False | |
| def test_translate_single(): | |
| """Testa la traduzione di un singolo testo.""" | |
| print("π Test Traduzione Singola...") | |
| try: | |
| params = { | |
| "text": "Ciao mondo! Come stai oggi?", | |
| "target_language": "en" | |
| } | |
| response = requests.post(f"{BASE_URL}/translate-single", params=params) | |
| data = response.json() | |
| print(f"Status: {response.status_code}") | |
| print(f"Testo originale: {data['original_text']}") | |
| print(f"Testo tradotto: {data['translated_text']}") | |
| print("β Traduzione singola OK\n") | |
| return True | |
| except Exception as e: | |
| print(f"β Test traduzione singola failed: {e}\n") | |
| return False | |
| def test_translate_locales(): | |
| """Testa la traduzione di oggetti locale complessi.""" | |
| print("π Test Traduzione Locales...") | |
| try: | |
| locale_data = { | |
| "locales": { | |
| "welcome_message": "Benvenuto nella nostra applicazione!", | |
| "button_save": "Salva documento", | |
| "error_required": "Questo campo Γ¨ obbligatorio", | |
| "html_content": "<p>Clicca <a href='#'>qui</a> per continuare</p>", | |
| "template_message": "Ciao {name}, hai {count} nuovi messaggi", | |
| "complex_html": "<div class='alert'><strong>Attenzione:</strong> Operazione completata con successo!</div>" | |
| } | |
| } | |
| params = {"target_language": "en"} | |
| response = requests.post( | |
| f"{BASE_URL}/translate", | |
| json=locale_data, | |
| params=params | |
| ) | |
| data = response.json() | |
| print(f"Status: {response.status_code}") | |
| print("Traduzioni:") | |
| for key, value in data["translated_locales"].items(): | |
| original = locale_data["locales"][key] | |
| print(f" {key}:") | |
| print(f" IT: {original}") | |
| print(f" EN: {value}") | |
| print("β Traduzione locales OK\n") | |
| return True | |
| except Exception as e: | |
| print(f"β Test traduzione locales failed: {e}\n") | |
| return False | |
| def test_multiple_languages(): | |
| """Testa la traduzione verso lingue diverse.""" | |
| print("π£οΈ Test Lingue Multiple...") | |
| try: | |
| locale_data = { | |
| "locales": { | |
| "greeting": "Buongiorno!", | |
| "goodbye": "Arrivederci!" | |
| } | |
| } | |
| languages_to_test = ["en", "fr", "de", "es"] | |
| for lang in languages_to_test: | |
| params = {"target_language": lang} | |
| response = requests.post( | |
| f"{BASE_URL}/translate", | |
| json=locale_data, | |
| params=params | |
| ) | |
| data = response.json() | |
| print(f"Traduzione in {lang}:") | |
| for key, value in data["translated_locales"].items(): | |
| print(f" {key}: {value}") | |
| print("β Test lingue multiple OK\n") | |
| return True | |
| except Exception as e: | |
| print(f"β Test lingue multiple failed: {e}\n") | |
| return False | |
| def test_error_handling(): | |
| """Testa la gestione degli errori.""" | |
| print("β οΈ Test Gestione Errori...") | |
| try: | |
| # Test lingua non supportata | |
| locale_data = {"locales": {"test": "Ciao"}} | |
| params = {"target_language": "xx"} # Lingua inesistente | |
| response = requests.post( | |
| f"{BASE_URL}/translate", | |
| json=locale_data, | |
| params=params | |
| ) | |
| print(f"Status per lingua inesistente: {response.status_code}") | |
| if response.status_code == 400: | |
| print("β Gestione errore lingua inesistente OK") | |
| # Test body malformato | |
| response = requests.post( | |
| f"{BASE_URL}/translate", | |
| json={"wrong_field": "test"}, | |
| params={"target_language": "en"} | |
| ) | |
| print(f"Status per body malformato: {response.status_code}") | |
| print("β Test gestione errori OK\n") | |
| return True | |
| except Exception as e: | |
| print(f"β Test gestione errori failed: {e}\n") | |
| return False | |
| def main(): | |
| """Esegue tutti i test.""" | |
| print("π Avvio test del Local Translation Server\n") | |
| print("=" * 50) | |
| tests = [ | |
| test_health_check, | |
| test_get_languages, | |
| test_translate_single, | |
| test_translate_locales, | |
| test_multiple_languages, | |
| test_error_handling | |
| ] | |
| passed = 0 | |
| total = len(tests) | |
| for test in tests: | |
| try: | |
| if test(): | |
| passed += 1 | |
| time.sleep(1) # Pausa tra i test | |
| except Exception as e: | |
| print(f"β Test failed with exception: {e}\n") | |
| print("=" * 50) | |
| print(f"π Risultati: {passed}/{total} test passati") | |
| if passed == total: | |
| print("π Tutti i test sono passati!") | |
| else: | |
| print("β οΈ Alcuni test sono falliti. Controlla i log sopra.") | |
| if __name__ == "__main__": | |
| main() | |