From e76cd3a708b0fe5a6ce4c4009ed583eee8100a4b Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 10 Jun 2026 18:26:09 +0200 Subject: [PATCH] test: remove sync_legacy_series_to_db tests - Removed TestSyncSeriesFromDataFiles class from test_anime_service.py - Updated TestSyncAnimeFolders tests to expect sync_count=0 - Removed TestSyncSeriesToDatabase class from test_data_file_db_sync.py --- tests/integration/test_data_file_db_sync.py | 75 ---------- tests/unit/test_anime_service.py | 145 +------------------- tests/unit/test_initialization_service.py | 15 +- 3 files changed, 6 insertions(+), 229 deletions(-) diff --git a/tests/integration/test_data_file_db_sync.py b/tests/integration/test_data_file_db_sync.py index 6145fb1..7567f79 100644 --- a/tests/integration/test_data_file_db_sync.py +++ b/tests/integration/test_data_file_db_sync.py @@ -110,81 +110,6 @@ class TestGetAllSeriesFromDataFiles: assert len(result) == 0 -class TestSyncSeriesToDatabase: - """Test sync_legacy_series_to_db function from anime_service.""" - - @pytest.mark.asyncio - async def test_sync_with_empty_directory(self): - """Test sync with empty anime directory.""" - from src.server.services.anime_service import sync_legacy_series_to_db - - with tempfile.TemporaryDirectory() as tmp_dir: - with patch('src.server.SeriesApp.Loaders'), \ - patch('src.server.SeriesApp.SerieScanner'): - count = await sync_legacy_series_to_db(tmp_dir) - - assert count == 0 - # Function should complete successfully with no series - - @pytest.mark.asyncio - async def test_sync_adds_new_series_to_database(self): - """Test that sync adds new series to database. - - This is a more realistic test that verifies series data is loaded - from files and the sync function attempts to add them to the DB. - The actual DB interaction is tested in test_add_to_db_creates_record. - """ - from src.server.services.anime_service import sync_legacy_series_to_db - - with tempfile.TemporaryDirectory() as tmp_dir: - # Create test data files - _create_test_data_file( - tmp_dir, - folder="Sync Test Anime", - key="sync-test-anime", - name="Sync Test Anime", - episodes={1: [1, 2]} - ) - - # First verify that we can load the series from files - with patch('src.server.SeriesApp.Loaders'), \ - patch('src.server.SeriesApp.SerieScanner'): - app = SeriesApp(tmp_dir) - series = app.get_all_series_from_data_files() - assert len(series) == 1 - assert series[0].key == "sync-test-anime" - - # Now test that the sync function loads series and handles DB - # gracefully (even if DB operations fail, it should not crash) - with patch('src.server.SeriesApp.Loaders'), \ - patch('src.server.SeriesApp.SerieScanner'): - # The function should return 0 because DB isn't available - # but should not crash - count = await sync_legacy_series_to_db(tmp_dir) - - # Since no real DB, it will fail gracefully - # Function returns 0 when DB operations fail - assert isinstance(count, int) - assert count == 0 - - @pytest.mark.asyncio - async def test_sync_handles_exceptions_gracefully(self): - """Test that sync handles exceptions without crashing.""" - from src.server.services.anime_service import sync_legacy_series_to_db - - # Make SeriesApp raise an exception during initialization - with patch('src.server.SeriesApp.Loaders'), \ - patch('src.server.SeriesApp.SerieScanner'), \ - patch( - 'src.server.SeriesApp.SerieList', - side_effect=Exception("Test error") - ): - count = await sync_legacy_series_to_db("/fake/path") - - assert count == 0 - # Function should complete without crashing - - class TestEndToEndSync: """End-to-end tests for the sync functionality.""" diff --git a/tests/unit/test_anime_service.py b/tests/unit/test_anime_service.py index 81f7d76..09cb88e 100644 --- a/tests/unit/test_anime_service.py +++ b/tests/unit/test_anime_service.py @@ -13,11 +13,7 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest -from src.server.services.anime_service import ( - AnimeService, - AnimeServiceError, - sync_legacy_series_to_db, -) +from src.server.services.anime_service import AnimeService, AnimeServiceError from src.server.services.progress_service import ProgressService @@ -1302,142 +1298,3 @@ class TestGetNFOStatisticsSelfManaged: assert result["with_tmdb_id"] == 40 -class TestSyncSeriesFromDataFiles: - """Test module-level sync_legacy_series_to_db function.""" - - @pytest.mark.asyncio - async def test_sync_adds_new_series(self, tmp_path): - """Should create series for data files not in DB.""" - mock_serie = MagicMock() - mock_serie.key = "new-series" - mock_serie.name = "New Series" - mock_serie.site = "aniworld.to" - mock_serie.folder = "New Series" - mock_serie.episodeDict = {1: [1]} - - mock_session = AsyncMock() - mock_ctx = AsyncMock() - mock_ctx.__aenter__ = AsyncMock(return_value=mock_session) - mock_ctx.__aexit__ = AsyncMock(return_value=False) - - with patch( - "src.server.services.anime_service.SeriesApp" - ) as MockApp, patch( - "src.server.database.connection.get_db_session", - return_value=mock_ctx, - ), patch( - "src.server.database.service.AnimeSeriesService.get_by_key", - new_callable=AsyncMock, - return_value=None, - ), patch( - "src.server.database.service.AnimeSeriesService.create", - new_callable=AsyncMock, - return_value=MagicMock(id=1), - ) as mock_create, patch( - "src.server.database.service.EpisodeService.create", - new_callable=AsyncMock, - ): - mock_app_instance = MagicMock() - mock_app_instance.get_all_series_from_data_files.return_value = [ - mock_serie - ] - MockApp.return_value = mock_app_instance - - count = await sync_legacy_series_to_db(str(tmp_path)) - - assert count == 1 - mock_create.assert_called_once() - - @pytest.mark.asyncio - async def test_sync_skips_existing(self, tmp_path): - """Already-existing series should be skipped.""" - mock_serie = MagicMock() - mock_serie.key = "exists" - mock_serie.name = "Exists" - mock_serie.site = "x" - mock_serie.folder = "Exists" - mock_serie.episodeDict = {} - - mock_session = AsyncMock() - mock_ctx = AsyncMock() - mock_ctx.__aenter__ = AsyncMock(return_value=mock_session) - mock_ctx.__aexit__ = AsyncMock(return_value=False) - - with patch( - "src.server.services.anime_service.SeriesApp" - ) as MockApp, patch( - "src.server.database.connection.get_db_session", - return_value=mock_ctx, - ), patch( - "src.server.database.service.AnimeSeriesService.get_by_key", - new_callable=AsyncMock, - return_value=MagicMock(), - ), patch( - "src.server.database.service.AnimeSeriesService.create", - new_callable=AsyncMock, - ) as mock_create: - mock_app_instance = MagicMock() - mock_app_instance.get_all_series_from_data_files.return_value = [ - mock_serie - ] - MockApp.return_value = mock_app_instance - - count = await sync_legacy_series_to_db(str(tmp_path)) - - assert count == 0 - mock_create.assert_not_called() - - @pytest.mark.asyncio - async def test_sync_no_data_files(self, tmp_path): - """Empty directory should return 0.""" - with patch( - "src.server.services.anime_service.SeriesApp" - ) as MockApp: - mock_app_instance = MagicMock() - mock_app_instance.get_all_series_from_data_files.return_value = [] - MockApp.return_value = mock_app_instance - - count = await sync_legacy_series_to_db(str(tmp_path)) - - assert count == 0 - - @pytest.mark.asyncio - async def test_sync_handles_empty_name(self, tmp_path): - """Series with empty name should use folder as fallback.""" - mock_serie = MagicMock() - mock_serie.key = "no-name" - mock_serie.name = "" - mock_serie.site = "x" - mock_serie.folder = "FallbackFolder" - mock_serie.episodeDict = {} - - mock_session = AsyncMock() - mock_ctx = AsyncMock() - mock_ctx.__aenter__ = AsyncMock(return_value=mock_session) - mock_ctx.__aexit__ = AsyncMock(return_value=False) - - with patch( - "src.server.services.anime_service.SeriesApp" - ) as MockApp, patch( - "src.server.database.connection.get_db_session", - return_value=mock_ctx, - ), patch( - "src.server.database.service.AnimeSeriesService.get_by_key", - new_callable=AsyncMock, - return_value=None, - ), patch( - "src.server.database.service.AnimeSeriesService.create", - new_callable=AsyncMock, - return_value=MagicMock(id=1), - ) as mock_create: - mock_app_instance = MagicMock() - mock_app_instance.get_all_series_from_data_files.return_value = [ - mock_serie - ] - MockApp.return_value = mock_app_instance - - count = await sync_legacy_series_to_db(str(tmp_path)) - - assert count == 1 - # The name should have been set to folder - assert mock_serie.name == "FallbackFolder" diff --git a/tests/unit/test_initialization_service.py b/tests/unit/test_initialization_service.py index 6194adb..2205fc9 100644 --- a/tests/unit/test_initialization_service.py +++ b/tests/unit/test_initialization_service.py @@ -161,14 +161,11 @@ class TestSyncAnimeFolders: async def test_sync_anime_folders_without_progress(self): """Test syncing anime folders without progress service.""" with patch('src.server.services.initialization_service.settings') as mock_settings, \ - patch('src.server.services.initialization_service.os.path.isdir', return_value=True), \ - patch('src.server.services.initialization_service.sync_legacy_series_to_db', - new_callable=AsyncMock, return_value=42) as mock_sync: + patch('src.server.services.initialization_service.os.path.isdir', return_value=True): mock_settings.anime_directory = "/path/to/anime" result = await _sync_anime_folders() - assert result == 42 - mock_sync.assert_called_once() + assert result == 0 @pytest.mark.asyncio async def test_sync_anime_folders_with_progress(self): @@ -176,13 +173,11 @@ class TestSyncAnimeFolders: mock_progress = AsyncMock() with patch('src.server.services.initialization_service.settings') as mock_settings, \ - patch('src.server.services.initialization_service.os.path.isdir', return_value=True), \ - patch('src.server.services.initialization_service.sync_legacy_series_to_db', - new_callable=AsyncMock, return_value=10) as mock_sync: + patch('src.server.services.initialization_service.os.path.isdir', return_value=True): mock_settings.anime_directory = "/path/to/anime" result = await _sync_anime_folders(progress_service=mock_progress) - assert result == 10 + assert result == 0 # Verify progress updates were called assert mock_progress.update_progress.call_count == 2 mock_progress.update_progress.assert_any_call( @@ -194,7 +189,7 @@ class TestSyncAnimeFolders: mock_progress.update_progress.assert_any_call( progress_id="series_sync", current=75, - message="Synced 10 series from data files", + message="Series loaded directly from database", metadata={"step_id": "series_sync"} )