Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Tests for task handlers."""
|
||||
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
@@ -21,7 +22,7 @@ class TestTaskHandlerRegistry:
|
||||
@pytest.fixture
|
||||
def mock_player_service(self):
|
||||
"""Create mock player service."""
|
||||
return MagicMock()
|
||||
return AsyncMock()
|
||||
|
||||
@pytest.fixture
|
||||
def task_registry(
|
||||
@@ -31,8 +32,11 @@ class TestTaskHandlerRegistry:
|
||||
mock_player_service,
|
||||
) -> TaskHandlerRegistry:
|
||||
"""Create task handler registry fixture."""
|
||||
def mock_db_session_factory():
|
||||
return db_session
|
||||
return TaskHandlerRegistry(
|
||||
db_session,
|
||||
mock_db_session_factory,
|
||||
mock_credit_service,
|
||||
mock_player_service,
|
||||
)
|
||||
@@ -46,7 +50,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="Unknown Task",
|
||||
task_type="UNKNOWN_TYPE", # Invalid type
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
)
|
||||
|
||||
with pytest.raises(TaskExecutionError, match="No handler registered"):
|
||||
@@ -61,7 +65,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="Daily Credit Recharge",
|
||||
task_type=TaskType.CREDIT_RECHARGE,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={},
|
||||
)
|
||||
|
||||
@@ -84,7 +88,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="User Credit Recharge",
|
||||
task_type=TaskType.CREDIT_RECHARGE,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={"user_id": str(test_user_id)},
|
||||
)
|
||||
|
||||
@@ -107,7 +111,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="User Credit Recharge",
|
||||
task_type=TaskType.CREDIT_RECHARGE,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={"user_id": test_user_id}, # UUID object instead of string
|
||||
)
|
||||
|
||||
@@ -118,13 +122,13 @@ class TestTaskHandlerRegistry:
|
||||
async def test_handle_play_sound_success(
|
||||
self,
|
||||
task_registry: TaskHandlerRegistry,
|
||||
test_sound_id: uuid.UUID,
|
||||
test_sound_id: int,
|
||||
):
|
||||
"""Test successful play sound task handling."""
|
||||
task = ScheduledTask(
|
||||
name="Play Sound",
|
||||
task_type=TaskType.PLAY_SOUND,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={"sound_id": str(test_sound_id)},
|
||||
)
|
||||
|
||||
@@ -134,8 +138,9 @@ class TestTaskHandlerRegistry:
|
||||
mock_sound.filename = "test_sound.mp3"
|
||||
|
||||
with patch.object(task_registry.sound_repository, "get_by_id", return_value=mock_sound):
|
||||
with patch("app.services.vlc_player.VLCPlayerService") as mock_vlc_class:
|
||||
with patch("app.services.task_handlers.VLCPlayerService") as mock_vlc_class:
|
||||
mock_vlc_service = AsyncMock()
|
||||
mock_vlc_service.play_sound.return_value = True
|
||||
mock_vlc_class.return_value = mock_vlc_service
|
||||
|
||||
await task_registry.execute_task(task)
|
||||
@@ -151,7 +156,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="Play Sound",
|
||||
task_type=TaskType.PLAY_SOUND,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={}, # Missing sound_id
|
||||
)
|
||||
|
||||
@@ -166,7 +171,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="Play Sound",
|
||||
task_type=TaskType.PLAY_SOUND,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={"sound_id": "invalid-uuid"},
|
||||
)
|
||||
|
||||
@@ -176,13 +181,13 @@ class TestTaskHandlerRegistry:
|
||||
async def test_handle_play_sound_not_found(
|
||||
self,
|
||||
task_registry: TaskHandlerRegistry,
|
||||
test_sound_id: uuid.UUID,
|
||||
test_sound_id: int,
|
||||
):
|
||||
"""Test play sound task with non-existent sound."""
|
||||
task = ScheduledTask(
|
||||
name="Play Sound",
|
||||
task_type=TaskType.PLAY_SOUND,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={"sound_id": str(test_sound_id)},
|
||||
)
|
||||
|
||||
@@ -193,13 +198,13 @@ class TestTaskHandlerRegistry:
|
||||
async def test_handle_play_sound_uuid_parameter(
|
||||
self,
|
||||
task_registry: TaskHandlerRegistry,
|
||||
test_sound_id: uuid.UUID,
|
||||
test_sound_id: int,
|
||||
):
|
||||
"""Test play sound task with UUID parameter (not string)."""
|
||||
task = ScheduledTask(
|
||||
name="Play Sound",
|
||||
task_type=TaskType.PLAY_SOUND,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={"sound_id": test_sound_id}, # UUID object
|
||||
)
|
||||
|
||||
@@ -207,7 +212,7 @@ class TestTaskHandlerRegistry:
|
||||
mock_sound.filename = "test_sound.mp3"
|
||||
|
||||
with patch.object(task_registry.sound_repository, "get_by_id", return_value=mock_sound):
|
||||
with patch("app.services.vlc_player.VLCPlayerService") as mock_vlc_class:
|
||||
with patch("app.services.task_handlers.VLCPlayerService") as mock_vlc_class:
|
||||
mock_vlc_service = AsyncMock()
|
||||
mock_vlc_class.return_value = mock_vlc_service
|
||||
|
||||
@@ -219,13 +224,13 @@ class TestTaskHandlerRegistry:
|
||||
self,
|
||||
task_registry: TaskHandlerRegistry,
|
||||
mock_player_service,
|
||||
test_playlist_id: uuid.UUID,
|
||||
test_playlist_id: int,
|
||||
):
|
||||
"""Test successful play playlist task handling."""
|
||||
task = ScheduledTask(
|
||||
name="Play Playlist",
|
||||
task_type=TaskType.PLAY_PLAYLIST,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={
|
||||
"playlist_id": str(test_playlist_id),
|
||||
"play_mode": "loop",
|
||||
@@ -244,20 +249,20 @@ class TestTaskHandlerRegistry:
|
||||
task_registry.playlist_repository.get_by_id.assert_called_once_with(test_playlist_id)
|
||||
mock_player_service.load_playlist.assert_called_once_with(test_playlist_id)
|
||||
mock_player_service.set_mode.assert_called_once_with("loop")
|
||||
mock_player_service.set_shuffle.assert_called_once_with(True)
|
||||
mock_player_service.set_shuffle.assert_called_once_with(shuffle=True)
|
||||
mock_player_service.play.assert_called_once()
|
||||
|
||||
async def test_handle_play_playlist_minimal_parameters(
|
||||
self,
|
||||
task_registry: TaskHandlerRegistry,
|
||||
mock_player_service,
|
||||
test_playlist_id: uuid.UUID,
|
||||
test_playlist_id: int,
|
||||
):
|
||||
"""Test play playlist task with minimal parameters."""
|
||||
task = ScheduledTask(
|
||||
name="Play Playlist",
|
||||
task_type=TaskType.PLAY_PLAYLIST,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={"playlist_id": str(test_playlist_id)},
|
||||
)
|
||||
|
||||
@@ -269,7 +274,7 @@ class TestTaskHandlerRegistry:
|
||||
|
||||
# Should use default values
|
||||
mock_player_service.set_mode.assert_called_once_with("continuous")
|
||||
mock_player_service.set_shuffle.assert_called_once_with(False)
|
||||
mock_player_service.set_shuffle.assert_not_called()
|
||||
|
||||
async def test_handle_play_playlist_missing_playlist_id(
|
||||
self,
|
||||
@@ -279,7 +284,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="Play Playlist",
|
||||
task_type=TaskType.PLAY_PLAYLIST,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={}, # Missing playlist_id
|
||||
)
|
||||
|
||||
@@ -294,7 +299,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="Play Playlist",
|
||||
task_type=TaskType.PLAY_PLAYLIST,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={"playlist_id": "invalid-uuid"},
|
||||
)
|
||||
|
||||
@@ -304,13 +309,13 @@ class TestTaskHandlerRegistry:
|
||||
async def test_handle_play_playlist_not_found(
|
||||
self,
|
||||
task_registry: TaskHandlerRegistry,
|
||||
test_playlist_id: uuid.UUID,
|
||||
test_playlist_id: int,
|
||||
):
|
||||
"""Test play playlist task with non-existent playlist."""
|
||||
task = ScheduledTask(
|
||||
name="Play Playlist",
|
||||
task_type=TaskType.PLAY_PLAYLIST,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={"playlist_id": str(test_playlist_id)},
|
||||
)
|
||||
|
||||
@@ -322,7 +327,7 @@ class TestTaskHandlerRegistry:
|
||||
self,
|
||||
task_registry: TaskHandlerRegistry,
|
||||
mock_player_service,
|
||||
test_playlist_id: uuid.UUID,
|
||||
test_playlist_id: int,
|
||||
):
|
||||
"""Test play playlist task with various valid play modes."""
|
||||
mock_playlist = MagicMock()
|
||||
@@ -334,7 +339,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="Play Playlist",
|
||||
task_type=TaskType.PLAY_PLAYLIST,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={
|
||||
"playlist_id": str(test_playlist_id),
|
||||
"play_mode": mode,
|
||||
@@ -352,13 +357,13 @@ class TestTaskHandlerRegistry:
|
||||
self,
|
||||
task_registry: TaskHandlerRegistry,
|
||||
mock_player_service,
|
||||
test_playlist_id: uuid.UUID,
|
||||
test_playlist_id: int,
|
||||
):
|
||||
"""Test play playlist task with invalid play mode."""
|
||||
task = ScheduledTask(
|
||||
name="Play Playlist",
|
||||
task_type=TaskType.PLAY_PLAYLIST,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={
|
||||
"playlist_id": str(test_playlist_id),
|
||||
"play_mode": "invalid_mode",
|
||||
@@ -386,7 +391,7 @@ class TestTaskHandlerRegistry:
|
||||
task = ScheduledTask(
|
||||
name="Failing Task",
|
||||
task_type=TaskType.CREDIT_RECHARGE,
|
||||
scheduled_at=datetime.utcnow(),
|
||||
scheduled_at=datetime.now(tz=UTC),
|
||||
parameters={},
|
||||
)
|
||||
|
||||
@@ -403,13 +408,17 @@ class TestTaskHandlerRegistry:
|
||||
mock_player_service,
|
||||
):
|
||||
"""Test task registry initialization."""
|
||||
def mock_db_session_factory():
|
||||
return db_session
|
||||
registry = TaskHandlerRegistry(
|
||||
db_session,
|
||||
mock_db_session_factory,
|
||||
mock_credit_service,
|
||||
mock_player_service,
|
||||
)
|
||||
|
||||
assert registry.db_session == db_session
|
||||
assert registry.db_session_factory == mock_db_session_factory
|
||||
assert registry.credit_service == mock_credit_service
|
||||
assert registry.player_service == mock_player_service
|
||||
assert registry.sound_repository is not None
|
||||
|
||||
Reference in New Issue
Block a user