Spaces:
Runtime error
Runtime error
| """ | |
| Tests unitaires pour le projet Amazon Sentiment Analysis | |
| Teste les fonctions de traitement et de génération | |
| """ | |
| import sys | |
| import os | |
| # Import des modules à tester | |
| from data_processing import clean_text, label_to_sentiment, make_fake_email | |
| def test_clean_text(): | |
| """Test de la fonction de nettoyage de texte""" | |
| print("\n=== TEST : clean_text ===") | |
| # Test 1 : Texte avec ponctuation | |
| text1 = "Super produit !!! Je recommande." | |
| result1 = clean_text(text1) | |
| assert "super" in result1 | |
| assert "produit" in result1 | |
| assert "!" not in result1 | |
| print(f"✅ Test 1 réussi : '{text1}' → '{result1}'") | |
| # Test 2 : Texte avec chiffres | |
| text2 = "Livraison en 24h, excellent !" | |
| result2 = clean_text(text2) | |
| assert "24" not in result2 | |
| print(f"✅ Test 2 réussi : '{text2}' → '{result2}'") | |
| # Test 3 : Stopwords supprimés | |
| text3 = "Je suis très content de cet achat" | |
| result3 = clean_text(text3) | |
| assert "je" not in result3 # stopword supprimé | |
| assert "content" in result3 | |
| print(f"✅ Test 3 réussi : '{text3}' → '{result3}'") | |
| print("✅ Tous les tests clean_text réussis !") | |
| def test_label_to_sentiment(): | |
| """Test de la conversion label → sentiment""" | |
| print("\n=== TEST : label_to_sentiment ===") | |
| # Test labels négatifs (1-2 étoiles) | |
| assert label_to_sentiment(1) == "negatif" | |
| assert label_to_sentiment(2) == "negatif" | |
| print("✅ Labels 1-2 → négatif") | |
| # Test labels positifs (3-5 étoiles) | |
| assert label_to_sentiment(3) == "positif" | |
| assert label_to_sentiment(4) == "positif" | |
| assert label_to_sentiment(5) == "positif" | |
| print("✅ Labels 3-5 → positif") | |
| # Test valeur invalide | |
| assert label_to_sentiment("invalid") == "negatif" # Par défaut | |
| print("✅ Valeur invalide gérée") | |
| print("✅ Tous les tests label_to_sentiment réussis !") | |
| def test_make_fake_email(): | |
| """Test de la génération d'emails factices""" | |
| print("\n=== TEST : make_fake_email ===") | |
| # Test format | |
| email1 = make_fake_email(1) | |
| assert email1 == "client00001@example.com" | |
| print(f"✅ Email 1 : {email1}") | |
| email42 = make_fake_email(42) | |
| assert email42 == "client00042@example.com" | |
| assert "@example.com" in email42 | |
| print(f"✅ Email 42 : {email42}") | |
| print("✅ Tous les tests make_fake_email réussis !") | |
| def test_generer_response_mock(): | |
| """ | |
| Test simulé de la génération de réponse (sans charger le modèle) | |
| Vérifie que le prompt est bien construit | |
| """ | |
| print("\n=== TEST : build_reply_prompt ===") | |
| from generate_response import build_reply_prompt | |
| review = "Produit cassé" | |
| prompt = build_reply_prompt(review) | |
| # Vérifications | |
| assert "service client" in prompt.lower() | |
| assert "Produit cassé" in prompt | |
| assert "poli et professionnel" in prompt | |
| print("✅ Prompt correctement construit") | |
| print(f"Extrait : {prompt[:100]}...") | |
| def run_all_tests(): | |
| """Lance tous les tests""" | |
| print("=" * 80) | |
| print("🧪 LANCEMENT DES TESTS UNITAIRES") | |
| print("=" * 80) | |
| try: | |
| test_clean_text() | |
| test_label_to_sentiment() | |
| test_make_fake_email() | |
| test_generer_response_mock() | |
| print("\n" + "=" * 80) | |
| print("🎉 TOUS LES TESTS ONT RÉUSSI !") | |
| print("=" * 80) | |
| return True | |
| except AssertionError as e: | |
| print("\n" + "=" * 80) | |
| print(f"❌ ÉCHEC DES TESTS : {e}") | |
| print("=" * 80) | |
| return False | |
| except Exception as e: | |
| print("\n" + "=" * 80) | |
| print(f"❌ ERREUR LORS DES TESTS : {e}") | |
| print("=" * 80) | |
| return False | |
| if __name__ == "__main__": | |
| success = run_all_tests() | |
| sys.exit(0 if success else 1) | |