fix(api+tests): CI unit-test failures for WS progress endpoint #2104

Merged
auto-approve-bot merged 2 commits from feat/2051-ws-ci-fix into develop 2026-09-30 11:05:57 +08:00
Owner

问题

#2103 合入后,CI Unit Tests job 仍然失败(两个原因):

  1. app/db.py 和 app/dependencies.py 没有 honour settings.effective_database_url:这两个文件在模块级初始化 engine/SessionLocal 时硬编码用了 settings.DATABASE_URL(Postgres 地址),而 CI 的 unit-tests job 不启动 Postgres 服务(靠 USE_IN_MEMORY_DB=true 走 SQLite)。worker 的 worker_app/db.py 早就正确使用了 effective_database_url 属性,API 这两个文件没跟上,导致单测 import app.db 时连不上 Postgres → OperationalError。

  2. test_viral_video_ws.py 用全局 _SRC_PATCHES 双路 patch sqlalchemy_impl 包:作为 workaround 我之前加了模块级 patch,但这会污染 sys.modules 导致同进程跑其他 viral_video 测试时拿到 MagicMock 而非真实 session factory;另外 from worker_app.tasks import viral_video 走了另一条 sys.modules 路径创建了第二个 module 对象,patch 通过 apps.worker.* 路径打上去的东西在 worker_app.* 路径下看不到,造成跨文件测试隔离失败。

修复

  • apps/api/app/db.py、apps/api/app/dependencies.py:将 settings.DATABASE_URL 改为 settings.effective_database_url,与 worker_app 对齐。
  • tests/unit/test_viral_video_ws.py:移除所有全局 _SRC_PATCHES hack;改在各测试内部用 patch.object(_app_db, "SessionLocal", ...) 局部 patch;worker 模块 import 改为 from apps.worker.worker_app.tasks import viral_video 与其他测试保持一致。
  • 测试结果:CI-like 环境(USE_IN_MEMORY_DB=true + 不可达 DATABASE_URL)下 viral_video 全部 63 个测试一起通过。

关联

## 问题 #2103 合入后,CI Unit Tests job 仍然失败(两个原因): 1. **`app/db.py` 和 `app/dependencies.py` 没有 honour `settings.effective_database_url`**:这两个文件在模块级初始化 engine/SessionLocal 时硬编码用了 `settings.DATABASE_URL`(Postgres 地址),而 CI 的 unit-tests job 不启动 Postgres 服务(靠 `USE_IN_MEMORY_DB=true` 走 SQLite)。worker 的 `worker_app/db.py` 早就正确使用了 `effective_database_url` 属性,API 这两个文件没跟上,导致单测 import app.db 时连不上 Postgres → OperationalError。 2. **test_viral_video_ws.py 用全局 `_SRC_PATCHES` 双路 patch sqlalchemy_impl 包**:作为 workaround 我之前加了模块级 patch,但这会污染 sys.modules 导致同进程跑其他 viral_video 测试时拿到 MagicMock 而非真实 session factory;另外 `from worker_app.tasks import viral_video` 走了另一条 sys.modules 路径创建了第二个 module 对象,patch 通过 `apps.worker.*` 路径打上去的东西在 `worker_app.*` 路径下看不到,造成跨文件测试隔离失败。 ## 修复 - `apps/api/app/db.py`、`apps/api/app/dependencies.py`:将 `settings.DATABASE_URL` 改为 `settings.effective_database_url`,与 worker_app 对齐。 - `tests/unit/test_viral_video_ws.py`:移除所有全局 `_SRC_PATCHES` hack;改在各测试内部用 `patch.object(_app_db, "SessionLocal", ...)` 局部 patch;worker 模块 import 改为 `from apps.worker.worker_app.tasks import viral_video` 与其他测试保持一致。 - 测试结果:CI-like 环境(USE_IN_MEMORY_DB=true + 不可达 DATABASE_URL)下 viral_video 全部 63 个测试一起通过。 ## 关联 - Follow-up to #2103 / #2051
xiaoxia added 1 commit 2026-09-30 10:50:32 +08:00
fix(api): honour effective_database_url for CI SQLite fallback + simplify WS tests
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 56s
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 30s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m41s
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m16s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 3m28s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 3m44s
CI/CD Pipeline / Validate - Style (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Security (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
AI Code Review / AI Code Review (pull_request) Has been cancelled
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been cancelled
c453e81bdd
- apps/api/app/db.py and app/dependencies.py were still using settings.DATABASE_URL
  (hardcoded Postgres URL) instead of settings.effective_database_url, unlike the
  worker_app which correctly uses the property that honours USE_IN_MEMORY_DB.
  This caused OperationalError in CI unit-tests where no Postgres is available.
- Removed the module-level _SRC_PATCHES hack from test_viral_video_ws.py now that
  SQLite fallback works correctly. Switched the worker import to the apps.worker.*
  namespace to avoid creating a second module object that broke cross-file
  test isolation (patch applied through one path was invisible through the other).
- All 63 viral_video tests pass together in CI-like env
  (USE_IN_MEMORY_DB=true with unreachable DATABASE_URL).

🚀 预览环境已部署

项目 详情
PR号 #2104
预览链接 https://pr-2104.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #2104 | | 预览链接 | [https://pr-2104.preview.xiaoxiajianji.com](https://pr-2104.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
auto-approve-bot added 1 commit 2026-09-30 10:55:03 +08:00
style: auto-format with black + isort + ruff + prettier [skip ci-format-check]
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 6s
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 6s
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 52s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 44s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m51s
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m6s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 3m13s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 3m15s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 3m45s
AI Code Review / AI Code Review (pull_request) Successful in 6m38s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 9m57s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 10m0s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 1s
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 7m38s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 35s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 4m55s
778a909718
auto-approve-bot merged commit bf9249da19 into develop 2026-09-30 11:05:57 +08:00
auto-approve-bot deleted branch feat/2051-ws-ci-fix 2026-09-30 11:06:00 +08:00

🗑️ 预览环境已清理

PR #2104 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #2104 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Sign in to join this conversation.