-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstructure-analyzer.test.ts
More file actions
335 lines (277 loc) · 12.3 KB
/
Copy pathstructure-analyzer.test.ts
File metadata and controls
335 lines (277 loc) · 12.3 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { EditorState } from "@codemirror/state";
import { beforeEach, describe, expect, it } from "vitest";
import { SqlParser } from "../parser.js";
import { SqlStructureAnalyzer } from "../structure-analyzer.js";
describe("SqlStructureAnalyzer", () => {
let analyzer: SqlStructureAnalyzer;
let state: EditorState;
beforeEach(() => {
const parser = new SqlParser({ dialect: "PostgresQL" });
analyzer = new SqlStructureAnalyzer(parser);
});
const createState = (content: string) => {
return EditorState.create({ doc: content });
};
describe("analyzeDocument", () => {
it("should identify single SQL statement", () => {
state = createState("SELECT * FROM users WHERE id = 1;");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT * FROM users WHERE id = 1");
expect(statements[0].type).toBe("select");
expect(statements[0].isValid).toBe(true);
expect(statements[0].lineFrom).toBe(1);
expect(statements[0].lineTo).toBe(1);
});
it("should identify multiple SQL statements", () => {
state = createState(`
SELECT * FROM users;
INSERT INTO users (name) VALUES ('John');
DELETE FROM users WHERE id = 1;
`);
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(3);
expect(statements[0].type).toBe("select");
expect(statements[1].type).toBe("insert");
expect(statements[2].type).toBe("delete");
});
it("should handle statements without semicolons", () => {
state = createState("SELECT * FROM users WHERE id = 1");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT * FROM users WHERE id = 1");
expect(statements[0].type).toBe("select");
});
it("should handle semicolons in string literals", () => {
state = createState(`SELECT 'Hello; World' FROM users; UPDATE users SET name = 'Test;';`);
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(2);
expect(statements[0].content).toBe("SELECT 'Hello; World' FROM users");
expect(statements[1].content).toBe("UPDATE users SET name = 'Test;'");
});
it("should determine correct statement types", () => {
const testCases = [
{ sql: "SELECT * FROM users", expected: "select" },
{ sql: "INSERT INTO users VALUES (1)", expected: "insert" },
{ sql: "UPDATE users SET name = 'test'", expected: "update" },
{ sql: "DELETE FROM users", expected: "delete" },
{ sql: "CREATE TABLE users (id INT)", expected: "create" },
{ sql: "DROP TABLE users", expected: "drop" },
{ sql: "ALTER TABLE users ADD COLUMN email VARCHAR(255)", expected: "alter" },
{ sql: "USE database_name", expected: "use" },
{ sql: "SHOW TABLES", expected: "other" },
];
testCases.forEach(({ sql, expected }) => {
state = createState(`${sql};`);
const statements = analyzer.analyzeDocument(state);
expect(statements[0].type).toBe(expected);
});
});
it("should handle invalid SQL statements", () => {
state = createState("SELECT * FROM;");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].isValid).toBe(false);
});
it("should cache results for identical content", () => {
const content = "SELECT * FROM users;";
state = createState(content);
const statements1 = analyzer.analyzeDocument(state);
const statements2 = analyzer.analyzeDocument(state);
expect(statements1).toBe(statements2); // Should be the same reference (cached)
});
});
describe("getStatementAtPosition", () => {
beforeEach(() => {
state = createState(`SELECT * FROM users;
INSERT INTO users (name) VALUES ('John');
DELETE FROM users WHERE id = 1;`);
});
it("should return correct statement for cursor position", () => {
const statement1 = analyzer.getStatementAtPosition(state, 5); // Inside first SELECT
const statement2 = analyzer.getStatementAtPosition(state, 25); // Inside INSERT
const statement3 = analyzer.getStatementAtPosition(state, 80); // Inside DELETE
expect(statement1?.type).toBe("select");
expect(statement2?.type).toBe("insert");
expect(statement3?.type).toBe("delete");
});
it("should return null for position outside any statement", () => {
state = createState(" \n\n ");
const statement = analyzer.getStatementAtPosition(state, 2);
expect(statement).toBeNull();
});
});
describe("getStatementsInRange", () => {
beforeEach(() => {
state = createState(`SELECT * FROM users;
INSERT INTO users (name) VALUES ('John');
DELETE FROM users WHERE id = 1;`);
});
it("should return statements that intersect with range", () => {
const statements = analyzer.getStatementsInRange(state, 0, 50);
expect(statements).toHaveLength(2); // SELECT and INSERT
expect(statements[0].type).toBe("select");
expect(statements[1].type).toBe("insert");
});
it("should return single statement when range is within one statement", () => {
const statements = analyzer.getStatementsInRange(state, 5, 10);
expect(statements).toHaveLength(1);
expect(statements[0].type).toBe("select");
});
it("should return all statements when range covers entire document", () => {
const statements = analyzer.getStatementsInRange(state, 0, state.doc.toString().length);
expect(statements).toHaveLength(3);
});
});
describe("clearCache", () => {
it("should clear internal cache", () => {
state = createState("SELECT * FROM users;");
// Populate cache
analyzer.analyzeDocument(state);
// Clear cache
analyzer.clearCache();
// Should re-analyze (though we can't directly test this, we ensure no errors)
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
});
});
describe("multiline statements", () => {
it("should handle statements spanning multiple lines", () => {
state = createState(`SELECT u.id,
u.name,
u.email
FROM users u
WHERE u.active = true
AND u.created_at > '2023-01-01';`);
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].lineFrom).toBe(1);
expect(statements[0].lineTo).toBe(6);
expect(statements[0].type).toBe("select");
});
});
describe("comment handling", () => {
describe("single-line comments (--)", () => {
it("should strip single-line comments from statement content", () => {
state = createState("SELECT * FROM users -- this is a comment\nWHERE id = 1;");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT * FROM users \nWHERE id = 1");
expect(statements[0].type).toBe("select");
});
it("should handle single-line comment at end of statement", () => {
state = createState("SELECT * FROM users WHERE id = 1; -- comment");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT * FROM users WHERE id = 1");
});
it("should handle statement that is entirely a comment", () => {
state = createState("-- This is just a comment\nSELECT * FROM users;");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT * FROM users");
expect(statements[0].type).toBe("select");
});
it("should handle multiple single-line comments", () => {
state = createState(`
-- First comment
SELECT * FROM users -- inline comment
-- Another comment
WHERE id = 1; -- final comment
`);
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content.trim()).toBe(
"SELECT * FROM users \n \n WHERE id = 1",
);
});
});
describe("multi-line comments (/* */)", () => {
it("should strip multi-line comments from statement content", () => {
state = createState("SELECT * FROM users /* this is a comment */ WHERE id = 1;");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT * FROM users WHERE id = 1");
expect(statements[0].type).toBe("select");
});
it("should handle multi-line comments spanning multiple lines", () => {
state = createState(`SELECT * FROM users /*
This is a multi-line
comment that spans
several lines
*/ WHERE id = 1;`);
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT * FROM users WHERE id = 1");
});
it("should handle nested-like comment patterns", () => {
state = createState("SELECT * FROM users /* comment /* nested */ text */ WHERE id = 1;");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT * FROM users text */ WHERE id = 1");
});
});
describe("comments in string literals", () => {
it("should not strip comment patterns inside string literals", () => {
state = createState("SELECT 'This -- is not a comment' FROM users;");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT 'This -- is not a comment' FROM users");
});
it("should not strip multi-line comment patterns inside string literals", () => {
state = createState("SELECT 'This /* is not */ a comment' FROM users;");
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe("SELECT 'This /* is not */ a comment' FROM users");
});
it("should handle mixed real comments and string literal comment patterns", () => {
state = createState(
"SELECT 'Text -- not comment' FROM users -- real comment\nWHERE id = 1;",
);
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].content).toBe(
"SELECT 'Text -- not comment' FROM users \nWHERE id = 1",
);
});
});
describe("mixed comments and statements", () => {
it("should handle statements separated by comments", () => {
state = createState(`
SELECT * FROM users; -- First query
/* Comment between queries */
INSERT INTO users (name) VALUES ('John'); -- Second query
`);
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(2);
expect(statements[0].content.trim()).toBe("SELECT * FROM users");
expect(statements[0].type).toBe("select");
expect(statements[1].content.trim()).toBe("INSERT INTO users (name) VALUES ('John')");
expect(statements[1].type).toBe("insert");
});
it("should not create statements from comment-only content", () => {
state = createState(`
-- Just a comment
/* Another comment */
SELECT * FROM users;
-- Final comment
`);
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(1);
expect(statements[0].type).toBe("select");
});
it("should handle semicolons in comments", () => {
state = createState(`
SELECT * FROM users; -- Comment with; semicolon
/* Multi-line comment with;
semicolon; on multiple; lines */
INSERT INTO logs VALUES (1);
`);
const statements = analyzer.analyzeDocument(state);
expect(statements).toHaveLength(2);
expect(statements[0].type).toBe("select");
expect(statements[1].type).toBe("insert");
});
});
});
});