forked from adrianandrei-ca/pgunit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pgunit--0.1.0.sql
432 lines (412 loc) · 10.7 KB
/
pgunit--0.1.0.sql
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pgunit" to load this file. \quit
create type @[email protected] as (
test_name varchar,
successful boolean,
failed boolean,
erroneous boolean,
error_message varchar,
duration interval
);
--
-- Use select * from run_all() to execute all test cases
--
create or replace function @[email protected]_all ()
returns setof @[email protected]
as $$
begin
return query
select
*
from
@[email protected]_suite (null);
end;
$$
language plpgsql
set search_path
from
current;
--
-- Executes all test cases part of a suite and returns the test results.
--
-- Each test case will have a setup procedure run first, then a precondition,
-- then the test itself, followed by a postcondition and a tear down.
--
-- The test case stored procedure name has to match 'test_case_<p_suite>%' patern.
-- It is assumed the setup and precondition procedures are in the same schema as
-- the test stored procedure.
--
-- select * from run_suite('my_test'); will run all tests that will have
-- 'test_case_my_test' prefix.
create or replace function @[email protected]_suite (p_suite text)
returns setof @[email protected]
as $$
declare
l_proc RECORD;
l_sid integer;
l_row @[email protected]%rowtype;
l_start_ts timestamp;
l_cmd text;
l_condition text;
l_precondition_cmd text;
l_postcondition_cmd text;
begin
l_sid := pg_backend_pid();
for l_proc in
select
p.proname,
n.nspname
from
pg_catalog.pg_proc p
join pg_catalog.pg_namespace n on p.pronamespace = n.oid
where
p.proname like 'test/_case/_' || COALESCE(p_suite, '') || '%' escape '/'
order by
p.proname loop
-- check for setup
l_condition := @[email protected]_procname (l_proc.proname, 2, 'test_setup');
if l_condition is not null then
l_cmd := 'DO $body$ begin perform ' || quote_ident(l_proc.nspname) || '.' || quote_ident(l_condition) || '(); end; $body$';
perform
@[email protected] (l_cmd);
end if;
l_row.test_name := quote_ident(l_proc.proname);
-- check for precondition
l_condition := @[email protected]_procname (l_proc.proname, 2, 'test_precondition');
if l_condition is not null then
l_precondition_cmd := 'perform @[email protected]_condition(''' || quote_ident(l_proc.nspname) || '.' || quote_ident(l_condition) || '''); ';
else
l_precondition_cmd := '';
end if;
-- check for postcondition
l_condition := @[email protected]_procname (l_proc.proname, 2, 'test_postcondition');
if l_condition is not null then
l_postcondition_cmd := 'perform @[email protected]_condition(''' || quote_ident(l_proc.nspname) || '.' || quote_ident(l_condition) || '''); ';
else
l_postcondition_cmd := '';
end if;
-- execute the test
l_start_ts := clock_timestamp();
begin
l_cmd := 'DO $body$ begin ' || l_precondition_cmd || 'perform ' || quote_ident(l_proc.nspname) || '.' || quote_ident(l_proc.proname) || '(); ' || l_postcondition_cmd || ' end; $body$';
perform
@[email protected] (l_cmd);
l_row.successful := true;
l_row.failed := false;
l_row.erroneous := false;
l_row.error_message := 'OK';
exception
when triggered_action_exception then
l_row.successful := false;
l_row.failed := true;
l_row.erroneous := false;
l_row.error_message := SQLERRM;
when others then
l_row.successful := false;
l_row.failed := false;
l_row.erroneous := true;
l_row.error_message := SQLERRM;
end;
l_row.duration = clock_timestamp() - l_start_ts;
return next l_row;
-- check for teardown
l_condition := @[email protected]_procname (l_proc.proname, 2, 'test_teardown');
if l_condition is not null then
l_cmd := 'DO $body$ begin perform ' || quote_ident(l_proc.nspname) || '.' || quote_ident(l_condition) || '(); end; $body$';
perform
@[email protected] (l_cmd);
end if;
end loop;
end;
$$
language plpgsql
set search_path
from
current;
--
-- recreates a _ separated string from parts array
--
create or replace function @[email protected]_procname (parts text[], p_from integer default 1, p_to integer default null)
returns text
as $$
declare
name text := '';
idx integer;
begin
if p_to is null then
p_to := array_length(parts, 1);
end if;
name := parts[p_from];
for idx in (p_from + 1)..p_to loop
name := name || '_' || parts[idx];
end loop;
return name;
end;
$$
language plpgsql
set search_path
from
current immutable;
--
-- Returns the procedure name matching the pattern below
-- <result_prefix>_<test_case_name>
-- Ex: result_prefix = test_setup and test_case_name = company_finance_invoice then it searches for:
-- test_setup_company_finance_invoice()
-- test_setup_company_finance()
-- test_setup_company()
--
-- It returns the name of the first stored procedure present in the database
--
create or replace function @[email protected]_procname (test_case_name text, expected_name_count integer, result_prefix text)
returns text
as $$
declare
array_name text[];
array_proc text[];
idx integer;
len integer;
proc_name text;
is_valid integer;
begin
array_name := string_to_array(test_case_name, '_');
len := array_length(array_name, 1);
for idx in expected_name_count + 1..len loop
array_proc := array_proc || array_name[idx];
end loop;
len := array_length(array_proc, 1);
for idx in reverse len..1 loop
proc_name := result_prefix || '_' || @[email protected]_procname (array_proc, 1, idx);
select
1 into is_valid
from
pg_catalog.pg_proc
where
proname = proc_name;
if is_valid = 1 then
return proc_name;
end if;
end loop;
return null;
end;
$$
language plpgsql
set search_path
from
current;
--
-- executes a condition boolean function
--
create or replace function @[email protected]_condition (proc_name text)
returns void
as $$
declare
status boolean;
begin
execute 'select ' || proc_name || '()' into status;
if status then
return;
end if;
raise exception 'condition failure: %()', proc_name
using errcode = 'triggered_action_exception';
end;
$$
language plpgsql
set search_path
from
current;
--
-- Use: select terminate('db name'); to terminate all locked processes
--
create or replace function @[email protected] (db varchar)
returns setof record
as $$
select
pg_terminate_backend(pid),
query
from
pg_stat_activity
where
pid != pg_backend_pid()
and datname = db
and state = 'active';
$$
language sql;
--
-- Use: perform autonomous('UPDATE|INSERT|DELETE|SELECT sp() ...'); to
-- change data in a separate transaction.
--
create or replace function @[email protected] (p_statement varchar)
returns void
as $$
declare
l_error_text character varying;
l_error_detail character varying;
begin
set search_path to default;
execute p_statement;
set search_path from current; -- TODO ugly
exception
when triggered_action_exception then
-- this is triggered when condition is false but should be true and vice versa
set search_path from current; -- TODO ugly
raise exception 'Condition Failure (or check pre-, post conditions)' using errcode = 'triggered_action_exception';
when others then
set search_path from current; -- TODO ugly
get stacked diagnostics l_error_text = message_text,
l_error_detail = pg_exception_detail;
raise exception '%',l_error_text using errcode = 'syntax_error';
rollback;
-- never actually reached
raise exception '%: Error on executing: % % %', sqlstate, p_statement, l_error_text, l_error_detail using errcode = sqlstate;
end;
$$
language plpgsql
set search_path
from
current;
create or replace function @[email protected] (message varchar, condition boolean)
returns void
as $$
begin
if condition then
null;
else
raise exception 'assertTrue failure: %', message
using errcode = 'triggered_action_exception';
end if;
end;
$$
language plpgsql
set search_path
from
current immutable;
create or replace function @[email protected] (condition boolean)
returns void
as $$
begin
if condition then
null;
else
raise exception 'assertTrue failure'
using errcode = 'triggered_action_exception';
end if;
end;
$$
language plpgsql
set search_path
from
current immutable;
create or replace function @[email protected] (message varchar, condition boolean)
returns void
as $$
begin
if not condition then
null;
else
raise exception 'assertFalse failure: %', message
using errcode = 'triggered_action_exception';
end if;
end;
$$
language plpgsql
set search_path
from
current immutable;
create or replace function @[email protected] (condition boolean)
returns void
as $$
begin
if not condition then
null;
else
raise exception 'assertFalse failure'
using errcode = 'triggered_action_exception';
end if;
end;
$$
language plpgsql
set search_path
from
current immutable;
create or replace function @[email protected] (message varchar, ANYELEMENT)
returns void
as $$
begin
if $2 is null then
raise exception 'assertNotNull failure: %', message
using errcode = 'triggered_action_exception';
end if;
end;
$$
language plpgsql
set search_path
from
current immutable;
create or replace function @[email protected] (ANYELEMENT)
returns void
as $$
begin
if $2 is null then
raise exception 'assertNotNull failure'
using errcode = 'triggered_action_exception';
end if;
end;
$$
language plpgsql
set search_path
from
current immutable;
create or replace function @[email protected] (varchar, ANYELEMENT)
returns void
as $$
begin
if $2 is not null then
raise exception 'assertNull failure: %', $1
using errcode = 'triggered_action_exception';
end if;
end;
$$
language plpgsql
set search_path
from
current immutable;
create or replace function @[email protected] (ANYELEMENT)
returns void
as $$
begin
if $1 is not null then
raise exception 'assertNull failure'
using errcode = 'triggered_action_exception';
end if;
end;
$$
language plpgsql
set search_path
from
current immutable;
create or replace function @[email protected] (ANYELEMENT)
returns void
as $$
begin
if $1 is not null then
raise exception 'assertNull failure'
using errcode = 'triggered_action_exception';
end if;
end;
$$
language plpgsql
set search_path
from
current immutable;
create or replace function @[email protected] (varchar)
returns void
as $$
begin
raise exception 'test failure: %', $1
using errcode = 'triggered_action_exception';
end;
$$
language plpgsql
set search_path
from
current immutable;