From b0be3d15ccc3b7d7e4ed09967c5d799d8d2dd8b5 Mon Sep 17 00:00:00 2001 From: yml Date: Tue, 24 Nov 2026 19:14:40 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=8B=86=E5=88=86=E6=85=A2=E9=80=9F?= =?UTF-8?q?=E8=BF=81=E7=A7=BB=E6=B5=8B=E8=AF=95=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=20pre-push?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 迁移测试标记 migration,单独运行约 130 秒 - 非迁移测试约 90 秒,日常开发跑快速集 - pre-push 默认跳过 migration;改迁移/模型/迁移测试时自动全量 - FULL_TESTS=1 可强制全量,不删除任何迁移测试 --- .githooks/pre-push | 13 +++++++++++-- README.md | 11 +++++++++-- pyproject.toml | 3 +++ tests/test_login_attempt_migration.py | 2 ++ tests/test_migration_high_value_roundtrip.py | 2 ++ tests/test_migrations.py | 2 ++ 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/.githooks/pre-push b/.githooks/pre-push index 3215524..f7932aa 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -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 通过" diff --git a/README.md b/README.md index 32e075c..4133491 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/pyproject.toml b/pyproject.toml index 8e86c98..f7875cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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. diff --git a/tests/test_login_attempt_migration.py b/tests/test_login_attempt_migration.py index 2ac17b4..3bf5f5f 100644 --- a/tests/test_login_attempt_migration.py +++ b/tests/test_login_attempt_migration.py @@ -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() diff --git a/tests/test_migration_high_value_roundtrip.py b/tests/test_migration_high_value_roundtrip.py index f37ef43..7d96e7e 100644 --- a/tests/test_migration_high_value_roundtrip.py +++ b/tests/test_migration_high_value_roundtrip.py @@ -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)}" diff --git a/tests/test_migrations.py b/tests/test_migrations.py index db1fca4..43106e9 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -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())