Refactor test cases for improved readability and consistency
All checks were successful
Backend CI / lint (push) Successful in 9m49s
Backend CI / test (push) Successful in 6m15s

- Adjusted function signatures in various test files to enhance clarity by aligning parameters.
- Updated patching syntax for better readability across test cases.
- Improved formatting and spacing in test assertions and mock setups.
- Ensured consistent use of async/await patterns in async test functions.
- Enhanced comments for better understanding of test intentions.
This commit is contained in:
JSC
2025-08-01 20:53:30 +02:00
parent d926779fe4
commit 6068599a47
39 changed files with 691 additions and 286 deletions

View File

@@ -131,11 +131,15 @@ class TestPlayerService:
yield mock
@pytest.fixture
def player_service(self, mock_db_session_factory, mock_vlc_instance, mock_socket_manager):
def player_service(
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) -> None:
def test_init_creates_player_service(
self, mock_db_session_factory, mock_vlc_instance,
) -> None:
"""Test that player service initializes correctly."""
with patch("app.services.player.socket_manager"):
service = PlayerService(mock_db_session_factory)
@@ -152,7 +156,9 @@ class TestPlayerService:
assert service._loop is None
@pytest.mark.asyncio
async def test_start_initializes_service(self, player_service, mock_vlc_instance) -> None:
async def test_start_initializes_service(
self, player_service, mock_vlc_instance,
) -> None:
"""Test that start method initializes the service."""
with patch.object(player_service, "reload_playlist", new_callable=AsyncMock):
await player_service.start()
@@ -197,7 +203,9 @@ class TestPlayerService:
mock_file_path.exists.return_value = True
mock_path.return_value = mock_file_path
with patch.object(player_service, "_broadcast_state", new_callable=AsyncMock):
with patch.object(
player_service, "_broadcast_state", new_callable=AsyncMock,
):
mock_media = Mock()
player_service._vlc_instance.media_new.return_value = mock_media
player_service._player.play.return_value = 0 # Success
@@ -252,7 +260,9 @@ class TestPlayerService:
"""Test pausing when not playing does nothing."""
player_service.state.status = PlayerStatus.STOPPED
with patch.object(player_service, "_broadcast_state", new_callable=AsyncMock) as mock_broadcast:
with patch.object(
player_service, "_broadcast_state", new_callable=AsyncMock,
) as mock_broadcast:
await player_service.pause()
assert player_service.state.status == PlayerStatus.STOPPED
@@ -264,8 +274,12 @@ class TestPlayerService:
player_service.state.status = PlayerStatus.PLAYING
player_service.state.current_sound_position = 5000
with patch.object(player_service, "_process_play_count", new_callable=AsyncMock):
with patch.object(player_service, "_broadcast_state", new_callable=AsyncMock):
with patch.object(
player_service, "_process_play_count", new_callable=AsyncMock,
):
with patch.object(
player_service, "_broadcast_state", new_callable=AsyncMock,
):
await player_service.stop_playback()
assert player_service.state.status == PlayerStatus.STOPPED
@@ -314,7 +328,9 @@ class TestPlayerService:
"""Test seeking when stopped does nothing."""
player_service.state.status = PlayerStatus.STOPPED
with patch.object(player_service, "_broadcast_state", new_callable=AsyncMock) as mock_broadcast:
with patch.object(
player_service, "_broadcast_state", new_callable=AsyncMock,
) as mock_broadcast:
await player_service.seek(15000)
player_service._player.set_position.assert_not_called()
@@ -364,7 +380,9 @@ class TestPlayerService:
mock_playlist = Mock()
mock_playlist.id = 1
mock_playlist.name = "Test Playlist"
mock_repo.get_current_playlist.return_value = mock_playlist # Return current playlist directly
mock_repo.get_current_playlist.return_value = (
mock_playlist # Return current playlist directly
)
# Mock sounds
sound1 = Sound(id=1, name="Song 1", filename="song1.mp3", duration=30000)
@@ -372,7 +390,9 @@ class TestPlayerService:
mock_sounds = [sound1, sound2]
mock_repo.get_playlist_sounds.return_value = mock_sounds
with patch.object(player_service, "_broadcast_state", new_callable=AsyncMock):
with patch.object(
player_service, "_broadcast_state", new_callable=AsyncMock,
):
await player_service.reload_playlist()
assert player_service.state.playlist_id == 1
@@ -394,7 +414,9 @@ class TestPlayerService:
sound2 = Sound(id=2, name="Song 2", filename="song2.mp3", duration=45000)
sounds = [sound1, sound2]
with patch.object(player_service, "_stop_playback", new_callable=AsyncMock) as mock_stop:
with patch.object(
player_service, "_stop_playback", new_callable=AsyncMock,
) as mock_stop:
await player_service._handle_playlist_id_changed(1, 2, sounds)
# Should stop playback and set first track as current
@@ -404,11 +426,15 @@ class TestPlayerService:
assert player_service.state.current_sound_id == 1
@pytest.mark.asyncio
async def test_handle_playlist_id_changed_empty_playlist(self, player_service) -> None:
async def test_handle_playlist_id_changed_empty_playlist(
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) as mock_stop:
with patch.object(
player_service, "_stop_playback", new_callable=AsyncMock,
) as mock_stop:
await player_service._handle_playlist_id_changed(1, 2, [])
mock_stop.assert_called_once()
@@ -417,7 +443,9 @@ class TestPlayerService:
assert player_service.state.current_sound_id is None
@pytest.mark.asyncio
async def test_handle_same_playlist_track_exists_same_index(self, player_service) -> None:
async def test_handle_same_playlist_track_exists_same_index(
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)
sound2 = Sound(id=2, name="Song 2", filename="song2.mp3", duration=45000)
@@ -426,11 +454,15 @@ class TestPlayerService:
await player_service._handle_same_playlist_track_check(1, 0, sounds)
# Should update sound object reference but keep same index
assert player_service.state.current_sound_index == 0 # Should be set to 0 from new_index
assert (
player_service.state.current_sound_index == 0
) # Should be set to 0 from new_index
assert player_service.state.current_sound == sound1
@pytest.mark.asyncio
async def test_handle_same_playlist_track_exists_different_index(self, player_service) -> None:
async def test_handle_same_playlist_track_exists_different_index(
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)
sound2 = Sound(id=1, name="Song 1", filename="song1.mp3", duration=30000)
@@ -450,7 +482,9 @@ class TestPlayerService:
sound2 = Sound(id=3, name="Song 3", filename="song3.mp3", duration=60000)
sounds = [sound1, sound2] # Track with ID 1 is missing
with patch.object(player_service, "_handle_track_removed", new_callable=AsyncMock) as mock_removed:
with patch.object(
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)
@@ -461,7 +495,9 @@ class TestPlayerService:
sound1 = Sound(id=2, name="Song 2", filename="song2.mp3", duration=45000)
sounds = [sound1]
with patch.object(player_service, "_stop_playback", new_callable=AsyncMock) as mock_stop:
with patch.object(
player_service, "_stop_playback", new_callable=AsyncMock,
) as mock_stop:
await player_service._handle_track_removed(1, sounds)
mock_stop.assert_called_once()
@@ -474,7 +510,9 @@ class TestPlayerService:
"""Test handling when current track is removed with empty playlist."""
player_service.state.status = PlayerStatus.PLAYING
with patch.object(player_service, "_stop_playback", new_callable=AsyncMock) as mock_stop:
with patch.object(
player_service, "_stop_playback", new_callable=AsyncMock,
) as mock_stop:
await player_service._handle_track_removed(1, [])
mock_stop.assert_called_once()
@@ -562,14 +600,20 @@ class TestPlayerService:
mock_playlist = Mock()
mock_playlist.id = 2 # Different ID
mock_playlist.name = "New Playlist"
mock_repo.get_current_playlist.return_value = mock_playlist # Return current playlist directly
mock_repo.get_current_playlist.return_value = (
mock_playlist # Return current playlist directly
)
sound1 = Sound(id=1, name="Song 1", filename="song1.mp3", duration=30000)
mock_sounds = [sound1]
mock_repo.get_playlist_sounds.return_value = mock_sounds
with patch.object(player_service, "_stop_playback", new_callable=AsyncMock) as mock_stop:
with patch.object(player_service, "_broadcast_state", new_callable=AsyncMock):
with patch.object(
player_service, "_stop_playback", new_callable=AsyncMock,
) as mock_stop:
with patch.object(
player_service, "_broadcast_state", new_callable=AsyncMock,
):
await player_service.reload_playlist()
# Should stop and reset to first track
@@ -597,7 +641,9 @@ class TestPlayerService:
mock_playlist = Mock()
mock_playlist.id = 1
mock_playlist.name = "Same Playlist"
mock_repo.get_current_playlist.return_value = mock_playlist # Return current playlist directly
mock_repo.get_current_playlist.return_value = (
mock_playlist # Return current playlist directly
)
# Track 2 moved to index 0
sound1 = Sound(id=2, name="Song 2", filename="song2.mp3", duration=45000)
@@ -605,7 +651,9 @@ class TestPlayerService:
mock_sounds = [sound1, sound2] # Track 2 now at index 0
mock_repo.get_playlist_sounds.return_value = mock_sounds
with patch.object(player_service, "_broadcast_state", new_callable=AsyncMock):
with patch.object(
player_service, "_broadcast_state", new_callable=AsyncMock,
):
await player_service.reload_playlist()
# Should update index but keep same track
@@ -614,7 +662,6 @@ class TestPlayerService:
assert player_service.state.current_sound_id == 2 # Same track
assert player_service.state.current_sound == sound1
def test_get_next_index_continuous_mode(self, player_service) -> None:
"""Test getting next index in continuous mode."""
player_service.state.mode = PlayerMode.CONTINUOUS
@@ -734,7 +781,8 @@ class TestPlayerService:
# Verify sound play count was updated
mock_sound_repo.update.assert_called_once_with(
mock_sound, {"play_count": 6},
mock_sound,
{"play_count": 6},
)
# Verify SoundPlayed record was created with None user_id for player