Fix NFO database query errors

- Fixed async context manager issue in anime.py (use get_sync_session)
- Fixed async methods in anime_service.py to use async with
- Fixed folder_name attribute error (should be folder)
- All three methods now properly handle database sessions
This commit is contained in:
2026-01-18 11:56:22 +01:00
parent 4408874d37
commit db1e7fa54b
4 changed files with 173 additions and 177 deletions

View File

@@ -110,124 +110,32 @@ For each task completed:
## TODO List:
### 🎯 Priority: NFO FSK Rating Implementation ✅ COMPLETED
All tasks completed! The NFO database query issue has been resolved.
**Task: Implement German FSK Rating Support in NFO Files**
## Recently Fixed Issues:
**Status: COMPLETED**
### ✅ Fixed: NFO Database Query Error (2026-01-18)
**Implementation Summary:**
**Issue:** WARNING: Could not fetch NFO data from database: '\_AsyncGeneratorContextManager' object has no attribute 'query'
All requirements have been successfully implemented and tested:
**Root Cause:**
1. **TMDB API Integration**: Added `get_tv_show_content_ratings()` method to TMDBClient
2. **Data Model**: Added optional `fsk` field to `TVShowNFO` model
3. **FSK Extraction**: Implemented `_extract_fsk_rating()` method in NFOService with comprehensive mapping:
- Maps TMDB German ratings (0, 6, 12, 16, 18) to FSK format
- Handles already formatted FSK strings
- Supports partial matches (e.g., "Ab 16 Jahren" → "FSK 16")
- Fallback to None when German rating unavailable
4. **XML Generation**: Updated `generate_tvshow_nfo()` to prefer FSK over MPAA when available
5.**Configuration**: Added `nfo_prefer_fsk_rating` setting (default: True)
6.**Comprehensive Testing**: Added 31 new tests across test_nfo_service.py and test_nfo_generator.py
- All 112 NFO-related tests passing
- Test coverage includes FSK extraction, XML generation, edge cases, and integration
1. Synchronous code in `anime.py` was calling `get_db_session()` which returns an async context manager, but wasn't using `async with`.
2. Async methods in `anime_service.py` were calling `get_db_session()` without using `async with`.
3. Incorrect attribute name: used `folder_name` instead of `folder`.
**Solution:**
1. Changed `anime.py` line ~323 to use `get_sync_session()` instead of `get_db_session()` for synchronous database access.
2. Updated three async methods in `anime_service.py` to properly use `async with get_db_session()`:
- `update_nfo_status()`
- `get_series_without_nfo()`
- `get_nfo_statistics()`
3. Fixed attribute name from `db_series.folder_name` to `db_series.folder` in `anime.py`.
**Files Modified:**
- `src/core/entities/nfo_models.py` - Added `fsk` field
- `src/core/services/nfo_service.py` - Added FSK extraction and TMDB API call
- `src/core/services/tmdb_client.py` - Added content ratings endpoint
- `src/core/utils/nfo_generator.py` - Updated XML generation to prefer FSK
- `src/config/settings.py` - Added `nfo_prefer_fsk_rating` setting
- [src/server/api/anime.py](../src/server/api/anime.py)
- [src/server/services/anime_service.py](../src/server/services/anime_service.py)
**Files Created:**
- `tests/unit/test_nfo_service.py` - 23 comprehensive unit tests
**Files Updated:**
- `tests/unit/test_nfo_generator.py` - Added 5 FSK-specific tests
**Acceptance Criteria Met:**
- ✅ NFO files contain FSK rating when available from TMDB
- ✅ Fallback to MPAA rating if FSK not available
- ✅ Configuration setting to prefer FSK over MPAA
- ✅ Unit tests cover all FSK values and fallback scenarios
- ✅ Existing NFO functionality remains unchanged
- ✅ Documentation updated with FSK support details
---
### 🧪 Priority: Comprehensive NFO and Image Download Tests ✅ COMPLETED
**Task: Add Comprehensive Tests for NFO Creation and Media Downloads**
**Status: COMPLETED**
**Implementation Summary:**
Significantly expanded test coverage for NFO functionality with comprehensive unit and integration tests:
1.**Unit Tests Expanded** ([test_nfo_service.py](tests/unit/test_nfo_service.py)):
- Added 13 tests for media file downloads (poster, logo, fanart)
- Tests for various image sizes and configurations
- Tests for download failures and edge cases
- Configuration testing for NFO service settings
- **Total: 44 tests** in test_nfo_service.py
2.**Integration Tests Created** ([test_nfo_integration.py](tests/integration/test_nfo_integration.py)):
- Complete NFO creation workflow with all media files
- NFO creation without media downloads
- Correct folder structure verification
- NFO update workflow with media re-download
- Error handling and recovery
- Concurrent NFO operations (batch creation)
- Data integrity validation
- **Total: 10 comprehensive integration tests**
3.**API Endpoint Tests**: Existing test_nfo_endpoints.py already covers all NFO API endpoints
**Files Modified:**
- `tests/unit/test_nfo_service.py` - Added 23 new tests for media downloads and configuration
**Files Created:**
- `tests/integration/test_nfo_integration.py` - 10 comprehensive integration tests
**Test Results:**
-**44 tests passing** in test_nfo_service.py (unit)
-**10 tests passing** in test_nfo_integration.py (integration)
-**112 total NFO-related unit tests passing**
-**All tests verify**:
- FSK rating extraction and mapping
- Media file download scenarios
- NFO creation and update workflows
- Error handling and edge cases
- Concurrent operations
- Data integrity
**Acceptance Criteria Met:**
- ✅ Comprehensive unit tests for all media download scenarios
- ✅ Integration tests verify complete workflows
- ✅ Tests validate file system state after operations
- ✅ Edge cases and error scenarios covered
- ✅ Concurrent operations tested
- ✅ All tests use proper mocking for TMDB API
- ✅ Test fixtures provide realistic test data
**Test Coverage Highlights:**
- Media download with all combinations (poster/logo/fanart)
- Different image sizes (original, w500, w780, w342)
- Missing media scenarios
- Concurrent NFO creation
- NFO update workflows
- FSK rating preservation
- Complete metadata integrity
---
**Verification:** Server now starts without warnings, and NFO metadata can be fetched from the database correctly.