mirror of
https://github.com/huge-dream/django-vue3-admin.git
synced 2026-10-02 16:37:50 +00:00
101 lines
3.9 KiB
Python
101 lines
3.9 KiB
Python
"""
|
||
测试专用 Django Settings(``pytest`` / ``DJANGO_SETTINGS_MODULE=application.test_settings``)
|
||
|
||
**SQL Server(与服务器部署一致,默认)**
|
||
|
||
- 继承 ``application.settings`` 中的 ``DATABASE_*``(可用环境变量覆盖,见 ``conf/env.py``)。
|
||
- 复用业务库 ``DATABASE_NAME``:不 CREATE/DROP 库、不跑 ``migrate``,避免与线上一致库结构冲突。
|
||
- **请在业务库上至少执行过一次** ``python manage.py migrate``(含 ``sync`` 应用),确保存在 ``pis_sync_record`` 等表;否则同步接口仍可返回 JSON,但审计表无数据、依赖 ``SyncRecord`` 的断言会失败。
|
||
- 依赖 ``mssql-django`` 的 ``DatabaseCreation`` 补丁(见下方 ``_install_shared_test_database``)。
|
||
|
||
**无 SQL Server 时(本地 CI / 开发机)**
|
||
|
||
- 设置环境变量 ``PIS_TEST_USE_SQLITE=1``(或 ``TEST_USE_SQLITE=1``):使用项目目录下 ``.pytest/pis_test_runner.sqlite3``,
|
||
走 Django **默认**测试库创建与 ``migrate``,不挂「复用库」逻辑。
|
||
|
||
本模块仅处理 **SQL Server(mssql-django)** 与上述 SQLite 测试模式,不再包含 PostgreSQL / MySQL 专用分支。
|
||
"""
|
||
import os
|
||
|
||
from application.settings import * # noqa
|
||
|
||
_use_sqlite_for_tests = os.environ.get(
|
||
"PIS_TEST_USE_SQLITE", os.environ.get("TEST_USE_SQLITE", "")
|
||
).lower() in ("1", "true", "yes")
|
||
|
||
if _use_sqlite_for_tests:
|
||
_sqlite_dir = BASE_DIR / ".pytest"
|
||
_sqlite_dir.mkdir(exist_ok=True)
|
||
DATABASES = {
|
||
"default": {
|
||
"ENGINE": "django.db.backends.sqlite3",
|
||
"NAME": str(_sqlite_dir / "pis_test_runner.sqlite3"),
|
||
"TEST": {
|
||
"SERIALIZE": False,
|
||
},
|
||
}
|
||
}
|
||
else:
|
||
DATABASES["default"].setdefault("TEST", {})
|
||
DATABASES["default"]["TEST"]["NAME"] = DATABASE_NAME
|
||
DATABASES["default"]["TEST"]["SERIALIZE"] = False
|
||
|
||
|
||
def _install_shared_test_database(creation_cls, base_module):
|
||
"""
|
||
绑定测试到已有库:不建库、不 migrate,仅切换连接并初始化缓存表(失败则忽略)。
|
||
"""
|
||
|
||
class _SharedTestDatabase(creation_cls):
|
||
def _create_test_db(self, verbosity, autoclobber=False, keepdb=False):
|
||
return self._get_test_db_name()
|
||
|
||
def _destroy_test_db(self, test_database_name, verbosity):
|
||
pass
|
||
|
||
def create_test_db(
|
||
self, verbosity=1, autoclobber=False, serialize=True, keepdb=False
|
||
):
|
||
from django.conf import settings as django_settings
|
||
from django.core.management import call_command
|
||
|
||
test_database_name = self._get_test_db_name()
|
||
if verbosity >= 1:
|
||
self.log(
|
||
"Binding tests to shared database %s (skip migrate)..."
|
||
% self._get_database_display_str(verbosity, test_database_name)
|
||
)
|
||
|
||
self._create_test_db(verbosity, autoclobber, keepdb)
|
||
|
||
self.connection.close()
|
||
django_settings.DATABASES[self.connection.alias]["NAME"] = test_database_name
|
||
self.connection.settings_dict["NAME"] = test_database_name
|
||
|
||
if serialize:
|
||
self.connection._test_serialized_contents = (
|
||
self.serialize_db_to_string()
|
||
)
|
||
|
||
try:
|
||
call_command(
|
||
"createcachetable",
|
||
database=self.connection.alias,
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
self.connection.ensure_connection()
|
||
return test_database_name
|
||
|
||
base_module.DatabaseWrapper.creation_class = _SharedTestDatabase
|
||
|
||
|
||
if not _use_sqlite_for_tests:
|
||
_engine = (DATABASES["default"].get("ENGINE") or "").lower()
|
||
if "mssql" in _engine or "sql_server" in _engine:
|
||
import mssql.base
|
||
from mssql.creation import DatabaseCreation as _MsSqlDatabaseCreation
|
||
|
||
_install_shared_test_database(_MsSqlDatabaseCreation, mssql.base)
|