-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathdeparsing.test.js
More file actions
70 lines (61 loc) · 2.26 KB
/
deparsing.test.js
File metadata and controls
70 lines (61 loc) · 2.26 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
const query = require("../");
const { describe, it, before, after, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
describe("Query Deparsing", () => {
before(async () => {
await query.parse("SELECT 1");
});
describe("Sync Deparsing", () => {
it("should deparse a simple query", () => {
const sql = 'SELECT * FROM users';
const parseTree = query.parseSync(sql);
const deparsed = query.deparseSync(parseTree);
assert.equal(deparsed, sql);
});
it("should deparse a complex query", () => {
const sql = 'SELECT a, b, c FROM t1 JOIN t2 ON t1.id = t2.id WHERE t1.x > 10';
const parseTree = query.parseSync(sql);
const deparsed = query.deparseSync(parseTree);
assert.equal(deparsed, sql);
});
it("should fail to deparse without protobuf data", () => {
assert.throws(() => query.deparseSync({}), /No parseTree provided/);
});
});
describe("Async Deparsing", () => {
it("should return a promise resolving to same result", async () => {
const sql = 'SELECT * FROM users';
const parseTree = await query.parse(sql);
const deparsed = await query.deparse(parseTree);
assert.equal(deparsed, sql);
});
it("should reject when no protobuf data", async () => {
try {
await query.deparse({});
throw new Error('should have rejected');
} catch (err) {
assert.equal(err.message, 'No parseTree provided');
}
});
});
describe("Round-trip parsing and deparsing", () => {
it("should maintain query semantics through round-trip", async () => {
const sql = 'SELECT a, b, c FROM t1 JOIN t2 ON t1.id = t2.id WHERE t1.x > 10';
const parseTree = await query.parse(sql);
const deparsed = await query.deparse(parseTree);
assert.equal(deparsed, sql);
});
});
it('should deparse a parse tree', async () => {
const sql = 'SELECT * FROM users';
const parseTree = await query.parse(sql);
const deparsed = await query.deparse(parseTree);
assert.equal(deparsed, sql);
});
it('should throw on invalid parse tree', () => {
try {
query.deparseSync({});
} catch (err) { }
assert.throws(() => query.deparseSync({}), /No parseTree provided/);
});
});