Refactor sound and extraction services to include user and timestamp fields
- Updated ExtractionInfo to include user_id, created_at, and updated_at fields. - Modified ExtractionService to return user and timestamp information in extraction responses. - Enhanced sound serialization in PlayerState to include extraction URL if available. - Adjusted PlaylistRepository to load sound extractions when retrieving playlist sounds. - Added tests for new fields in extraction and sound endpoints, ensuring proper response structure. - Created new test file endpoints for sound downloads and thumbnail retrievals, including success and error cases. - Refactored various test cases for consistency and clarity, ensuring proper mocking and assertions.
This commit is contained in:
@@ -94,6 +94,48 @@ class TestPlayerState:
|
||||
assert result["type"] == "SDB"
|
||||
assert result["thumbnail"] == "test.jpg"
|
||||
assert result["play_count"] == 5
|
||||
assert result["extract_url"] is None
|
||||
|
||||
def test_serialize_sound_with_extraction_url(self) -> None:
|
||||
"""Test serializing a sound object with extraction URL."""
|
||||
from app.models.extraction import Extraction
|
||||
|
||||
state = PlayerState()
|
||||
sound = Sound(
|
||||
id=1,
|
||||
name="Test Song",
|
||||
filename="test.mp3",
|
||||
duration=30000,
|
||||
size=1024,
|
||||
type="EXT",
|
||||
thumbnail="test.jpg",
|
||||
play_count=5,
|
||||
)
|
||||
|
||||
# Mock extraction relationship
|
||||
extraction = Extraction(
|
||||
id=1,
|
||||
url="https://www.youtube.com/watch?v=test",
|
||||
service="youtube",
|
||||
service_id="test",
|
||||
title="Test Song",
|
||||
status="completed",
|
||||
user_id=1,
|
||||
sound_id=1,
|
||||
)
|
||||
sound.extractions = [extraction]
|
||||
|
||||
result = state._serialize_sound(sound)
|
||||
|
||||
assert result["id"] == 1
|
||||
assert result["name"] == "Test Song"
|
||||
assert result["filename"] == "test.mp3"
|
||||
assert result["duration"] == 30000
|
||||
assert result["size"] == 1024
|
||||
assert result["type"] == "EXT"
|
||||
assert result["thumbnail"] == "test.jpg"
|
||||
assert result["play_count"] == 5
|
||||
assert result["extract_url"] == "https://www.youtube.com/watch?v=test"
|
||||
|
||||
def test_serialize_sound_with_none(self) -> None:
|
||||
"""Test serializing None sound."""
|
||||
@@ -132,13 +174,18 @@ class TestPlayerService:
|
||||
|
||||
@pytest.fixture
|
||||
def player_service(
|
||||
self, mock_db_session_factory, mock_vlc_instance, mock_socket_manager,
|
||||
self,
|
||||
mock_db_session_factory,
|
||||
mock_vlc_instance,
|
||||
mock_socket_manager,
|
||||
):
|
||||
"""Create a player service instance for testing."""
|
||||
return PlayerService(mock_db_session_factory)
|
||||
|
||||
def test_init_creates_player_service(
|
||||
self, mock_db_session_factory, mock_vlc_instance,
|
||||
self,
|
||||
mock_db_session_factory,
|
||||
mock_vlc_instance,
|
||||
) -> None:
|
||||
"""Test that player service initializes correctly."""
|
||||
with patch("app.services.player.socket_manager"):
|
||||
@@ -157,7 +204,9 @@ class TestPlayerService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_initializes_service(
|
||||
self, player_service, mock_vlc_instance,
|
||||
self,
|
||||
player_service,
|
||||
mock_vlc_instance,
|
||||
) -> None:
|
||||
"""Test that start method initializes the service."""
|
||||
with patch.object(player_service, "reload_playlist", new_callable=AsyncMock):
|
||||
@@ -204,7 +253,9 @@ class TestPlayerService:
|
||||
mock_path.return_value = mock_file_path
|
||||
|
||||
with patch.object(
|
||||
player_service, "_broadcast_state", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_broadcast_state",
|
||||
new_callable=AsyncMock,
|
||||
):
|
||||
mock_media = Mock()
|
||||
player_service._vlc_instance.media_new.return_value = mock_media
|
||||
@@ -261,7 +312,9 @@ class TestPlayerService:
|
||||
player_service.state.status = PlayerStatus.STOPPED
|
||||
|
||||
with patch.object(
|
||||
player_service, "_broadcast_state", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_broadcast_state",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_broadcast:
|
||||
await player_service.pause()
|
||||
|
||||
@@ -275,10 +328,14 @@ class TestPlayerService:
|
||||
player_service.state.current_sound_position = 5000
|
||||
|
||||
with patch.object(
|
||||
player_service, "_process_play_count", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_process_play_count",
|
||||
new_callable=AsyncMock,
|
||||
):
|
||||
with patch.object(
|
||||
player_service, "_broadcast_state", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_broadcast_state",
|
||||
new_callable=AsyncMock,
|
||||
):
|
||||
await player_service.stop_playback()
|
||||
|
||||
@@ -329,7 +386,9 @@ class TestPlayerService:
|
||||
player_service.state.status = PlayerStatus.STOPPED
|
||||
|
||||
with patch.object(
|
||||
player_service, "_broadcast_state", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_broadcast_state",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_broadcast:
|
||||
await player_service.seek(15000)
|
||||
|
||||
@@ -391,7 +450,9 @@ class TestPlayerService:
|
||||
mock_repo.get_playlist_sounds.return_value = mock_sounds
|
||||
|
||||
with patch.object(
|
||||
player_service, "_broadcast_state", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_broadcast_state",
|
||||
new_callable=AsyncMock,
|
||||
):
|
||||
await player_service.reload_playlist()
|
||||
|
||||
@@ -415,7 +476,9 @@ class TestPlayerService:
|
||||
sounds = [sound1, sound2]
|
||||
|
||||
with patch.object(
|
||||
player_service, "_stop_playback", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_stop_playback",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_stop:
|
||||
await player_service._handle_playlist_id_changed(1, 2, sounds)
|
||||
|
||||
@@ -427,13 +490,16 @@ class TestPlayerService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_playlist_id_changed_empty_playlist(
|
||||
self, player_service,
|
||||
self,
|
||||
player_service,
|
||||
) -> None:
|
||||
"""Test handling playlist ID change with empty playlist."""
|
||||
player_service.state.status = PlayerStatus.PLAYING
|
||||
|
||||
with patch.object(
|
||||
player_service, "_stop_playback", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_stop_playback",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_stop:
|
||||
await player_service._handle_playlist_id_changed(1, 2, [])
|
||||
|
||||
@@ -444,7 +510,8 @@ class TestPlayerService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_same_playlist_track_exists_same_index(
|
||||
self, player_service,
|
||||
self,
|
||||
player_service,
|
||||
) -> None:
|
||||
"""Test handling same playlist when track exists at same index."""
|
||||
sound1 = Sound(id=1, name="Song 1", filename="song1.mp3", duration=30000)
|
||||
@@ -461,7 +528,8 @@ class TestPlayerService:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_same_playlist_track_exists_different_index(
|
||||
self, player_service,
|
||||
self,
|
||||
player_service,
|
||||
) -> None:
|
||||
"""Test handling same playlist when track exists at different index."""
|
||||
sound1 = Sound(id=2, name="Song 2", filename="song2.mp3", duration=45000)
|
||||
@@ -483,7 +551,9 @@ class TestPlayerService:
|
||||
sounds = [sound1, sound2] # Track with ID 1 is missing
|
||||
|
||||
with patch.object(
|
||||
player_service, "_handle_track_removed", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_handle_track_removed",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_removed:
|
||||
await player_service._handle_same_playlist_track_check(1, 0, sounds)
|
||||
mock_removed.assert_called_once_with(1, sounds)
|
||||
@@ -496,7 +566,9 @@ class TestPlayerService:
|
||||
sounds = [sound1]
|
||||
|
||||
with patch.object(
|
||||
player_service, "_stop_playback", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_stop_playback",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_stop:
|
||||
await player_service._handle_track_removed(1, sounds)
|
||||
|
||||
@@ -511,7 +583,9 @@ class TestPlayerService:
|
||||
player_service.state.status = PlayerStatus.PLAYING
|
||||
|
||||
with patch.object(
|
||||
player_service, "_stop_playback", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_stop_playback",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_stop:
|
||||
await player_service._handle_track_removed(1, [])
|
||||
|
||||
@@ -609,10 +683,14 @@ class TestPlayerService:
|
||||
mock_repo.get_playlist_sounds.return_value = mock_sounds
|
||||
|
||||
with patch.object(
|
||||
player_service, "_stop_playback", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_stop_playback",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_stop:
|
||||
with patch.object(
|
||||
player_service, "_broadcast_state", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_broadcast_state",
|
||||
new_callable=AsyncMock,
|
||||
):
|
||||
await player_service.reload_playlist()
|
||||
|
||||
@@ -652,7 +730,9 @@ class TestPlayerService:
|
||||
mock_repo.get_playlist_sounds.return_value = mock_sounds
|
||||
|
||||
with patch.object(
|
||||
player_service, "_broadcast_state", new_callable=AsyncMock,
|
||||
player_service,
|
||||
"_broadcast_state",
|
||||
new_callable=AsyncMock,
|
||||
):
|
||||
await player_service.reload_playlist()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user