+36
-1
@@ -2,12 +2,47 @@ 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({
|
||||
const csv = parseData("name,n\nAda,3", "csv");
|
||||
expect(csv.rows[0]).toEqual({
|
||||
name: "Ada",
|
||||
n: 3,
|
||||
});
|
||||
expect(csv.evidence.csv).toMatchObject({
|
||||
rawRows: [["Ada", "3"]],
|
||||
cells: expect.arrayContaining([
|
||||
expect.objectContaining({ field: "n", raw: "3", inferred: "number" }),
|
||||
]),
|
||||
});
|
||||
expect(parseData('{"a":1}\n{"a":2}', "ndjson").rows).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("makes inference explicit and preserves unsafe-number evidence", () => {
|
||||
expect(
|
||||
parseData("id,empty\n9007199254740993,null", "csv", {
|
||||
csvInference: "safe",
|
||||
inferNulls: false,
|
||||
}).rows[0],
|
||||
).toEqual({ id: "9007199254740993", empty: "null" });
|
||||
const json = parseData(
|
||||
'{"id":9007199254740993,"decimal":0.12345678901234567,"exact":"9007199254740993"}',
|
||||
"json",
|
||||
);
|
||||
expect(json.evidence.jsonNumbers).toEqual([
|
||||
expect.objectContaining({
|
||||
raw: "9007199254740993",
|
||||
risk: "unsafe-integer",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
raw: "0.12345678901234567",
|
||||
risk: "precision-risk",
|
||||
}),
|
||||
]);
|
||||
expect(() =>
|
||||
parseData('{"id":9007199254740993}', "json", {
|
||||
rejectUnsafeJsonNumbers: true,
|
||||
}),
|
||||
).toThrow(/precision risk/iu);
|
||||
});
|
||||
it("converts static XML and rejects entities", () => {
|
||||
expect(
|
||||
parseData(
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseDuckDbQuery } from "../../src/core/duckdb";
|
||||
|
||||
describe("DuckDB query boundary", () => {
|
||||
it("records a single SELECT token stream", () => {
|
||||
expect(
|
||||
parseDuckDbQuery("SELECT team, avg(score) FROM data GROUP BY team"),
|
||||
).toMatchObject({
|
||||
type: "duckdb-query",
|
||||
statement: "SELECT",
|
||||
tokens: expect.arrayContaining([
|
||||
{ kind: "word", value: "data", offset: 29 },
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
"INSTALL httpfs",
|
||||
"SELECT * FROM data; DROP TABLE data",
|
||||
"COPY data TO 'x.csv'",
|
||||
"PRAGMA version",
|
||||
])("rejects non-read-only statement %s", (source) => {
|
||||
expect(() => parseDuckDbQuery(source)).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseData } from "../../src/core/data";
|
||||
import { runQuery } from "../../src/core/query";
|
||||
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}]',
|
||||
@@ -32,4 +32,31 @@ describe("query engine", () => {
|
||||
/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 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user