factory/.codex/memory/reference_test_database.md

16 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 项目测试数据库
- 生产问题查询时,默认数据库连接可以根据工厂或环境切换到不同 IP 和业务库;这类连接只用于授权范围内的生产数据只读验证。
- 所有 Django、pytest 及其他自动化测试必须与当前生产查询连接解耦,始终使用固定的 `test_ehs_develop` 测试数据库连接。
- 不能仅依赖当前默认连接的 Django 自动 `test_<NAME>` 命名;即使当前业务库是 `bxerp` 或其他库,测试也不应转而使用 `test_bxerp` 或其他派生库。
- 不得在生产数据库上运行会建表、迁移、写入或清理数据的测试。
- 生产查询与固定测试库的连接参数均从本机已忽略配置中读取,不在项目记忆、源码或提交信息中记录凭据。
当前实现:
- 固定测试连接保存在已忽略的 `config/conf_test.py`
- `server/test_settings.py` 加载该连接,`manage.py test` 会自动选择测试 settings其他 `manage.py` 命令仍使用当前业务库连接。
- 测试 settings 中的基础 `NAME``TEST.NAME` 都必须是 `test_ehs_develop`,并在启动时校验,防止通过测试 settings 误操作 `ehs_develop` 或任何生产库。
- pytest-django 通过项目根目录 `pytest.ini` 固定使用 `server.test_settings`
- 本地重复运行测试时优先使用 `.venv\\Scripts\\python.exe manage.py test --keepdb --noinput`