Spaces:
Build error
Build error
File size: 14,329 Bytes
acd758a |
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
"""Unit tests for ProcessingRequestDto"""
import pytest
from src.application.dtos.processing_request_dto import ProcessingRequestDto
from src.application.dtos.audio_upload_dto import AudioUploadDto
class TestProcessingRequestDto:
"""Test cases for ProcessingRequestDto"""
@pytest.fixture
def sample_audio_upload(self):
"""Create sample audio upload DTO"""
return AudioUploadDto(
filename="test_audio.wav",
content=b"fake_audio_content_" + b"x" * 1000,
content_type="audio/wav"
)
def test_valid_processing_request_dto(self, sample_audio_upload):
"""Test creating a valid ProcessingRequestDto"""
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro",
speed=1.0,
source_language="en"
)
assert dto.audio == sample_audio_upload
assert dto.asr_model == "whisper-small"
assert dto.target_language == "es"
assert dto.voice == "kokoro"
assert dto.speed == 1.0
assert dto.source_language == "en"
assert dto.additional_params == {}
def test_processing_request_dto_with_defaults(self, sample_audio_upload):
"""Test creating ProcessingRequestDto with default values"""
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-medium",
target_language="fr",
voice="dia"
)
assert dto.speed == 1.0 # Default speed
assert dto.source_language is None # Default source language
assert dto.additional_params == {} # Default additional params
def test_processing_request_dto_with_additional_params(self, sample_audio_upload):
"""Test creating ProcessingRequestDto with additional parameters"""
additional_params = {
"custom_param": "value",
"another_param": 123
}
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-large",
target_language="de",
voice="cosyvoice2",
additional_params=additional_params
)
assert dto.additional_params == additional_params
def test_invalid_audio_type_validation(self):
"""Test validation with invalid audio type"""
with pytest.raises(ValueError, match="Audio must be an AudioUploadDto instance"):
ProcessingRequestDto(
audio="invalid_audio", # Not AudioUploadDto
asr_model="whisper-small",
target_language="es",
voice="kokoro"
)
def test_empty_asr_model_validation(self, sample_audio_upload):
"""Test validation with empty ASR model"""
with pytest.raises(ValueError, match="ASR model cannot be empty"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="",
target_language="es",
voice="kokoro"
)
def test_unsupported_asr_model_validation(self, sample_audio_upload):
"""Test validation with unsupported ASR model"""
with pytest.raises(ValueError, match="Unsupported ASR model"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="invalid-model",
target_language="es",
voice="kokoro"
)
def test_supported_asr_models(self, sample_audio_upload):
"""Test all supported ASR models"""
supported_models = ['whisper-small', 'whisper-medium', 'whisper-large', 'parakeet']
for model in supported_models:
# Should not raise exception
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model=model,
target_language="es",
voice="kokoro"
)
assert dto.asr_model == model
def test_empty_target_language_validation(self, sample_audio_upload):
"""Test validation with empty target language"""
with pytest.raises(ValueError, match="Target language cannot be empty"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="",
voice="kokoro"
)
def test_unsupported_target_language_validation(self, sample_audio_upload):
"""Test validation with unsupported target language"""
with pytest.raises(ValueError, match="Unsupported target language"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="invalid-lang",
voice="kokoro"
)
def test_unsupported_source_language_validation(self, sample_audio_upload):
"""Test validation with unsupported source language"""
with pytest.raises(ValueError, match="Unsupported source language"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro",
source_language="invalid-lang"
)
def test_supported_languages(self, sample_audio_upload):
"""Test all supported languages"""
supported_languages = [
'en', 'es', 'fr', 'de', 'it', 'pt', 'ru', 'ja', 'ko', 'zh',
'ar', 'hi', 'tr', 'pl', 'nl', 'sv', 'da', 'no', 'fi'
]
for lang in supported_languages:
# Should not raise exception
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language=lang,
voice="kokoro",
source_language=lang
)
assert dto.target_language == lang
assert dto.source_language == lang
def test_empty_voice_validation(self, sample_audio_upload):
"""Test validation with empty voice"""
with pytest.raises(ValueError, match="Voice cannot be empty"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice=""
)
def test_unsupported_voice_validation(self, sample_audio_upload):
"""Test validation with unsupported voice"""
with pytest.raises(ValueError, match="Unsupported voice"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="invalid-voice"
)
def test_supported_voices(self, sample_audio_upload):
"""Test all supported voices"""
supported_voices = ['kokoro', 'dia', 'cosyvoice2', 'dummy']
for voice in supported_voices:
# Should not raise exception
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice=voice
)
assert dto.voice == voice
def test_speed_range_validation_too_low(self, sample_audio_upload):
"""Test validation with speed too low"""
with pytest.raises(ValueError, match="Speed must be between 0.5 and 2.0"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro",
speed=0.3 # Too low
)
def test_speed_range_validation_too_high(self, sample_audio_upload):
"""Test validation with speed too high"""
with pytest.raises(ValueError, match="Speed must be between 0.5 and 2.0"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro",
speed=2.5 # Too high
)
def test_valid_speed_range(self, sample_audio_upload):
"""Test valid speed range"""
valid_speeds = [0.5, 1.0, 1.5, 2.0]
for speed in valid_speeds:
# Should not raise exception
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro",
speed=speed
)
assert dto.speed == speed
def test_invalid_additional_params_type(self, sample_audio_upload):
"""Test validation with invalid additional params type"""
with pytest.raises(ValueError, match="Additional params must be a dictionary"):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro",
additional_params="invalid" # Not a dict
)
def test_requires_translation_property_same_language(self, sample_audio_upload):
"""Test requires_translation property when source and target are same"""
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="en",
voice="kokoro",
source_language="en"
)
assert dto.requires_translation is False
def test_requires_translation_property_different_languages(self, sample_audio_upload):
"""Test requires_translation property when source and target are different"""
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro",
source_language="en"
)
assert dto.requires_translation is True
def test_requires_translation_property_no_source(self, sample_audio_upload):
"""Test requires_translation property when no source language specified"""
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro"
)
assert dto.requires_translation is True # Assume translation needed
def test_to_dict_method(self, sample_audio_upload):
"""Test to_dict method"""
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro",
speed=1.5,
source_language="en",
additional_params={"custom": "value"}
)
result = dto.to_dict()
assert result['audio'] == sample_audio_upload.to_dict()
assert result['asr_model'] == "whisper-small"
assert result['target_language'] == "es"
assert result['source_language'] == "en"
assert result['voice'] == "kokoro"
assert result['speed'] == 1.5
assert result['requires_translation'] is True
assert result['additional_params'] == {"custom": "value"}
def test_from_dict_method(self, sample_audio_upload):
"""Test from_dict class method"""
data = {
'audio': sample_audio_upload,
'asr_model': 'whisper-medium',
'target_language': 'fr',
'voice': 'dia',
'speed': 1.2,
'source_language': 'en',
'additional_params': {'test': 'value'}
}
dto = ProcessingRequestDto.from_dict(data)
assert dto.audio == sample_audio_upload
assert dto.asr_model == 'whisper-medium'
assert dto.target_language == 'fr'
assert dto.voice == 'dia'
assert dto.speed == 1.2
assert dto.source_language == 'en'
assert dto.additional_params == {'test': 'value'}
def test_from_dict_method_with_audio_dict(self):
"""Test from_dict method with audio as dictionary"""
audio_data = {
'filename': 'test.wav',
'content': b'fake_content' + b'x' * 1000,
'content_type': 'audio/wav'
}
data = {
'audio': audio_data,
'asr_model': 'whisper-small',
'target_language': 'es',
'voice': 'kokoro'
}
dto = ProcessingRequestDto.from_dict(data)
assert isinstance(dto.audio, AudioUploadDto)
assert dto.audio.filename == 'test.wav'
assert dto.audio.content_type == 'audio/wav'
def test_from_dict_method_with_defaults(self, sample_audio_upload):
"""Test from_dict method with default values"""
data = {
'audio': sample_audio_upload,
'asr_model': 'whisper-small',
'target_language': 'es',
'voice': 'kokoro'
}
dto = ProcessingRequestDto.from_dict(data)
assert dto.speed == 1.0 # Default
assert dto.source_language is None # Default
assert dto.additional_params is None # Default
def test_validation_called_on_init(self, sample_audio_upload):
"""Test that validation is called during initialization"""
# This should trigger validation and raise an error
with pytest.raises(ValueError):
ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="", # Invalid empty model
target_language="es",
voice="kokoro"
)
def test_additional_params_default_initialization(self, sample_audio_upload):
"""Test that additional_params is initialized to empty dict if None"""
dto = ProcessingRequestDto(
audio=sample_audio_upload,
asr_model="whisper-small",
target_language="es",
voice="kokoro",
additional_params=None
)
assert dto.additional_params == {} |