-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutine-params.test.ts
More file actions
232 lines (204 loc) · 9.65 KB
/
Copy pathroutine-params.test.ts
File metadata and controls
232 lines (204 loc) · 9.65 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
import { describe, test, expect } from "bun:test";
import { parseRoutines } from "../routine.ts";
describe("parseRoutines — parameters", () => {
test("no parameters", () => {
const sql = `CREATE FUNCTION app.get_version() RETURNS text
LANGUAGE sql AS $$ SELECT '1.0'; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([]);
});
test("single named parameter — dir is null when unspecified", () => {
const sql = `CREATE FUNCTION app.get_user(_id integer) RETURNS void
LANGUAGE plpgsql AS $$ begin null; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_id", type: "integer" }]);
});
test("multiple named parameters", () => {
const sql = `CREATE FUNCTION app.create_order(_customer text, _amount numeric, _note text) RETURNS integer
LANGUAGE plpgsql AS $$ begin return 1; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([
{ dir: null, name: "_customer", type: "text" },
{ dir: null, name: "_amount", type: "numeric" },
{ dir: null, name: "_note", type: "text" },
]);
});
test("unnamed parameters", () => {
const sql = `CREATE FUNCTION app.add(integer, integer) RETURNS integer
LANGUAGE sql AS $$ SELECT $1 + $2; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([
{ dir: null, name: null, type: "integer" },
{ dir: null, name: null, type: "integer" },
]);
});
test("IN modifier", () => {
const sql = `CREATE PROCEDURE app.archive(IN _days integer)
LANGUAGE plpgsql AS $$ begin null; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: "in", name: "_days", type: "integer" }]);
});
test("OUT modifier", () => {
const sql = `CREATE PROCEDURE app.get_stats(OUT _total integer, OUT _avg numeric)
LANGUAGE plpgsql AS $$ begin _total := 0; _avg := 0; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([
{ dir: "out", name: "_total", type: "integer" },
{ dir: "out", name: "_avg", type: "numeric" },
]);
});
test("INOUT modifier", () => {
const sql = `CREATE PROCEDURE app.increment(INOUT _counter integer)
LANGUAGE plpgsql AS $$ begin _counter := _counter + 1; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: "inout", name: "_counter", type: "integer" }]);
});
test("VARIADIC modifier", () => {
const sql = `CREATE FUNCTION app.concat_all(VARIADIC _parts text[]) RETURNS text
LANGUAGE sql AS $$ SELECT array_to_string(_parts, ','); $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: "variadic", name: "_parts", type: "text[]" }]);
});
test("mixed IN and OUT modifiers", () => {
const sql = `CREATE PROCEDURE app.divide(IN _a integer, IN _b integer, OUT _result numeric)
LANGUAGE plpgsql AS $$ begin _result := _a::numeric / _b; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([
{ dir: "in", name: "_a", type: "integer" },
{ dir: "in", name: "_b", type: "integer" },
{ dir: "out", name: "_result", type: "numeric" },
]);
});
test("unnamed parameter with IN modifier", () => {
const sql = `CREATE PROCEDURE app.reset(IN integer)
LANGUAGE plpgsql AS $$ begin null; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: "in", name: null, type: "integer" }]);
});
test("strips simple DEFAULT value", () => {
const sql = `CREATE FUNCTION app.list_items(_limit integer DEFAULT 10) RETURNS void
LANGUAGE plpgsql AS $$ begin null; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_limit", type: "integer" }]);
});
test("strips DEFAULT with cast", () => {
const sql = `CREATE FUNCTION app.search(_query text DEFAULT NULL::text) RETURNS void
LANGUAGE plpgsql AS $$ begin null; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_query", type: "text" }]);
});
test("strips DEFAULT with string literal and cast", () => {
const sql = `CREATE FUNCTION app.greet(_lang text DEFAULT 'en'::text) RETURNS text
LANGUAGE sql AS $$ SELECT 'hello'; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_lang", type: "text" }]);
});
test("strips DEFAULT with jsonb literal", () => {
const sql = `CREATE FUNCTION app.process(_data jsonb DEFAULT '{}'::jsonb) RETURNS void
LANGUAGE plpgsql AS $$ begin null; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_data", type: "jsonb" }]);
});
test("strips DEFAULT with parenthesized expression", () => {
const sql = `CREATE PROCEDURE app.do_thing(IN _flag text DEFAULT (NULL::text = NULL::text))
LANGUAGE plpgsql AS $$ begin null; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: "in", name: "_flag", type: "text" }]);
});
test("multiple parameters with defaults", () => {
const sql = `CREATE FUNCTION app.connect(_host text, _port integer DEFAULT 5432, _ssl boolean DEFAULT true) RETURNS void
LANGUAGE plpgsql AS $$ begin null; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([
{ dir: null, name: "_host", type: "text" },
{ dir: null, name: "_port", type: "integer" },
{ dir: null, name: "_ssl", type: "boolean" },
]);
});
test("array types", () => {
const sql = `CREATE FUNCTION app.sum_all(_values integer[]) RETURNS integer
LANGUAGE sql AS $$ SELECT 0; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_values", type: "integer[]" }]);
});
test("unnamed multi-word type: character varying", () => {
const sql = `CREATE FUNCTION app.echo(character varying) RETURNS character varying
LANGUAGE sql AS $$ SELECT $1; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: null, type: "character varying" }]);
});
test("named multi-word type: character varying", () => {
const sql = `CREATE FUNCTION app.echo(_val character varying) RETURNS character varying
LANGUAGE sql AS $$ SELECT _val; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_val", type: "character varying" }]);
});
test("unnamed multi-word type: double precision", () => {
const sql = `CREATE FUNCTION app.square(double precision) RETURNS double precision
LANGUAGE sql AS $$ SELECT $1 * $1; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: null, type: "double precision" }]);
});
test("named multi-word type: timestamp with time zone", () => {
const sql = `CREATE FUNCTION app.format_ts(_ts timestamp with time zone) RETURNS text
LANGUAGE sql AS $$ SELECT _ts::text; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_ts", type: "timestamp with time zone" }]);
});
test("unnamed multi-word type: timestamp without time zone", () => {
const sql = `CREATE FUNCTION app.to_epoch(timestamp without time zone) RETURNS bigint
LANGUAGE sql AS $$ SELECT 0::bigint; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: null, type: "timestamp without time zone" }]);
});
test("numeric with precision", () => {
const sql = `CREATE FUNCTION app.round_price(_val numeric(10,2)) RETURNS numeric
LANGUAGE sql AS $$ SELECT _val; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_val", type: "numeric(10,2)" }]);
});
test("case insensitive modifiers", () => {
const sql = `create procedure app.do_it(in _x integer default 0)
language plpgsql as $$ begin null; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: "in", name: "_x", type: "integer" }]);
});
test("RETURNS TABLE does not pollute parameters", () => {
const sql = `CREATE FUNCTION app.list_users(_active boolean) RETURNS TABLE(id integer, name text)
LANGUAGE plpgsql AS $$ begin return query select 1, 'a'::text; end; $$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([{ dir: null, name: "_active", type: "boolean" }]);
});
test("real pg_dump output with complex defaults", () => {
const sql = `CREATE FUNCTION inventory.find_products(_category text, _min_price numeric DEFAULT 0, _tags jsonb DEFAULT '{}'::jsonb, _limit integer DEFAULT 100) RETURNS TABLE(id integer, name text, price numeric)
LANGUAGE plpgsql SECURITY DEFINER
AS $$
begin
return query select p.id, p.name, p.price
from inventory.products p
where p.category = _category
and p.price >= _min_price
limit _limit;
end;
$$;`;
const [r] = parseRoutines(sql);
expect(r.parameters).toEqual([
{ dir: null, name: "_category", type: "text" },
{ dir: null, name: "_min_price", type: "numeric" },
{ dir: null, name: "_tags", type: "jsonb" },
{ dir: null, name: "_limit", type: "integer" },
]);
});
test("multiple routines each have their own parameters", () => {
const sql = `CREATE FUNCTION app.add(_a integer, _b integer) RETURNS integer
LANGUAGE sql AS $$ SELECT _a + _b; $$;
CREATE PROCEDURE app.log_event(IN _msg text)
LANGUAGE plpgsql AS $$ begin null; end; $$;`;
const results = parseRoutines(sql);
expect(results[0].parameters).toEqual([
{ dir: null, name: "_a", type: "integer" },
{ dir: null, name: "_b", type: "integer" },
]);
expect(results[1].parameters).toEqual([{ dir: "in", name: "_msg", type: "text" }]);
});
});