-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathscan.test.js
More file actions
279 lines (225 loc) Β· 10.9 KB
/
scan.test.js
File metadata and controls
279 lines (225 loc) Β· 10.9 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const query = require("../");
const { describe, it, before, after, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
describe("Query Scanning", () => {
before(async () => {
await query.parse("SELECT 1");
});
describe("Sync Scanning", () => {
it("should return a scan result with version and tokens", () => {
const result = query.scanSync("SELECT 1");
assert.equal(typeof result, "object");
assert.ok("version" in result);
assert.ok("tokens" in result);
assert.equal(typeof result.version, "number");
assert.ok(Array.isArray(result.tokens));
});
it("should scan a simple SELECT query correctly", () => {
const result = query.scanSync("SELECT 1");
assert.equal(result.tokens.length, 2);
// First token should be SELECT
const selectToken = result.tokens[0];
assert.equal(selectToken.text, "SELECT");
assert.equal(selectToken.start, 0);
assert.equal(selectToken.end, 6);
assert.equal(selectToken.tokenName, "UNKNOWN"); // SELECT is mapped as UNKNOWN in our simplified mapping
assert.equal(selectToken.keywordName, "RESERVED_KEYWORD");
// Second token should be 1
const numberToken = result.tokens[1];
assert.equal(numberToken.text, "1");
assert.equal(numberToken.start, 7);
assert.equal(numberToken.end, 8);
assert.equal(numberToken.tokenName, "ICONST");
assert.equal(numberToken.keywordName, "NO_KEYWORD");
});
it("should scan tokens with correct positions", () => {
const sql = "SELECT * FROM users";
const result = query.scanSync(sql);
assert.equal(result.tokens.length, 4);
// Verify each token position matches the original SQL
result.tokens.forEach(token => {
const actualText = sql.substring(token.start, token.end);
assert.equal(token.text, actualText);
});
});
it("should identify different token types", () => {
const result = query.scanSync("SELECT 'string', 123, 3.14, $1 FROM users");
const tokenTypes = result.tokens.map(t => t.tokenName);
assert.ok(tokenTypes.includes("SCONST")); // String constant
assert.ok(tokenTypes.includes("ICONST")); // Integer constant
assert.ok(tokenTypes.includes("FCONST")); // Float constant
assert.ok(tokenTypes.includes("PARAM")); // Parameter marker
// Note: keywords like FROM may be tokenized as UNKNOWN in our simplified mapping
assert.ok(tokenTypes.includes("UNKNOWN")); // Keywords and identifiers
});
it("should identify operators and punctuation", () => {
const result = query.scanSync("SELECT * FROM users WHERE id = 1");
const operators = result.tokens.filter(t =>
t.tokenName.startsWith("ASCII_") || t.text === "="
);
assert.ok(operators.length > 0);
assert.equal(operators.some(t => t.text === "*"), true);
assert.equal(operators.some(t => t.text === "="), true);
});
it("should classify keyword types correctly", () => {
const result = query.scanSync("SELECT COUNT(*) FROM users WHERE active = true");
const reservedKeywords = result.tokens.filter(t =>
t.keywordName === "RESERVED_KEYWORD"
);
const unreservedKeywords = result.tokens.filter(t =>
t.keywordName === "UNRESERVED_KEYWORD"
);
assert.ok(reservedKeywords.length > 0);
// SELECT, FROM, WHERE should be reserved keywords
assert.equal(reservedKeywords.some(t => t.text === "SELECT"), true);
assert.equal(reservedKeywords.some(t => t.text === "FROM"), true);
assert.equal(reservedKeywords.some(t => t.text === "WHERE"), true);
});
it("should handle complex queries with parameters", () => {
const result = query.scanSync("SELECT * FROM users WHERE id = $1 AND name = $2");
const params = result.tokens.filter(t => t.tokenName === "PARAM");
assert.equal(params.length, 2);
assert.equal(params[0].text, "$1");
assert.equal(params[1].text, "$2");
});
it("should handle string escaping in JSON output", () => {
const result = query.scanSync("SELECT 'text with \"quotes\" and \\backslash'");
const stringToken = result.tokens.find(t => t.tokenName === "SCONST");
assert.ok(stringToken);
assert.ok(stringToken.text.includes('"'));
assert.ok(stringToken.text.includes('\\'));
});
it("should scan INSERT statements", () => {
const result = query.scanSync("INSERT INTO table VALUES (1, 'text', 3.14)");
assert.equal(result.tokens.some(t => t.text === "INSERT"), true);
assert.equal(result.tokens.some(t => t.text === "INTO"), true);
assert.equal(result.tokens.some(t => t.text === "VALUES"), true);
assert.equal(result.tokens.some(t => t.tokenName === "ICONST"), true);
assert.equal(result.tokens.some(t => t.tokenName === "SCONST"), true);
assert.equal(result.tokens.some(t => t.tokenName === "FCONST"), true);
});
it("should scan UPDATE statements", () => {
const result = query.scanSync("UPDATE users SET name = 'John' WHERE id = 1");
assert.equal(result.tokens.some(t => t.text === "UPDATE"), true);
assert.equal(result.tokens.some(t => t.text === "SET"), true);
assert.equal(result.tokens.some(t => t.text === "="), true);
});
it("should scan DELETE statements", () => {
const result = query.scanSync("DELETE FROM users WHERE active = false");
assert.equal(result.tokens.some(t => t.text === "DELETE"), true);
assert.equal(result.tokens.some(t => t.text === "FROM"), true);
assert.equal(result.tokens.some(t => t.text === "WHERE"), true);
});
it("should handle empty or whitespace-only input", () => {
const result = query.scanSync(" ");
assert.equal(result.tokens.length, 0);
});
it("should handle unusual input gracefully", () => {
// The scanner is more permissive than the parser and may tokenize unusual input
const result = query.scanSync("$$$INVALID$$$");
assert.equal(typeof result, "object");
assert.ok(Array.isArray(result.tokens));
// Scanner may still produce tokens even for unusual input
});
it("should preserve original token order", () => {
const sql = "SELECT id, name FROM users ORDER BY name";
const result = query.scanSync(sql);
// Tokens should be in order of appearance
for (let i = 1; i < result.tokens.length; i++) {
assert.ok(result.tokens[i].start >= result.tokens[i-1].end);
}
});
});
describe("Async Scanning", () => {
it("should return a promise resolving to same result as sync", async () => {
const testQuery = "SELECT * FROM users WHERE id = $1";
const resultPromise = query.scan(testQuery);
const result = await resultPromise;
assert.ok(resultPromise instanceof Promise);
assert.deepEqual(result, query.scanSync(testQuery));
});
it("should handle complex queries asynchronously", async () => {
const testQuery = "SELECT COUNT(*) as total FROM orders WHERE status = 'completed' AND created_at > '2023-01-01'";
const result = await query.scan(testQuery);
assert.equal(typeof result, "object");
assert.ok(Array.isArray(result.tokens));
assert.ok(result.tokens.length > 10);
});
it("should handle unusual input asynchronously", async () => {
// Scanner is more permissive than parser
const result = await query.scan("$$$INVALID$$$");
assert.equal(typeof result, "object");
assert.ok(Array.isArray(result.tokens));
});
});
describe("Edge Cases", () => {
it("should handle queries with comments", () => {
const result = query.scanSync("SELECT 1 -- this is a comment");
// Should have at least SELECT and 1 tokens
assert.ok(result.tokens.length >= 2);
assert.equal(result.tokens.some(t => t.text === "SELECT"), true);
assert.equal(result.tokens.some(t => t.text === "1"), true);
});
it("should handle very long identifiers", () => {
const longIdentifier = "a".repeat(100);
const result = query.scanSync(`SELECT ${longIdentifier} FROM table`);
const identToken = result.tokens.find(t => t.text === longIdentifier);
assert.ok(identToken);
assert.equal(identToken.tokenName, "IDENT");
});
it("should handle special PostgreSQL operators", () => {
const result = query.scanSync("SELECT id::text FROM users");
assert.equal(result.tokens.some(t => t.text === "::"), true);
const typecastToken = result.tokens.find(t => t.text === "::");
assert.equal(typecastToken?.tokenName, "TYPECAST");
});
it("should provide consistent version information", () => {
const result1 = query.scanSync("SELECT 1");
const result2 = query.scanSync("INSERT INTO table VALUES (1)");
assert.equal(result1.version, result2.version);
assert.equal(typeof result1.version, "number");
assert.ok(result1.version > 0);
});
it("should handle multi-line dollar-quoted strings without JSON errors", () => {
// Without the fix, scanSync throws:
// "Bad control character in string literal"
// because build_scan_json() doesn't escape \n in the token text.
const sql = `CREATE FUNCTION test() RETURNS void AS $$
BEGIN
RAISE NOTICE 'hello';
END;
$$ LANGUAGE plpgsql`;
const result = query.scanSync(sql);
assert.equal(typeof result, "object");
assert.ok(Array.isArray(result.tokens));
assert.ok(result.tokens.length > 0);
// The dollar-quoted body spans multiple lines
const dollarToken = result.tokens.find(t => t.text.includes('BEGIN'));
assert.ok(dollarToken, "should have a token containing the function body");
assert.ok(dollarToken.text.includes('\n'), "token text should preserve newlines");
});
it("should handle dollar-quoted tokens with tabs", () => {
// Tab characters also break JSON.parse when unescaped.
const sql = `SELECT $$line1
indented
line3$$`;
const result = query.scanSync(sql);
assert.equal(typeof result, "object");
assert.ok(Array.isArray(result.tokens));
const dollarToken = result.tokens.find(t => t.text.includes('indented'));
assert.ok(dollarToken, "should have a token containing the tabbed content");
});
it("should handle multi-line block comments", () => {
// C-style block comments spanning multiple lines hit the same bug.
const sql = `SELECT 1; /* multi
line
comment */ SELECT 2`;
const result = query.scanSync(sql);
assert.equal(typeof result, "object");
assert.ok(Array.isArray(result.tokens));
const commentToken = result.tokens.find(t => t.tokenName === "C_COMMENT");
assert.ok(commentToken, "should have a C_COMMENT token");
assert.ok(commentToken.text.includes('\n'), "comment text should preserve newlines");
});
});
});