Fix generator exception handling in database dependencies
- Add proper exception handling in get_database_session and get_optional_database_session - Prevents 'generator didn't stop after athrow()' error when HTTPException is raised - Add mock for BackgroundLoaderService in anime endpoint tests - Update test expectations to match 202 Accepted response for async add_series endpoint
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Tests for anime API endpoints."""
|
||||
import asyncio
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
@@ -122,11 +123,19 @@ def reset_auth_state():
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_series_app_dependency():
|
||||
"""Override the series_app dependency with FakeSeriesApp."""
|
||||
from src.server.services.background_loader_service import (
|
||||
get_background_loader_service,
|
||||
)
|
||||
from src.server.utils.dependencies import get_series_app
|
||||
|
||||
fake_app = FakeSeriesApp()
|
||||
app.dependency_overrides[get_series_app] = lambda: fake_app
|
||||
|
||||
# Mock background loader service
|
||||
mock_background_loader = AsyncMock()
|
||||
mock_background_loader.add_series_loading_task = AsyncMock()
|
||||
app.dependency_overrides[get_background_loader_service] = lambda: mock_background_loader
|
||||
|
||||
yield fake_app
|
||||
|
||||
# Clean up
|
||||
@@ -262,13 +271,11 @@ async def test_add_series_endpoint_authenticated(authenticated_client):
|
||||
json={"link": "test-anime-link", "name": "Test New Anime"}
|
||||
)
|
||||
|
||||
# The endpoint should succeed (returns 200 or may fail if series exists)
|
||||
assert response.status_code in (200, 400)
|
||||
# The endpoint should succeed with 202 Accepted (async operation)
|
||||
assert response.status_code == 202
|
||||
data = response.json()
|
||||
|
||||
if response.status_code == 200:
|
||||
assert data["status"] == "success"
|
||||
assert "Test New Anime" in data["message"]
|
||||
assert data["status"] == "success"
|
||||
assert "Test New Anime" in data["message"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -310,7 +317,7 @@ async def test_add_series_extracts_key_from_full_url(authenticated_client):
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 202
|
||||
data = response.json()
|
||||
assert data["key"] == "attack-on-titan"
|
||||
|
||||
@@ -326,7 +333,7 @@ async def test_add_series_sanitizes_folder_name(authenticated_client):
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 202
|
||||
data = response.json()
|
||||
|
||||
# Folder should not contain invalid characters
|
||||
@@ -337,7 +344,7 @@ async def test_add_series_sanitizes_folder_name(authenticated_client):
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_series_returns_missing_episodes(authenticated_client):
|
||||
"""Test that add_series returns missing episodes info."""
|
||||
"""Test that add_series returns loading progress info."""
|
||||
response = await authenticated_client.post(
|
||||
"/api/anime/add",
|
||||
json={
|
||||
@@ -346,14 +353,13 @@ async def test_add_series_returns_missing_episodes(authenticated_client):
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 202
|
||||
data = response.json()
|
||||
|
||||
# Response should contain missing episodes fields
|
||||
assert "missing_episodes" in data
|
||||
assert "total_missing" in data
|
||||
assert isinstance(data["missing_episodes"], dict)
|
||||
assert isinstance(data["total_missing"], int)
|
||||
# Response should contain loading_progress fields (async endpoint)
|
||||
assert "loading_status" in data
|
||||
assert "loading_progress" in data
|
||||
assert isinstance(data["loading_progress"], dict)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -367,7 +373,7 @@ async def test_add_series_response_structure(authenticated_client):
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 202
|
||||
data = response.json()
|
||||
|
||||
# Verify all expected fields are present
|
||||
@@ -375,8 +381,8 @@ async def test_add_series_response_structure(authenticated_client):
|
||||
assert "message" in data
|
||||
assert "key" in data
|
||||
assert "folder" in data
|
||||
assert "missing_episodes" in data
|
||||
assert "total_missing" in data
|
||||
assert "loading_status" in data
|
||||
assert "loading_progress" in data
|
||||
|
||||
# Status should be success or exists
|
||||
assert data["status"] in ("success", "exists")
|
||||
@@ -401,7 +407,7 @@ async def test_add_series_special_characters_in_name(authenticated_client):
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.status_code == 202
|
||||
data = response.json()
|
||||
|
||||
# Get just the folder name (last part of path)
|
||||
|
||||
Reference in New Issue
Block a user