Spaces:
Build error
Build error
File size: 8,388 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 |
"""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" |