forked from nene/prettier-plugin-sql-cst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.test.ts
More file actions
299 lines (276 loc) · 7.47 KB
/
Copy pathfunction.test.ts
File metadata and controls
299 lines (276 loc) · 7.47 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
import dedent from "dedent-js";
import { pretty, testBigquery, testPostgresql } from "../test_utils";
describe("function", () => {
describe("create function", () => {
it(`formats CREATE FUNCTION`, async () => {
await testBigquery(dedent`
CREATE FUNCTION my_func(arg1 INT64, arg2 STRING, arg3 ANY TYPE) AS
(SELECT * FROM client)
`);
});
it(`formats long parameter list to multiple lines`, async () => {
await testPostgresql(dedent`
CREATE FUNCTION my_func(
IN first_name TEXT,
OUT last_name TEXT,
year_of_birth INT DEFAULT 2000,
INOUT age INT = 0,
VARIADIC other_names TEXT[]
) AS 'SELECT 1'
`);
});
it(`formats CREATE TEMP FUNCTION`, async () => {
await testBigquery(dedent`
CREATE TEMPORARY FUNCTION my_func() AS
(SELECT 1)
`);
});
it(`formats OR REPLACE`, async () => {
await testBigquery(dedent`
CREATE OR REPLACE FUNCTION my_func() AS
(SELECT 1)
`);
});
it(`formats IF NOT EXISTS`, async () => {
await testBigquery(dedent`
CREATE FUNCTION IF NOT EXISTS my_func() AS
(SELECT 1)
`);
});
it(`formats RETURNS clause`, async () => {
await testBigquery(dedent`
CREATE FUNCTION my_func()
RETURNS INT64
AS
(SELECT 1)
`);
});
it(`formats RETURNS TABLE`, async () => {
await testPostgresql(dedent`
CREATE FUNCTION foo()
RETURNS TABLE (id INT, name TEXT)
AS 'SELECT 1'
`);
});
it(`formats OPTIONS (...)`, async () => {
await testBigquery(dedent`
CREATE FUNCTION my_func()
AS
(SELECT 1)
OPTIONS (description = 'constant-value function')
`);
});
it(`formats CREATE TABLE FUNCTION`, async () => {
await testBigquery(dedent`
CREATE TABLE FUNCTION my_func()
RETURNS TABLE<id INT, name STRING>
AS
(SELECT 1, 'John')
`);
});
it(`formats creation of remote function`, async () => {
await testBigquery(dedent`
CREATE FUNCTION my_func()
RETURNS INT64
REMOTE WITH CONNECTION us.myconnection
OPTIONS (endpoint = 'https://us-central1-myproject.cloudfunctions.net/multi')
`);
});
it(`formats PostgreSQL-specific clauses`, async () => {
await testPostgresql(dedent`
CREATE FUNCTION my_func()
RETURNS INT
LANGUAGE SQL
IMMUTABLE
NOT LEAKPROOF
CALLED ON NULL INPUT
EXTERNAL SECURITY DEFINER
PARALLEL UNSAFE
COST 100
ROWS 1000
SUPPORT schm.foo
TRANSFORM FOR TYPE INT, FOR TYPE VARCHAR(100)
RETURN 5 + 5
`);
});
it(`formats WINDOW function loaded from object file`, async () => {
await testPostgresql(dedent`
CREATE FUNCTION my_func()
RETURNS INT
AS 'my_lib.so', 'my_func'
LANGUAGE C
WINDOW
STRICT
`);
});
it(`formats SET config variables`, async () => {
await testPostgresql(dedent`
CREATE FUNCTION my_func()
SET search_path TO my_schema, my_other_schema
SET check_function_bodies = DEFAULT
SET client_min_messages FROM CURRENT
BEGIN ATOMIC
RETURN 1;
END
`);
});
it(`formats JavaScript FUNCTION`, async () => {
await testBigquery(dedent`
CREATE FUNCTION gen_random()
RETURNS FLOAT64
NOT DETERMINISTIC
LANGUAGE js
AS r'''
return Math.random();
'''
`);
});
it(`reformats JavaScript in JS function`, async () => {
expect(
await pretty(
dedent`
CREATE FUNCTION gen_random()
RETURNS FLOAT64
LANGUAGE js
AS ' if(true) {return Math.random () *2}'
`,
{ dialect: "bigquery" },
),
).toBe(dedent`
CREATE FUNCTION gen_random()
RETURNS FLOAT64
LANGUAGE js
AS r'''
if (true) {
return Math.random() * 2;
}
'''
`);
});
it(`quotes JavaScript in double-quotes when single-quotes can't be used`, async () => {
expect(
await pretty(
dedent`
CREATE FUNCTION contains_quotes(x STRING)
RETURNS FLOAT64
LANGUAGE js
AS " return /'''/.test(x) "
`,
{ dialect: "bigquery" },
),
).toBe(dedent`
CREATE FUNCTION contains_quotes(x STRING)
RETURNS FLOAT64
LANGUAGE js
AS r"""
return /'''/.test(x);
"""
`);
});
it(`does not reformat JavaScript when neither ''' or """ can be easily used for quoting`, async () => {
expect(
await pretty(
dedent`
CREATE FUNCTION contains_quotes(x STRING)
RETURNS FLOAT64
LANGUAGE js
AS " return /'''|\\"\\"\\"/.test(x) "
`,
{ dialect: "bigquery" },
),
).toBe(dedent`
CREATE FUNCTION contains_quotes(x STRING)
RETURNS FLOAT64
LANGUAGE js
AS " return /'''|\\"\\"\\"/.test(x) "
`);
});
it(`formats dollar-quoted SQL function`, async () => {
await testPostgresql(dedent`
CREATE FUNCTION my_func()
RETURNS INT64
LANGUAGE sql
AS $$
SELECT 1;
$$
`);
});
it(`reformats SQL in dollar-quoted SQL function`, async () => {
expect(
await pretty(
dedent`
CREATE FUNCTION my_func()
RETURNS INT64
LANGUAGE sql
AS $body$SELECT 1;
select 2$body$
`,
{ dialect: "postgresql" },
),
).toBe(dedent`
CREATE FUNCTION my_func()
RETURNS INT64
LANGUAGE sql
AS $body$
SELECT 1;
SELECT 2;
$body$
`);
});
it(`formats single-quoted SQL function`, async () => {
await testPostgresql(dedent`
CREATE FUNCTION my_func()
RETURNS TEXT
LANGUAGE sql
AS '
SELECT ''foo'';
'
`);
});
it(`reformats SQL in single-quoted SQL function`, async () => {
expect(
await pretty(
dedent`
CREATE FUNCTION my_func()
RETURNS TEXT
LANGUAGE sql
AS 'SELECT ''foo'';
select ''bar'''
`,
{ dialect: "postgresql" },
),
).toBe(dedent`
CREATE FUNCTION my_func()
RETURNS TEXT
LANGUAGE sql
AS '
SELECT ''foo'';
SELECT ''bar'';
'
`);
});
});
describe("drop function", () => {
it(`formats DROP FUNCTION`, async () => {
await testBigquery(`DROP FUNCTION my_schema.my_func`);
});
it(`formats DROP TABLE FUNCTION`, async () => {
await testBigquery(`DROP TABLE FUNCTION my_func`);
});
it(`formats IF EXISTS`, async () => {
await testBigquery(`DROP FUNCTION IF EXISTS my_func`);
});
it(`formats parameter list`, async () => {
await testPostgresql(`DROP FUNCTION my_func(foo INT, bar TEXT)`);
});
it(`formats long parameter list and CASCADE|RESTRICT`, async () => {
await testPostgresql(dedent`
DROP FUNCTION is_user_allowed_to_enter(
user_id INT,
event_id INT,
OUT event_date DATE
) CASCADE
`);
});
});
});