feat: 新增 DB checker — 支持 PostgreSQL/MySQL/SQLite 连接测试与 SQL 查询断言
- 实现 db 类型 checker,使用 Bun 内置 SQL 类 - 支持 db.url 连接字符串和可选 db.query 查询语句 - expect 支持 maxDurationMs、rowCount、rows 逐列校验 - 凭据屏蔽序列化输出 - SQLite 内存数据库测试覆盖
This commit is contained in:
158
tests/server/checker/runner/db/execute.test.ts
Normal file
158
tests/server/checker/runner/db/execute.test.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import type { ResolvedDbTarget } from "../../../../../src/server/checker/runner/db/types";
|
||||
import type { CheckerContext } from "../../../../../src/server/checker/runner/types";
|
||||
|
||||
import { DbChecker } from "../../../../../src/server/checker/runner/db/execute";
|
||||
|
||||
const checker = new DbChecker();
|
||||
|
||||
function makeCtx(timeoutMs = 5000): CheckerContext {
|
||||
const controller = new AbortController();
|
||||
setTimeout(() => controller.abort(), timeoutMs);
|
||||
return { signal: controller.signal };
|
||||
}
|
||||
|
||||
function makeTarget(db: Partial<ResolvedDbTarget["db"]>, overrides?: Partial<ResolvedDbTarget>): ResolvedDbTarget {
|
||||
return {
|
||||
db: {
|
||||
url: "sqlite://:memory:",
|
||||
...db,
|
||||
},
|
||||
group: "default",
|
||||
intervalMs: 60000,
|
||||
name: "test-db",
|
||||
timeoutMs: 5000,
|
||||
type: "db",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("DbChecker", () => {
|
||||
test("无 query 时仅测试连接成功", async () => {
|
||||
const result = await checker.execute(makeTarget({}), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("connected");
|
||||
expect(result.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("执行查询成功", async () => {
|
||||
const result = await checker.execute(makeTarget({ query: "SELECT 1 as num, 'hello' as str" }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("1 rows");
|
||||
expect(result.durationMs).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
test("查询返回多行", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ query: "SELECT 1 as n UNION ALL SELECT 2 UNION ALL SELECT 3" }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("3 rows");
|
||||
});
|
||||
|
||||
test("查询返回空结果", async () => {
|
||||
const result = await checker.execute(makeTarget({ query: "SELECT 1 as n WHERE 1=0" }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("0 rows");
|
||||
});
|
||||
|
||||
test("连接失败返回 connect phase 错误", async () => {
|
||||
const result = await checker.execute(makeTarget({ url: "sqlite:///nonexistent/path/db.db" }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("connect");
|
||||
expect(result.failure!.message).toBeTruthy();
|
||||
});
|
||||
|
||||
test("SQL 语法错误返回 query phase 错误", async () => {
|
||||
const result = await checker.execute(makeTarget({ query: "SELECT INVALID SQL" }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("query");
|
||||
expect(result.failure!.message).toBeTruthy();
|
||||
});
|
||||
|
||||
test("maxDurationMs 超时返回失败", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ query: "SELECT 1" }, { expect: { maxDurationMs: -1 } }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("rowCount 断言通过", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ query: "SELECT 1 UNION ALL SELECT 2" }, { expect: { rowCount: { gte: 2 } } }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("rowCount 断言失败", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ query: "SELECT 1 UNION ALL SELECT 2" }, { expect: { rowCount: { gte: 5 } } }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("rowCount");
|
||||
});
|
||||
|
||||
test("rows 断言通过(字面量形式)", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ query: "SELECT 100 as cnt" }, { expect: { rows: [{ cnt: 100 }] } }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("rows 断言通过(operator 形式)", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ query: "SELECT 150 as cnt" }, { expect: { rows: [{ cnt: { gte: 100 } }] } }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("rows 断言失败", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ query: "SELECT 50 as cnt" }, { expect: { rows: [{ cnt: { gte: 100 } }] } }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("row");
|
||||
expect(result.failure!.path).toBe("rows[0].cnt");
|
||||
});
|
||||
|
||||
test("rows 结果行数不足", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ query: "SELECT 1 as n" }, { expect: { rows: [{ n: 1 }, { n: 2 }] } }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("row");
|
||||
expect(result.failure!.message).toContain("行数不足");
|
||||
});
|
||||
|
||||
test("rows 只检查声明的列", async () => {
|
||||
const result = await checker.execute(
|
||||
makeTarget({ query: "SELECT 1 as cnt, 'ignored' as other" }, { expect: { rows: [{ cnt: { gte: 1 } }] } }),
|
||||
makeCtx(),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("serialize 屏蔽凭据", () => {
|
||||
const target = makeTarget({ url: "postgres://user:pass@host:5432/db" });
|
||||
const s = checker.serialize(target);
|
||||
expect(s.target).toBe("postgres://***:***@host:5432/db");
|
||||
const config = JSON.parse(s.config) as { url: string };
|
||||
expect(config.url).toBe("postgres://***:***@host:5432/db");
|
||||
});
|
||||
|
||||
test("serialize 无凭据的 url 保持原样", () => {
|
||||
const target = makeTarget({ url: "sqlite:///data/app.db" });
|
||||
const s = checker.serialize(target);
|
||||
expect(s.target).toBe("sqlite:///data/app.db");
|
||||
});
|
||||
});
|
||||
134
tests/server/checker/runner/db/expect.test.ts
Normal file
134
tests/server/checker/runner/db/expect.test.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import { checkRowCount, checkRows } from "../../../../../src/server/checker/runner/db/expect";
|
||||
|
||||
describe("checkRowCount", () => {
|
||||
test("空数组通过 rowCount gte 0", () => {
|
||||
const result = checkRowCount([], { gte: 0 });
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("非数组视为 0 行", () => {
|
||||
const result = checkRowCount(null, { gte: 0 });
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("rowCount gte 通过", () => {
|
||||
const result = checkRowCount([1, 2, 3], { gte: 3 });
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("rowCount gte 失败", () => {
|
||||
const result = checkRowCount([1, 2], { gte: 3 });
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("rowCount");
|
||||
expect(result.failure!.path).toBe("rowCount");
|
||||
});
|
||||
|
||||
test("rowCount equals 通过", () => {
|
||||
const result = checkRowCount([1, 2, 3], { equals: 3 });
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("rowCount equals 失败", () => {
|
||||
const result = checkRowCount([1, 2, 3], { equals: 5 });
|
||||
expect(result.matched).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkRows", () => {
|
||||
test("非数组返回失败", () => {
|
||||
const result = checkRows(null, [{ col: 1 }]);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("row");
|
||||
expect(result.failure!.path).toBe("rows");
|
||||
});
|
||||
|
||||
test("空规则通过", () => {
|
||||
const result = checkRows([], []);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("单行单列匹配(字面量)", () => {
|
||||
const result = checkRows([{ col: "value" }], [{ col: "value" }]);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("单行单列匹配(operator)", () => {
|
||||
const result = checkRows([{ col: 100 }], [{ col: { gte: 50 } }]);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("单行单列不匹配", () => {
|
||||
const result = checkRows([{ col: 10 }], [{ col: { gte: 50 } }]);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("row");
|
||||
expect(result.failure!.path).toBe("rows[0].col");
|
||||
});
|
||||
|
||||
test("多行多列全部匹配", () => {
|
||||
const result = checkRows(
|
||||
[
|
||||
{ id: 1, name: "Alice" },
|
||||
{ id: 2, name: "Bob" },
|
||||
],
|
||||
[{ id: { gte: 1 } }, { name: "Bob" }],
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("多行中有一行不匹配", () => {
|
||||
const result = checkRows([{ col: 1 }, { col: 2 }], [{ col: { gte: 2 } }, { col: { gte: 3 } }]);
|
||||
expect(result.matched).toBe(false);
|
||||
// 第一行 { col: 1 } 不满足 { gte: 2 },所以失败在第一行
|
||||
expect(result.failure!.path).toBe("rows[0].col");
|
||||
});
|
||||
|
||||
test("结果行数不足", () => {
|
||||
const result = checkRows([{ col: 1 }], [{ col: 1 }, { col: 2 }]);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.message).toContain("行数不足");
|
||||
});
|
||||
|
||||
test("只检查声明的列", () => {
|
||||
const result = checkRows([{ col: 1, other: "ignored" }], [{ col: { gte: 0 } }]);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("行不是对象返回失败", () => {
|
||||
const result = checkRows(["not-an-object"] as unknown[], [{ col: 1 }]);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.path).toBe("rows[0]");
|
||||
});
|
||||
|
||||
test("列不存在视为 undefined", () => {
|
||||
const result = checkRows([{}], [{ col: { exists: false } }]);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("列存在且值为 null", () => {
|
||||
const result = checkRows([{ col: null }], [{ col: { empty: true } }]);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("contains 匹配字符串", () => {
|
||||
const result = checkRows([{ text: "hello world" }], [{ text: { contains: "hello" } }]);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("match 正则匹配", () => {
|
||||
const result = checkRows([{ code: "ABC-123" }], [{ code: { match: "^ABC-" } }]);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("多个断言同时满足", () => {
|
||||
const result = checkRows([{ val: 50 }], [{ val: { gte: 10, lte: 100 } }]);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("多个断言中有一个不满足", () => {
|
||||
const result = checkRows([{ val: 50 }], [{ val: { gte: 10, lte: 30 } }]);
|
||||
expect(result.matched).toBe(false);
|
||||
});
|
||||
});
|
||||
154
tests/server/checker/runner/db/validate.test.ts
Normal file
154
tests/server/checker/runner/db/validate.test.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import { validateDbConfig } from "../../../../../src/server/checker/runner/db/validate";
|
||||
|
||||
describe("validateDbConfig", () => {
|
||||
test("空配置无问题", () => {
|
||||
const result = validateDbConfig({ defaults: {}, targets: [] });
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("缺少 db.url 返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ name: "test", type: "db" }],
|
||||
});
|
||||
expect(result.length).toBeGreaterThan(0);
|
||||
const dbError = result.find((e) => e.path.includes("db"));
|
||||
expect(dbError).toBeDefined();
|
||||
expect(dbError!.code).toBe("required");
|
||||
});
|
||||
|
||||
test("db.url 为空字符串返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ db: { url: "" }, name: "test", type: "db" }],
|
||||
});
|
||||
const urlError = result.find((e) => e.path.includes("db.url"));
|
||||
expect(urlError).toBeDefined();
|
||||
expect(urlError!.code).toBe("required");
|
||||
});
|
||||
|
||||
test("db.query 为空字符串返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ db: { query: "", url: "sqlite://:memory:" }, name: "test", type: "db" }],
|
||||
});
|
||||
const queryError = result.find((e) => e.path.includes("db.query"));
|
||||
expect(queryError).toBeDefined();
|
||||
expect(queryError!.code).toBe("invalid-value");
|
||||
});
|
||||
|
||||
test("db 分组未知字段返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ db: { timeout: 5, url: "sqlite://:memory:" }, name: "test", type: "db" }],
|
||||
});
|
||||
const unknownError = result.find((e) => e.path.includes("db.timeout"));
|
||||
expect(unknownError).toBeDefined();
|
||||
expect(unknownError!.code).toBe("unknown-field");
|
||||
});
|
||||
|
||||
test("expect.maxDurationMs 非数字返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ db: { url: "sqlite://:memory:" }, expect: { maxDurationMs: "invalid" }, name: "test", type: "db" }],
|
||||
});
|
||||
const durationError = result.find((e) => e.path.includes("expect.maxDurationMs"));
|
||||
expect(durationError).toBeDefined();
|
||||
expect(durationError!.code).toBe("invalid-type");
|
||||
});
|
||||
|
||||
test("expect.rowCount 非法 operator 返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ db: { url: "sqlite://:memory:" }, expect: { rowCount: { foo: 1 } }, name: "test", type: "db" }],
|
||||
});
|
||||
const rowCountError = result.find((e) => e.path.includes("expect.rowCount"));
|
||||
expect(rowCountError).toBeDefined();
|
||||
expect(rowCountError!.code).toBe("unknown-operator");
|
||||
});
|
||||
|
||||
test("expect.rows 不是数组返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ db: { url: "sqlite://:memory:" }, expect: { rows: "not-array" }, name: "test", type: "db" }],
|
||||
});
|
||||
const rowsError = result.find((e) => e.path.includes("expect.rows"));
|
||||
expect(rowsError).toBeDefined();
|
||||
expect(rowsError!.code).toBe("invalid-type");
|
||||
});
|
||||
|
||||
test("expect.rows 元素不是对象返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ db: { url: "sqlite://:memory:" }, expect: { rows: ["not-object"] }, name: "test", type: "db" }],
|
||||
});
|
||||
const rowError = result.find((e) => e.path.includes("expect.rows[0]"));
|
||||
expect(rowError).toBeDefined();
|
||||
expect(rowError!.code).toBe("invalid-type");
|
||||
});
|
||||
|
||||
test("expect.rows 中 match 正则非法返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [
|
||||
{
|
||||
db: { url: "sqlite://:memory:" },
|
||||
expect: { rows: [{ name: { match: "[invalid" } }] },
|
||||
name: "test",
|
||||
type: "db",
|
||||
},
|
||||
],
|
||||
});
|
||||
const matchError = result.find((e) => e.path.includes("expect.rows[0].name"));
|
||||
expect(matchError).toBeDefined();
|
||||
expect(matchError!.code).toBe("invalid-regex");
|
||||
});
|
||||
|
||||
test("expect 未知字段返回错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ db: { url: "sqlite://:memory:" }, expect: { status: [200] }, name: "test", type: "db" }],
|
||||
});
|
||||
const unknownError = result.find((e) => e.path.includes("expect.status"));
|
||||
expect(unknownError).toBeDefined();
|
||||
expect(unknownError!.code).toBe("unknown-field");
|
||||
});
|
||||
|
||||
test("有效配置无错误", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [
|
||||
{
|
||||
db: { query: "SELECT 1", url: "sqlite://:memory:" },
|
||||
expect: { maxDurationMs: 5000, rowCount: { gte: 1 }, rows: [{ cnt: { gte: 1 } }] },
|
||||
name: "test",
|
||||
type: "db",
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("忽略非 db 类型 target", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [{ name: "test", type: "http" }],
|
||||
});
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("多个 db target 分别校验", () => {
|
||||
const result = validateDbConfig({
|
||||
defaults: {},
|
||||
targets: [
|
||||
{ db: { url: "sqlite://:memory:" }, name: "db1", type: "db" },
|
||||
{ db: { url: "" }, name: "db2", type: "db" },
|
||||
],
|
||||
});
|
||||
expect(result.length).toBeGreaterThan(0);
|
||||
const db2Error = result.find((e) => e.targetName === "db2");
|
||||
expect(db2Error).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -66,8 +66,8 @@ describe("CheckerRegistry", () => {
|
||||
const second = createDefaultCheckerRegistry();
|
||||
first.register(createChecker("custom"));
|
||||
|
||||
expect(first.supportedTypes).toEqual(["http", "cmd", "custom"]);
|
||||
expect(second.supportedTypes).toEqual(["http", "cmd"]);
|
||||
expect(first.supportedTypes).toEqual(["http", "cmd", "db", "custom"]);
|
||||
expect(second.supportedTypes).toEqual(["http", "cmd", "db"]);
|
||||
expect(
|
||||
first.definitions.every(
|
||||
(checker) => checker.schemas.config && checker.schemas.defaults && checker.schemas.expect,
|
||||
|
||||
Reference in New Issue
Block a user