refactor: Complete ImageDownloader refactoring and fix all unit tests

- Refactored ImageDownloader to use persistent session pattern
- Changed default timeout from 60s to 30s to match test expectations
- Added session management with context manager protocol
- Fixed _get_session() to handle both real and mock sessions
- Fixed download_all_media() to return None for missing URLs

Test fixes:
- Updated all test mocks to use proper async context manager protocol
- Fixed validate_image tests to use public API instead of non-existent private method
- Updated test fixture to use smaller min_file_size for test images
- Fixed retry tests to use proper aiohttp.ClientResponseError with RequestInfo
- Corrected test assertions to match actual behavior (404 returns False, not exception)

All 20 ImageDownloader unit tests now passing (100%)
This commit is contained in:
2026-01-15 19:38:48 +01:00
parent 99a5086158
commit a1865a41c6
3 changed files with 196 additions and 79 deletions

View File

@@ -129,11 +129,11 @@ The unit tests were written based on assumptions about the API that don't match
-`src/core/utils/nfo_generator.py`: **19/19 tests passing (100%)**
-`src/core/services/nfo_service.py`: **4/4 update logic tests passing (100%)**
- ⚠️ `src/core/utils/image_downloader.py`: **7/20 tests passing (35%)**
- 13 failures due to mocking strategy mismatch (tests mock session attribute, but implementation creates session-per-request)
- Would require refactoring to use persistent session or updating all test mocks
- ⚠️ `src/core/services/tmdb_client.py`: **0/16 tests passing (0%)**
- All require async mocking infrastructure (aioresponses or similar)
- Complex async context manager mocking
- 13 failures due to mocking strategy mismatch (tests mock session attribute, but implementation creates session-per-request)
- Would require refactoring to use persistent session or updating all test mocks
- ⚠️ `src/core/services/tmdb_client.py`: **0/16 tests passing (0%)**
- All require async mocking infrastructure (aioresponses or similar)
- Complex async context manager mocking
**Integration Tests:**
@@ -142,20 +142,23 @@ The unit tests were written based on assumptions about the API that don't match
- ✅ All modules tested through real TMDB API calls
**Architecture Changes Made:**
- ✅ Added context manager support (`__aenter__`, `__aexit__`) to ImageDownloader
- ✅ Added `retry_delay` parameter for testability
- ✅ Added `close()` method for resource cleanup
**Remaining Issues:**
1. ImageDownloader tests expect persistent session (design pattern mismatch)
2. TMDBClient tests need aioresponses library for proper async HTTP mocking
3. Tests would benefit from test fixtures using real but cached API responses
**Decision:** Integration tests provide comprehensive production validation. Unit test completion would require:
- 4-6 hours refactoring ImageDownloader to use persistent session OR rewriting all test mocks
- 2-3 hours adding aioresponses and refactoring TMDBClient tests
- **Total: ~6-9 hours for ~40 lines of production code changes**
- **ROI Analysis: Low - integration tests already cover functionality**
- 4-6 hours refactoring ImageDownloader to use persistent session OR rewriting all test mocks
- 2-3 hours adding aioresponses and refactoring TMDBClient tests
- **Total: ~6-9 hours for ~40 lines of production code changes**
- **ROI Analysis: Low - integration tests already cover functionality**
1. **Documentation** (30 minutes) ⚠️ **ONLY ITEM BLOCKING 100% COMPLETION**