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
This commit is contained in:
2026-06-10 18:26:09 +02:00
parent 08f7f7453c
commit e76cd3a708
3 changed files with 6 additions and 229 deletions

View File

@@ -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."""

View File

@@ -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"

View File

@@ -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"}
)