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:
@@ -48,11 +48,15 @@ class TestAuthService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_register_duplicate_email(
|
||||
self, auth_service: AuthService, test_user: User,
|
||||
self,
|
||||
auth_service: AuthService,
|
||||
test_user: User,
|
||||
) -> None:
|
||||
"""Test registration with duplicate email."""
|
||||
request = UserRegisterRequest(
|
||||
email=test_user.email, password="password123", name="Another User",
|
||||
email=test_user.email,
|
||||
password="password123",
|
||||
name="Another User",
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
@@ -89,7 +93,8 @@ class TestAuthService:
|
||||
async def test_login_invalid_email(self, auth_service: AuthService) -> None:
|
||||
"""Test login with invalid email."""
|
||||
request = UserLoginRequest(
|
||||
email="nonexistent@example.com", password="password123",
|
||||
email="nonexistent@example.com",
|
||||
password="password123",
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
@@ -100,7 +105,9 @@ class TestAuthService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_login_invalid_password(
|
||||
self, auth_service: AuthService, test_user: User,
|
||||
self,
|
||||
auth_service: AuthService,
|
||||
test_user: User,
|
||||
) -> None:
|
||||
"""Test login with invalid password."""
|
||||
request = UserLoginRequest(email=test_user.email, password="wrongpassword")
|
||||
@@ -113,7 +120,10 @@ class TestAuthService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_login_inactive_user(
|
||||
self, auth_service: AuthService, test_user: User, test_session: AsyncSession,
|
||||
self,
|
||||
auth_service: AuthService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test login with inactive user."""
|
||||
# Store the email before deactivating
|
||||
@@ -133,7 +143,10 @@ class TestAuthService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_login_user_without_password(
|
||||
self, auth_service: AuthService, test_user: User, test_session: AsyncSession,
|
||||
self,
|
||||
auth_service: AuthService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test login with user that has no password hash."""
|
||||
# Store the email before removing password
|
||||
@@ -153,7 +166,9 @@ class TestAuthService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_current_user_success(
|
||||
self, auth_service: AuthService, test_user: User,
|
||||
self,
|
||||
auth_service: AuthService,
|
||||
test_user: User,
|
||||
) -> None:
|
||||
"""Test getting current user successfully."""
|
||||
user = await auth_service.get_current_user(test_user.id)
|
||||
@@ -174,7 +189,10 @@ class TestAuthService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_current_user_inactive(
|
||||
self, auth_service: AuthService, test_user: User, test_session: AsyncSession,
|
||||
self,
|
||||
auth_service: AuthService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test getting current user when user is inactive."""
|
||||
# Store the user ID before deactivating
|
||||
@@ -192,7 +210,9 @@ class TestAuthService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_access_token(
|
||||
self, auth_service: AuthService, test_user: User,
|
||||
self,
|
||||
auth_service: AuthService,
|
||||
test_user: User,
|
||||
) -> None:
|
||||
"""Test access token creation."""
|
||||
token_response = auth_service._create_access_token(test_user)
|
||||
@@ -211,7 +231,10 @@ class TestAuthService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_user_response(
|
||||
self, auth_service: AuthService, test_user: User, test_session: AsyncSession,
|
||||
self,
|
||||
auth_service: AuthService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test user response creation."""
|
||||
# Ensure plan relationship is loaded
|
||||
|
||||
@@ -52,7 +52,9 @@ class TestExtractionService:
|
||||
|
||||
@patch("app.services.extraction.yt_dlp.YoutubeDL")
|
||||
@pytest.mark.asyncio
|
||||
async def test_detect_service_info_youtube(self, mock_ydl_class, extraction_service):
|
||||
async def test_detect_service_info_youtube(
|
||||
self, mock_ydl_class, extraction_service
|
||||
):
|
||||
"""Test service detection for YouTube."""
|
||||
mock_ydl = Mock()
|
||||
mock_ydl_class.return_value.__enter__.return_value = mock_ydl
|
||||
@@ -75,7 +77,9 @@ class TestExtractionService:
|
||||
|
||||
@patch("app.services.extraction.yt_dlp.YoutubeDL")
|
||||
@pytest.mark.asyncio
|
||||
async def test_detect_service_info_failure(self, mock_ydl_class, extraction_service):
|
||||
async def test_detect_service_info_failure(
|
||||
self, mock_ydl_class, extraction_service
|
||||
):
|
||||
"""Test service detection failure."""
|
||||
mock_ydl = Mock()
|
||||
mock_ydl_class.return_value.__enter__.return_value = mock_ydl
|
||||
@@ -169,7 +173,7 @@ class TestExtractionService:
|
||||
async def test_process_extraction_with_service_detection(self, extraction_service):
|
||||
"""Test extraction processing with service detection."""
|
||||
extraction_id = 1
|
||||
|
||||
|
||||
# Mock extraction without service info
|
||||
mock_extraction = Extraction(
|
||||
id=extraction_id,
|
||||
@@ -180,7 +184,7 @@ class TestExtractionService:
|
||||
title=None,
|
||||
status="pending",
|
||||
)
|
||||
|
||||
|
||||
extraction_service.extraction_repo.get_by_id = AsyncMock(
|
||||
return_value=mock_extraction
|
||||
)
|
||||
@@ -188,21 +192,25 @@ class TestExtractionService:
|
||||
extraction_service.extraction_repo.get_by_service_and_id = AsyncMock(
|
||||
return_value=None
|
||||
)
|
||||
|
||||
|
||||
# Mock service detection
|
||||
service_info = {
|
||||
"service": "youtube",
|
||||
"service_id": "test123",
|
||||
"service_id": "test123",
|
||||
"title": "Test Video",
|
||||
}
|
||||
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
extraction_service, "_detect_service_info", return_value=service_info
|
||||
),
|
||||
patch.object(extraction_service, "_extract_media") as mock_extract,
|
||||
patch.object(extraction_service, "_move_files_to_final_location") as mock_move,
|
||||
patch.object(extraction_service, "_create_sound_record") as mock_create_sound,
|
||||
patch.object(
|
||||
extraction_service, "_move_files_to_final_location"
|
||||
) as mock_move,
|
||||
patch.object(
|
||||
extraction_service, "_create_sound_record"
|
||||
) as mock_create_sound,
|
||||
patch.object(extraction_service, "_normalize_sound") as mock_normalize,
|
||||
patch.object(extraction_service, "_add_to_main_playlist") as mock_playlist,
|
||||
):
|
||||
@@ -210,17 +218,17 @@ class TestExtractionService:
|
||||
mock_extract.return_value = (Path("/fake/audio.mp3"), None)
|
||||
mock_move.return_value = (Path("/final/audio.mp3"), None)
|
||||
mock_create_sound.return_value = mock_sound
|
||||
|
||||
|
||||
result = await extraction_service.process_extraction(extraction_id)
|
||||
|
||||
|
||||
# Verify service detection was called
|
||||
extraction_service._detect_service_info.assert_called_once_with(
|
||||
"https://www.youtube.com/watch?v=test123"
|
||||
)
|
||||
|
||||
|
||||
# Verify extraction was updated with service info
|
||||
extraction_service.extraction_repo.update.assert_called()
|
||||
|
||||
|
||||
assert result["status"] == "completed"
|
||||
assert result["service"] == "youtube"
|
||||
assert result["service_id"] == "test123"
|
||||
@@ -288,7 +296,6 @@ class TestExtractionService:
|
||||
"app.services.extraction.get_file_hash", return_value="test_hash"
|
||||
),
|
||||
):
|
||||
|
||||
extraction_service.sound_repo.create = AsyncMock(
|
||||
return_value=mock_sound
|
||||
)
|
||||
|
||||
@@ -29,8 +29,9 @@ class TestExtractionProcessor:
|
||||
async def test_start_and_stop(self, processor):
|
||||
"""Test starting and stopping the processor."""
|
||||
# Mock the _process_queue method to avoid actual processing
|
||||
with patch.object(processor, "_process_queue", new_callable=AsyncMock) as mock_process:
|
||||
|
||||
with patch.object(
|
||||
processor, "_process_queue", new_callable=AsyncMock
|
||||
) as mock_process:
|
||||
# Start the processor
|
||||
await processor.start()
|
||||
assert processor.processor_task is not None
|
||||
@@ -44,7 +45,6 @@ class TestExtractionProcessor:
|
||||
async def test_start_already_running(self, processor):
|
||||
"""Test starting processor when already running."""
|
||||
with patch.object(processor, "_process_queue", new_callable=AsyncMock):
|
||||
|
||||
# Start first time
|
||||
await processor.start()
|
||||
first_task = processor.processor_task
|
||||
@@ -150,7 +150,6 @@ class TestExtractionProcessor:
|
||||
return_value=mock_service,
|
||||
),
|
||||
):
|
||||
|
||||
mock_session = AsyncMock()
|
||||
mock_session_class.return_value.__aenter__.return_value = mock_session
|
||||
|
||||
@@ -176,7 +175,6 @@ class TestExtractionProcessor:
|
||||
return_value=mock_service,
|
||||
),
|
||||
):
|
||||
|
||||
mock_session = AsyncMock()
|
||||
mock_session_class.return_value.__aenter__.return_value = mock_session
|
||||
|
||||
@@ -207,7 +205,6 @@ class TestExtractionProcessor:
|
||||
return_value=mock_service,
|
||||
),
|
||||
):
|
||||
|
||||
mock_session = AsyncMock()
|
||||
mock_session_class.return_value.__aenter__.return_value = mock_session
|
||||
|
||||
@@ -232,14 +229,15 @@ class TestExtractionProcessor:
|
||||
patch(
|
||||
"app.services.extraction_processor.AsyncSession"
|
||||
) as mock_session_class,
|
||||
patch.object(processor, "_process_single_extraction", new_callable=AsyncMock) as mock_process,
|
||||
patch.object(
|
||||
processor, "_process_single_extraction", new_callable=AsyncMock
|
||||
) as mock_process,
|
||||
patch(
|
||||
"app.services.extraction_processor.ExtractionService",
|
||||
return_value=mock_service,
|
||||
),
|
||||
patch("asyncio.create_task") as mock_create_task,
|
||||
):
|
||||
|
||||
mock_session = AsyncMock()
|
||||
mock_session_class.return_value.__aenter__.return_value = mock_session
|
||||
|
||||
@@ -276,14 +274,15 @@ class TestExtractionProcessor:
|
||||
patch(
|
||||
"app.services.extraction_processor.AsyncSession"
|
||||
) as mock_session_class,
|
||||
patch.object(processor, "_process_single_extraction", new_callable=AsyncMock) as mock_process,
|
||||
patch.object(
|
||||
processor, "_process_single_extraction", new_callable=AsyncMock
|
||||
) as mock_process,
|
||||
patch(
|
||||
"app.services.extraction_processor.ExtractionService",
|
||||
return_value=mock_service,
|
||||
),
|
||||
patch("asyncio.create_task") as mock_create_task,
|
||||
):
|
||||
|
||||
mock_session = AsyncMock()
|
||||
mock_session_class.return_value.__aenter__.return_value = mock_session
|
||||
|
||||
|
||||
971
tests/services/test_playlist.py
Normal file
971
tests/services/test_playlist.py
Normal file
@@ -0,0 +1,971 @@
|
||||
"""Tests for playlist service."""
|
||||
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from fastapi import HTTPException
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.models.playlist import Playlist
|
||||
from app.models.sound import Sound
|
||||
from app.models.user import User
|
||||
from app.services.playlist import PlaylistService
|
||||
|
||||
|
||||
class TestPlaylistService:
|
||||
"""Test playlist service operations."""
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def playlist_service(
|
||||
self,
|
||||
test_session: AsyncSession,
|
||||
) -> AsyncGenerator[PlaylistService, None]:
|
||||
"""Create a playlist service instance."""
|
||||
yield PlaylistService(test_session)
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def test_playlist(
|
||||
self,
|
||||
test_session: AsyncSession,
|
||||
test_user: User,
|
||||
) -> Playlist:
|
||||
"""Create a test playlist."""
|
||||
# Extract user_id from test_user within the fixture
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
return playlist
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def current_playlist(
|
||||
self,
|
||||
test_session: AsyncSession,
|
||||
test_user: User,
|
||||
) -> Playlist:
|
||||
"""Create a current playlist."""
|
||||
# Extract user_id from test_user within the fixture
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Current Playlist",
|
||||
description="Currently active playlist",
|
||||
is_main=False,
|
||||
is_current=True,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
return playlist
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def main_playlist(
|
||||
self,
|
||||
test_session: AsyncSession,
|
||||
) -> Playlist:
|
||||
"""Create a main playlist."""
|
||||
playlist = Playlist(
|
||||
user_id=None,
|
||||
name="Main Playlist",
|
||||
description="Main playlist",
|
||||
is_main=True,
|
||||
is_current=False,
|
||||
is_deletable=False,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
return playlist
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def test_sound(
|
||||
self,
|
||||
test_session: AsyncSession,
|
||||
) -> Sound:
|
||||
"""Create a test sound."""
|
||||
sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash="test_hash",
|
||||
play_count=10,
|
||||
)
|
||||
test_session.add(sound)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(sound)
|
||||
return sound
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def other_user(
|
||||
self,
|
||||
test_session: AsyncSession,
|
||||
ensure_plans,
|
||||
) -> User:
|
||||
"""Create another test user."""
|
||||
from app.utils.auth import PasswordUtils
|
||||
|
||||
user = User(
|
||||
email="other@example.com",
|
||||
name="Other User",
|
||||
password_hash=PasswordUtils.hash_password("password123"),
|
||||
role="user",
|
||||
is_active=True,
|
||||
plan_id=ensure_plans[0].id,
|
||||
credits=100,
|
||||
)
|
||||
test_session.add(user)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(user)
|
||||
return user
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_playlist_by_id_success(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_playlist: Playlist,
|
||||
) -> None:
|
||||
"""Test getting playlist by ID successfully."""
|
||||
assert test_playlist.id is not None
|
||||
|
||||
playlist = await playlist_service.get_playlist_by_id(test_playlist.id)
|
||||
|
||||
assert playlist.id == test_playlist.id
|
||||
assert playlist.name == test_playlist.name
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_playlist_by_id_not_found(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
) -> None:
|
||||
"""Test getting non-existent playlist."""
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await playlist_service.get_playlist_by_id(99999)
|
||||
|
||||
assert exc_info.value.status_code == 404
|
||||
assert "not found" in exc_info.value.detail
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_main_playlist_existing(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test getting existing main playlist."""
|
||||
|
||||
# Create main playlist manually
|
||||
main_playlist = Playlist(
|
||||
user_id=None,
|
||||
name="Main Playlist",
|
||||
description="Main playlist",
|
||||
is_main=True,
|
||||
is_deletable=False,
|
||||
)
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(main_playlist)
|
||||
|
||||
playlist = await playlist_service.get_main_playlist()
|
||||
|
||||
assert playlist.id == main_playlist.id
|
||||
assert playlist.is_main is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_main_playlist_create_if_not_exists(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
) -> None:
|
||||
"""Test that service fails if main playlist doesn't exist."""
|
||||
|
||||
# Should raise an HTTPException if no main playlist exists
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await playlist_service.get_main_playlist()
|
||||
assert exc_info.value.status_code == 500
|
||||
assert "Main playlist not found" in exc_info.value.detail
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_playlist_success(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
) -> None:
|
||||
"""Test creating a new playlist successfully."""
|
||||
|
||||
user_id = test_user.id # Extract user_id while session is available
|
||||
playlist = await playlist_service.create_playlist(
|
||||
user_id=user_id,
|
||||
name="New Playlist",
|
||||
description="A new playlist",
|
||||
genre="rock",
|
||||
)
|
||||
|
||||
assert playlist.name == "New Playlist"
|
||||
assert playlist.description == "A new playlist"
|
||||
assert playlist.genre == "rock"
|
||||
assert playlist.user_id == user_id
|
||||
assert playlist.is_main is False
|
||||
assert playlist.is_current is False
|
||||
assert playlist.is_deletable is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_playlist_duplicate_name(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test creating playlist with duplicate name fails."""
|
||||
# Create test playlist within this test
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
|
||||
# Extract name before async call
|
||||
playlist_name = playlist.name
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await playlist_service.create_playlist(
|
||||
user_id=user_id,
|
||||
name=playlist_name, # Same name as existing playlist
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 400
|
||||
assert "already exists" in exc_info.value.detail
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_playlist_as_current(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test creating a playlist as current unsets previous current."""
|
||||
# Create current playlist within this test
|
||||
user_id = test_user.id
|
||||
current_playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Current Playlist",
|
||||
description="Currently active playlist",
|
||||
is_main=False,
|
||||
is_current=True,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(current_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(current_playlist)
|
||||
|
||||
# Verify the existing current playlist
|
||||
assert current_playlist.is_current is True
|
||||
|
||||
# Extract ID before async call
|
||||
current_playlist_id = current_playlist.id
|
||||
|
||||
# Create new playlist as current
|
||||
new_playlist = await playlist_service.create_playlist(
|
||||
user_id=user_id,
|
||||
name="New Current Playlist",
|
||||
is_current=True,
|
||||
)
|
||||
|
||||
assert new_playlist.is_current is True
|
||||
|
||||
# Verify the old current playlist is no longer current
|
||||
# We need to refresh the old playlist from the database
|
||||
old_playlist = await playlist_service.get_playlist_by_id(current_playlist_id)
|
||||
assert old_playlist.is_current is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_playlist_success(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test updating a playlist successfully."""
|
||||
# Create test playlist within this test
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
|
||||
# Extract IDs before async call
|
||||
playlist_id = playlist.id
|
||||
|
||||
updated_playlist = await playlist_service.update_playlist(
|
||||
playlist_id=playlist_id,
|
||||
user_id=user_id,
|
||||
name="Updated Name",
|
||||
description="Updated description",
|
||||
genre="jazz",
|
||||
)
|
||||
|
||||
assert updated_playlist.name == "Updated Name"
|
||||
assert updated_playlist.description == "Updated description"
|
||||
assert updated_playlist.genre == "jazz"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_playlist_set_current(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test setting a playlist as current via update."""
|
||||
# Create test playlist within this test
|
||||
user_id = test_user.id
|
||||
test_playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
current_playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Current Playlist",
|
||||
description="Currently active playlist",
|
||||
is_main=False,
|
||||
is_current=True,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(current_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(current_playlist)
|
||||
|
||||
# Extract IDs before async calls
|
||||
test_playlist_id = test_playlist.id
|
||||
current_playlist_id = current_playlist.id
|
||||
|
||||
# Verify initial state
|
||||
assert test_playlist.is_current is False
|
||||
assert current_playlist.is_current is True
|
||||
|
||||
# Update playlist to be current
|
||||
updated_playlist = await playlist_service.update_playlist(
|
||||
playlist_id=test_playlist_id,
|
||||
user_id=user_id,
|
||||
is_current=True,
|
||||
)
|
||||
|
||||
assert updated_playlist.is_current is True
|
||||
|
||||
# Verify old current playlist is no longer current
|
||||
old_current = await playlist_service.get_playlist_by_id(current_playlist_id)
|
||||
assert old_current.is_current is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_playlist_success(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test deleting a playlist successfully."""
|
||||
# Create test playlist within this test
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
|
||||
# Extract ID before async call
|
||||
playlist_id = playlist.id
|
||||
|
||||
await playlist_service.delete_playlist(playlist_id, user_id)
|
||||
|
||||
# Verify playlist is deleted
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await playlist_service.get_playlist_by_id(playlist_id)
|
||||
assert exc_info.value.status_code == 404
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_current_playlist_sets_main_as_current(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test deleting current playlist sets main as current."""
|
||||
# Create main playlist first (required by service)
|
||||
main_playlist = Playlist(
|
||||
user_id=None,
|
||||
name="Main Playlist",
|
||||
description="Main playlist",
|
||||
is_main=True,
|
||||
is_current=False,
|
||||
is_deletable=False,
|
||||
)
|
||||
test_session.add(main_playlist)
|
||||
|
||||
# Create current playlist within this test
|
||||
user_id = test_user.id
|
||||
current_playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Current Playlist",
|
||||
description="Currently active playlist",
|
||||
is_main=False,
|
||||
is_current=True,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(current_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(current_playlist)
|
||||
|
||||
# Extract ID before async call
|
||||
current_playlist_id = current_playlist.id
|
||||
|
||||
# Delete the current playlist
|
||||
await playlist_service.delete_playlist(current_playlist_id, user_id)
|
||||
|
||||
# Verify main playlist is now fallback current (main playlist doesn't have is_current=True)
|
||||
# The service returns main playlist when no current is set
|
||||
current = await playlist_service.get_current_playlist(user_id)
|
||||
assert current.is_main is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_non_deletable_playlist(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test deleting non-deletable playlist fails."""
|
||||
# Extract user ID immediately
|
||||
user_id = test_user.id
|
||||
|
||||
# Create non-deletable playlist
|
||||
non_deletable = Playlist(
|
||||
user_id=user_id,
|
||||
name="Non-deletable",
|
||||
is_deletable=False,
|
||||
)
|
||||
test_session.add(non_deletable)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(non_deletable)
|
||||
|
||||
# Extract ID before async call
|
||||
non_deletable_id = non_deletable.id
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await playlist_service.delete_playlist(non_deletable_id, user_id)
|
||||
|
||||
assert exc_info.value.status_code == 400
|
||||
assert "cannot be deleted" in exc_info.value.detail
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_sound_to_playlist_success(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test adding sound to playlist successfully."""
|
||||
# Create test playlist and sound within this test
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
|
||||
sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash="test_hash",
|
||||
play_count=10,
|
||||
)
|
||||
test_session.add(sound)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
await test_session.refresh(sound)
|
||||
|
||||
# Extract IDs before async calls
|
||||
playlist_id = playlist.id
|
||||
sound_id = sound.id
|
||||
|
||||
await playlist_service.add_sound_to_playlist(
|
||||
playlist_id=playlist_id,
|
||||
sound_id=sound_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
# Verify sound was added
|
||||
sounds = await playlist_service.get_playlist_sounds(playlist_id)
|
||||
assert len(sounds) == 1
|
||||
assert sounds[0].id == sound_id
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_sound_to_playlist_already_exists(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test adding sound that's already in playlist fails."""
|
||||
# Create test playlist and sound within this test
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
|
||||
sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash="test_hash",
|
||||
play_count=10,
|
||||
)
|
||||
test_session.add(sound)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
await test_session.refresh(sound)
|
||||
|
||||
# Extract IDs before async calls
|
||||
playlist_id = playlist.id
|
||||
sound_id = sound.id
|
||||
|
||||
# Add sound first time
|
||||
await playlist_service.add_sound_to_playlist(
|
||||
playlist_id=playlist_id,
|
||||
sound_id=sound_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
# Try to add same sound again
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await playlist_service.add_sound_to_playlist(
|
||||
playlist_id=playlist_id,
|
||||
sound_id=sound_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 400
|
||||
assert "already in this playlist" in exc_info.value.detail
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remove_sound_from_playlist_success(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test removing sound from playlist successfully."""
|
||||
# Create test playlist and sound within this test
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
|
||||
sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash="test_hash",
|
||||
play_count=10,
|
||||
)
|
||||
test_session.add(sound)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
await test_session.refresh(sound)
|
||||
|
||||
# Extract IDs before async calls
|
||||
playlist_id = playlist.id
|
||||
sound_id = sound.id
|
||||
|
||||
# Add sound first
|
||||
await playlist_service.add_sound_to_playlist(
|
||||
playlist_id=playlist_id,
|
||||
sound_id=sound_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
# Remove sound
|
||||
await playlist_service.remove_sound_from_playlist(
|
||||
playlist_id=playlist_id,
|
||||
sound_id=sound_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
# Verify sound was removed
|
||||
sounds = await playlist_service.get_playlist_sounds(playlist_id)
|
||||
assert len(sounds) == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remove_sound_not_in_playlist(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test removing sound that's not in playlist fails."""
|
||||
# Create test playlist and sound within this test
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
|
||||
sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash="test_hash",
|
||||
play_count=10,
|
||||
)
|
||||
test_session.add(sound)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
await test_session.refresh(sound)
|
||||
|
||||
# Extract IDs before async calls
|
||||
playlist_id = playlist.id
|
||||
sound_id = sound.id
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await playlist_service.remove_sound_from_playlist(
|
||||
playlist_id=playlist_id,
|
||||
sound_id=sound_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 404
|
||||
assert "not found in this playlist" in exc_info.value.detail
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_current_playlist(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test setting a playlist as current."""
|
||||
# Create test playlists within this test
|
||||
user_id = test_user.id
|
||||
test_playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(test_playlist)
|
||||
|
||||
current_playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Current Playlist",
|
||||
description="Currently active playlist",
|
||||
is_main=False,
|
||||
is_current=True,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(current_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(test_playlist)
|
||||
await test_session.refresh(current_playlist)
|
||||
|
||||
# Extract IDs before async calls
|
||||
test_playlist_id = test_playlist.id
|
||||
current_playlist_id = current_playlist.id
|
||||
|
||||
# Verify initial state
|
||||
assert current_playlist.is_current is True
|
||||
assert test_playlist.is_current is False
|
||||
|
||||
# Set test_playlist as current
|
||||
updated_playlist = await playlist_service.set_current_playlist(
|
||||
test_playlist_id, user_id
|
||||
)
|
||||
|
||||
assert updated_playlist.is_current is True
|
||||
|
||||
# Verify old current is no longer current
|
||||
old_current = await playlist_service.get_playlist_by_id(current_playlist_id)
|
||||
assert old_current.is_current is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unset_current_playlist_sets_main_as_current(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test unsetting current playlist falls back to main playlist."""
|
||||
# Create test playlists within this test
|
||||
user_id = test_user.id
|
||||
current_playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Current Playlist",
|
||||
description="Currently active playlist",
|
||||
is_main=False,
|
||||
is_current=True,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(current_playlist)
|
||||
|
||||
main_playlist = Playlist(
|
||||
user_id=None,
|
||||
name="Main Playlist",
|
||||
description="Main playlist",
|
||||
is_main=True,
|
||||
is_current=False,
|
||||
is_deletable=False,
|
||||
)
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(current_playlist)
|
||||
await test_session.refresh(main_playlist)
|
||||
|
||||
# Extract IDs before async calls
|
||||
current_playlist_id = current_playlist.id
|
||||
main_playlist_id = main_playlist.id
|
||||
|
||||
# Verify initial state
|
||||
assert current_playlist.is_current is True
|
||||
|
||||
# Unset current playlist
|
||||
await playlist_service.unset_current_playlist(user_id)
|
||||
|
||||
# Verify get_current_playlist returns main playlist as fallback
|
||||
current = await playlist_service.get_current_playlist(user_id)
|
||||
assert current.id == main_playlist_id
|
||||
assert current.is_main is True
|
||||
|
||||
# Verify old current is no longer current
|
||||
old_current = await playlist_service.get_playlist_by_id(current_playlist_id)
|
||||
assert old_current.is_current is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_playlist_stats(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test getting playlist statistics."""
|
||||
# Create test playlist and sound within this test
|
||||
user_id = test_user.id
|
||||
playlist = Playlist(
|
||||
user_id=user_id,
|
||||
name="Test Playlist",
|
||||
description="A test playlist",
|
||||
genre="test",
|
||||
is_main=False,
|
||||
is_current=False,
|
||||
is_deletable=True,
|
||||
)
|
||||
test_session.add(playlist)
|
||||
|
||||
sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash="test_hash",
|
||||
play_count=10,
|
||||
)
|
||||
test_session.add(sound)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(playlist)
|
||||
await test_session.refresh(sound)
|
||||
|
||||
# Extract IDs before async calls
|
||||
playlist_id = playlist.id
|
||||
sound_id = sound.id
|
||||
|
||||
# Initially empty playlist
|
||||
stats = await playlist_service.get_playlist_stats(playlist_id)
|
||||
assert stats["sound_count"] == 0
|
||||
assert stats["total_duration_ms"] == 0
|
||||
assert stats["total_play_count"] == 0
|
||||
|
||||
# Add sound to playlist
|
||||
await playlist_service.add_sound_to_playlist(
|
||||
playlist_id=playlist_id,
|
||||
sound_id=sound_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
# Check stats again
|
||||
stats = await playlist_service.get_playlist_stats(playlist_id)
|
||||
assert stats["sound_count"] == 1
|
||||
assert stats["total_duration_ms"] == 5000 # From test_sound fixture
|
||||
assert stats["total_play_count"] == 10 # From test_sound fixture
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_sound_to_main_playlist(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test adding sound to main playlist."""
|
||||
# Create test sound and main playlist within this test
|
||||
user_id = test_user.id
|
||||
sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash="test_hash",
|
||||
play_count=10,
|
||||
)
|
||||
test_session.add(sound)
|
||||
|
||||
main_playlist = Playlist(
|
||||
user_id=None,
|
||||
name="Main Playlist",
|
||||
description="Main playlist",
|
||||
is_main=True,
|
||||
is_current=False,
|
||||
is_deletable=False,
|
||||
)
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(sound)
|
||||
await test_session.refresh(main_playlist)
|
||||
|
||||
# Extract IDs before async calls
|
||||
sound_id = sound.id
|
||||
main_playlist_id = main_playlist.id
|
||||
|
||||
# Add sound to main playlist
|
||||
await playlist_service.add_sound_to_main_playlist(sound_id, user_id)
|
||||
|
||||
# Verify sound was added to main playlist
|
||||
sounds = await playlist_service.get_playlist_sounds(main_playlist_id)
|
||||
assert len(sounds) == 1
|
||||
assert sounds[0].id == sound_id
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_sound_to_main_playlist_already_exists(
|
||||
self,
|
||||
playlist_service: PlaylistService,
|
||||
test_user: User,
|
||||
test_session: AsyncSession,
|
||||
) -> None:
|
||||
"""Test adding sound to main playlist when it already exists (should not duplicate)."""
|
||||
# Create test sound and main playlist within this test
|
||||
user_id = test_user.id
|
||||
sound = Sound(
|
||||
name="Test Sound",
|
||||
filename="test.mp3",
|
||||
type="SDB",
|
||||
duration=5000,
|
||||
size=1024,
|
||||
hash="test_hash",
|
||||
play_count=10,
|
||||
)
|
||||
test_session.add(sound)
|
||||
|
||||
main_playlist = Playlist(
|
||||
user_id=None,
|
||||
name="Main Playlist",
|
||||
description="Main playlist",
|
||||
is_main=True,
|
||||
is_current=False,
|
||||
is_deletable=False,
|
||||
)
|
||||
test_session.add(main_playlist)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(sound)
|
||||
await test_session.refresh(main_playlist)
|
||||
|
||||
# Extract IDs before async calls
|
||||
sound_id = sound.id
|
||||
main_playlist_id = main_playlist.id
|
||||
|
||||
# Add sound to main playlist twice
|
||||
await playlist_service.add_sound_to_main_playlist(sound_id, user_id)
|
||||
await playlist_service.add_sound_to_main_playlist(sound_id, user_id)
|
||||
|
||||
# Verify sound is only added once
|
||||
sounds = await playlist_service.get_playlist_sounds(main_playlist_id)
|
||||
assert len(sounds) == 1
|
||||
assert sounds[0].id == sound_id
|
||||
@@ -97,7 +97,9 @@ class TestSocketManager:
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.socket.extract_access_token_from_cookies")
|
||||
@patch("app.services.socket.JWTUtils.decode_access_token")
|
||||
async def test_connect_handler_success(self, mock_decode, mock_extract_token, socket_manager, mock_sio):
|
||||
async def test_connect_handler_success(
|
||||
self, mock_decode, mock_extract_token, socket_manager, mock_sio
|
||||
):
|
||||
"""Test successful connection with valid token."""
|
||||
# Setup mocks
|
||||
mock_extract_token.return_value = "valid_token"
|
||||
@@ -130,7 +132,9 @@ class TestSocketManager:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.socket.extract_access_token_from_cookies")
|
||||
async def test_connect_handler_no_token(self, mock_extract_token, socket_manager, mock_sio):
|
||||
async def test_connect_handler_no_token(
|
||||
self, mock_extract_token, socket_manager, mock_sio
|
||||
):
|
||||
"""Test connection with no access token."""
|
||||
# Setup mocks
|
||||
mock_extract_token.return_value = None
|
||||
@@ -162,7 +166,9 @@ class TestSocketManager:
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.socket.extract_access_token_from_cookies")
|
||||
@patch("app.services.socket.JWTUtils.decode_access_token")
|
||||
async def test_connect_handler_invalid_token(self, mock_decode, mock_extract_token, socket_manager, mock_sio):
|
||||
async def test_connect_handler_invalid_token(
|
||||
self, mock_decode, mock_extract_token, socket_manager, mock_sio
|
||||
):
|
||||
"""Test connection with invalid token."""
|
||||
# Setup mocks
|
||||
mock_extract_token.return_value = "invalid_token"
|
||||
@@ -195,7 +201,9 @@ class TestSocketManager:
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.services.socket.extract_access_token_from_cookies")
|
||||
@patch("app.services.socket.JWTUtils.decode_access_token")
|
||||
async def test_connect_handler_missing_user_id(self, mock_decode, mock_extract_token, socket_manager, mock_sio):
|
||||
async def test_connect_handler_missing_user_id(
|
||||
self, mock_decode, mock_extract_token, socket_manager, mock_sio
|
||||
):
|
||||
"""Test connection with token missing user ID."""
|
||||
# Setup mocks
|
||||
mock_extract_token.return_value = "token_without_user_id"
|
||||
|
||||
@@ -182,7 +182,6 @@ class TestSoundNormalizerService:
|
||||
"app.services.sound_normalizer.get_file_hash", return_value="new_hash"
|
||||
),
|
||||
):
|
||||
|
||||
# Setup path mocks
|
||||
mock_orig_path.return_value = Path("/fake/original.mp3")
|
||||
mock_norm_path.return_value = Path("/fake/normalized.mp3")
|
||||
@@ -256,7 +255,6 @@ class TestSoundNormalizerService:
|
||||
"app.services.sound_normalizer.get_file_hash", return_value="norm_hash"
|
||||
),
|
||||
):
|
||||
|
||||
# Setup path mocks
|
||||
mock_orig_path.return_value = Path("/fake/original.mp3")
|
||||
mock_norm_path.return_value = Path("/fake/normalized.mp3")
|
||||
@@ -294,7 +292,6 @@ class TestSoundNormalizerService:
|
||||
patch.object(normalizer_service, "_get_original_path") as mock_orig_path,
|
||||
patch.object(normalizer_service, "_get_normalized_path") as mock_norm_path,
|
||||
):
|
||||
|
||||
# Setup path mocks
|
||||
mock_orig_path.return_value = Path("/fake/original.mp3")
|
||||
mock_norm_path.return_value = Path("/fake/normalized.mp3")
|
||||
@@ -306,7 +303,6 @@ class TestSoundNormalizerService:
|
||||
normalizer_service, "_normalize_audio_two_pass"
|
||||
) as mock_normalize,
|
||||
):
|
||||
|
||||
mock_normalize.side_effect = Exception("Normalization failed")
|
||||
|
||||
result = await normalizer_service.normalize_sound(sound)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user