init
This commit is contained in:
commit
fe636432d0
154 files changed
+30381
No files matched your search
@@ -0,0 +1,114 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
os.environ["DATABASE_URL"] = "sqlite:///./test_document_cleaning.db"
|
||||
os.environ["DATA_ROOT"] = "./test_data"
|
||||
os.environ["PUBLIC_BASE_URL"] = "http://testserver"
|
||||
os.environ["INTEGRATION_API_KEY"] = "integration-secret"
|
||||
os.environ["INTEGRATION_DOWNLOAD_SECRET"] = "integration-download-secret"
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from app.config import get_settings # noqa: E402
|
||||
from app.database import Base, get_db # noqa: E402
|
||||
from app.main import app # noqa: E402
|
||||
from app.models import Department, SystemSetting, User # noqa: E402
|
||||
from app.security import hash_password # noqa: E402
|
||||
|
||||
|
||||
TEST_DB = Path("./test_document_cleaning.db")
|
||||
TEST_DATA_ROOT = Path("./test_data")
|
||||
if TEST_DB.exists():
|
||||
TEST_DB.unlink()
|
||||
if TEST_DATA_ROOT.exists():
|
||||
shutil.rmtree(TEST_DATA_ROOT, ignore_errors=True)
|
||||
TEST_DATA_ROOT.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
engine = create_engine("sqlite:///./test_document_cleaning.db", connect_args={"check_same_thread": False})
|
||||
TestingSessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, future=True)
|
||||
|
||||
|
||||
def override_get_db():
|
||||
db = TestingSessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def initialized_db():
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
Base.metadata.create_all(bind=engine)
|
||||
shutil.rmtree(TEST_DATA_ROOT, ignore_errors=True)
|
||||
get_settings().ensure_data_dirs()
|
||||
db = TestingSessionLocal()
|
||||
db.add(
|
||||
Department(
|
||||
id=1,
|
||||
name="测试部门",
|
||||
description="测试部门",
|
||||
)
|
||||
)
|
||||
db.add(
|
||||
SystemSetting(
|
||||
id=1,
|
||||
dify_permission="all_team_members",
|
||||
indexing_technique="high_quality",
|
||||
search_method="hybrid_search",
|
||||
doc_form="text_model",
|
||||
top_k=5,
|
||||
score_threshold_enabled=True,
|
||||
score_threshold="0.35",
|
||||
)
|
||||
)
|
||||
db.add(
|
||||
User(
|
||||
username="admin",
|
||||
full_name="System Admin",
|
||||
password_hash=hash_password("admin123456"),
|
||||
role="super_admin",
|
||||
is_active=True,
|
||||
primary_department_id=1,
|
||||
)
|
||||
)
|
||||
db.add(
|
||||
User(
|
||||
username="adam",
|
||||
full_name="Adam",
|
||||
password_hash=hash_password("admin123456"),
|
||||
role="admin",
|
||||
is_active=True,
|
||||
primary_department_id=1,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
db.close()
|
||||
yield
|
||||
shutil.rmtree(TEST_DATA_ROOT, ignore_errors=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(initialized_db):
|
||||
with TestClient(app) as test_client:
|
||||
yield test_client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db_session(initialized_db):
|
||||
db = TestingSessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in new issue
Block a user