Spaces:
Build error
Build error
File size: 10,166 Bytes
6aea21a |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
"""Tests for AudioProcessingService."""
import pytest
from unittest.mock import Mock, MagicMock
from src.domain.services.audio_processing_service import AudioProcessingService
from src.domain.models.audio_content import AudioContent
from src.domain.models.text_content import TextContent
from src.domain.models.voice_settings import VoiceSettings
from src.domain.models.translation_request import TranslationRequest
from src.domain.models.speech_synthesis_request import SpeechSynthesisRequest
from src.domain.exceptions import (
AudioProcessingException,
SpeechRecognitionException,
TranslationFailedException,
SpeechSynthesisException
)
class TestAudioProcessingService:
"""Test cases for AudioProcessingService."""
@pytest.fixture
def mock_stt_service(self):
"""Mock speech recognition service."""
return Mock()
@pytest.fixture
def mock_translation_service(self):
"""Mock translation service."""
return Mock()
@pytest.fixture
def mock_tts_service(self):
"""Mock speech synthesis service."""
return Mock()
@pytest.fixture
def audio_processing_service(self, mock_stt_service, mock_translation_service, mock_tts_service):
"""AudioProcessingService instance with mocked dependencies."""
return AudioProcessingService(
speech_recognition_service=mock_stt_service,
translation_service=mock_translation_service,
speech_synthesis_service=mock_tts_service
)
@pytest.fixture
def sample_audio(self):
"""Sample audio content for testing."""
return AudioContent(
data=b"fake_audio_data",
format="wav",
sample_rate=22050,
duration=10.0,
filename="test.wav"
)
@pytest.fixture
def sample_voice_settings(self):
"""Sample voice settings for testing."""
return VoiceSettings(
voice_id="test_voice",
speed=1.0,
language="es"
)
@pytest.fixture
def sample_text_content(self):
"""Sample text content for testing."""
return TextContent(
text="Hello world",
language="en"
)
def test_successful_pipeline_processing(
self,
audio_processing_service,
mock_stt_service,
mock_translation_service,
mock_tts_service,
sample_audio,
sample_voice_settings,
sample_text_content
):
"""Test successful processing through the complete pipeline."""
# Arrange
original_text = TextContent(text="Hello world", language="en")
translated_text = TextContent(text="Hola mundo", language="es")
output_audio = AudioContent(
data=b"synthesized_audio",
format="wav",
sample_rate=22050,
duration=5.0
)
mock_stt_service.transcribe.return_value = original_text
mock_translation_service.translate.return_value = translated_text
mock_tts_service.synthesize.return_value = output_audio
# Act
result = audio_processing_service.process_audio_pipeline(
audio=sample_audio,
target_language="es",
voice_settings=sample_voice_settings
)
# Assert
assert result.success is True
assert result.original_text == original_text
assert result.translated_text == translated_text
assert result.audio_output == output_audio
assert result.error_message is None
assert result.processing_time >= 0
# Verify service calls
mock_stt_service.transcribe.assert_called_once_with(sample_audio, "whisper-base")
mock_translation_service.translate.assert_called_once()
mock_tts_service.synthesize.assert_called_once()
def test_no_translation_needed_same_language(
self,
audio_processing_service,
mock_stt_service,
mock_translation_service,
mock_tts_service,
sample_audio
):
"""Test pipeline when no translation is needed (same language)."""
# Arrange
original_text = TextContent(text="Hola mundo", language="es")
voice_settings = VoiceSettings(voice_id="test_voice", speed=1.0, language="es")
output_audio = AudioContent(
data=b"synthesized_audio",
format="wav",
sample_rate=22050,
duration=5.0
)
mock_stt_service.transcribe.return_value = original_text
mock_tts_service.synthesize.return_value = output_audio
# Act
result = audio_processing_service.process_audio_pipeline(
audio=sample_audio,
target_language="es",
voice_settings=voice_settings
)
# Assert
assert result.success is True
assert result.original_text == original_text
assert result.translated_text == original_text # Same as original
assert result.audio_output == output_audio
# Translation service should not be called
mock_translation_service.translate.assert_not_called()
def test_validation_error_none_audio(self, audio_processing_service, sample_voice_settings):
"""Test validation error when audio is None."""
# Act
result = audio_processing_service.process_audio_pipeline(
audio=None,
target_language="es",
voice_settings=sample_voice_settings
)
# Assert
assert result.success is False
assert "Audio content cannot be None" in result.error_message
def test_validation_error_empty_target_language(self, audio_processing_service, sample_audio, sample_voice_settings):
"""Test validation error when target language is empty."""
# Act
result = audio_processing_service.process_audio_pipeline(
audio=sample_audio,
target_language="",
voice_settings=sample_voice_settings
)
# Assert
assert result.success is False
assert "Target language cannot be empty" in result.error_message
def test_validation_error_language_mismatch(self, audio_processing_service, sample_audio):
"""Test validation error when voice settings language doesn't match target language."""
# Arrange
voice_settings = VoiceSettings(voice_id="test_voice", speed=1.0, language="en")
# Act
result = audio_processing_service.process_audio_pipeline(
audio=sample_audio,
target_language="es",
voice_settings=voice_settings
)
# Assert
assert result.success is False
assert "Voice settings language (en) must match target language (es)" in result.error_message
def test_validation_error_audio_too_long(self, audio_processing_service, sample_voice_settings):
"""Test validation error when audio is too long."""
# Arrange
long_audio = AudioContent(
data=b"fake_audio_data",
format="wav",
sample_rate=22050,
duration=400.0 # Exceeds 300s limit
)
# Act
result = audio_processing_service.process_audio_pipeline(
audio=long_audio,
target_language="es",
voice_settings=sample_voice_settings
)
# Assert
assert result.success is False
assert "exceeds maximum allowed duration" in result.error_message
def test_stt_failure_handling(
self,
audio_processing_service,
mock_stt_service,
sample_audio,
sample_voice_settings
):
"""Test handling of STT service failure."""
# Arrange
mock_stt_service.transcribe.side_effect = Exception("STT service unavailable")
# Act
result = audio_processing_service.process_audio_pipeline(
audio=sample_audio,
target_language="es",
voice_settings=sample_voice_settings
)
# Assert
assert result.success is False
assert "Speech recognition failed" in result.error_message
assert result.processing_time >= 0
def test_translation_failure_handling(
self,
audio_processing_service,
mock_stt_service,
mock_translation_service,
sample_audio,
sample_voice_settings
):
"""Test handling of translation service failure."""
# Arrange
original_text = TextContent(text="Hello world", language="en")
mock_stt_service.transcribe.return_value = original_text
mock_translation_service.translate.side_effect = Exception("Translation service unavailable")
# Act
result = audio_processing_service.process_audio_pipeline(
audio=sample_audio,
target_language="es",
voice_settings=sample_voice_settings
)
# Assert
assert result.success is False
assert "Translation failed" in result.error_message
assert result.processing_time >= 0
def test_tts_failure_handling(
self,
audio_processing_service,
mock_stt_service,
mock_translation_service,
mock_tts_service,
sample_audio,
sample_voice_settings
):
"""Test handling of TTS service failure."""
# Arrange
original_text = TextContent(text="Hello world", language="en")
translated_text = TextContent(text="Hola mundo", language="es")
mock_stt_service.transcribe.return_value = original_text
mock_translation_service.translate.return_value = translated_text
mock_tts_service.synthesize.side_effect = Exception("TTS service unavailable")
# Act
result = audio_processing_service.process_audio_pipeline(
audio=sample_audio,
target_language="es",
voice_settings=sample_voice_settings
)
# Assert
assert result.success is False
assert "Speech synthesis failed" in result.error_message
assert result.processing_time >= 0 |