-
Notifications
You must be signed in to change notification settings - Fork 9
/
pg_query.test.js
177 lines (166 loc) · 3.2 KB
/
pg_query.test.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import Module from "./pg_query";
let pgQuery;
beforeAll(async () => {
pgQuery = await new Module();
});
test("normalize", () => {
expect(pgQuery.normalize("select 1")).toStrictEqual({
error: null,
normalized_query: "select $1",
});
});
test("parse", () => {
const initial = pgQuery.parse("select 1");
// Tests shouldn't be version dependent
const { version, ...expected } = initial["parse_tree"];
expect(expected).toStrictEqual({
stmts: [
{
stmt: {
SelectStmt: {
targetList: [
{
ResTarget: {
val: {
A_Const: {
ival: { ival: 1 },
location: 7,
},
},
location: 7,
},
},
],
limitOption: "LIMIT_OPTION_DEFAULT",
op: "SETOP_NONE",
},
},
},
],
});
});
test("parsePlpgsql", () => {
const expected = pgQuery.parsePlpgsql(`
CREATE OR REPLACE FUNCTION cs_fmt_browser_version(
v_name varchar,
v_version varchar
)
RETURNS varchar AS $$
BEGIN
IF v_version IS NULL THEN
RETURN v_name;
END IF;
RETURN v_name || '/' || v_version;
END;
$$
LANGUAGE plpgsql`);
expect(expected).toStrictEqual({
plpgsql_funcs: [
{
PLpgSQL_function: {
datums: [
{
PLpgSQL_var: {
refname: "v_name",
datatype: {
PLpgSQL_type: { typname: "UNKNOWN" },
},
},
},
{
PLpgSQL_var: {
refname: "v_version",
datatype: {
PLpgSQL_type: { typname: "UNKNOWN" },
},
},
},
{
PLpgSQL_var: {
refname: "found",
datatype: {
PLpgSQL_type: { typname: "UNKNOWN" },
},
},
},
],
action: {
PLpgSQL_stmt_block: {
lineno: 2,
body: [
{
PLpgSQL_stmt_if: {
lineno: 3,
cond: {
PLpgSQL_expr: {
parseMode: 2,
query: "v_version IS NULL",
},
},
then_body: [
{
PLpgSQL_stmt_return: {
lineno: 4,
expr: {
PLpgSQL_expr: {
parseMode: 2,
query: "v_name",
},
},
},
},
],
},
},
{
PLpgSQL_stmt_return: {
lineno: 6,
expr: {
PLpgSQL_expr: {
parseMode: 2,
query: "v_name || '/' || v_version",
},
},
},
},
],
},
},
},
},
],
error: null,
});
});
test("fingerprint", () => {
expect(pgQuery.fingerprint("select 1 from foo where a = b")).toStrictEqual({
error: null,
fingerprint_str: "27e78aa34bd5bf64",
stderr_buffer: "",
});
});
test("scan", () => {
const { tokens, version, ...remaining } = pgQuery.scan("select 1");
expect(
Array.from({ length: tokens.size() }, (_, idx) => tokens.get(idx))
).toStrictEqual([
{
text: "select",
start: 0,
end: 6,
token_kind: "SELECT",
keyword_kind: "RESERVED_KEYWORD",
},
{
text: "1",
start: 7,
end: 8,
token_kind: "ICONST",
keyword_kind: "NO_KEYWORD",
},
]);
expect(remaining).toStrictEqual({
error: null,
stderr_buffer: "",
});
});