-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.sql
More file actions
283 lines (268 loc) · 6.72 KB
/
base.sql
File metadata and controls
283 lines (268 loc) · 6.72 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
\set ECHO none
\i test/pgxntool/setup.sql
\i test/load.sql
/*
CREATE TABLE process(
template text
, parameter jsonb
, result text
);
*/
-- Protect against infinite loops
SET statement_timeout = '2 s';
SELECT no_plan();
SELECT is(
trunklet.process_language(
'format'
, text $$No parameters.$$
, NULL::jsonb
)
, $$No parameters.$$
);
SELECT is(
trunklet.process_language(
'format'
, text $$No parameters.$$
, jsonb $${"Moo": "cow"}$$
)
, $$No parameters.$$
);
SELECT is(
trunklet.process_language(
'format'
, text $$%nope%OLx%nope2%Os$$
, NULL::jsonb
)
, 'NULLx'
, $$Optional parameter.$$
);
SELECT is(
trunklet.process_language(
'format'
, text $$%nope%OLx%nope2%Os$$
, jsonb $${"Moo": "cow"}$$
)
, 'NULLx'
, $$Optional parameter.$$
);
SELECT is(
trunklet.process_language(
'format'
, text $$A %test%s test.$$
, jsonb $${
"test": "simple"
}$$
)
, $$A simple test.$$
);
SELECT is(
trunklet.process_language(
'format'
, text $$%% 1 %% 2 %%$$
, jsonb $${"Moo": "cow"}$$
)
, $$% 1 % 2 %$$
);
SELECT is(
trunklet.process_language(
'format'
, text $$%%%Moo%s%%%Moo%L%%%Moo%I%%$$
, jsonb $${"Moo": "says cow"}$$
)
, $$%says cow%'says cow'%"says cow"%$$
);
SELECT is(
trunklet.process_language(
'format'
, $$%start%s%middle%s%end%s$$::text
, jsonb $${
"start": "This is the start.\n",
"middle": "This is the middle.\n",
"end": "This is the end.\n"
}$$
)
, $$This is the start.
This is the middle.
This is the end.
$$
);
SELECT is(
trunklet.process_language(
'format'
, replace(
$$%opt%OR%value%R%opt%OR%value%R$$
, 'R'
, replace
)
, j
)
, repeat( format(
replace( '%R%R', 'R', replace )
, NULL
, j->>'value'
), 2 )
, format( 'test optional handling with %s and %s', replace, j->>'value' )
)
FROM unnest('{s,L}'::text[]) r(replace)
, (SELECT row_to_json(v) AS j FROM (VALUES
(to_json(1.1))
, (to_json(NULL::int))
, (to_json('text'::text))
, (to_json(true))
) v(value) ) j
;
SELECT throws_ok(
$$SELECT trunklet.process_language( 'format', 'some stuff%test parameter%OIsome more stuff', NULL::json )$$
, '22004' -- null_value_not_allowed
, 'SQL identifier format option ("I") not allowed with optional parameters'
);
-- This is meant to ensure the DETAIL is correct, but it doesn't work on travis :(
/*
SAVEPOINT a;
\unset ON_ERROR_STOP
\set VERBOSITY default
\echo THIS ERROR IS OK!
SELECT 'DETAIL: parameter "test parameter" at template position ' || strpos( 'some stuff%test parameter%OIsome more stuff', '%' );
SELECT trunklet.process_language( 'format', 'some stuff%test parameter%OIsome more stuff', NULL::json );
ROLLBACK TO a;
\set ON_ERROR_STOP 1
\set VERBOSITY VERBOSE
*/
SELECT throws_ok(
format(
$$SELECT trunklet.process_language( 'format', %L::text, %L::jsonb )$$
, '%Moo%' || optional || specifier
, '{"Moo":1}'
)
, format(
'Unexpected character "%s" in format specifier "%s"'
, specifier
, optional || specifier
)
)
FROM unnest('{"",a,S,i,l}'::text[]) s(specifier)
, unnest('{"",O}'::text[]) o(optional)
;
-- This is meant to ensure the DETAIL is correct, but it doesn't work on travis :(
/*
SAVEPOINT a;
\unset ON_ERROR_STOP
\set VERBOSITY default
\echo THIS ERROR IS OK!
SELECT 'DETAIL: parameter "Moo" at template position ' || strpos( 'more%Moo%OSmore', '%' );
SELECT trunklet.process_language('format', 'more%Moo%OSmore', NULL::json);
ROLLBACK TO a;
\echo THIS ERROR IS OK!
SELECT 'DETAIL: parameter "Moo" at template position ' || strpos( 'more%Moo%OSmore', '%' );
SELECT trunklet.process_language('format', 'more%Moo%lmore', NULL::json);
ROLLBACK TO a;
\set ON_ERROR_STOP 1
\set VERBOSITY VERBOSE
*/
SELECT throws_like(
format(
$$SELECT trunklet.process_language( 'format', %L::text, %L::jsonb )$$
, replace(input, 's', replacement)
, param
)
, 'parameter "mia" not found'
)
FROM (VALUES
('%mia%s ')
, (' %mia%s')
, (' %mia%s ')
, ('%mia%s')
) input( input )
, (VALUES ('{"Moo":1}'), (NULL)
) param( param )
, unnest('{s,I,L}'::text[]) u(replacement)
;
CREATE TEMP VIEW extract_test AS
SELECT *
FROM ( VALUES
( '{s1}'::text[], '{ "s1": "string 1" }'::jsonb , 'single string'::text )
, ( '{num}' , '{ "num": 1.1 }' , 'number' )
, ( '{t,f,"null"}', '{ "t":true,"f":false,"null":null }' , 'multiple' )
, ( '{num,bogus}' , '{ "num": 1.1 }' , 'num & bogus' )
, ( '{bogus}' , NULL , 'bogus' )
) AS v( extract_list, expected, description )
;
SELECT is(
trunklet.extract_parameters(
'format'
, '{
"s1": "string 1",
"s2": "string 2",
"num": 1.1,
"t": true,
"f": false,
"null": null
}'::jsonb
, extract_list
)::jsonb
, expected
, 'Test extract_paramaters() for ' || description
)
FROM extract_test
;
/*
* Common syntax checking code
*/
SELECT throws_ok(
format(
$$SELECT trunklet.$$ || format
, j
)
, format( 'parameters must be a JSON object, not %s', jsonb_typeof(j->'value') )
, format(
$$SELECT trunklet.$$ || format
, j
)
)
FROM
unnest(array[
$$process_language('format', 'template', (%L::jsonb)->'value')$$
, $$extract_parameters('format', (%L::jsonb)->'value', '{a}'::text[])$$
]) f(format)
, (SELECT row_to_json(v)::jsonb AS j FROM (VALUES
(to_json(1.1))
, (to_json('text'::text))
, (to_json(true))
, (to_json(array[1,2,3]))
) v(value) ) j
;
SELECT throws_ok(
format(
$$SELECT trunklet.$$ || format
, format( '{"%s":1}', key )
)
, NULL
, 'parameter names may not contain "%"'
, format || ': parameter names may not contain "%"'
)
FROM
unnest(array[
$$process_language('format', 'template', %L::jsonb)$$
, $$extract_parameters('format', %L::jsonb, '{a}'::text[])$$
]) f(format)
, unnest('{% key,key %,k%k}'::text[]) k(key)
;
SELECT throws_ok(
format(
$$SELECT trunklet.$$ || format
, j
)
, format( '%s is not supported as a parameter type', jsonb_typeof(j->'value') )
)
FROM
unnest(array[
$$process_language('format', 'template', %L::jsonb)$$
, $$extract_parameters('format', %L::jsonb, '{a}'::text[])$$
]) f(format)
, (SELECT row_to_json(v)::jsonb AS j FROM (VALUES
('{"key":"value"}'::json)
, (to_json(array[1,2,3]))
) v(value) ) j
;
\i test/pgxntool/finish.sql
-- vi: expandtab ts=2 sw=2