- 报价单生效自动推销售价 → 配送成本价(台账+重试+rearm,映射优先) - 编辑生效单改价走内部审核(quote_sheet_change_requests) - 双向价格传递:A 提交配送商审核 / B 拉回并反查映射后写价、D3 回执 - SKU 映射:D4 字典推送、D5 完整拉取(截断不删)、只读本地副本 - 迁移登记 tenant_business_tables(三张租户表数组互相补齐,避免顺序敏感)
119 lines
5.0 KiB
TypeScript
119 lines
5.0 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
|
|
/**
|
|
* 报价单成本价同步台账**真库**验证(默认跳过;`QUOTE_COST_SYNC_DB_TEST=1` 才跑)。
|
|
*
|
|
* 为什么需要这一份:单测里的 DB 是脚本化 mock(等于我写的第二份 SQL 语义),
|
|
* 这一份连真 Postgres(本地 `supplier_delivery`),只验证**不依赖对方系统**的那一半:
|
|
*
|
|
* · 迁移真的落库(表 / 唯一索引 / `status` CHECK 都存在,不是只写在文件里);
|
|
* · 唯一键 `(quote_sheet_code, main_branch_code)` 真的会让 `ON CONFLICT DO NOTHING` 命中
|
|
* ⇒ "一张单 × 一个分公司只推一次" 有硬约束兜底(并发下也不会出两行);
|
|
* · `status` CHECK 真的拦住非法取值。
|
|
*
|
|
* 纪律:只碰 `public.main_system_quote_cost_pushes`,且只碰 `branch_code` 以 `BR_TESTQCS` 开头的临时行,跑完删干净。
|
|
*
|
|
* 跑法(本地):
|
|
* cd server && QUOTE_COST_SYNC_DB_TEST=1 npx vitest run tests/mainSystemQuoteCostSyncDb.integration.test.ts
|
|
*/
|
|
|
|
const RUN = process.env.QUOTE_COST_SYNC_DB_TEST === '1';
|
|
const TEST_BRANCH = `BR_TESTQCS_${Date.now()}`;
|
|
const SHEET = `QS_TESTQCS_${Date.now()}`;
|
|
|
|
// 默认(未设开关)连 src/db 都不加载,保证常规套件不受影响
|
|
const db = RUN ? await import('../src/db') : null;
|
|
const pool = () => db!.pool;
|
|
|
|
async function cleanup() {
|
|
await pool().query(`DELETE FROM public.main_system_quote_cost_pushes WHERE branch_code LIKE 'BR_TESTQCS%'`);
|
|
}
|
|
|
|
async function insertLedger(mainBranchCode: string, status = 'pending') {
|
|
return pool().query<{ id: string }>(
|
|
`INSERT INTO public.main_system_quote_cost_pushes
|
|
(branch_code, quote_sheet_code, main_branch_code, client_request_id, status)
|
|
VALUES ($1, $2, $3, gen_random_uuid(), $4::varchar)
|
|
ON CONFLICT (quote_sheet_code, main_branch_code) DO NOTHING
|
|
RETURNING id::text AS id`,
|
|
[TEST_BRANCH, SHEET, mainBranchCode, status]
|
|
);
|
|
}
|
|
|
|
describe.skipIf(!RUN)('报价单成本价同步台账 · 真库(迁移约束 + 幂等键)', () => {
|
|
beforeAll(async () => {
|
|
await cleanup();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await cleanup();
|
|
await pool().end();
|
|
});
|
|
|
|
it('迁移已落库:表 / 唯一索引 / status CHECK 都在 public', async () => {
|
|
const table = await pool().query(
|
|
`SELECT table_name FROM information_schema.tables
|
|
WHERE table_schema = 'public' AND table_name = 'main_system_quote_cost_pushes'`
|
|
);
|
|
expect(table.rows).toHaveLength(1);
|
|
|
|
const index = await pool().query(
|
|
`SELECT indexname FROM pg_indexes
|
|
WHERE schemaname = 'public' AND tablename = 'main_system_quote_cost_pushes'
|
|
AND indexname = 'uq_main_system_quote_cost_pushes'`
|
|
);
|
|
expect(index.rows).toHaveLength(1);
|
|
|
|
const check = await pool().query(
|
|
`SELECT conname FROM pg_constraint
|
|
WHERE conrelid = 'public.main_system_quote_cost_pushes'::regclass
|
|
AND conname = 'main_system_quote_cost_pushes_status_chk'`
|
|
);
|
|
expect(check.rows).toHaveLength(1);
|
|
});
|
|
|
|
it('唯一键生效:同一 (报价单, 分公司) 第二次插入被 ON CONFLICT 吃掉(0 行)', async () => {
|
|
const first = await insertLedger('BR_QC_A');
|
|
expect(first.rowCount).toBe(1);
|
|
const second = await insertLedger('BR_QC_A');
|
|
expect(second.rowCount).toBe(0);
|
|
const count = await pool().query<{ n: number }>(
|
|
`SELECT count(*)::int AS n FROM public.main_system_quote_cost_pushes
|
|
WHERE quote_sheet_code = $1 AND main_branch_code = 'BR_QC_A'`,
|
|
[SHEET]
|
|
);
|
|
expect(Number(count.rows[0]?.n)).toBe(1);
|
|
});
|
|
|
|
it('不同分公司各自一行(一单多分公司各推一次)', async () => {
|
|
const a = await insertLedger('BR_QC_B');
|
|
const b = await insertLedger('BR_QC_C');
|
|
expect(a.rowCount).toBe(1);
|
|
expect(b.rowCount).toBe(1);
|
|
});
|
|
|
|
it('status CHECK 拦住非法取值(拼错的终态不会静默入库)', async () => {
|
|
await expect(insertLedger('BR_QC_BAD', 'synced')).rejects.toThrow();
|
|
});
|
|
|
|
it('整单级跳过用 * 也受唯一键约束(重复触发只写一次)', async () => {
|
|
const first = await insertLedger('*', 'skipped');
|
|
const second = await insertLedger('*', 'skipped');
|
|
expect(first.rowCount).toBe(1);
|
|
expect(second.rowCount).toBe(0);
|
|
});
|
|
|
|
// tester 补充(2026-10-18):并发不是「连续插两次」的同义词 —— 并发下真正兜底的是唯一索引本身
|
|
it('并发 10 次同 (报价单, 分公司) 插入 ⇒ 恰好 1 行(唯一键在并发下收敛,不是靠应用层先查后写)', async () => {
|
|
const results = await Promise.all(Array.from({ length: 10 }, () => insertLedger('BR_QC_RACE')));
|
|
const inserted = results.filter((r) => r.rowCount === 1).length;
|
|
expect(inserted, '只允许 1 次插入成功').toBe(1);
|
|
const count = await pool().query<{ n: number }>(
|
|
`SELECT count(*)::int AS n FROM public.main_system_quote_cost_pushes
|
|
WHERE quote_sheet_code = $1 AND main_branch_code = 'BR_QC_RACE'`,
|
|
[SHEET]
|
|
);
|
|
expect(Number(count.rows[0]?.n)).toBe(1);
|
|
}, 30000);
|
|
});
|