2022-04-11 18:34:09 +00:00
|
|
|
import { expect, test } from "@jest/globals";
|
|
|
|
import { applyQuery, parseQuery } from "./engine";
|
|
|
|
|
|
|
|
test("Test parser", () => {
|
|
|
|
let parsedBasicQuery = parseQuery(`page`);
|
|
|
|
expect(parsedBasicQuery.table).toBe("page");
|
|
|
|
|
|
|
|
let parsedQuery1 = parseQuery(
|
|
|
|
`task where completed = false and dueDate <= "{{today}}" order by dueDate desc limit 5`
|
|
|
|
);
|
|
|
|
expect(parsedQuery1.table).toBe("task");
|
|
|
|
expect(parsedQuery1.orderBy).toBe("dueDate");
|
|
|
|
expect(parsedQuery1.orderDesc).toBe(true);
|
|
|
|
expect(parsedQuery1.limit).toBe(5);
|
|
|
|
expect(parsedQuery1.filter.length).toBe(2);
|
|
|
|
expect(parsedQuery1.filter[0]).toStrictEqual({
|
|
|
|
op: "=",
|
|
|
|
prop: "completed",
|
|
|
|
value: false,
|
|
|
|
});
|
|
|
|
expect(parsedQuery1.filter[1]).toStrictEqual({
|
|
|
|
op: "<=",
|
|
|
|
prop: "dueDate",
|
|
|
|
value: "{{today}}",
|
|
|
|
});
|
|
|
|
|
2022-04-12 11:33:07 +00:00
|
|
|
let parsedQuery2 = parseQuery(`page where name =~ /interview\\/.*/"`);
|
2022-04-11 18:34:09 +00:00
|
|
|
expect(parsedQuery2.table).toBe("page");
|
|
|
|
expect(parsedQuery2.filter.length).toBe(1);
|
|
|
|
expect(parsedQuery2.filter[0]).toStrictEqual({
|
2022-04-12 11:33:07 +00:00
|
|
|
op: "=~",
|
2022-04-11 18:34:09 +00:00
|
|
|
prop: "name",
|
2022-04-19 14:54:47 +00:00
|
|
|
value: "interview\\/.*",
|
2022-04-11 18:34:09 +00:00
|
|
|
});
|
2022-04-13 12:46:52 +00:00
|
|
|
|
|
|
|
let parsedQuery3 = parseQuery(`page where something != null`);
|
|
|
|
expect(parsedQuery3.table).toBe("page");
|
|
|
|
expect(parsedQuery3.filter.length).toBe(1);
|
|
|
|
expect(parsedQuery3.filter[0]).toStrictEqual({
|
|
|
|
op: "!=",
|
|
|
|
prop: "something",
|
|
|
|
value: null,
|
|
|
|
});
|
2022-04-21 09:46:33 +00:00
|
|
|
|
|
|
|
expect(parseQuery(`page select name`).select).toStrictEqual(["name"]);
|
|
|
|
expect(parseQuery(`page select name, age`).select).toStrictEqual([
|
|
|
|
"name",
|
|
|
|
"age",
|
|
|
|
]);
|
2022-04-11 18:34:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test("Test performing the queries", () => {
|
|
|
|
let data: any[] = [
|
|
|
|
{ name: "interview/My Interview", lastModified: 1 },
|
|
|
|
{ name: "interview/My Interview 2", lastModified: 2 },
|
|
|
|
{ name: "Pete", age: 38 },
|
|
|
|
{ name: "Angie", age: 28 },
|
|
|
|
];
|
|
|
|
|
2022-04-12 11:33:07 +00:00
|
|
|
expect(
|
|
|
|
applyQuery(parseQuery(`page where name =~ /interview\\/.*/`), data)
|
|
|
|
).toStrictEqual([
|
2022-04-11 18:34:09 +00:00
|
|
|
{ name: "interview/My Interview", lastModified: 1 },
|
|
|
|
{ name: "interview/My Interview 2", lastModified: 2 },
|
|
|
|
]);
|
|
|
|
expect(
|
2022-04-12 11:33:07 +00:00
|
|
|
applyQuery(
|
|
|
|
parseQuery(`page where name =~ /interview\\/.*/ order by lastModified`),
|
|
|
|
data
|
|
|
|
)
|
2022-04-11 18:34:09 +00:00
|
|
|
).toStrictEqual([
|
|
|
|
{ name: "interview/My Interview", lastModified: 1 },
|
|
|
|
{ name: "interview/My Interview 2", lastModified: 2 },
|
|
|
|
]);
|
|
|
|
expect(
|
|
|
|
applyQuery(
|
2022-04-12 11:33:07 +00:00
|
|
|
parseQuery(
|
|
|
|
`page where name =~ /interview\\/.*/ order by lastModified desc`
|
|
|
|
),
|
2022-04-11 18:34:09 +00:00
|
|
|
data
|
|
|
|
)
|
|
|
|
).toStrictEqual([
|
|
|
|
{ name: "interview/My Interview 2", lastModified: 2 },
|
|
|
|
{ name: "interview/My Interview", lastModified: 1 },
|
|
|
|
]);
|
2022-04-12 11:33:07 +00:00
|
|
|
expect(applyQuery(parseQuery(`page where age > 30`), data)).toStrictEqual([
|
2022-04-11 18:34:09 +00:00
|
|
|
{ name: "Pete", age: 38 },
|
|
|
|
]);
|
2022-04-12 11:33:07 +00:00
|
|
|
expect(
|
|
|
|
applyQuery(parseQuery(`page where age > 28 and age < 38`), data)
|
|
|
|
).toStrictEqual([]);
|
2022-04-21 09:46:33 +00:00
|
|
|
expect(
|
|
|
|
applyQuery(parseQuery(`page where age > 30 select name`), data)
|
|
|
|
).toStrictEqual([{ name: "Pete" }]);
|
2022-04-11 18:34:09 +00:00
|
|
|
});
|