Add comprehensive tests for playlist service and refactor socket service tests
- Introduced a new test suite for the PlaylistService covering various functionalities including creation, retrieval, updating, and deletion of playlists. - Added tests for handling sounds within playlists, ensuring correct behavior when adding/removing sounds and managing current playlists. - Refactored socket service tests for improved readability by adjusting function signatures. - Cleaned up unnecessary whitespace in sound normalizer and sound scanner tests for consistency. - Enhanced audio utility tests to ensure accurate hash and size calculations, including edge cases for nonexistent files. - Removed redundant blank lines in cookie utility tests for cleaner code.
This commit is contained in:
@@ -24,22 +24,22 @@ class TestAudioUtils:
|
||||
try:
|
||||
# Calculate hash using our function
|
||||
result_hash = get_file_hash(temp_path)
|
||||
|
||||
|
||||
# Calculate expected hash manually
|
||||
expected_hash = hashlib.sha256(test_content.encode()).hexdigest()
|
||||
|
||||
|
||||
# Verify the hash is correct
|
||||
assert result_hash == expected_hash
|
||||
assert len(result_hash) == 64 # SHA-256 hash length
|
||||
assert isinstance(result_hash, str)
|
||||
|
||||
|
||||
finally:
|
||||
temp_path.unlink()
|
||||
|
||||
def test_get_file_hash_binary_content(self):
|
||||
"""Test file hash calculation with binary content."""
|
||||
# Create a temporary file with binary content
|
||||
test_bytes = b"\x00\x01\x02\x03\xFF\xFE\xFD"
|
||||
test_bytes = b"\x00\x01\x02\x03\xff\xfe\xfd"
|
||||
with tempfile.NamedTemporaryFile(mode="wb", delete=False) as f:
|
||||
f.write(test_bytes)
|
||||
temp_path = Path(f.name)
|
||||
@@ -47,15 +47,15 @@ class TestAudioUtils:
|
||||
try:
|
||||
# Calculate hash using our function
|
||||
result_hash = get_file_hash(temp_path)
|
||||
|
||||
|
||||
# Calculate expected hash manually
|
||||
expected_hash = hashlib.sha256(test_bytes).hexdigest()
|
||||
|
||||
|
||||
# Verify the hash is correct
|
||||
assert result_hash == expected_hash
|
||||
assert len(result_hash) == 64 # SHA-256 hash length
|
||||
assert isinstance(result_hash, str)
|
||||
|
||||
|
||||
finally:
|
||||
temp_path.unlink()
|
||||
|
||||
@@ -68,15 +68,15 @@ class TestAudioUtils:
|
||||
try:
|
||||
# Calculate hash using our function
|
||||
result_hash = get_file_hash(temp_path)
|
||||
|
||||
|
||||
# Calculate expected hash for empty content
|
||||
expected_hash = hashlib.sha256(b"").hexdigest()
|
||||
|
||||
|
||||
# Verify the hash is correct
|
||||
assert result_hash == expected_hash
|
||||
assert len(result_hash) == 64 # SHA-256 hash length
|
||||
assert isinstance(result_hash, str)
|
||||
|
||||
|
||||
finally:
|
||||
temp_path.unlink()
|
||||
|
||||
@@ -91,15 +91,15 @@ class TestAudioUtils:
|
||||
try:
|
||||
# Calculate hash using our function
|
||||
result_hash = get_file_hash(temp_path)
|
||||
|
||||
|
||||
# Calculate expected hash manually
|
||||
expected_hash = hashlib.sha256(test_content.encode()).hexdigest()
|
||||
|
||||
|
||||
# Verify the hash is correct
|
||||
assert result_hash == expected_hash
|
||||
assert len(result_hash) == 64 # SHA-256 hash length
|
||||
assert isinstance(result_hash, str)
|
||||
|
||||
|
||||
finally:
|
||||
temp_path.unlink()
|
||||
|
||||
@@ -114,15 +114,15 @@ class TestAudioUtils:
|
||||
try:
|
||||
# Get size using our function
|
||||
result_size = get_file_size(temp_path)
|
||||
|
||||
|
||||
# Get expected size using pathlib directly
|
||||
expected_size = temp_path.stat().st_size
|
||||
|
||||
|
||||
# Verify the size is correct
|
||||
assert result_size == expected_size
|
||||
assert result_size > 0
|
||||
assert isinstance(result_size, int)
|
||||
|
||||
|
||||
finally:
|
||||
temp_path.unlink()
|
||||
|
||||
@@ -135,18 +135,18 @@ class TestAudioUtils:
|
||||
try:
|
||||
# Get size using our function
|
||||
result_size = get_file_size(temp_path)
|
||||
|
||||
|
||||
# Verify the size is zero
|
||||
assert result_size == 0
|
||||
assert isinstance(result_size, int)
|
||||
|
||||
|
||||
finally:
|
||||
temp_path.unlink()
|
||||
|
||||
def test_get_file_size_binary_file(self):
|
||||
"""Test file size calculation for binary file."""
|
||||
# Create a temporary file with binary content
|
||||
test_bytes = b"\x00\x01\x02\x03\xFF\xFE\xFD" * 100 # 700 bytes
|
||||
test_bytes = b"\x00\x01\x02\x03\xff\xfe\xfd" * 100 # 700 bytes
|
||||
with tempfile.NamedTemporaryFile(mode="wb", delete=False) as f:
|
||||
f.write(test_bytes)
|
||||
temp_path = Path(f.name)
|
||||
@@ -154,12 +154,12 @@ class TestAudioUtils:
|
||||
try:
|
||||
# Get size using our function
|
||||
result_size = get_file_size(temp_path)
|
||||
|
||||
|
||||
# Verify the size is correct
|
||||
assert result_size == len(test_bytes)
|
||||
assert result_size == 700
|
||||
assert isinstance(result_size, int)
|
||||
|
||||
|
||||
finally:
|
||||
temp_path.unlink()
|
||||
|
||||
@@ -168,10 +168,10 @@ class TestAudioUtils:
|
||||
"""Test successful audio duration extraction."""
|
||||
# Mock ffmpeg.probe to return duration
|
||||
mock_probe.return_value = {"format": {"duration": "123.456"}}
|
||||
|
||||
|
||||
temp_path = Path("/fake/path/test.mp3")
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
|
||||
# Verify duration is converted correctly (seconds to milliseconds)
|
||||
assert duration == 123456 # 123.456 seconds * 1000 = 123456 ms
|
||||
assert isinstance(duration, int)
|
||||
@@ -182,10 +182,10 @@ class TestAudioUtils:
|
||||
"""Test audio duration extraction with integer duration."""
|
||||
# Mock ffmpeg.probe to return integer duration
|
||||
mock_probe.return_value = {"format": {"duration": "60"}}
|
||||
|
||||
|
||||
temp_path = Path("/fake/path/test.wav")
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
|
||||
# Verify duration is converted correctly
|
||||
assert duration == 60000 # 60 seconds * 1000 = 60000 ms
|
||||
assert isinstance(duration, int)
|
||||
@@ -196,10 +196,10 @@ class TestAudioUtils:
|
||||
"""Test audio duration extraction with zero duration."""
|
||||
# Mock ffmpeg.probe to return zero duration
|
||||
mock_probe.return_value = {"format": {"duration": "0.0"}}
|
||||
|
||||
|
||||
temp_path = Path("/fake/path/silent.mp3")
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
|
||||
# Verify duration is zero
|
||||
assert duration == 0
|
||||
assert isinstance(duration, int)
|
||||
@@ -210,10 +210,10 @@ class TestAudioUtils:
|
||||
"""Test audio duration extraction with fractional seconds."""
|
||||
# Mock ffmpeg.probe to return fractional duration
|
||||
mock_probe.return_value = {"format": {"duration": "45.123"}}
|
||||
|
||||
|
||||
temp_path = Path("/fake/path/test.flac")
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
|
||||
# Verify duration is converted and rounded correctly
|
||||
assert duration == 45123 # 45.123 seconds * 1000 = 45123 ms
|
||||
assert isinstance(duration, int)
|
||||
@@ -224,10 +224,10 @@ class TestAudioUtils:
|
||||
"""Test audio duration extraction when ffmpeg fails."""
|
||||
# Mock ffmpeg.probe to raise an exception
|
||||
mock_probe.side_effect = Exception("FFmpeg error: file not found")
|
||||
|
||||
|
||||
temp_path = Path("/fake/path/nonexistent.mp3")
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
|
||||
# Verify duration defaults to 0 on error
|
||||
assert duration == 0
|
||||
assert isinstance(duration, int)
|
||||
@@ -238,10 +238,10 @@ class TestAudioUtils:
|
||||
"""Test audio duration extraction when format info is missing."""
|
||||
# Mock ffmpeg.probe to return data without format info
|
||||
mock_probe.return_value = {"streams": []}
|
||||
|
||||
|
||||
temp_path = Path("/fake/path/corrupt.mp3")
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
|
||||
# Verify duration defaults to 0 when format info is missing
|
||||
assert duration == 0
|
||||
assert isinstance(duration, int)
|
||||
@@ -252,10 +252,10 @@ class TestAudioUtils:
|
||||
"""Test audio duration extraction when duration is missing."""
|
||||
# Mock ffmpeg.probe to return format without duration
|
||||
mock_probe.return_value = {"format": {"size": "1024"}}
|
||||
|
||||
|
||||
temp_path = Path("/fake/path/noduration.mp3")
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
|
||||
# Verify duration defaults to 0 when duration is missing
|
||||
assert duration == 0
|
||||
assert isinstance(duration, int)
|
||||
@@ -266,10 +266,10 @@ class TestAudioUtils:
|
||||
"""Test audio duration extraction with invalid duration value."""
|
||||
# Mock ffmpeg.probe to return invalid duration
|
||||
mock_probe.return_value = {"format": {"duration": "invalid"}}
|
||||
|
||||
|
||||
temp_path = Path("/fake/path/invalid.mp3")
|
||||
duration = get_audio_duration(temp_path)
|
||||
|
||||
|
||||
# Verify duration defaults to 0 when duration is invalid
|
||||
assert duration == 0
|
||||
assert isinstance(duration, int)
|
||||
@@ -278,7 +278,7 @@ class TestAudioUtils:
|
||||
def test_get_file_hash_nonexistent_file(self):
|
||||
"""Test file hash calculation for nonexistent file."""
|
||||
nonexistent_path = Path("/fake/nonexistent/file.mp3")
|
||||
|
||||
|
||||
# Should raise FileNotFoundError for nonexistent file
|
||||
with pytest.raises(FileNotFoundError):
|
||||
get_file_hash(nonexistent_path)
|
||||
@@ -286,7 +286,7 @@ class TestAudioUtils:
|
||||
def test_get_file_size_nonexistent_file(self):
|
||||
"""Test file size calculation for nonexistent file."""
|
||||
nonexistent_path = Path("/fake/nonexistent/file.mp3")
|
||||
|
||||
|
||||
# Should raise FileNotFoundError for nonexistent file
|
||||
with pytest.raises(FileNotFoundError):
|
||||
get_file_size(nonexistent_path)
|
||||
get_file_size(nonexistent_path)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"""Tests for cookie utilities."""
|
||||
|
||||
|
||||
from app.utils.cookies import extract_access_token_from_cookies, parse_cookies
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user