Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

chore: add sqlite3 to conftest (#38475)

Asuka Minatof3ba28463beb
diff --git a/api/tests/unit_tests/commands/test_legacy_model_type_migration.py b/api/tests/unit_tests/commands/test_legacy_model_type_migration.py
index e14c8ed3243..9eb73b82516 100644
--- a/api/tests/unit_tests/commands/test_legacy_model_type_migration.py
+++ b/api/tests/unit_tests/commands/test_legacy_model_type_migration.py
@@ -32,15 +32,6 @@ from tests.helpers.legacy_model_type_migration import (
)
-def sqlite_engine(tmp_path: Path) -> sa.Engine:
- engine = sa.create_engine(f"sqlite:///{tmp_path / 'legacy_model_type_migration.sqlite'}")
- try:
- yield engine
- finally:
- engine.dispose()
-
-
@pytest.fixture
def dirty_fixture(sqlite_engine: sa.Engine):
return seed_legacy_model_type_dirty_data(sqlite_engine)
diff --git a/api/tests/unit_tests/conftest.py b/api/tests/unit_tests/conftest.py
index 7174530e976..d95e5e8501d 100644
--- a/api/tests/unit_tests/conftest.py
+++ b/api/tests/unit_tests/conftest.py
@@ -1,9 +1,12 @@
import os
+from collections.abc import Iterator
from unittest.mock import MagicMock, patch
import pytest
from flask import Flask
from sqlalchemy import create_engine
+from sqlalchemy.engine import Engine
+from sqlalchemy.orm import Session, sessionmaker
# Getting the absolute path of the current file's directory
ABS_PATH = os.path.dirname(os.path.abspath(__file__))
@@ -34,6 +37,7 @@ os.environ.setdefault("STORAGE_TYPE", "opendal")
from core.db.session_factory import configure_session_factory, session_factory
from extensions import ext_redis
+from models.base import TypeBase
def _patch_redis_clients_on_loaded_modules():
@@ -113,6 +117,29 @@ def _unit_test_engine():
engine.dispose()
+def sqlite_engine() -> Iterator[Engine]:
+ """Create an isolated in-memory SQLite engine for tests that need a disposable database."""
+
+ engine = create_engine("sqlite:///:memory:")
+ try:
+ yield engine
+ finally:
+ engine.dispose()
+
+
+def sqlite_session(request: pytest.FixtureRequest, sqlite_engine: Engine) -> Iterator[Session]:
+ """Yield a SQLite session after creating the model tables passed through ``request.param``."""
+
+ models: tuple[type[TypeBase], ...] = request.param
+ tables = [model.metadata.tables[model.__tablename__] for model in models]
+ TypeBase.metadata.create_all(sqlite_engine, tables=tables)
+ session_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
+ with session_factory() as session:
+ yield session
+
+
@pytest.fixture(autouse=True)
def _configure_session_factory(_unit_test_engine):
try:
diff --git a/api/tests/unit_tests/services/test_account_service.py b/api/tests/unit_tests/services/test_account_service.py
index 498e295447e..233191ca0b7 100644
--- a/api/tests/unit_tests/services/test_account_service.py
+++ b/api/tests/unit_tests/services/test_account_service.py
@@ -1,16 +1,13 @@
import json
-from collections.abc import Iterator
from datetime import datetime, timedelta
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
-from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from configs import dify_config
from models.account import Account, AccountStatus, TenantAccountRole, TenantStatus
-from models.base import TypeBase
from services.account_service import AccountService, RegisterService, TenantService
from services.errors.account import (
AccountAlreadyInTenantError,
@@ -22,16 +19,6 @@ from services.errors.account import (
)
-def sqlite_session(request: pytest.FixtureRequest) -> Iterator[Session]:
- models: tuple[type[TypeBase], ...] = request.param
- engine = create_engine("sqlite:///:memory:")
- tables = [model.metadata.tables[model.__tablename__] for model in models]
- Account.metadata.create_all(engine, tables=tables)
- with Session(engine, expire_on_commit=False) as session:
- yield session
-
-
class TestAccountAssociatedDataFactory:
"""Factory class for creating test data and mock objects for account service tests."""