Spaces:
Running
Running
| """ | |
| Tests unitaires pour l'API MMS ASR/TTS | |
| """ | |
| import pytest | |
| import json | |
| import io | |
| from flask import Flask | |
| import sys | |
| from pathlib import Path | |
| # Importe l'app | |
| sys.path.insert(0, str(Path(__file__).parent)) | |
| # Note: Pour les tests complets, il faudrait utiliser une version mockée | |
| # des modèles ou des fixtures avec des modèles minimalistes | |
| def client(): | |
| """Crée un client Flask pour les tests""" | |
| from app_v2 import app | |
| app.config['TESTING'] = True | |
| with app.test_client() as client: | |
| yield client | |
| class TestAPI: | |
| """Tests des endpoints""" | |
| def test_health(self, client): | |
| """Test le endpoint /health""" | |
| response = client.get('/health') | |
| assert response.status_code == 200 | |
| data = response.get_json() | |
| assert 'status' in data | |
| assert data['status'] == 'healthy' | |
| def test_index(self, client): | |
| """Test le endpoint racine""" | |
| response = client.get('/') | |
| assert response.status_code == 200 | |
| data = response.get_json() | |
| assert 'name' in data | |
| assert 'endpoints' in data | |
| def test_supported_languages(self, client): | |
| """Test le endpoint /supported-languages""" | |
| response = client.get('/supported-languages') | |
| assert response.status_code == 200 | |
| data = response.get_json() | |
| assert 'asr' in data | |
| assert 'tts' in data | |
| assert 'eng' in data['tts']['languages'] | |
| def test_models_info(self, client): | |
| """Test le endpoint /models-info""" | |
| response = client.get('/models-info') | |
| assert response.status_code == 200 | |
| data = response.get_json() | |
| assert 'asr' in data | |
| assert 'tts' in data | |
| def test_tts_missing_text(self, client): | |
| """Test TTS sans texte""" | |
| response = client.post('/tts', | |
| data=json.dumps({}), | |
| content_type='application/json' | |
| ) | |
| assert response.status_code == 400 | |
| data = response.get_json() | |
| assert 'error' in data | |
| def test_tts_empty_text(self, client): | |
| """Test TTS avec texte vide""" | |
| response = client.post('/tts', | |
| data=json.dumps({'text': ' '}), | |
| content_type='application/json' | |
| ) | |
| assert response.status_code == 400 | |
| def test_asr_missing_file(self, client): | |
| """Test ASR sans fichier""" | |
| response = client.post('/asr') | |
| assert response.status_code == 400 | |
| data = response.get_json() | |
| assert 'error' in data | |
| def test_404(self, client): | |
| """Test endpoint inexistant""" | |
| response = client.get('/nonexistent') | |
| assert response.status_code == 404 | |
| class TestLanguageMapping: | |
| """Tests du mapping des langues""" | |
| def test_supported_languages(self): | |
| """Vérifie que les langues documentées sont configurées""" | |
| from app_v2 import LANGUAGE_MAPPING | |
| expected_languages = ['beh', 'bba', 'ddn', 'ewe', 'gej', 'tbz', 'yor', 'eng'] | |
| for lang in expected_languages: | |
| assert lang in LANGUAGE_MAPPING, f"Langue {lang} manquante" | |
| def test_language_mapping_format(self): | |
| """Vérifie le format du mapping des langues""" | |
| from app_v2 import LANGUAGE_MAPPING | |
| for lang, model_id in LANGUAGE_MAPPING.items(): | |
| assert isinstance(lang, str) | |
| assert isinstance(model_id, str) | |
| assert model_id.startswith('facebook/mms-tts-') | |
| class TestConfig: | |
| """Tests de configuration""" | |
| def test_sample_rate(self): | |
| """Test que SAMPLE_RATE est correct""" | |
| from app_v2 import SAMPLE_RATE | |
| assert SAMPLE_RATE == 16000 | |
| def test_max_audio_length(self): | |
| """Test que MAX_AUDIO_LENGTH est raisonnable""" | |
| from app_v2 import MAX_AUDIO_LENGTH | |
| assert 10 <= MAX_AUDIO_LENGTH <= 120 | |
| def test_max_text_length(self): | |
| """Test que MAX_TEXT_LENGTH est raisonnable""" | |
| from app_v2 import MAX_TEXT_LENGTH | |
| assert 100 <= MAX_TEXT_LENGTH <= 5000 | |
| if __name__ == '__main__': | |
| pytest.main([__file__, '-v']) | |