Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -405,8 +405,17 @@ class TestVLCPlayerService:
|
||||
async def test_record_play_count_success(self, vlc_service_with_db) -> None:
|
||||
"""Test successful play count recording."""
|
||||
# Mock session and repositories
|
||||
mock_session = AsyncMock()
|
||||
vlc_service_with_db.db_session_factory.return_value = mock_session
|
||||
mock_session = MagicMock()
|
||||
# Make async methods async mocks but keep sync methods as regular mocks
|
||||
mock_session.commit = AsyncMock()
|
||||
mock_session.refresh = AsyncMock()
|
||||
mock_session.close = AsyncMock()
|
||||
|
||||
# Mock the context manager behavior
|
||||
mock_context_manager = AsyncMock()
|
||||
mock_context_manager.__aenter__ = AsyncMock(return_value=mock_session)
|
||||
mock_context_manager.__aexit__ = AsyncMock(return_value=None)
|
||||
vlc_service_with_db.db_session_factory.return_value = mock_context_manager
|
||||
|
||||
mock_sound_repo = AsyncMock()
|
||||
mock_user_repo = AsyncMock()
|
||||
@@ -449,18 +458,18 @@ class TestVLCPlayerService:
|
||||
|
||||
# Verify sound repository calls
|
||||
mock_sound_repo.get_by_id.assert_called_once_with(1)
|
||||
mock_sound_repo.update.assert_called_once_with(
|
||||
test_sound,
|
||||
{"play_count": 1},
|
||||
)
|
||||
|
||||
# Verify user repository calls
|
||||
mock_user_repo.get_by_id.assert_called_once_with(1)
|
||||
|
||||
# Verify session operations
|
||||
mock_session.add.assert_called_once()
|
||||
mock_session.commit.assert_called_once()
|
||||
mock_session.close.assert_called_once()
|
||||
# Verify session operations (called twice: once for sound, once for sound_played)
|
||||
assert mock_session.add.call_count == 2
|
||||
# Commit is called twice: once after updating sound, once after adding sound_played
|
||||
assert mock_session.commit.call_count == 2
|
||||
# Context manager handles session cleanup, so no explicit close() call
|
||||
|
||||
# Verify the sound's play count was incremented
|
||||
assert test_sound.play_count == 1
|
||||
|
||||
# Verify socket broadcast
|
||||
mock_socket.broadcast_to_all.assert_called_once_with(
|
||||
@@ -488,8 +497,17 @@ class TestVLCPlayerService:
|
||||
) -> None:
|
||||
"""Test play count recording always creates a new SoundPlayed record."""
|
||||
# Mock session and repositories
|
||||
mock_session = AsyncMock()
|
||||
vlc_service_with_db.db_session_factory.return_value = mock_session
|
||||
mock_session = MagicMock()
|
||||
# Make async methods async mocks but keep sync methods as regular mocks
|
||||
mock_session.commit = AsyncMock()
|
||||
mock_session.refresh = AsyncMock()
|
||||
mock_session.close = AsyncMock()
|
||||
|
||||
# Mock the context manager behavior
|
||||
mock_context_manager = AsyncMock()
|
||||
mock_context_manager.__aenter__ = AsyncMock(return_value=mock_session)
|
||||
mock_context_manager.__aexit__ = AsyncMock(return_value=None)
|
||||
vlc_service_with_db.db_session_factory.return_value = mock_context_manager
|
||||
|
||||
mock_sound_repo = AsyncMock()
|
||||
mock_user_repo = AsyncMock()
|
||||
@@ -530,17 +548,19 @@ class TestVLCPlayerService:
|
||||
|
||||
await vlc_service_with_db._record_play_count(1, "Test Sound")
|
||||
|
||||
# Verify sound play count was updated
|
||||
mock_sound_repo.update.assert_called_once_with(
|
||||
test_sound,
|
||||
{"play_count": 6},
|
||||
)
|
||||
# Verify sound repository calls
|
||||
mock_sound_repo.get_by_id.assert_called_once_with(1)
|
||||
|
||||
# Verify new SoundPlayed record was always added
|
||||
mock_session.add.assert_called_once()
|
||||
# Verify user repository calls
|
||||
mock_user_repo.get_by_id.assert_called_once_with(1)
|
||||
|
||||
# Verify commit happened
|
||||
mock_session.commit.assert_called_once()
|
||||
# Verify session operations (called twice: once for sound, once for sound_played)
|
||||
assert mock_session.add.call_count == 2
|
||||
# Commit is called twice: once after updating sound, once after adding sound_played
|
||||
assert mock_session.commit.call_count == 2
|
||||
|
||||
# Verify the sound's play count was incremented from 5 to 6
|
||||
assert test_sound.play_count == 6
|
||||
|
||||
def test_uses_shared_sound_path_utility(self, vlc_service, sample_sound) -> None:
|
||||
"""Test that VLC service uses the shared sound path utility."""
|
||||
|
||||
Reference in New Issue
Block a user