Skip NFO creation if exists and update DB

This commit is contained in:
2026-01-19 20:45:05 +01:00
parent 01f828c799
commit bfbae88ade
2 changed files with 153 additions and 2 deletions

View File

@@ -97,6 +97,126 @@ class TestBackgroundLoaderService:
async def test_duplicate_task_handling(self, background_loader):
"""Test that duplicate tasks for same series are handled correctly."""
key = "test-series"
@pytest.mark.asyncio
async def test_load_nfo_skips_if_exists(self, background_loader, mock_series_app):
"""Test that NFO creation is skipped if NFO already exists."""
# Mock has_nfo to return True (NFO exists)
mock_series_app.nfo_service.has_nfo.return_value = True
task = SeriesLoadingTask(
key="test-series",
folder="Test Series",
name="Test Series",
year=2023
)
# Mock database
mock_db = AsyncMock()
mock_series_db = Mock()
mock_series_db.has_nfo = False
mock_series_db.logo_loaded = False
mock_series_db.images_loaded = False
with patch('src.server.database.service.AnimeSeriesService.get_by_key') as mock_get:
mock_get.return_value = mock_series_db
# Execute
await background_loader._load_nfo_and_images(task, mock_db)
# Verify NFO creation was NOT called
mock_series_app.nfo_service.create_tvshow_nfo.assert_not_called()
# Verify progress was updated
assert task.progress["nfo"] is True
assert task.progress["logo"] is True
assert task.progress["images"] is True
# Verify database was updated
assert mock_series_db.has_nfo is True
assert mock_series_db.logo_loaded is True
assert mock_series_db.images_loaded is True
mock_db.commit.assert_called_once()
@pytest.mark.asyncio
async def test_load_nfo_creates_if_not_exists(self, background_loader, mock_series_app):
"""Test that NFO is created if it doesn't exist."""
# Mock has_nfo to return False (NFO doesn't exist)
mock_series_app.nfo_service.has_nfo.return_value = False
mock_series_app.nfo_service.create_tvshow_nfo.return_value = "/test/anime/Test Series/tvshow.nfo"
task = SeriesLoadingTask(
key="test-series",
folder="Test Series",
name="Test Series",
year=2023
)
# Mock database
mock_db = AsyncMock()
mock_series_db = Mock()
mock_series_db.has_nfo = False
with patch('src.server.database.service.AnimeSeriesService.get_by_key') as mock_get:
mock_get.return_value = mock_series_db
# Execute
await background_loader._load_nfo_and_images(task, mock_db)
# Verify NFO creation WAS called
mock_series_app.nfo_service.create_tvshow_nfo.assert_called_once_with(
serie_name="Test Series",
serie_folder="Test Series",
year=2023,
download_poster=True,
download_logo=True,
download_fanart=True
)
# Verify progress was updated
assert task.progress["nfo"] is True
assert task.progress["logo"] is True
assert task.progress["images"] is True
# Verify database was updated
assert mock_series_db.has_nfo is True
mock_db.commit.assert_called_once()
@pytest.mark.asyncio
async def test_load_nfo_doesnt_update_if_already_marked(self, background_loader, mock_series_app):
"""Test that database is not updated if NFO is already marked in DB."""
# Mock has_nfo to return True (NFO exists)
mock_series_app.nfo_service.has_nfo.return_value = True
task = SeriesLoadingTask(
key="test-series",
folder="Test Series",
name="Test Series"
)
# Mock database - NFO already marked
mock_db = AsyncMock()
mock_series_db = Mock()
mock_series_db.has_nfo = True # Already marked
mock_series_db.logo_loaded = True
mock_series_db.images_loaded = True
with patch('src.server.database.service.AnimeSeriesService.get_by_key') as mock_get:
mock_get.return_value = mock_series_db
# Execute
await background_loader._load_nfo_and_images(task, mock_db)
# Verify database commit was still called
mock_db.commit.assert_called_once()
# Verify NFO creation was NOT called
mock_series_app.nfo_service.create_tvshow_nfo.assert_not_called()
@pytest.mark.asyncio
async def test_duplicate_task_handling_continued(self, background_loader):
"""Test that duplicate tasks for same series are handled correctly."""
key = "test-series"
await background_loader.add_series_loading_task(
key=key,