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:
JSC
2025-07-29 19:25:46 +02:00
parent 301b5dd794
commit 5ed19c8f0f
31 changed files with 4248 additions and 194 deletions

View File

@@ -41,6 +41,7 @@ class TestSoundScannerService:
try:
from app.utils.audio import get_file_hash
hash_value = get_file_hash(temp_path)
assert len(hash_value) == 64 # SHA-256 hash length
assert isinstance(hash_value, str)
@@ -56,6 +57,7 @@ class TestSoundScannerService:
try:
from app.utils.audio import get_file_size
size = get_file_size(temp_path)
assert size > 0
assert isinstance(size, int)
@@ -83,6 +85,7 @@ class TestSoundScannerService:
temp_path = Path("/fake/path/test.mp3")
from app.utils.audio import get_audio_duration
duration = get_audio_duration(temp_path)
assert duration == 123456 # 123.456 seconds * 1000 = 123456 ms
@@ -95,6 +98,7 @@ class TestSoundScannerService:
temp_path = Path("/fake/path/test.mp3")
from app.utils.audio import get_audio_duration
duration = get_audio_duration(temp_path)
assert duration == 0
@@ -129,10 +133,11 @@ class TestSoundScannerService:
)
# Mock file operations to return same hash
with patch("app.services.sound_scanner.get_file_hash", return_value="same_hash"), \
patch("app.services.sound_scanner.get_audio_duration", return_value=120000), \
patch("app.services.sound_scanner.get_file_size", return_value=1024):
with (
patch("app.services.sound_scanner.get_file_hash", return_value="same_hash"),
patch("app.services.sound_scanner.get_audio_duration", return_value=120000),
patch("app.services.sound_scanner.get_file_size", return_value=1024),
):
# Create a temporary file
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
temp_path = Path(f.name)
@@ -175,10 +180,11 @@ class TestSoundScannerService:
scanner_service.sound_repo.create = AsyncMock(return_value=created_sound)
# Mock file operations
with patch("app.services.sound_scanner.get_file_hash", return_value="test_hash"), \
patch("app.services.sound_scanner.get_audio_duration", return_value=120000), \
patch("app.services.sound_scanner.get_file_size", return_value=1024):
with (
patch("app.services.sound_scanner.get_file_hash", return_value="test_hash"),
patch("app.services.sound_scanner.get_audio_duration", return_value=120000),
patch("app.services.sound_scanner.get_file_size", return_value=1024),
):
# Create a temporary file
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
temp_path = Path(f.name)
@@ -208,7 +214,9 @@ class TestSoundScannerService:
assert call_args["duration"] == 120000 # Duration in ms
assert call_args["size"] == 1024
assert call_args["hash"] == "test_hash"
assert call_args["is_deletable"] is False # SDB sounds are not deletable
assert (
call_args["is_deletable"] is False
) # SDB sounds are not deletable
finally:
temp_path.unlink()
@@ -229,10 +237,11 @@ class TestSoundScannerService:
scanner_service.sound_repo.update = AsyncMock(return_value=existing_sound)
# Mock file operations to return new values
with patch("app.services.sound_scanner.get_file_hash", return_value="new_hash"), \
patch("app.services.sound_scanner.get_audio_duration", return_value=120000), \
patch("app.services.sound_scanner.get_file_size", return_value=1024):
with (
patch("app.services.sound_scanner.get_file_hash", return_value="new_hash"),
patch("app.services.sound_scanner.get_audio_duration", return_value=120000),
patch("app.services.sound_scanner.get_file_size", return_value=1024),
):
# Create a temporary file
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
temp_path = Path(f.name)
@@ -259,7 +268,9 @@ class TestSoundScannerService:
assert results["files"][0]["reason"] == "file was modified"
# Verify sound_repo.update was called with correct data
call_args = scanner_service.sound_repo.update.call_args[0][1] # update_data
call_args = scanner_service.sound_repo.update.call_args[0][
1
] # update_data
assert call_args["duration"] == 120000
assert call_args["size"] == 1024
assert call_args["hash"] == "new_hash"
@@ -283,10 +294,13 @@ class TestSoundScannerService:
scanner_service.sound_repo.create = AsyncMock(return_value=created_sound)
# Mock file operations
with patch("app.services.sound_scanner.get_file_hash", return_value="custom_hash"), \
patch("app.services.sound_scanner.get_audio_duration", return_value=60000), \
patch("app.services.sound_scanner.get_file_size", return_value=2048):
with (
patch(
"app.services.sound_scanner.get_file_hash", return_value="custom_hash"
),
patch("app.services.sound_scanner.get_audio_duration", return_value=60000),
patch("app.services.sound_scanner.get_file_size", return_value=2048),
):
# Create a temporary file
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
temp_path = Path(f.name)
@@ -301,7 +315,9 @@ class TestSoundScannerService:
"errors": 0,
"files": [],
}
await scanner_service._sync_audio_file(temp_path, "CUSTOM", None, results)
await scanner_service._sync_audio_file(
temp_path, "CUSTOM", None, results
)
assert results["added"] == 1
assert results["skipped"] == 0