65b0bb04f8
把 CarLog v2.8 全套源码 + 配置导入到 i 仓库作为 baseline: - server/src/ (13 个路由 + middleware + services + config) - server/migrations/ (0001~0018 共 18 个迁移 + mysql) - server/test/ (12 文件 101 测试) - client/src/ (20 个 view + components + stores + api + composables) - client/public/ + client/scripts/ - 全部配置文件 (.editorconfig, .eslintrc.json, .prettierrc.json, vitest.config.js, lighthouserc.json, .pa11yci.json, package.json, carlog-init.sql) - .husky/pre-commit (git hooks) - docs/install/ (宝塔部署文档) 不含: - node_modules/ (本地 npm install) - .env (敏感, 走 .env.example) - *.zip / *.log / *.sqlite / .DS_Store 新增文档 docs/DEV-PLAN.md: - Phase 1: 平台基座 (019 migration + 3 个 platform 路由 + 3 个 view) - Phase 2: CarLog 子系统化 (后端 routes/ → subsystems/carlog/ + 前端 views/ → views/subsystems/carlog/ + 元数据驱动菜单) - Phase 3: 验证 (测试 + E2E + DB 完整性) - 交付清单 + commit 模板 + 给 Mavis review 的材料 后续 Trae 实施, 提交后我 code review + 跑测试。
71 lines
2.9 KiB
JavaScript
71 lines
2.9 KiB
JavaScript
// server/test/db.softWhere.test.js
|
|
// 测试 softWhere() helper 在所有 SQL 形态下的行为
|
|
import { describe, it, expect } from 'vitest';
|
|
import { softWhere } from '../src/db.js';
|
|
|
|
describe('softWhere()', () => {
|
|
it('纯 SELECT 无 WHERE → 末尾追加 WHERE is_deleted = 0', () => {
|
|
expect(softWhere('vehicles', 'SELECT * FROM vehicles')).toBe(
|
|
'SELECT * FROM vehicles WHERE vehicles.is_deleted = 0'
|
|
);
|
|
});
|
|
|
|
it('SELECT ... WHERE id = ? → 注入到 WHERE 之后', () => {
|
|
const r = softWhere('vehicles', 'SELECT * FROM vehicles WHERE id = ?');
|
|
expect(r).toBe('SELECT * FROM vehicles WHERE vehicles.is_deleted = 0 AND id = ?');
|
|
});
|
|
|
|
it('WHERE 子句前替换', () => {
|
|
const r = softWhere('vehicles', 'SELECT * FROM vehicles WHERE plate LIKE ?', 'v');
|
|
expect(r).toBe('SELECT * FROM vehicles WHERE v.is_deleted = 0 AND plate LIKE ?');
|
|
});
|
|
|
|
it('已有 is_deleted 条件 → 不重复', () => {
|
|
const sql = 'SELECT * FROM vehicles WHERE is_deleted = 0 AND id = ?';
|
|
expect(softWhere('vehicles', sql)).toBe(sql);
|
|
});
|
|
|
|
it('表带别名 → 使用别名', () => {
|
|
const r = softWhere('vehicles', 'SELECT v.* FROM vehicles v WHERE v.id = ?', 'v');
|
|
expect(r).toBe('SELECT v.* FROM vehicles v WHERE v.is_deleted = 0 AND v.id = ?');
|
|
});
|
|
|
|
it('ORDER BY 在末尾 → WHERE 插在 ORDER 之前', () => {
|
|
const r = softWhere('vehicles', 'SELECT * FROM vehicles ORDER BY id DESC');
|
|
expect(r).toBe('SELECT * FROM vehicles WHERE vehicles.is_deleted = 0 ORDER BY id DESC');
|
|
});
|
|
|
|
it('GROUP BY 在末尾 → WHERE 插在 GROUP 之前', () => {
|
|
const r = softWhere('vehicles', 'SELECT COUNT(*) FROM vehicles GROUP BY type');
|
|
expect(r).toBe(
|
|
'SELECT COUNT(*) FROM vehicles WHERE vehicles.is_deleted = 0 GROUP BY type'
|
|
);
|
|
});
|
|
|
|
it('LIMIT 在末尾 → WHERE 插在 LIMIT 之前', () => {
|
|
const r = softWhere('vehicles', 'SELECT * FROM vehicles LIMIT 10');
|
|
expect(r).toBe('SELECT * FROM vehicles WHERE vehicles.is_deleted = 0 LIMIT 10');
|
|
});
|
|
|
|
it('末尾分号 → 去掉再追加', () => {
|
|
const r = softWhere('vehicles', 'SELECT * FROM vehicles;');
|
|
expect(r).toBe('SELECT * FROM vehicles WHERE vehicles.is_deleted = 0');
|
|
});
|
|
|
|
it('is_deleted 写为大写 IS_DELETED → 跳过', () => {
|
|
const sql = 'SELECT * FROM vehicles WHERE IS_DELETED = 0';
|
|
expect(softWhere('vehicles', sql)).toBe(sql);
|
|
});
|
|
|
|
it('不区分表名大小写', () => {
|
|
const r = softWhere('VEHICLES', 'SELECT * FROM vehicles');
|
|
expect(r).toBe('SELECT * FROM vehicles WHERE VEHICLES.is_deleted = 0');
|
|
});
|
|
|
|
it('UPDATE/DELETE 也支持', () => {
|
|
expect(softWhere('vehicles', 'DELETE FROM vehicles WHERE id = ?')).toBe(
|
|
'DELETE FROM vehicles WHERE vehicles.is_deleted = 0 AND id = ?'
|
|
);
|
|
});
|
|
});
|