Spaces:
Build error
Build error
File size: 23,536 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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
"""Unit tests for ConfigurationApplicationService"""
import pytest
import os
import json
from unittest.mock import Mock, MagicMock, patch, mock_open
from src.application.services.configuration_service import (
ConfigurationApplicationService,
ConfigurationException
)
from src.infrastructure.config.app_config import AppConfig
from src.infrastructure.config.dependency_container import DependencyContainer
class TestConfigurationApplicationService:
"""Test cases for ConfigurationApplicationService"""
@pytest.fixture
def mock_container(self):
"""Create mock dependency container"""
container = Mock(spec=DependencyContainer)
return container
@pytest.fixture
def mock_config(self):
"""Create mock application config"""
config = Mock(spec=AppConfig)
# Mock configuration methods
config.get_tts_config.return_value = {
'preferred_providers': ['kokoro', 'dia'],
'default_speed': 1.0,
'default_language': 'en',
'enable_streaming': False,
'max_text_length': 5000
}
config.get_stt_config.return_value = {
'preferred_providers': ['whisper', 'parakeet'],
'default_model': 'whisper',
'chunk_length_s': 30,
'batch_size': 16,
'enable_vad': True
}
config.get_translation_config.return_value = {
'default_provider': 'nllb',
'model_name': 'nllb-200-3.3B',
'max_chunk_length': 512,
'batch_size': 8,
'cache_translations': True
}
config.get_processing_config.return_value = {
'temp_dir': '/tmp',
'cleanup_temp_files': True,
'max_file_size_mb': 100,
'supported_audio_formats': ['wav', 'mp3', 'flac'],
'processing_timeout_seconds': 300
}
config.get_logging_config.return_value = {
'level': 'INFO',
'enable_file_logging': False,
'log_file_path': '/tmp/app.log',
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
}
# Mock config objects for attribute access
config.tts = Mock()
config.stt = Mock()
config.translation = Mock()
config.processing = Mock()
config.logging = Mock()
return config
@pytest.fixture
def service(self, mock_container, mock_config):
"""Create ConfigurationApplicationService instance"""
mock_container.resolve.return_value = mock_config
return ConfigurationApplicationService(mock_container, mock_config)
def test_initialization(self, mock_container, mock_config):
"""Test service initialization"""
service = ConfigurationApplicationService(mock_container, mock_config)
assert service._container == mock_container
assert service._config == mock_config
assert service._error_mapper is not None
def test_initialization_without_config(self, mock_container, mock_config):
"""Test service initialization without explicit config"""
mock_container.resolve.return_value = mock_config
service = ConfigurationApplicationService(mock_container)
assert service._container == mock_container
assert service._config == mock_config
mock_container.resolve.assert_called_once_with(AppConfig)
def test_get_current_configuration_success(self, service, mock_config):
"""Test successful current configuration retrieval"""
result = service.get_current_configuration()
assert 'tts' in result
assert 'stt' in result
assert 'translation' in result
assert 'processing' in result
assert 'logging' in result
# Verify all config methods were called
mock_config.get_tts_config.assert_called_once()
mock_config.get_stt_config.assert_called_once()
mock_config.get_translation_config.assert_called_once()
mock_config.get_processing_config.assert_called_once()
mock_config.get_logging_config.assert_called_once()
def test_get_current_configuration_failure(self, service, mock_config):
"""Test current configuration retrieval failure"""
mock_config.get_tts_config.side_effect = Exception("Config error")
with pytest.raises(ConfigurationException, match="Failed to retrieve configuration"):
service.get_current_configuration()
def test_get_tts_configuration_success(self, service, mock_config):
"""Test successful TTS configuration retrieval"""
result = service.get_tts_configuration()
assert result['preferred_providers'] == ['kokoro', 'dia']
assert result['default_speed'] == 1.0
mock_config.get_tts_config.assert_called_once()
def test_get_tts_configuration_failure(self, service, mock_config):
"""Test TTS configuration retrieval failure"""
mock_config.get_tts_config.side_effect = Exception("TTS config error")
with pytest.raises(ConfigurationException, match="Failed to retrieve TTS configuration"):
service.get_tts_configuration()
def test_get_stt_configuration_success(self, service, mock_config):
"""Test successful STT configuration retrieval"""
result = service.get_stt_configuration()
assert result['preferred_providers'] == ['whisper', 'parakeet']
assert result['default_model'] == 'whisper'
mock_config.get_stt_config.assert_called_once()
def test_get_stt_configuration_failure(self, service, mock_config):
"""Test STT configuration retrieval failure"""
mock_config.get_stt_config.side_effect = Exception("STT config error")
with pytest.raises(ConfigurationException, match="Failed to retrieve STT configuration"):
service.get_stt_configuration()
def test_get_translation_configuration_success(self, service, mock_config):
"""Test successful translation configuration retrieval"""
result = service.get_translation_configuration()
assert result['default_provider'] == 'nllb'
assert result['model_name'] == 'nllb-200-3.3B'
mock_config.get_translation_config.assert_called_once()
def test_get_translation_configuration_failure(self, service, mock_config):
"""Test translation configuration retrieval failure"""
mock_config.get_translation_config.side_effect = Exception("Translation config error")
with pytest.raises(ConfigurationException, match="Failed to retrieve translation configuration"):
service.get_translation_configuration()
def test_get_processing_configuration_success(self, service, mock_config):
"""Test successful processing configuration retrieval"""
result = service.get_processing_configuration()
assert result['temp_dir'] == '/tmp'
assert result['max_file_size_mb'] == 100
mock_config.get_processing_config.assert_called_once()
def test_get_processing_configuration_failure(self, service, mock_config):
"""Test processing configuration retrieval failure"""
mock_config.get_processing_config.side_effect = Exception("Processing config error")
with pytest.raises(ConfigurationException, match="Failed to retrieve processing configuration"):
service.get_processing_configuration()
def test_get_logging_configuration_success(self, service, mock_config):
"""Test successful logging configuration retrieval"""
result = service.get_logging_configuration()
assert result['level'] == 'INFO'
assert result['enable_file_logging'] is False
mock_config.get_logging_config.assert_called_once()
def test_get_logging_configuration_failure(self, service, mock_config):
"""Test logging configuration retrieval failure"""
mock_config.get_logging_config.side_effect = Exception("Logging config error")
with pytest.raises(ConfigurationException, match="Failed to retrieve logging configuration"):
service.get_logging_configuration()
def test_update_tts_configuration_success(self, service, mock_config):
"""Test successful TTS configuration update"""
updates = {
'default_speed': 1.5,
'enable_streaming': True
}
result = service.update_tts_configuration(updates)
# Verify setattr was called for valid attributes
assert hasattr(mock_config.tts, 'default_speed')
assert hasattr(mock_config.tts, 'enable_streaming')
# Verify updated config was returned
mock_config.get_tts_config.assert_called()
def test_update_tts_configuration_validation_error(self, service):
"""Test TTS configuration update with validation error"""
updates = {
'default_speed': 5.0 # Invalid speed > 3.0
}
with pytest.raises(ConfigurationException, match="default_speed must be between 0.1 and 3.0"):
service.update_tts_configuration(updates)
def test_update_stt_configuration_success(self, service, mock_config):
"""Test successful STT configuration update"""
updates = {
'chunk_length_s': 60,
'enable_vad': False
}
result = service.update_stt_configuration(updates)
# Verify setattr was called for valid attributes
assert hasattr(mock_config.stt, 'chunk_length_s')
assert hasattr(mock_config.stt, 'enable_vad')
# Verify updated config was returned
mock_config.get_stt_config.assert_called()
def test_update_stt_configuration_validation_error(self, service):
"""Test STT configuration update with validation error"""
updates = {
'chunk_length_s': -10 # Invalid negative value
}
with pytest.raises(ConfigurationException, match="chunk_length_s must be a positive integer"):
service.update_stt_configuration(updates)
def test_update_translation_configuration_success(self, service, mock_config):
"""Test successful translation configuration update"""
updates = {
'max_chunk_length': 1024,
'cache_translations': False
}
result = service.update_translation_configuration(updates)
# Verify setattr was called for valid attributes
assert hasattr(mock_config.translation, 'max_chunk_length')
assert hasattr(mock_config.translation, 'cache_translations')
# Verify updated config was returned
mock_config.get_translation_config.assert_called()
def test_update_translation_configuration_validation_error(self, service):
"""Test translation configuration update with validation error"""
updates = {
'max_chunk_length': 0 # Invalid zero value
}
with pytest.raises(ConfigurationException, match="max_chunk_length must be a positive integer"):
service.update_translation_configuration(updates)
def test_update_processing_configuration_success(self, service, mock_config):
"""Test successful processing configuration update"""
updates = {
'max_file_size_mb': 200,
'cleanup_temp_files': False
}
with patch('pathlib.Path.mkdir'):
result = service.update_processing_configuration(updates)
# Verify setattr was called for valid attributes
assert hasattr(mock_config.processing, 'max_file_size_mb')
assert hasattr(mock_config.processing, 'cleanup_temp_files')
# Verify updated config was returned
mock_config.get_processing_config.assert_called()
def test_update_processing_configuration_validation_error(self, service):
"""Test processing configuration update with validation error"""
updates = {
'max_file_size_mb': -50 # Invalid negative value
}
with pytest.raises(ConfigurationException, match="max_file_size_mb must be a positive integer"):
service.update_processing_configuration(updates)
def test_validate_tts_updates_valid(self, service):
"""Test TTS updates validation with valid data"""
updates = {
'preferred_providers': ['kokoro', 'dia'],
'default_speed': 1.5,
'default_language': 'es',
'enable_streaming': True,
'max_text_length': 10000
}
# Should not raise exception
service._validate_tts_updates(updates)
def test_validate_tts_updates_invalid_provider(self, service):
"""Test TTS updates validation with invalid provider"""
updates = {
'preferred_providers': ['invalid_provider']
}
with pytest.raises(ConfigurationException, match="Invalid TTS provider"):
service._validate_tts_updates(updates)
def test_validate_tts_updates_invalid_speed(self, service):
"""Test TTS updates validation with invalid speed"""
updates = {
'default_speed': 5.0 # Too high
}
with pytest.raises(ConfigurationException, match="default_speed must be between 0.1 and 3.0"):
service._validate_tts_updates(updates)
def test_validate_stt_updates_valid(self, service):
"""Test STT updates validation with valid data"""
updates = {
'preferred_providers': ['whisper', 'parakeet'],
'default_model': 'whisper',
'chunk_length_s': 45,
'batch_size': 32,
'enable_vad': False
}
# Should not raise exception
service._validate_stt_updates(updates)
def test_validate_stt_updates_invalid_provider(self, service):
"""Test STT updates validation with invalid provider"""
updates = {
'preferred_providers': ['invalid_stt']
}
with pytest.raises(ConfigurationException, match="Invalid STT provider"):
service._validate_stt_updates(updates)
def test_validate_translation_updates_valid(self, service):
"""Test translation updates validation with valid data"""
updates = {
'default_provider': 'nllb',
'model_name': 'nllb-200-1.3B',
'max_chunk_length': 256,
'batch_size': 4,
'cache_translations': False
}
# Should not raise exception
service._validate_translation_updates(updates)
def test_validate_processing_updates_valid(self, service):
"""Test processing updates validation with valid data"""
updates = {
'temp_dir': '/tmp/test',
'cleanup_temp_files': True,
'max_file_size_mb': 150,
'supported_audio_formats': ['wav', 'mp3'],
'processing_timeout_seconds': 600
}
with patch('pathlib.Path.mkdir'):
# Should not raise exception
service._validate_processing_updates(updates)
def test_validate_processing_updates_invalid_format(self, service):
"""Test processing updates validation with invalid audio format"""
updates = {
'supported_audio_formats': ['wav', 'invalid_format']
}
with pytest.raises(ConfigurationException, match="Invalid audio format"):
service._validate_processing_updates(updates)
def test_save_configuration_to_file_success(self, service, mock_config):
"""Test successful configuration save to file"""
file_path = "/tmp/config.json"
service.save_configuration_to_file(file_path)
mock_config.save_configuration.assert_called_once_with(file_path)
def test_save_configuration_to_file_failure(self, service, mock_config):
"""Test configuration save to file failure"""
file_path = "/tmp/config.json"
mock_config.save_configuration.side_effect = Exception("Save failed")
with pytest.raises(ConfigurationException, match="Failed to save configuration"):
service.save_configuration_to_file(file_path)
@patch('os.path.exists')
def test_load_configuration_from_file_success(self, mock_exists, service, mock_container):
"""Test successful configuration load from file"""
file_path = "/tmp/config.json"
mock_exists.return_value = True
with patch('src.infrastructure.config.app_config.AppConfig') as mock_app_config:
new_config = Mock()
mock_app_config.return_value = new_config
result = service.load_configuration_from_file(file_path)
# Verify new config was created and registered
mock_app_config.assert_called_once_with(config_file=file_path)
mock_container.register_singleton.assert_called_once_with(AppConfig, new_config)
@patch('os.path.exists')
def test_load_configuration_from_file_not_found(self, mock_exists, service):
"""Test configuration load from non-existent file"""
file_path = "/tmp/nonexistent.json"
mock_exists.return_value = False
with pytest.raises(ConfigurationException, match="Configuration file not found"):
service.load_configuration_from_file(file_path)
def test_reload_configuration_success(self, service, mock_config):
"""Test successful configuration reload"""
result = service.reload_configuration()
mock_config.reload_configuration.assert_called_once()
assert 'tts' in result
def test_reload_configuration_failure(self, service, mock_config):
"""Test configuration reload failure"""
mock_config.reload_configuration.side_effect = Exception("Reload failed")
with pytest.raises(ConfigurationException, match="Failed to reload configuration"):
service.reload_configuration()
def test_get_provider_availability(self, service, mock_container):
"""Test provider availability check"""
# Mock factories
mock_tts_factory = Mock()
mock_stt_factory = Mock()
mock_translation_factory = Mock()
mock_container.resolve.side_effect = [mock_tts_factory, mock_stt_factory, mock_translation_factory]
# Mock successful provider creation
mock_tts_factory.create_provider.return_value = Mock()
mock_stt_factory.create_provider.return_value = Mock()
mock_translation_factory.get_default_provider.return_value = Mock()
result = service.get_provider_availability()
assert 'tts' in result
assert 'stt' in result
assert 'translation' in result
# All providers should be available
assert all(result['tts'].values())
assert all(result['stt'].values())
assert result['translation']['nllb'] is True
def test_get_system_info(self, service, mock_config):
"""Test system information retrieval"""
mock_config.config_file = "/tmp/config.json"
mock_config.processing.temp_dir = "/tmp"
mock_config.logging.level = "INFO"
mock_config.processing.supported_audio_formats = ['wav', 'mp3']
mock_config.processing.max_file_size_mb = 100
mock_config.processing.processing_timeout_seconds = 300
with patch.object(service, 'get_provider_availability', return_value={}):
result = service.get_system_info()
assert result['config_file'] == "/tmp/config.json"
assert result['temp_directory'] == "/tmp"
assert result['log_level'] == "INFO"
assert 'supported_languages' in result
assert 'supported_audio_formats' in result
assert 'max_file_size_mb' in result
def test_validate_configuration_success(self, service, mock_config):
"""Test successful configuration validation"""
# Mock valid configuration
mock_config.get_tts_config.return_value = {
'default_speed': 1.0,
'max_text_length': 5000
}
mock_config.get_stt_config.return_value = {
'chunk_length_s': 30,
'batch_size': 16
}
mock_config.get_processing_config.return_value = {
'temp_dir': '/tmp',
'max_file_size_mb': 100
}
mock_config.get_logging_config.return_value = {
'level': 'INFO'
}
with patch('os.path.exists', return_value=True):
result = service.validate_configuration()
# Should have no issues
assert all(len(issues) == 0 for issues in result.values())
def test_validate_configuration_with_issues(self, service, mock_config):
"""Test configuration validation with issues"""
# Mock invalid configuration
mock_config.get_tts_config.return_value = {
'default_speed': 5.0, # Invalid
'max_text_length': -100 # Invalid
}
mock_config.get_stt_config.return_value = {
'chunk_length_s': -10, # Invalid
'batch_size': 0 # Invalid
}
mock_config.get_processing_config.return_value = {
'temp_dir': '/nonexistent', # Invalid
'max_file_size_mb': -50 # Invalid
}
mock_config.get_logging_config.return_value = {
'level': 'INVALID' # Invalid
}
with patch('os.path.exists', return_value=False):
result = service.validate_configuration()
# Should have issues in each category
assert len(result['tts']) > 0
assert len(result['stt']) > 0
assert len(result['processing']) > 0
assert len(result['logging']) > 0
def test_reset_to_defaults_success(self, service, mock_container):
"""Test successful configuration reset to defaults"""
with patch('src.infrastructure.config.app_config.AppConfig') as mock_app_config:
default_config = Mock()
mock_app_config.return_value = default_config
result = service.reset_to_defaults()
# Verify new default config was created and registered
mock_app_config.assert_called_once_with()
mock_container.register_singleton.assert_called_once_with(AppConfig, default_config)
def test_reset_to_defaults_failure(self, service):
"""Test configuration reset to defaults failure"""
with patch('src.infrastructure.config.app_config.AppConfig', side_effect=Exception("Reset failed")):
with pytest.raises(ConfigurationException, match="Failed to reset configuration"):
service.reset_to_defaults()
def test_cleanup(self, service):
"""Test service cleanup"""
# Should not raise exception
service.cleanup()
def test_context_manager(self, service):
"""Test service as context manager"""
with patch.object(service, 'cleanup') as mock_cleanup:
with service as svc:
assert svc == service
mock_cleanup.assert_called_once() |