-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.test.js
More file actions
executable file
·152 lines (130 loc) · 3.96 KB
/
Copy pathparse.test.js
File metadata and controls
executable file
·152 lines (130 loc) · 3.96 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
import test from "node:test";
import * as unit from "./parse.js";
process.stdout.write("\x1Bc");
const simpleModule = `
-- name: Simple :prepare :one
select 1;
`;
test("test simple module", (t) => {
try {
const [parsed] = unit.parseModule(simpleModule).toArray();
// console.log("parsed simple module", parsed);
t.assert.strictEqual(parsed.name, "Simple");
t.assert.strictEqual(parsed.execution, ":one");
t.assert.strictEqual(parsed.prepared, true);
t.assert.strictEqual(parsed.query, "select 1");
// console.log("params", parsed.params, parsed.params.size, parsed.params.size === 0);
// console.log("selectFields", parsed.selectFields, parsed.selectFields.size);
// t.assert.strictEqual(parsed.params.size(), 0);
t.assert.strictEqual(parsed.selectFields.length, 1);
t.assert.strictEqual(parsed.returningClause.length, 0);
} catch (err) {
console.error(err);
throw err;
}
});
const multiStatementModule = `
-- name: Multi :one
select 1;
-- :many
select *
from table;
`;
test("test multi-statement module", (t) => {
const [module] = unit.parseModule(multiStatementModule).toArray();
// console.log("parsed multi statement module", module);
t.assert.strictEqual(module.length, 2);
});
const deleteModule = `
-- name: Del :one
delete from table
where name = :name
returning name, timestamp;
`;
test("test delete module", (t) => {
const [module] = unit.parseModule(deleteModule).toArray();
// console.log("parsed delete module", module);
t.assert.deepEqual(module.name, "Del");
});
const multiModule = `
-- name: AnotherDel :one
delete from table
where id = :id
returning id;
-- name: Multi
select statuses.id, status, "\`" as "bigBoy"
from statuses
join joinable on joinable.status_id = id
where joinable.can_join
-- name: Update :many
with updated as (
update updatable
set num = 1
where num <> 1
returning id
)
select num * random(),
json_build_object('k', 'v'),
'value' as alias
from updated;
`;
test("test multi module", (t) => {
const modules = unit.parseModule(multiModule).toArray();
// console.log("parsed multi module", modules);
t.assert.deepEqual(modules.length, 3);
t.assert.deepEqual(modules[1].selectFields[2], '"bigBoy"');
});
test("test codegen", (t) => {
const { js, dts } = unit.codegen(unit.parseModule(multiModule), "test.sql", false, "");
// console.log("codegen js", js);
// console.log("==========================");
// console.log("codegen dts", dts);
});
const nonParsableModule = `select 1;`;
test("test non parsable module", (t) => {
const modules = unit.parseModule(nonParsableModule).toArray();
// console.log("non parsable module", modules);
t.assert.deepEqual(modules.length, 0);
});
const realUseCaseTest = `
-- name: InsertMessage :one
insert into account.messages (to_user, from_user, listing_table, listing_id, content)
select :to, :from, tableoid::regclass, :listingId, :content
from account.listing
where id = :listingId
returning messages.id;
-- name: MarkRead :one
update account.messages
set read_at = current_timestamp
where id = :id and read_at is not null and to_user = :to
returning from_user as "from";
`;
test("test real use case", (t) => {
const { js, dts } = unit.codegen(unit.parseModule(realUseCaseTest), "test.sql", false, "");
// console.log("codegen js", js);
// console.log("==========================");
// console.log("codegen dts", dts);
});
const iterableQuery = `
-- name: AnotherDel :iterable
select *
from t
where stuff = :stuff;
`;
test("test iterable query", (t) => {
const { js, dts } = unit.codegen(unit.parseModule(iterableQuery), "test.sql", false, "");
// console.log("codegen js", js);
// console.log("==========================");
// console.log("codegen dts", dts);
});
const cursorQuery = `
-- name: AnotherDel :cursor :array
select *
from t;
`;
test("test cursor query", (t) => {
const { js, dts } = unit.codegen(unit.parseModule(cursorQuery), "test.sql", false, "");
// console.log("codegen js", js);
// console.log("==========================");
// console.log("codegen dts", dts);
});