-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdatalogUtils.test.ts
More file actions
121 lines (112 loc) · 3.41 KB
/
Copy pathdatalogUtils.test.ts
File metadata and controls
121 lines (112 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { describe, expect, it } from "vitest";
import compileDatalog, { toVar } from "~/utils/compileDatalog";
import gatherDatalogVariablesFromClause from "~/utils/gatherDatalogVariablesFromClause";
import replaceDatalogVariables from "~/utils/replaceDatalogVariables";
describe("compileDatalog", () => {
it("sanitizes variable names", () => {
expect(toVar('a b"(c)')).toBe("abc");
});
it("compiles nested and-clauses", () => {
const query = compileDatalog({
type: "and-clause",
clauses: [
{
type: "data-pattern",
arguments: [
{ type: "variable", value: "node" },
{ type: "constant", value: ":node/title" },
{ type: "constant", value: '"Hello"' },
],
},
{
type: "pred-expr",
pred: "=",
arguments: [
{ type: "variable", value: "node" },
{ type: "variable", value: "match" },
],
},
],
});
expect(query).toContain("(and");
expect(query).toContain('[?node :node/title "Hello"]');
expect(query).toContain("[(= ?node ?match)]");
});
});
describe("gatherDatalogVariablesFromClause", () => {
it("collects variables from nested clauses", () => {
const variables = gatherDatalogVariablesFromClause({
type: "and-clause",
clauses: [
{
type: "data-pattern",
arguments: [
{ type: "variable", value: "a" },
{ type: "constant", value: ":rel" },
{ type: "variable", value: "b" },
],
},
{
type: "or-join-clause",
variables: [
{ type: "variable", value: "c" },
{ type: "variable", value: "d" },
],
clauses: [],
},
],
});
expect(Array.from(variables).sort()).toEqual(["a", "b", "c", "d"]);
});
});
describe("replaceDatalogVariables", () => {
it("replaces explicit variable names and function bindings", () => {
const [clause] = replaceDatalogVariables(
[{ from: "node", to: "page" }],
[
{
type: "fn-expr",
fn: "get",
arguments: [{ type: "variable", value: "node" }],
binding: {
type: "bind-scalar",
variable: { type: "variable", value: "node" },
},
},
],
);
expect(clause.type).toBe("fn-expr");
if (clause.type !== "fn-expr") return;
expect(clause.arguments[0]).toMatchObject({ value: "page" });
expect(clause.binding).toMatchObject({
variable: { value: "page" },
});
});
it("supports transform replacement for all variables", () => {
const [clause] = replaceDatalogVariables(
[{ from: true, to: (v) => `${v}-v2` }],
[
{
type: "not-join-clause",
variables: [{ type: "variable", value: "a" }],
clauses: [
{
type: "data-pattern",
arguments: [
{ type: "variable", value: "a" },
{ type: "constant", value: ":x" },
{ type: "variable", value: "b" },
],
},
],
},
],
);
expect(clause.type).toBe("not-join-clause");
if (clause.type !== "not-join-clause") return;
expect(clause.variables[0].value).toBe("a-v2");
expect(clause.clauses[0]).toMatchObject({
arguments: [{ value: "a-v2" }, { value: ":x" }, { value: "b-v2" }],
});
});
});