Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

3.0 KB
Raw Blame
import tempfile
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import patch

import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.pool import StaticPool
from sqlalchemy.orm import Session, Session as SASession, sessionmaker

import folder_paths
from app.assets import mode
from app.assets.database.models import Base


def _memory_engine():
    # One shared connection: upload previews are stored from a worker thread.
    return create_engine("sqlite:///:memory:", poolclass=StaticPool, connect_args={"check_same_thread": False})


@pytest.fixture(autouse=True)
def autoclean_unit_test_assets():
    """Override parent autouse fixture - service unit tests don't need server cleanup."""
    yield


@pytest.fixture(autouse=True)
def initialised_hash_mode():
    class _HashingOff:
        enable_asset_hashing = False

    mode.init(_HashingOff())
    yield
    mode.init(None)


@pytest.fixture
def db_engine():
    """In-memory SQLite engine for fast unit tests."""
    engine = _memory_engine()
    Base.metadata.create_all(engine)
    return engine


@pytest.fixture
def db_engine_fk():
    """In-memory SQLite engine with foreign key enforcement enabled."""
    engine = _memory_engine()

    @event.listens_for(engine, "connect")
    def _set_pragma(dbapi_connection, connection_record):
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys=ON")
        cursor.close()

    Base.metadata.create_all(engine)
    return engine


@pytest.fixture
def session(db_engine):
    """Session fixture for tests that need direct DB access."""
    with Session(db_engine) as sess:
        yield sess


@pytest.fixture
def mock_create_session(db_engine):
    """Patch create_session to use our in-memory database."""
    @contextmanager
    def _create_session():
        with SASession(db_engine) as sess:
            yield sess

    with patch("app.assets.services.ingest.create_session", _create_session), \
         patch("app.database.db.WriteSession", sessionmaker(bind=db_engine)), \
         patch("app.assets.services.asset_management.create_session", _create_session), \
         patch("app.assets.services.tagging.create_session", _create_session):
        yield _create_session


@pytest.fixture
def temp_dir():
    """Temporary directory for file operations."""
    with tempfile.TemporaryDirectory() as tmpdir:
        yield Path(tmpdir)


@pytest.fixture
def roots(tmp_path: Path):
    """input/, output/ and previews/ in tmp_path, as Core's roots."""
    saved = (folder_paths.get_output_directory(), folder_paths.get_input_directory(), folder_paths.get_previews_directory())
    folder_paths.set_output_directory(str(tmp_path / "output"))
    folder_paths.set_input_directory(str(tmp_path / "input"))
    folder_paths.set_previews_directory(str(tmp_path / "previews"))
    for name in ("output", "input", "previews"):
        (tmp_path / name).mkdir()
    yield tmp_path
    folder_paths.set_output_directory(saved[0])
    folder_paths.set_input_directory(saved[1])
    folder_paths.set_previews_directory(saved[2])