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.

perf(api): avoid numbered temporary directories for test databases (#43127)

Stephen Zhouf24586323b7c
diff --git a/api/tests/unit_tests/conftest.py b/api/tests/unit_tests/conftest.py
index a634a2794d3..db8ba7e1809 100644
--- a/api/tests/unit_tests/conftest.py
+++ b/api/tests/unit_tests/conftest.py
@@ -4,6 +4,7 @@ import os
import shutil
from collections.abc import Callable, Iterator
from pathlib import Path
+from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
@@ -125,18 +126,22 @@ def config_overrides(monkeypatch: pytest.MonkeyPatch) -> Callable[..., None]:
@pytest.fixture
-def _sqlite_engine(_sqlite_database_template: Path, tmp_path: Path) -> Iterator[Engine]:
- """Create an engine over a pristine per-test copy of the SQLite schema."""
+def _sqlite_engine(_sqlite_database_template: Path) -> Iterator[Engine]:
+ """Copy the schema into an isolated directory without pytest's numbered scan.
- database_path = tmp_path / "unit-tests.sqlite3"
- shutil.copyfile(_sqlite_database_template, database_path)
- engine = create_engine(URL.create("sqlite", database=str(database_path)))
-
- try:
- yield engine
- finally:
- engine.dispose()
- database_path.unlink(missing_ok=True)
+ ``tmp_path`` searches all preceding test directories for a free number. This
+ autouse dependency needs only a unique, disposable directory, including any
+ SQLite journal files, so keep it separate from test-owned ``tmp_path`` data.
+ """
+ with TemporaryDirectory(prefix="case-", dir=_sqlite_database_template.parent) as directory:
+ database_path = Path(directory) / "unit-tests.sqlite3"
+ shutil.copyfile(_sqlite_database_template, database_path)
+ engine = create_engine(URL.create("sqlite", database=str(database_path)))
+
+ try:
+ yield engine
+ finally:
+ engine.dispose()
@pytest.fixture(scope="session")