Files
SupplierSystem/server/tests/afterSaleMainReturnAmountBackfill.test.ts
T

36 lines
1.5 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 { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
/**
* 20261072 金额回填契约:after_sales.amount 只补主系统退回、且金额为空/0 的售后单;
* 金额 = Σ(退回 quantity × 本地 order_items 最早一行 price);无匹配/价格 0 跳过;可重复执行。
*/
const sql = readFileSync(
path.join(__dirname, '..', '..', 'migrations', '20261072_after_sales_main_return_amount_backfill.sql'),
'utf8'
);
describe('20261072 售后金额回填', () => {
it('只补主系统退回、amount 为空/0 的行', () => {
expect(sql).toContain('UPDATE %1$I.after_sales a');
expect(sql).toContain("a.aftersale_type = 'main_system_return'");
expect(sql).toMatch(/a\.amount IS NULL OR a\.amount = 0/);
});
it('按 main_system 退回行 × 本地最早 order_items.price 求和', () => {
expect(sql).toContain("r.source = 'main_system'");
expect(sql).toContain('r.quantity * oi.price');
expect(sql).toMatch(/ORDER BY oi\.created_at ASC, oi\.id ASC/);
expect(sql).toContain('ROUND(SUM(r.quantity * oi.price)::numeric, 2)');
expect(sql).toContain('oi.price > 0');
});
it('带 FORCE RLS 上下文,且可重复执行(第二次只补 amount 为空的行)', () => {
expect(sql).toMatch(/set_config\('app\.branch_code'/);
expect(sql).toContain('set_config(\'search_path\'');
expect(sql).toMatch(/DO \$\$/);
expect(sql).toContain("schema_name LIKE 'tenant\\_%'");
});
});