Spaces:
Build error
Build error
"""Unit tests for AudioUploadDto""" | |
import pytest | |
import os | |
from src.application.dtos.audio_upload_dto import AudioUploadDto | |
class TestAudioUploadDto: | |
"""Test cases for AudioUploadDto""" | |
def test_valid_audio_upload_dto(self): | |
"""Test creating a valid AudioUploadDto""" | |
filename = "test_audio.wav" | |
content = b"fake_audio_content_" + b"x" * 1000 # 1KB+ of fake audio | |
content_type = "audio/wav" | |
dto = AudioUploadDto( | |
filename=filename, | |
content=content, | |
content_type=content_type | |
) | |
assert dto.filename == filename | |
assert dto.content == content | |
assert dto.content_type == content_type | |
assert dto.size == len(content) | |
def test_audio_upload_dto_with_explicit_size(self): | |
"""Test creating AudioUploadDto with explicit size""" | |
filename = "test_audio.mp3" | |
content = b"fake_audio_content_" + b"x" * 2000 | |
content_type = "audio/mpeg" | |
size = 2500 | |
dto = AudioUploadDto( | |
filename=filename, | |
content=content, | |
content_type=content_type, | |
size=size | |
) | |
assert dto.size == size # Should use explicit size, not calculated | |
def test_empty_filename_validation(self): | |
"""Test validation with empty filename""" | |
with pytest.raises(ValueError, match="Filename cannot be empty"): | |
AudioUploadDto( | |
filename="", | |
content=b"fake_audio_content_" + b"x" * 1000, | |
content_type="audio/wav" | |
) | |
def test_empty_content_validation(self): | |
"""Test validation with empty content""" | |
with pytest.raises(ValueError, match="Audio content cannot be empty"): | |
AudioUploadDto( | |
filename="test.wav", | |
content=b"", | |
content_type="audio/wav" | |
) | |
def test_empty_content_type_validation(self): | |
"""Test validation with empty content type""" | |
with pytest.raises(ValueError, match="Content type cannot be empty"): | |
AudioUploadDto( | |
filename="test.wav", | |
content=b"fake_audio_content_" + b"x" * 1000, | |
content_type="" | |
) | |
def test_unsupported_file_extension_validation(self): | |
"""Test validation with unsupported file extension""" | |
with pytest.raises(ValueError, match="Unsupported file extension"): | |
AudioUploadDto( | |
filename="test.xyz", | |
content=b"fake_audio_content_" + b"x" * 1000, | |
content_type="audio/wav" | |
) | |
def test_supported_file_extensions(self): | |
"""Test all supported file extensions""" | |
supported_extensions = ['.wav', '.mp3', '.m4a', '.flac', '.ogg'] | |
content = b"fake_audio_content_" + b"x" * 1000 | |
for ext in supported_extensions: | |
filename = f"test{ext}" | |
content_type = f"audio/{ext[1:]}" if ext != '.m4a' else "audio/mp4" | |
# Should not raise exception | |
dto = AudioUploadDto( | |
filename=filename, | |
content=content, | |
content_type=content_type | |
) | |
assert dto.file_extension == ext | |
def test_case_insensitive_extension_validation(self): | |
"""Test case insensitive extension validation""" | |
content = b"fake_audio_content_" + b"x" * 1000 | |
# Should work with uppercase extension | |
dto = AudioUploadDto( | |
filename="test.WAV", | |
content=content, | |
content_type="audio/wav" | |
) | |
assert dto.file_extension == ".wav" # Should be normalized to lowercase | |
def test_file_too_large_validation(self): | |
"""Test validation with file too large""" | |
max_size = 100 * 1024 * 1024 # 100MB | |
large_content = b"x" * (max_size + 1) | |
with pytest.raises(ValueError, match="File too large"): | |
AudioUploadDto( | |
filename="test.wav", | |
content=large_content, | |
content_type="audio/wav" | |
) | |
def test_file_too_small_validation(self): | |
"""Test validation with file too small""" | |
small_content = b"x" * 500 # Less than 1KB | |
with pytest.raises(ValueError, match="File too small"): | |
AudioUploadDto( | |
filename="test.wav", | |
content=small_content, | |
content_type="audio/wav" | |
) | |
def test_invalid_content_type_validation(self): | |
"""Test validation with invalid content type""" | |
content = b"fake_audio_content_" + b"x" * 1000 | |
with pytest.raises(ValueError, match="Invalid content type"): | |
AudioUploadDto( | |
filename="test.wav", | |
content=content, | |
content_type="text/plain" # Not audio/* | |
) | |
def test_file_extension_property(self): | |
"""Test file_extension property""" | |
dto = AudioUploadDto( | |
filename="test_audio.MP3", | |
content=b"fake_audio_content_" + b"x" * 1000, | |
content_type="audio/mpeg" | |
) | |
assert dto.file_extension == ".mp3" | |
def test_base_filename_property(self): | |
"""Test base_filename property""" | |
dto = AudioUploadDto( | |
filename="test_audio.wav", | |
content=b"fake_audio_content_" + b"x" * 1000, | |
content_type="audio/wav" | |
) | |
assert dto.base_filename == "test_audio" | |
def test_to_dict_method(self): | |
"""Test to_dict method""" | |
filename = "test_audio.wav" | |
content = b"fake_audio_content_" + b"x" * 1000 | |
content_type = "audio/wav" | |
dto = AudioUploadDto( | |
filename=filename, | |
content=content, | |
content_type=content_type | |
) | |
result = dto.to_dict() | |
assert result['filename'] == filename | |
assert result['content_type'] == content_type | |
assert result['size'] == len(content) | |
assert result['file_extension'] == ".wav" | |
# Should not include content in dict representation | |
assert 'content' not in result | |
def test_size_calculation_on_init(self): | |
"""Test that size is calculated automatically if not provided""" | |
content = b"fake_audio_content_" + b"x" * 1500 | |
dto = AudioUploadDto( | |
filename="test.wav", | |
content=content, | |
content_type="audio/wav" | |
) | |
assert dto.size == len(content) | |
def test_validation_called_on_init(self): | |
"""Test that validation is called during initialization""" | |
# This should trigger validation and raise an error | |
with pytest.raises(ValueError): | |
AudioUploadDto( | |
filename="", # Invalid empty filename | |
content=b"fake_audio_content_" + b"x" * 1000, | |
content_type="audio/wav" | |
) | |
def test_edge_case_minimum_valid_size(self): | |
"""Test edge case with minimum valid file size""" | |
min_content = b"x" * 1024 # Exactly 1KB | |
dto = AudioUploadDto( | |
filename="test.wav", | |
content=min_content, | |
content_type="audio/wav" | |
) | |
assert dto.size == 1024 | |
def test_edge_case_maximum_valid_size(self): | |
"""Test edge case with maximum valid file size""" | |
max_size = 100 * 1024 * 1024 # Exactly 100MB | |
max_content = b"x" * max_size | |
dto = AudioUploadDto( | |
filename="test.wav", | |
content=max_content, | |
content_type="audio/wav" | |
) | |
assert dto.size == max_size | |
def test_content_type_mismatch_handling(self): | |
"""Test handling of content type mismatch with filename""" | |
content = b"fake_audio_content_" + b"x" * 1000 | |
# This should still work as long as content_type starts with 'audio/' | |
dto = AudioUploadDto( | |
filename="test.wav", | |
content=content, | |
content_type="audio/mpeg" # Different from .wav extension | |
) | |
assert dto.content_type == "audio/mpeg" | |
assert dto.file_extension == ".wav" |