fix tests
This commit is contained in:
@@ -6,7 +6,7 @@ error handling, and progress reporting integration.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -15,16 +15,17 @@ from src.server.services.progress_service import ProgressService
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_series_app():
|
||||
def mock_series_app(tmp_path):
|
||||
"""Create a mock SeriesApp instance."""
|
||||
with patch("src.server.services.anime_service.SeriesApp") as mock_class:
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.series_list = []
|
||||
mock_instance.search = MagicMock(return_value=[])
|
||||
mock_instance.ReScan = MagicMock()
|
||||
mock_instance.download = MagicMock(return_value=True)
|
||||
mock_class.return_value = mock_instance
|
||||
yield mock_instance
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.directory_to_search = str(tmp_path)
|
||||
mock_instance.series_list = []
|
||||
mock_instance.search = AsyncMock(return_value=[])
|
||||
mock_instance.rescan = AsyncMock()
|
||||
mock_instance.download = AsyncMock(return_value=True)
|
||||
mock_instance.download_status = None
|
||||
mock_instance.scan_status = None
|
||||
return mock_instance
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -42,8 +43,7 @@ def mock_progress_service():
|
||||
def anime_service(tmp_path, mock_series_app, mock_progress_service):
|
||||
"""Create an AnimeService instance for testing."""
|
||||
return AnimeService(
|
||||
directory=str(tmp_path),
|
||||
max_workers=2,
|
||||
series_app=mock_series_app,
|
||||
progress_service=mock_progress_service,
|
||||
)
|
||||
|
||||
@@ -51,35 +51,38 @@ def anime_service(tmp_path, mock_series_app, mock_progress_service):
|
||||
class TestAnimeServiceInitialization:
|
||||
"""Test AnimeService initialization."""
|
||||
|
||||
def test_initialization_success(self, tmp_path, mock_progress_service):
|
||||
def test_initialization_success(
|
||||
self, mock_series_app, mock_progress_service
|
||||
):
|
||||
"""Test successful service initialization."""
|
||||
with patch("src.server.services.anime_service.SeriesApp"):
|
||||
service = AnimeService(
|
||||
directory=str(tmp_path),
|
||||
max_workers=2,
|
||||
progress_service=mock_progress_service,
|
||||
)
|
||||
|
||||
assert service._directory == str(tmp_path)
|
||||
assert service._executor is not None
|
||||
assert service._progress_service is mock_progress_service
|
||||
service = AnimeService(
|
||||
series_app=mock_series_app,
|
||||
progress_service=mock_progress_service,
|
||||
)
|
||||
|
||||
assert service._app is mock_series_app
|
||||
assert service._progress_service is mock_progress_service
|
||||
|
||||
def test_initialization_failure_raises_error(
|
||||
self, tmp_path, mock_progress_service
|
||||
):
|
||||
"""Test SeriesApp initialization failure raises error."""
|
||||
with patch(
|
||||
"src.server.services.anime_service.SeriesApp"
|
||||
) as mock_class:
|
||||
mock_class.side_effect = Exception("Initialization failed")
|
||||
|
||||
with pytest.raises(
|
||||
AnimeServiceError, match="Initialization failed"
|
||||
):
|
||||
AnimeService(
|
||||
directory=str(tmp_path),
|
||||
progress_service=mock_progress_service,
|
||||
)
|
||||
bad_series_app = MagicMock()
|
||||
bad_series_app.directory_to_search = str(tmp_path)
|
||||
|
||||
# Make event subscription fail
|
||||
def raise_error(*args):
|
||||
raise Exception("Initialization failed")
|
||||
|
||||
bad_series_app.__setattr__ = raise_error
|
||||
|
||||
with pytest.raises(
|
||||
AnimeServiceError, match="Initialization failed"
|
||||
):
|
||||
AnimeService(
|
||||
series_app=bad_series_app,
|
||||
progress_service=mock_progress_service,
|
||||
)
|
||||
|
||||
|
||||
class TestListMissing:
|
||||
@@ -321,12 +324,12 @@ class TestConcurrency:
|
||||
class TestFactoryFunction:
|
||||
"""Test factory function."""
|
||||
|
||||
def test_get_anime_service(self, tmp_path):
|
||||
def test_get_anime_service(self):
|
||||
"""Test get_anime_service factory function."""
|
||||
from src.server.services.anime_service import get_anime_service
|
||||
|
||||
# The factory function doesn't take directory anymore
|
||||
service = get_anime_service()
|
||||
|
||||
with patch("src.server.services.anime_service.SeriesApp"):
|
||||
service = get_anime_service(directory=str(tmp_path))
|
||||
|
||||
assert isinstance(service, AnimeService)
|
||||
assert service._directory == str(tmp_path)
|
||||
assert isinstance(service, AnimeService)
|
||||
assert service._app is not None
|
||||
|
||||
Reference in New Issue
Block a user