Release query Tools v0.1.0
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseData } from "../../src/core/data";
|
||||
describe("data parser", () => {
|
||||
it("parses CSV inference and NDJSON", () => {
|
||||
expect(parseData("name,n\nAda,3", "csv").rows[0]).toEqual({
|
||||
name: "Ada",
|
||||
n: 3,
|
||||
});
|
||||
expect(parseData('{"a":1}\n{"a":2}', "ndjson").rows).toHaveLength(2);
|
||||
});
|
||||
it("converts static XML and rejects entities", () => {
|
||||
expect(
|
||||
parseData(
|
||||
"<items><item id='1'>A</item><item id='2'>B</item></items>",
|
||||
"xml",
|
||||
).root,
|
||||
).toEqual({
|
||||
items: {
|
||||
item: [
|
||||
{ "@id": "1", "#text": "A" },
|
||||
{ "@id": "2", "#text": "B" },
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(() =>
|
||||
parseData('<!DOCTYPE x [<!ENTITY y "z">]><x>&y;</x>', "xml"),
|
||||
).toThrow(/entity/iu);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseData } from "../../src/core/data";
|
||||
import { 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,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user