test: 拆分慢速迁移测试并优化 pre-push
- 迁移测试标记 migration,单独运行约 130 秒 - 非迁移测试约 90 秒,日常开发跑快速集 - pre-push 默认跳过 migration;改迁移/模型/迁移测试时自动全量 - FULL_TESTS=1 可强制全量,不删除任何迁移测试
This commit is contained in:
+11
-2
@@ -18,6 +18,7 @@ fi
|
||||
|
||||
python_files=()
|
||||
frontend_changed=0
|
||||
migration_changed=0
|
||||
while read -r local_ref local_sha remote_ref remote_sha; do
|
||||
[ -z "$local_sha" ] && continue
|
||||
if [ "$local_sha" = "0000000000000000000000000000000000000000" ]; then
|
||||
@@ -37,6 +38,9 @@ while read -r local_ref local_sha remote_ref remote_sha; do
|
||||
web/frontend/*)
|
||||
frontend_changed=1
|
||||
;;
|
||||
web/backend/migrations/*|web/backend/models.py|tests/test_migrations.py|tests/test_migration_*.py|tests/test_login_attempt_migration.py)
|
||||
migration_changed=1
|
||||
;;
|
||||
esac
|
||||
done <<< "$changed_paths"
|
||||
done <<< "$push_lines"
|
||||
@@ -88,6 +92,11 @@ ensure_test_mysql() {
|
||||
|
||||
ensure_test_mysql
|
||||
|
||||
echo "[pre-push] 运行 pytest..."
|
||||
./dev.sh test -q
|
||||
if [ "${FULL_TESTS:-0}" = "1" ] || [ "$migration_changed" -eq 1 ]; then
|
||||
echo "[pre-push] 运行全量 pytest(含迁移测试)..."
|
||||
./dev.sh test -q
|
||||
else
|
||||
echo "[pre-push] 运行 pytest(跳过慢速迁移测试;FULL_TESTS=1 可跑全量)..."
|
||||
./dev.sh test -q -m "not migration"
|
||||
fi
|
||||
echo "[pre-push] pytest 通过"
|
||||
|
||||
@@ -142,7 +142,9 @@ python3 -c 'import secrets; print(secrets.token_urlsafe(32))'
|
||||
## 运行测试
|
||||
|
||||
```bash
|
||||
uv run --group dev pytest # 或 ./dev.sh test
|
||||
uv run --group dev pytest # 全量(含迁移测试,约 3-4 分钟)
|
||||
./dev.sh test -q -m "not migration" # 日常快速:约 90 秒,跳过 22 个慢速迁移测试
|
||||
./dev.sh test -q -m migration # 只跑迁移/回填测试
|
||||
uv run --group dev pytest tests/test_cookie_operations.py
|
||||
./dev.sh format tests conftest.py
|
||||
./dev.sh format-check tests conftest.py
|
||||
@@ -150,6 +152,11 @@ uv run --group dev pytest tests/test_cookie_operations.py
|
||||
docker compose --profile test run --rm test
|
||||
```
|
||||
|
||||
迁移测试会为每个用例创建临时 MySQL 库并重跑 Alembic 链,约占全量时间
|
||||
**60%**;保留它们,不改历史迁移数据。pre-push 默认跳过迁移测试,只有改到
|
||||
`web/backend/migrations/`、`web/backend/models.py` 或迁移测试文件时才跑全量;
|
||||
需要强制全量时用 `FULL_TESTS=1 git push`。
|
||||
|
||||
测试跑在**真实 MySQL** 上(不再使用 SQLite):生产是 MySQL,SQLite 与它有方言差异,
|
||||
曾出现「`ORDER BY ... NULLS FIRST` 本地全绿、生产直接 1064」的事故。因此:
|
||||
|
||||
@@ -170,7 +177,7 @@ docker compose --profile test run --rm test
|
||||
```
|
||||
|
||||
- **pre-commit**:暂存的 Python 文件做 Ruff 格式检查;改了 `web/frontend/` 则跑 `tsc -b`
|
||||
- **pre-push**:上述检查 + 全量 pytest
|
||||
- **pre-push**:上述检查 + pytest(普通改动跳过 migration marker;迁移/模型改动自动全量)
|
||||
- Pyright 默认不开(噪音大):`TYPECHECK=1 git push`
|
||||
- 紧急绕过:`git push --no-verify` 或 `SKIP_TESTS=1 git push`
|
||||
|
||||
|
||||
@@ -44,6 +44,9 @@ packages = ["core", "utils", "web"]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["tests"]
|
||||
markers = [
|
||||
"migration: 创建临时 MySQL 库并执行 Alembic 迁移链;比普通用例慢很多",
|
||||
]
|
||||
|
||||
[tool.ruff.lint.per-file-ignores]
|
||||
# FastAPI evaluates dependency metadata in route parameter defaults by design.
|
||||
|
||||
@@ -10,6 +10,7 @@ import secrets
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
import pytest
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from sqlalchemy import create_engine, text
|
||||
@@ -45,6 +46,7 @@ def _insert_task(conn, *, status: str, cookie: str, message: str = "") -> None:
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.migration
|
||||
class TestLoginAttemptMigration:
|
||||
def setup_method(self):
|
||||
creds = get_mysql_credentials()
|
||||
|
||||
@@ -5,6 +5,7 @@ import secrets
|
||||
from datetime import UTC, datetime
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
import pytest
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from sqlalchemy import create_engine, inspect, text
|
||||
@@ -15,6 +16,7 @@ from tests.support.db import get_mysql_credentials
|
||||
TARGET_REVISION = "20261106_0065"
|
||||
|
||||
|
||||
@pytest.mark.migration
|
||||
def test_0065_upgrade_downgrade_roundtrip():
|
||||
creds = get_mysql_credentials()
|
||||
mig_db = f"douyu_login_mig_rt_{secrets.token_hex(4)}"
|
||||
|
||||
@@ -15,6 +15,7 @@ from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
import pytest
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from alembic.script import ScriptDirectory
|
||||
@@ -132,6 +133,7 @@ def _migration_config() -> Config:
|
||||
return config
|
||||
|
||||
|
||||
@pytest.mark.migration
|
||||
class TestMigrationSmoke:
|
||||
def test_chain_is_linear_and_head_matches_latest_file(self):
|
||||
directory = ScriptDirectory.from_config(_migration_config())
|
||||
|
||||
Reference in New Issue
Block a user