Spaces:
Build error
Build error
File size: 20,898 Bytes
271b76a |
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 573 574 575 576 577 578 579 580 |
"""Integration tests for file handling and cleanup."""
import os
import tempfile
import shutil
import time
import pytest
from pathlib import Path
from unittest.mock import Mock, patch, MagicMock
from typing import List, Dict, Any
from src.application.services.audio_processing_service import AudioProcessingApplicationService
from src.application.dtos.audio_upload_dto import AudioUploadDto
from src.application.dtos.processing_request_dto import ProcessingRequestDto
from src.infrastructure.config.dependency_container import DependencyContainer
from src.infrastructure.config.app_config import AppConfig
from src.domain.models.audio_content import AudioContent
from src.domain.models.text_content import TextContent
class TestFileHandling:
"""Integration tests for file handling and cleanup."""
@pytest.fixture
def temp_base_dir(self):
"""Create base temporary directory for all tests."""
with tempfile.TemporaryDirectory() as temp_dir:
yield temp_dir
@pytest.fixture
def mock_config(self, temp_base_dir):
"""Create mock configuration with temporary directories."""
config = Mock(spec=AppConfig)
# Processing configuration with temp directory
config.get_processing_config.return_value = {
'max_file_size_mb': 50,
'supported_audio_formats': ['wav', 'mp3', 'flac', 'ogg'],
'temp_dir': temp_base_dir,
'cleanup_temp_files': True,
'max_temp_file_age_hours': 24,
'temp_file_prefix': 'audio_processing_'
}
# Logging configuration
config.get_logging_config.return_value = {
'level': 'INFO',
'enable_file_logging': True,
'log_file_path': os.path.join(temp_base_dir, 'processing.log'),
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
}
# STT configuration
config.get_stt_config.return_value = {
'preferred_providers': ['whisper-small']
}
# TTS configuration
config.get_tts_config.return_value = {
'preferred_providers': ['dummy']
}
return config
@pytest.fixture
def mock_container(self, mock_config):
"""Create mock dependency container."""
container = Mock(spec=DependencyContainer)
container.resolve.return_value = mock_config
# Mock providers
mock_stt_provider = Mock()
mock_stt_provider.transcribe.return_value = TextContent(
text="Test transcription",
language="en"
)
container.get_stt_provider.return_value = mock_stt_provider
mock_translation_provider = Mock()
mock_translation_provider.translate.return_value = TextContent(
text="Prueba de transcripción",
language="es"
)
container.get_translation_provider.return_value = mock_translation_provider
mock_tts_provider = Mock()
mock_tts_provider.synthesize.return_value = AudioContent(
data=b"synthesized_audio_data",
format="wav",
sample_rate=22050,
duration=2.0
)
container.get_tts_provider.return_value = mock_tts_provider
return container
@pytest.fixture
def audio_service(self, mock_container, mock_config):
"""Create audio processing service."""
return AudioProcessingApplicationService(mock_container, mock_config)
@pytest.fixture
def sample_audio_files(self, temp_base_dir):
"""Create sample audio files for testing."""
files = {}
# Create different audio file types
audio_formats = {
'wav': b'RIFF\x24\x00\x00\x00WAVEfmt \x10\x00\x00\x00',
'mp3': b'\xff\xfb\x90\x00\x00\x00\x00\x00\x00\x00\x00\x00',
'flac': b'fLaC\x00\x00\x00\x22\x10\x00\x10\x00',
'ogg': b'OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00'
}
for format_name, header in audio_formats.items():
file_path = os.path.join(temp_base_dir, f'test_audio.{format_name}')
with open(file_path, 'wb') as f:
f.write(header + b'\x00' * 1000) # Add some padding
files[format_name] = file_path
yield files
# Cleanup
for file_path in files.values():
if os.path.exists(file_path):
os.remove(file_path)
def test_temp_directory_creation(self, audio_service, temp_base_dir):
"""Test temporary directory creation and structure."""
# Create a processing request to trigger temp directory creation
audio_upload = AudioUploadDto(
filename="test.wav",
content=b"fake_audio_data",
content_type="audio/wav",
size=len(b"fake_audio_data")
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
# Process and check temp directory creation
result = audio_service.process_audio_pipeline(request)
assert result.success is True
# Verify base temp directory exists
assert os.path.exists(temp_base_dir)
assert os.path.isdir(temp_base_dir)
def test_input_file_handling(self, audio_service, sample_audio_files):
"""Test handling of different input audio file formats."""
for format_name, file_path in sample_audio_files.items():
with open(file_path, 'rb') as f:
content = f.read()
audio_upload = AudioUploadDto(
filename=f"test.{format_name}",
content=content,
content_type=f"audio/{format_name}",
size=len(content)
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="en",
voice="dummy",
speed=1.0,
requires_translation=False
)
result = audio_service.process_audio_pipeline(request)
assert result.success is True, f"Failed to process {format_name} file"
assert result.audio_path is not None
assert os.path.exists(result.audio_path)
def test_output_file_generation(self, audio_service, temp_base_dir):
"""Test output audio file generation."""
audio_upload = AudioUploadDto(
filename="input.wav",
content=b"input_audio_data",
content_type="audio/wav",
size=len(b"input_audio_data")
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
assert result.success is True
assert result.audio_path is not None
# Verify output file exists and has content
assert os.path.exists(result.audio_path)
assert os.path.getsize(result.audio_path) > 0
# Verify file is in expected location
assert temp_base_dir in result.audio_path
def test_temp_file_cleanup_success(self, audio_service, temp_base_dir):
"""Test temporary file cleanup after successful processing."""
initial_files = set(os.listdir(temp_base_dir))
audio_upload = AudioUploadDto(
filename="cleanup_test.wav",
content=b"cleanup_test_data",
content_type="audio/wav",
size=len(b"cleanup_test_data")
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
assert result.success is True
# Check that temporary processing files are cleaned up
# (output file should remain)
final_files = set(os.listdir(temp_base_dir))
new_files = final_files - initial_files
# Should only have the output file and possibly log files
assert len(new_files) <= 2 # output file + possible log file
def test_temp_file_cleanup_on_error(self, audio_service, temp_base_dir, mock_container):
"""Test temporary file cleanup when processing fails."""
# Mock STT provider to fail
mock_stt_provider = mock_container.get_stt_provider.return_value
mock_stt_provider.transcribe.side_effect = Exception("STT failed")
initial_files = set(os.listdir(temp_base_dir))
audio_upload = AudioUploadDto(
filename="error_test.wav",
content=b"error_test_data",
content_type="audio/wav",
size=len(b"error_test_data")
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
assert result.success is False
# Verify cleanup occurred even on error
final_files = set(os.listdir(temp_base_dir))
new_files = final_files - initial_files
# Should have minimal new files (possibly just log files)
assert len(new_files) <= 1
def test_large_file_handling(self, audio_service, temp_base_dir):
"""Test handling of large audio files."""
# Create large audio content (5MB)
large_content = b"x" * (5 * 1024 * 1024)
audio_upload = AudioUploadDto(
filename="large_file.wav",
content=large_content,
content_type="audio/wav",
size=len(large_content)
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
assert result.success is True
assert result.audio_path is not None
assert os.path.exists(result.audio_path)
def test_concurrent_file_handling(self, audio_service, temp_base_dir):
"""Test concurrent file handling and cleanup."""
import threading
import queue
results_queue = queue.Queue()
def process_file(file_id):
try:
audio_upload = AudioUploadDto(
filename=f"concurrent_{file_id}.wav",
content=f"concurrent_data_{file_id}".encode(),
content_type="audio/wav",
size=len(f"concurrent_data_{file_id}".encode())
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
results_queue.put((file_id, result))
except Exception as e:
results_queue.put((file_id, e))
# Start multiple threads
threads = []
for i in range(3):
thread = threading.Thread(target=process_file, args=(i,))
threads.append(thread)
thread.start()
# Wait for completion
for thread in threads:
thread.join()
# Verify results
results = {}
while not results_queue.empty():
file_id, result = results_queue.get()
if isinstance(result, Exception):
pytest.fail(f"Concurrent processing failed for file {file_id}: {result}")
results[file_id] = result
assert len(results) == 3
for file_id, result in results.items():
assert result.success is True
assert result.audio_path is not None
assert os.path.exists(result.audio_path)
def test_file_permission_handling(self, audio_service, temp_base_dir):
"""Test file permission handling."""
audio_upload = AudioUploadDto(
filename="permission_test.wav",
content=b"permission_test_data",
content_type="audio/wav",
size=len(b"permission_test_data")
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
assert result.success is True
assert result.audio_path is not None
# Verify file permissions
file_stat = os.stat(result.audio_path)
assert file_stat.st_mode & 0o600 # At least owner read/write
def test_disk_space_monitoring(self, audio_service, temp_base_dir):
"""Test disk space monitoring during processing."""
import shutil
# Get initial disk space
initial_space = shutil.disk_usage(temp_base_dir)
audio_upload = AudioUploadDto(
filename="space_test.wav",
content=b"space_test_data" * 1000, # Larger content
content_type="audio/wav",
size=len(b"space_test_data" * 1000)
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
assert result.success is True
# Verify disk space hasn't been exhausted
final_space = shutil.disk_usage(temp_base_dir)
assert final_space.free > 0
def test_file_naming_conventions(self, audio_service, temp_base_dir):
"""Test file naming conventions and uniqueness."""
results = []
# Process multiple files to test naming
for i in range(3):
audio_upload = AudioUploadDto(
filename=f"naming_test_{i}.wav",
content=f"naming_test_data_{i}".encode(),
content_type="audio/wav",
size=len(f"naming_test_data_{i}".encode())
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
results.append(result)
# Verify all results are successful
for result in results:
assert result.success is True
assert result.audio_path is not None
# Verify unique file names
output_paths = [r.audio_path for r in results]
assert len(set(output_paths)) == len(output_paths) # All unique
# Verify naming convention
for path in output_paths:
filename = os.path.basename(path)
assert filename.startswith("output_")
assert filename.endswith(".wav")
def test_file_encoding_handling(self, audio_service, temp_base_dir):
"""Test handling of different file encodings and special characters."""
# Test with filename containing special characters
special_filename = "test_file_ñáéíóú_测试.wav"
audio_upload = AudioUploadDto(
filename=special_filename,
content=b"encoding_test_data",
content_type="audio/wav",
size=len(b"encoding_test_data")
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
assert result.success is True
assert result.audio_path is not None
assert os.path.exists(result.audio_path)
def test_file_cleanup_context_manager(self, mock_container, mock_config, temp_base_dir):
"""Test file cleanup using context manager."""
initial_files = set(os.listdir(temp_base_dir))
with AudioProcessingApplicationService(mock_container, mock_config) as service:
audio_upload = AudioUploadDto(
filename="context_test.wav",
content=b"context_test_data",
content_type="audio/wav",
size=len(b"context_test_data")
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = service.process_audio_pipeline(request)
assert result.success is True
# Verify cleanup occurred when exiting context
final_files = set(os.listdir(temp_base_dir))
new_files = final_files - initial_files
# Should have minimal new files after context exit
assert len(new_files) <= 1 # Possibly just log file
def test_file_recovery_after_interruption(self, audio_service, temp_base_dir, mock_container):
"""Test file recovery mechanisms after processing interruption."""
# Mock provider to simulate interruption
mock_tts_provider = mock_container.get_tts_provider.return_value
mock_tts_provider.synthesize.side_effect = KeyboardInterrupt("Simulated interruption")
audio_upload = AudioUploadDto(
filename="interruption_test.wav",
content=b"interruption_test_data",
content_type="audio/wav",
size=len(b"interruption_test_data")
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
# Process should handle interruption gracefully
with pytest.raises(KeyboardInterrupt):
audio_service.process_audio_pipeline(request)
# Verify cleanup still occurred
# (In real implementation, this would be handled by signal handlers)
def test_file_metadata_preservation(self, audio_service, temp_base_dir):
"""Test preservation of file metadata during processing."""
original_filename = "metadata_test.wav"
original_content = b"metadata_test_data"
audio_upload = AudioUploadDto(
filename=original_filename,
content=original_content,
content_type="audio/wav",
size=len(original_content)
)
request = ProcessingRequestDto(
audio=audio_upload,
asr_model="whisper-small",
target_language="es",
voice="dummy",
speed=1.0,
requires_translation=True
)
result = audio_service.process_audio_pipeline(request)
assert result.success is True
assert result.metadata is not None
# Verify original filename is preserved in metadata
correlation_id = result.metadata.get('correlation_id')
assert correlation_id is not None
# Verify output file exists
assert result.audio_path is not None
assert os.path.exists(result.audio_path) |