Files
query-tools/tests/core/query.test.ts
T
zemion f44d0598da
Verify / verify (push) Canceled after 0s
Release Query Tools 0.2.0
2026-09-02 10:28:20 +02:00

63 lines
1.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { parseData } from "../../src/core/data";
import { parsePathQuery, parseSqlQuery, runQuery } from "../../src/core/query";
describe("query engine", () => {
const root = parseData(
'[{"team":"a","n":2},{"team":"a","n":4},{"team":"b","n":9}]',
"json",
).root;
it("filters groups aggregates sorts and limits", () => {
expect(
runQuery(
root,
"SELECT team, COUNT(*) AS count, AVG(n) AS mean WHERE n >= 2 GROUP BY team ORDER BY mean DESC LIMIT 2",
"sql",
).rows,
).toEqual([
{ team: "b", count: 1, mean: 9 },
{ team: "a", count: 2, mean: 3 },
]);
});
it("supports bounded path navigation and filtering", () => {
expect(runQuery(root, "$[?(@.n >= 4)].team", "path").value).toEqual([
"a",
"b",
]);
});
it("rejects unsupported syntax rather than evaluating it", () => {
expect(() => runQuery(root, "SELECT eval(n)", "sql")).toThrow(
/Invalid SELECT/iu,
);
expect(() => runQuery(root, "$..team", "path")).toThrow(
/Unsupported path/iu,
);
});
it("exposes parsed SQL and path ASTs", () => {
expect(
parseSqlQuery(
"SELECT team AS group_name WHERE n >= 2 ORDER BY n DESC LIMIT 4",
),
).toMatchObject({
type: "sql-query",
select: [{ type: "field", path: ["team"], alias: "group_name" }],
where: [{ type: "condition", path: ["n"], operator: ">=", value: 2 }],
orderBy: { path: ["n"], direction: "DESC" },
limit: 4,
});
expect(parsePathQuery("$[*][?(@.n >= 4)].team").segments).toEqual([
{ type: "wildcard" },
{
type: "filter",
condition: {
type: "condition",
path: ["n"],
operator: ">=",
value: 4,
},
},
{ type: "property", key: "team", quoted: false },
]);
});
});