diff --git a/docs/instructions.md b/docs/instructions.md index ac6631c..ebc04a4 100644 --- a/docs/instructions.md +++ b/docs/instructions.md @@ -121,10 +121,11 @@ For each task completed: ✅ **Task Completed Successfully** -### Issue Fixed: TypeError: 'async for' requires an object with __aiter__ method +### Issue Fixed: TypeError: 'async for' requires an object with **aiter** method **Problem:** The BackgroundLoaderService was crashing when trying to load series data with the error: + ``` TypeError: 'async for' requires an object with __aiter__ method, got _AsyncGeneratorContextManager ``` @@ -136,6 +137,7 @@ The code was incorrectly using `async for db in get_db_session():` to get a data **Solution:** Changed the database session acquisition from: + ```python async for db in get_db_session(): # ... code ... @@ -143,16 +145,19 @@ async for db in get_db_session(): ``` To the correct pattern: + ```python async with get_db_session() as db: # ... code ... ``` **Files Modified:** + 1. [src/server/services/background_loader_service.py](src/server/services/background_loader_service.py) - Fixed async context manager usage 2. [tests/unit/test_background_loader_session.py](tests/unit/test_background_loader_session.py) - Created comprehensive unit tests **Tests:** + - ✅ 5 new unit tests for background loader database session handling (all passing) - ✅ Tests verify proper async context manager usage - ✅ Tests verify error handling and progress tracking diff --git a/tests/unit/test_background_loader_session.py b/tests/unit/test_background_loader_session.py index 946488c..dc9cc51 100644 --- a/tests/unit/test_background_loader_session.py +++ b/tests/unit/test_background_loader_session.py @@ -4,14 +4,15 @@ Unit tests for BackgroundLoaderService database session handling. This module tests that the background loader service properly uses async context managers for database sessions, preventing TypeError with async for. """ -import pytest -from unittest.mock import AsyncMock, MagicMock, patch from datetime import datetime, timezone +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest from src.server.services.background_loader_service import ( BackgroundLoaderService, - SeriesLoadingTask, LoadingStatus, + SeriesLoadingTask, ) @@ -277,7 +278,7 @@ async def test_async_context_manager_usage(): """ from contextlib import asynccontextmanager from typing import AsyncGenerator - + # Create a test async context manager call_log = []