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>
This commit is contained in:
@@ -1,26 +1,24 @@
|
||||
// 站点域集成测试:约束在真 PG 上生效(唯一键、复合唯一键)。
|
||||
// 简易内联版——Task 13 收敛为公共 helper;本任务只需连库即可(getDb 从 apps/api 源码导入)。
|
||||
// 生命周期纪律:随机 key 插入 + finally 清理,可重复运行不残留脏数据。
|
||||
// Task 13 起夹具走公共 helpers(createSiteFixture/withTestDb);
|
||||
// 生命周期纪律不变:随机 key 插入 + finally 清理,可重复运行不残留脏数据。
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { getDb } from '../../apps/api/src/db/client.js'
|
||||
import { createSiteFixture, withTestDb } from './helpers.js'
|
||||
|
||||
describe('站点域约束', () => {
|
||||
it('sites.key 唯一;site_config_revisions (site_id,version) 唯一', async () => {
|
||||
const db = getDb()
|
||||
const key = `fixture-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
|
||||
const r = (await db.execute(`insert into sites (id, key, name, default_locale, production_origin)
|
||||
values (gen_random_uuid(), '${key}', 'Fixture', 'en', 'https://fixture.example') returning id`)) as any
|
||||
const siteId = r[0].id
|
||||
try {
|
||||
await expect(db.execute(`insert into sites (id, key, name, default_locale, production_origin)
|
||||
values (gen_random_uuid(), '${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(), '${siteId}', 1, '{"a":1}', 'c1')`)
|
||||
await expect(db.execute(`insert into site_config_revisions (id, site_id, version, data, checksum)
|
||||
values (gen_random_uuid(), '${siteId}', 1, '{"a":2}', 'c2')`)).rejects.toThrow()
|
||||
} finally {
|
||||
await db.execute(`delete from site_config_revisions where site_id = '${siteId}'`)
|
||||
await db.execute(`delete from sites where id = '${siteId}'`)
|
||||
}
|
||||
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()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user