Files
cms-website/apps/api/test/db.connect.test.ts
T

27 lines
1.1 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.
import { describe, it, expect } from 'vitest'
import { getDb } from '../src/db/client.js'
describe('数据库连接', () => {
it('连接本项目 PG(5434)并响应 select 1', async () => {
const db = getDb()
// postgres.js 驱动下 drizzle 的 execute 返回 RowList(行数组),
// 不是 node-postgres 风格的 { rows: [...] },故直接取 r[0]
const r = (await db.execute('select 1 as ok')) as any
expect(Number(r[0].ok)).toBe(1)
})
it('DATABASE_URL 缺失时 getDb 早失败并给出诊断信息(不静默连 localhost)', () => {
const savedTest = process.env.DATABASE_URL_TEST
const saved = process.env.DATABASE_URL
delete process.env.DATABASE_URL_TEST
delete process.env.DATABASE_URL
try {
// postgres(undefined) 会静默回退 OS 用户@localhost:5432,必须在此前置拦截
expect(() => getDb()).toThrow(/DATABASE_URL not set/)
} finally {
if (savedTest !== undefined) process.env.DATABASE_URL_TEST = savedTest
if (saved !== undefined) process.env.DATABASE_URL = saved
}
})
})