File size: 9,740 Bytes
b4f2238 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | """
Pytest 配置文件
提供测试 fixtures
"""
import os
import sys
from typing import Any, Generator, Iterator
import django
import pytest
from apps.core.utils.path import Path
# 添加项目路径
sys.path.insert(0, str(Path(__file__).parent / "apiSystem"))
# 设置 Django 配置
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apiSystem.settings")
# 排除脚本文件(非标准 pytest 测试)
collect_ignore = [
"tests/unit/automation/test_court_document.py",
]
@pytest.fixture(scope="session")
def django_db_setup(django_db_setup: Any, django_db_blocker: Any) -> Any:
"""
设置测试数据库
确保测试使用独立的测试数据库,不影响生产数据库
"""
with django_db_blocker.unblock():
# pytest-django 会自动创建测试数据库
# 这里只需要确保使用正确的数据库配置
from django.conf import settings
db_cfg = settings.DATABASES.get("default", {})
db_engine = str(db_cfg.get("ENGINE", ""))
db_name = str(db_cfg.get("NAME", ""))
if db_engine == "django.db.backends.sqlite3":
is_test_db = (
"test_" in db_name
or "_test" in db_name
or ":memory:" in db_name
or "memorydb" in db_name
or db_name == ":memory:"
)
else:
lowered_name = db_name.lower()
is_test_db = (
lowered_name.startswith("test_")
or lowered_name.endswith("_test")
or "_test_" in lowered_name
or lowered_name == "test"
)
assert is_test_db, f"错误:测试正在使用生产数据库 {db_name}!测试已中止。"
yield
# 测试结束后,pytest-django 会自动清理测试数据库
@pytest.fixture(scope="session")
def django_db_modify_db_settings() -> None:
"""
修改测试数据库设置
默认使用 PostgreSQL 测试库,也支持显式切换 SQLite。
"""
from django.conf import settings
inferred_engine = "sqlite" if (os.environ.get("DATABASE_PATH") or os.environ.get("TEST_DB_PATH")) else "postgresql"
test_db_engine = (os.environ.get("TEST_DB_ENGINE") or os.environ.get("DB_ENGINE") or inferred_engine).strip().lower()
if test_db_engine in ("sqlite", "sqlite3", "django.db.backends.sqlite3"):
test_db_path = (os.environ.get("TEST_DB_PATH") or os.environ.get("DATABASE_PATH") or ":memory:").strip()
settings.DATABASES["default"] = {
"ENGINE": "django.db.backends.sqlite3",
"NAME": test_db_path,
"ATOMIC_REQUESTS": False,
"CONN_MAX_AGE": 0,
"TIME_ZONE": "Asia/Shanghai",
"OPTIONS": {
"timeout": 20,
},
}
return
raw_test_password = os.environ.get("TEST_DB_PASSWORD")
raw_db_password = os.environ.get("DB_PASSWORD")
resolved_password = raw_test_password if raw_test_password is not None else raw_db_password
if resolved_password is None:
resolved_password = "postgres"
settings.DATABASES["default"] = {
"ENGINE": "django.db.backends.postgresql",
"NAME": (os.environ.get("TEST_DB_NAME") or "fachuan_test").strip(),
"USER": (os.environ.get("TEST_DB_USER") or os.environ.get("DB_USER") or "postgres").strip(),
"PASSWORD": resolved_password.strip(),
"HOST": (os.environ.get("TEST_DB_HOST") or os.environ.get("DB_HOST") or "127.0.0.1").strip(),
"PORT": int((os.environ.get("TEST_DB_PORT") or os.environ.get("DB_PORT") or "5432").strip()),
"ATOMIC_REQUESTS": False,
"CONN_MAX_AGE": 0,
"TIME_ZONE": "Asia/Shanghai",
"CONN_HEALTH_CHECKS": True,
}
@pytest.fixture
def api_client() -> Any:
"""提供 API 测试客户端"""
from django.test import Client
return Client()
@pytest.fixture
def authenticated_client(db: Any) -> Any:
"""提供已认证的测试客户端"""
from django.test import Client
from apps.organization.models import LawFirm, Lawyer
client = Client()
firm = LawFirm.objects.create(name="测试律所")
user = Lawyer.objects.create_user(
username="testuser",
password="testpass123",
is_admin=True,
law_firm=firm,
)
client.force_login(user)
return client
@pytest.fixture
def law_firm(db: Any) -> Any:
"""提供测试律所"""
from apps.organization.models import LawFirm
return LawFirm.objects.create(name="Fixture测试律所")
@pytest.fixture
def lawyer(db: Any, law_firm: Any) -> Any:
"""提供测试律师"""
from apps.organization.models import Lawyer
return Lawyer.objects.create_user(
username="fixturelawyer",
password="testpass123",
real_name="Fixture律师",
law_firm=law_firm,
)
@pytest.fixture
def admin_lawyer(db: Any, law_firm: Any) -> Any:
"""提供管理员律师"""
from apps.organization.models import Lawyer
return Lawyer.objects.create_user(
username="adminlawyer",
password="testpass123",
is_admin=True,
law_firm=law_firm,
)
@pytest.fixture
def client_entity(db: Any) -> Any:
"""提供测试客户"""
from apps.client.models import Client
return Client.objects.create(
name="Fixture测试客户",
client_type=Client.NATURAL,
is_our_client=True,
)
@pytest.fixture
def contract(db: Any, lawyer: Any) -> Any:
"""提供测试合同"""
from apps.contracts.models import Contract
return Contract.objects.create(
name="Fixture测试合同",
case_type="civil",
)
@pytest.fixture
def case(db: Any, contract: Any) -> Any:
"""提供测试案件"""
from apps.cases.models import Case
return Case.objects.create(
name="Fixture测试案件",
contract=contract,
)
# ========== Hypothesis 配置 ==========
from hypothesis import Verbosity, settings
# 配置 Hypothesis
settings.register_profile("default", max_examples=5, verbosity=Verbosity.normal)
settings.register_profile("ci", max_examples=1000, verbosity=Verbosity.verbose)
settings.register_profile("dev", max_examples=10, verbosity=Verbosity.verbose)
settings.register_profile("debug", max_examples=10, verbosity=Verbosity.debug)
# 加载配置(默认使用 default)
profile = os.getenv("HYPOTHESIS_PROFILE", "default")
settings.load_profile(profile)
# ========== 测试工具 Fixtures ==========
@pytest.fixture
def mock_contract_service() -> Any:
"""提供 Mock 合同服务"""
from tests.mocks import MockContractService
return MockContractService()
@pytest.fixture
def mock_case_service() -> Any:
"""提供 Mock 案件服务"""
from tests.mocks import MockCaseService
return MockCaseService()
@pytest.fixture
def mock_permission_service() -> Any:
"""提供 Mock 权限服务"""
from tests.mocks import MockPermissionService
return MockPermissionService()
@pytest.fixture
def mock_email_service() -> Any:
"""提供 Mock 邮件服务"""
from tests.mocks import MockEmailService
return MockEmailService()
@pytest.fixture
def query_counter(db: Any) -> Any:
"""
查询计数器
用于测试数据库查询优化,检测 N+1 查询问题
使用方法:
with query_counter() as counter:
# 执行操作
cases = Case.objects.select_related('contract').all()
list(cases)
assert counter.count <= 1 # 断言查询次数
"""
from django.db import connection
from django.test.utils import CaptureQueriesContext
class QueryCounter:
def __init__(self) -> None:
self.context: CaptureQueriesContext | None = None
self.count: int = 0
def __enter__(self) -> "QueryCounter":
self.context = CaptureQueriesContext(connection)
self.context.__enter__()
return self
def __exit__(self, *args: Any) -> None:
assert self.context is not None
self.context.__exit__(*args)
self.count = len(self.context.captured_queries)
@property
def queries(self) -> list[dict[str, Any]]:
"""获取所有查询"""
return self.context.captured_queries if self.context else []
def _counter() -> QueryCounter:
return QueryCounter()
return _counter
@pytest.fixture
def assert_num_queries(db: Any) -> Any:
"""
断言查询次数
使用方法:
with assert_num_queries(1):
# 执行操作,应该只有 1 次查询
cases = Case.objects.select_related('contract').all()
list(cases)
"""
from django.db import connection
from django.test.utils import CaptureQueriesContext
class AssertNumQueries:
def __init__(self, expected_count: int) -> None:
self.expected_count = expected_count
self.context: CaptureQueriesContext | None = None
def __enter__(self) -> "AssertNumQueries":
self.context = CaptureQueriesContext(connection)
self.context.__enter__()
return self
def __exit__(self, *args: Any) -> None:
assert self.context is not None
self.context.__exit__(*args)
actual_count = len(self.context.captured_queries)
if actual_count != self.expected_count:
queries = "\n".join(f"{i + 1}. {q['sql']}" for i, q in enumerate(self.context.captured_queries))
raise AssertionError(f"Expected {self.expected_count} queries, but got {actual_count}:\n{queries}")
return AssertNumQueries
|