Files
cms-website/tests/integration/db.sites.test.ts
T
浅念 f1c181de02 test(db): 空库一键迁移验收 + globalSetup 临时库 + 公共夹具收敛(阶段1验收)
- vitest globalSetup:每次运行 CREATE sf_test_<ts> 临时库 → drizzle-orm 程序化迁移器
  全量迁移(apps/api/src/db/migrate.ts,drizzle-kit 无法指向运行时建库)→ 设
  DATABASE_URL_TEST 让 getDb() 自动回退到临时库;teardown DROP(WITH FORCE)。
  有 default 导出时 vitest 不捕获具名 teardown,故用双具名导出。
- db.full-migrate.test.ts:21 表齐 + 四张不可变触发器在库 + sf_test_* 护栏断言
  (current_database 不符即红,杜绝静默连开发库)。
- helpers.ts 收敛:rand/rejectionMessage/createSiteFixture/withImmutableCleanup/
  withTestDb,Task 6–12 七文件内联版替换为 import(断言、对照组、清场顺序不变)。
- scripts/migrate-test.sh:docker compose up --wait + pnpm test 一键验收。
- 实证:75 测试全绿(72+3);开发库 21 表行数总和 0(零写入);无 sf_test_* 残留。

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-20 20:43:13 +08:00

25 lines
1.3 KiB
TypeScript
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.
// 站点域集成测试:约束在真 PG 上生效(唯一键、复合唯一键)。
// Task 13 起夹具走公共 helpers(createSiteFixture/withTestDb);
// 生命周期纪律不变:随机 key 插入 + finally 清理,可重复运行不残留脏数据。
import { describe, it, expect } from 'vitest'
import { createSiteFixture, withTestDb } from './helpers.js'
describe('站点域约束', () => {
it('sites.key 唯一;site_config_revisions (site_id,version) 唯一', async () => {
await withTestDb(async (db) => {
const site = await createSiteFixture(db, 'fixture')
try {
await expect(db.execute(`insert into sites (id, key, name, default_locale, production_origin)
values (gen_random_uuid(), '${site.key}', 'Dup', 'en', 'https://x.example')`)).rejects.toThrow()
await db.execute(`insert into site_config_revisions (id, site_id, version, data, checksum)
values (gen_random_uuid(), '${site.siteId}', 1, '{"a":1}', 'c1')`)
await expect(db.execute(`insert into site_config_revisions (id, site_id, version, data, checksum)
values (gen_random_uuid(), '${site.siteId}', 1, '{"a":2}', 'c2')`)).rejects.toThrow()
} finally {
await db.execute(`delete from site_config_revisions where site_id = '${site.siteId}'`)
await site.cleanup()
}
})
})
})