Files
ComfyUI/tests-unit/assets_test/conftest.py
bymyself 1ad4b76b55 Add comprehensive test suite for assets API
- conftest.py: Test fixtures (in-memory SQLite, mock UserManager, test image)
- schemas_test.py: 98 tests for Pydantic input validation
- helpers_test.py: 50 tests for utility functions
- queries_crud_test.py: 27 tests for core CRUD operations
- queries_filter_test.py: 28 tests for filtering/pagination
- queries_tags_test.py: 24 tests for tag operations
- routes_upload_test.py: 18 tests for upload endpoints
- routes_read_update_test.py: 21 tests for read/update endpoints
- routes_tags_delete_test.py: 17 tests for tags/delete endpoints

Total: 283 tests covering all 12 asset API endpoints
Amp-Thread-ID: https://ampcode.com/threads/T-019be932-d48b-76b9-843a-790e9d2a1f58
Co-authored-by: Amp <amp@ampcode.com>
2026-01-22 23:15:19 -08:00

105 lines
2.7 KiB
Python

"""
Pytest fixtures for assets API tests.
"""
import io
import pytest
from unittest.mock import MagicMock, patch
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from aiohttp import web
pytestmark = pytest.mark.asyncio
@pytest.fixture(scope="session")
def in_memory_engine():
"""Create an in-memory SQLite engine with all asset tables."""
engine = create_engine("sqlite:///:memory:", echo=False)
from app.database.models import Base
from app.assets.database.models import (
Asset,
AssetInfo,
AssetCacheState,
AssetInfoMeta,
AssetInfoTag,
Tag,
)
Base.metadata.create_all(engine)
yield engine
engine.dispose()
@pytest.fixture
def db_session(in_memory_engine) -> Session:
"""Create a fresh database session for each test."""
SessionLocal = sessionmaker(bind=in_memory_engine)
session = SessionLocal()
yield session
session.rollback()
session.close()
@pytest.fixture
def mock_user_manager():
"""Create a mock UserManager that returns a predictable owner_id."""
mock = MagicMock()
mock.get_request_user_id = MagicMock(return_value="test-user-123")
return mock
@pytest.fixture
def app(mock_user_manager) -> web.Application:
"""Create an aiohttp Application with assets routes registered."""
from app.assets.api.routes import register_assets_system
application = web.Application()
register_assets_system(application, mock_user_manager)
return application
@pytest.fixture
def test_image_bytes() -> bytes:
"""Generate a minimal valid PNG image (10x10 red pixels)."""
from PIL import Image
img = Image.new("RGB", (10, 10), color="red")
buffer = io.BytesIO()
img.save(buffer, format="PNG")
return buffer.getvalue()
@pytest.fixture
def tmp_upload_dir(tmp_path):
"""Create a temporary directory for uploads and patch folder_paths."""
upload_dir = tmp_path / "uploads"
upload_dir.mkdir()
with patch("folder_paths.get_temp_directory", return_value=str(tmp_path)):
yield tmp_path
@pytest.fixture(autouse=True)
def patch_create_session(in_memory_engine):
"""Patch create_session to use our in-memory database."""
SessionLocal = sessionmaker(bind=in_memory_engine)
with patch("app.database.db.Session", SessionLocal):
with patch("app.database.db.create_session", lambda: SessionLocal()):
with patch("app.database.db.can_create_session", return_value=True):
yield
async def test_fixtures_work(db_session, mock_user_manager):
"""Smoke test to verify fixtures are working."""
assert db_session is not None
assert mock_user_manager.get_request_user_id(None) == "test-user-123"