root/trunk/epic/test/test_asserts.sql

Revision 34, 17.6 kB (checked in by fumanchu, 3 years ago)

Fixed to work on PG 8.1.

Line 
1-- Tests for the various assert_* functions which Epic provides.
2-- To run, execute epic.sql, then this script, then test.run_module('test_asserts').
3
4SET search_path = test, public, pg_catalog;
5
6
7CREATE OR REPLACE FUNCTION test.test_assert_test_schema() RETURNS VOID AS $$
8-- Assert the correct operation of epic.assert_test_schema
9-- module: test_asserts
10DECLARE
11  nsoid           oid;
12  objname         text;
13BEGIN
14  -- assert_test_schema MUST create a 'test' schema.
15  -- Of course, if it didn't, this proc wouldn't compile, but what the hey.
16  SELECT pg_namespace.oid INTO nsoid FROM pg_namespace WHERE nspname = 'test';
17  IF nsoid IS NULL THEN
18    RAISE EXCEPTION 'assert_test_schema did not create a ''test'' schema.';
19  END IF;
20 
21  -- assert_test_schema MUST create a 'test.results' table
22  SELECT tablename INTO objname FROM pg_tables
23    WHERE schemaname = 'test' AND tablename = 'results';
24  IF objname IS NULL THEN
25    RAISE EXCEPTION 'assert_test_schema did not create a test.results table.';
26  END IF;
27 
28  RAISE EXCEPTION '[OK]';
29END;
30$$ LANGUAGE plpgsql;
31
32
33CREATE OR REPLACE FUNCTION test.test_assert_raises() RETURNS VOID AS $$
34-- Assert the correct operation of test.assert_raises
35-- module: test_asserts
36DECLARE
37  retval    text;
38  failed    bool;
39BEGIN
40  -- assert_raises() MUST return VOID if an error is raised.
41  SELECT INTO retval * FROM test.assert_raises('unknown', 'relation "unknown" does not exist', '42P01');
42  IF retval != '' THEN
43    RAISE EXCEPTION 'test.assert_raises() did not return void.';
44  END IF;
45 
46  -- assert_raises() MUST raise an exception if no error is raised.
47  failed := false;
48  BEGIN
49    SELECT INTO retval * FROM test.assert_raises('pg_namespace', '', '');
50  EXCEPTION WHEN OTHERS THEN
51    failed := true;
52    IF SQLERRM = 'Call: ''pg_namespace'' did not raise an error.' THEN
53      NULL;
54    ELSE
55      RAISE EXCEPTION 'test.assert_raises() did not raise the given message on falsehood. Raised: %', SQLERRM;
56    END IF;
57  END;
58  IF NOT failed THEN
59    RAISE EXCEPTION 'test.assert_raises() did not fail.';
60  END IF;
61 
62  RAISE EXCEPTION '[OK]';
63END;
64$$ LANGUAGE plpgsql;
65
66
67CREATE OR REPLACE FUNCTION test.test_assert() RETURNS VOID AS $$
68-- Assert the correct operation of test.assert
69-- module: test_asserts
70DECLARE
71  retval    text;
72BEGIN
73  -- assert() MUST return VOID if the given assertion holds.
74  SELECT INTO retval * FROM test.assert(true, 'truth is falsehood!');
75  IF retval != '' THEN
76    RAISE EXCEPTION 'test.assert() did not return void.';
77  END IF;
78 
79  -- assert() MUST raise an exception if the given assertion does not hold.
80  PERFORM test.assert_raises('test.assert(false, ''falsehood is truth'')',
81                             'falsehood is truth', 'P0001');
82 
83  -- assert() MUST raise an exception if the given assertion is NULL.
84  PERFORM test.assert_raises('test.assert(null, ''null should choke'')',
85                             'Assertion test may not be NULL.', 'P0001');
86 
87  RAISE EXCEPTION '[OK]';
88END;
89$$ LANGUAGE plpgsql;
90
91
92CREATE OR REPLACE FUNCTION test._return_void() RETURNS VOID AS $$
93BEGIN
94  RETURN;
95END;
96$$ LANGUAGE plpgsql;
97
98CREATE OR REPLACE FUNCTION test.test_assert_void() RETURNS VOID AS $$
99-- Assert the correct operation of test.assert_void
100-- module: test_asserts
101DECLARE
102  retval    text;
103BEGIN
104  -- assert_void() MUST return VOID if the given call returns VOID.
105  SELECT INTO retval * FROM test.assert_void('test._return_void()');
106  IF retval != '' THEN
107    RAISE EXCEPTION 'assert_void did not itself return void. Got ''%'' instead.', retval;
108  END IF;
109 
110  -- assert_void() MUST raise an exception if the given call does not return void.
111  PERFORM test.assert_raises('test.assert_void(''pg_namespace WHERE nspname = ''''pg_catalog'''''')',
112    'Call: ''pg_namespace WHERE nspname = ''pg_catalog'''' did not return void. Got ''pg_catalog'' instead.',
113    'P0001');
114 
115  RAISE EXCEPTION '[OK]';
116END;
117$$ LANGUAGE plpgsql;
118
119
120CREATE OR REPLACE FUNCTION test.test_assert_equal() RETURNS VOID AS $$
121-- Assert the correct operation of test.assert_equal
122-- module: test_asserts
123DECLARE
124  retval    text;
125BEGIN
126  -- assert_equal() MUST return VOID if the given assertion holds.
127  PERFORM test.assert_void('test.assert_equal(1, 1)');
128  PERFORM test.assert_void('test.assert_equal(''abc''::text, ''abc'');');
129 
130  -- assert_equal() MUST return VOID if both args are null.
131  PERFORM test.assert_void('test.assert_equal(NULL::int, NULL);');
132 
133  -- assert_equal() MUST raise an exception if the given assertion does not hold.
134  PERFORM test.assert_raises('test.assert_equal(1, 2)', '1 != 2', 'P0001');
135  PERFORM test.assert_raises('test.assert_equal(''abc''::text, ''xyz'')', 'abc != xyz', 'P0001');
136 
137  -- assert_equal() MUST raise an exception if only one arg is NULL.
138  PERFORM test.assert_raises('test.assert_equal(8, NULL::int)', '8 != <NULL>', 'P0001');
139  PERFORM test.assert_raises('test.assert_equal(NULL::int, 7)', '<NULL> != 7', 'P0001');
140 
141  -- assert_equal() will raise an undefined_function exception if the args have different types.
142  -- It would be nice to find a way around this (without writing M x N overloaded funcs).
143  PERFORM test.assert_raises('test.assert_equal(8, ''abc''::text)',
144    'function test.assert_equal(integer, text) does not exist', '42883');
145 
146  RAISE EXCEPTION '[OK]';
147END;
148$$ LANGUAGE plpgsql;
149
150
151CREATE OR REPLACE FUNCTION test.test_assert_not_equal() RETURNS VOID AS $$
152-- Assert the correct operation of test.assert_not_equal
153-- module: test_asserts
154DECLARE
155  retval    text;
156BEGIN
157  -- assert_not_equal() MUST return VOID if the given assertion does not hold.
158  PERFORM test.assert_void('test.assert_not_equal(1, 2);');
159  PERFORM test.assert_void('test.assert_not_equal(''abc''::text, ''xyz'');');
160 
161  -- assert_not_equal() MUST return VOID if only one arg is NULL.
162  PERFORM test.assert_void('test.assert_not_equal(8, NULL);');
163  PERFORM test.assert_void('test.assert_not_equal(NULL, 7);');
164 
165  -- assert_not_equal() MUST raise an exception if the given assertion holds.
166  PERFORM test.assert_raises('test.assert_not_equal(1, 1)', '1 = 1', 'P0001');
167  PERFORM test.assert_raises('test.assert_not_equal(''abc''::text, ''abc'')', 'abc = abc', 'P0001');
168 
169  -- assert_not_equal() MUST raise an exception if both args are NULL.
170  PERFORM test.assert_raises('test.assert_not_equal(NULL::int, NULL)', '<NULL> = <NULL>', 'P0001');
171 
172  -- assert_not_equal() will raise an undefined_function exception if the args have different types.
173  -- It would be nice to find a way around this (without writing M x N overloaded funcs).
174  PERFORM test.assert_raises('test.assert_not_equal(8, ''abc''::text)',
175    'function test.assert_not_equal(integer, text) does not exist', '42883');
176 
177  RAISE EXCEPTION '[OK]';
178END;
179$$ LANGUAGE plpgsql;
180
181
182CREATE OR REPLACE FUNCTION test.test_assert_less_than() RETURNS VOID AS $$
183-- Assert the correct operation of test.assert_less_than
184-- module: test_asserts
185DECLARE
186  retval    text;
187BEGIN
188  -- assert_less_than() MUST return VOID if a < b.
189  PERFORM test.assert_void('test.assert_less_than(1, 2);');
190  PERFORM test.assert_void('test.assert_less_than(''abc''::text, ''xyz'');');
191 
192  -- assert_less_than() MUST raise an exception if a >= b.
193  PERFORM test.assert_raises('test.assert_less_than(2, 1)', '2 not < 1', 'P0001');
194  PERFORM test.assert_raises('test.assert_less_than(1, 1)', '1 not < 1', 'P0001');
195  PERFORM test.assert_raises('test.assert_less_than(''abc''::text, ''abc'')',
196    'abc not < abc', 'P0001');
197  PERFORM test.assert_raises('test.assert_less_than(''xyz''::text, ''abc'')',
198    'xyz not < abc', 'P0001');
199 
200  -- assert_less_than() MUST raise an exception if either arg is NULL.
201  PERFORM test.assert_raises('test.assert_less_than(NULL::int, NULL)',
202    'Assertion arguments may not be NULL.', 'P0001');
203 
204  -- assert_less_than() will raise an undefined_function exception if the args have different types.
205  -- It would be nice to find a way around this (without writing M x N overloaded funcs).
206  PERFORM test.assert_raises('test.assert_less_than(8, ''abc''::text)',
207    'function test.assert_less_than(integer, text) does not exist', '42883');
208 
209  RAISE EXCEPTION '[OK]';
210END;
211$$ LANGUAGE plpgsql;
212
213
214CREATE OR REPLACE FUNCTION test.test_assert_less_than_or_equal() RETURNS VOID AS $$
215-- Assert the correct operation of test.assert_less_than_or_equal
216-- module: test_asserts
217DECLARE
218  retval    text;
219BEGIN
220  -- assert_less_than_or_equal() MUST return VOID if a <= b.
221  PERFORM test.assert_void('test.assert_less_than_or_equal(1, 2);');
222  PERFORM test.assert_void('test.assert_less_than_or_equal(1, 1);');
223  PERFORM test.assert_void('test.assert_less_than_or_equal(''abc''::text, ''xyz'');');
224  PERFORM test.assert_void('test.assert_less_than_or_equal(''abc''::text, ''abc'');');
225 
226  -- assert_less_than_or_equal() MUST raise an exception if a > b.
227  PERFORM test.assert_raises('test.assert_less_than_or_equal(2, 1)', '2 not <= 1', 'P0001');
228  PERFORM test.assert_raises('test.assert_less_than_or_equal(''xyz''::text, ''abc'')',
229    'xyz not <= abc', 'P0001');
230 
231  -- assert_less_than_or_equal() MUST raise an exception if either arg is NULL.
232  PERFORM test.assert_raises('test.assert_less_than_or_equal(NULL::int, NULL)',
233    'Assertion arguments may not be NULL.', 'P0001');
234 
235  -- assert_less_than_or_equal() will raise an undefined_function exception if the args have different types.
236  -- It would be nice to find a way around this (without writing M x N overloaded funcs).
237  PERFORM test.assert_raises('test.assert_less_than_or_equal(8, ''abc''::text)',
238    'function test.assert_less_than_or_equal(integer, text) does not exist', '42883');
239 
240  RAISE EXCEPTION '[OK]';
241END;
242$$ LANGUAGE plpgsql;
243
244
245CREATE OR REPLACE FUNCTION test.test_assert_greater_than() RETURNS VOID AS $$
246-- Assert the correct operation of test.assert_greater_than
247-- module: test_asserts
248DECLARE
249  retval    text;
250BEGIN
251  -- assert_greater_than() MUST return VOID if a > b.
252  PERFORM test.assert_void('test.assert_greater_than(2, 1);');
253  PERFORM test.assert_void('test.assert_greater_than(''xyz''::text, ''abc'');');
254 
255  -- assert_greater_than() MUST raise an exception if a <= b.
256  PERFORM test.assert_raises('test.assert_greater_than(1, 2)', '1 not > 2', 'P0001');
257  PERFORM test.assert_raises('test.assert_greater_than(1, 1)', '1 not > 1', 'P0001');
258  PERFORM test.assert_raises('test.assert_greater_than(''abc''::text, ''abc'')',
259    'abc not > abc', 'P0001');
260  PERFORM test.assert_raises('test.assert_greater_than(''abc''::text, ''xyz'')',
261    'abc not > xyz', 'P0001');
262 
263  -- assert_greater_than() MUST raise an exception if either arg is NULL.
264  PERFORM test.assert_raises('test.assert_greater_than(NULL::int, NULL)',
265    'Assertion arguments may not be NULL.', 'P0001');
266 
267  -- assert_greater_than() will raise an undefined_function exception if the args have different types.
268  -- It would be nice to find a way around this (without writing M x N overloaded funcs).
269  PERFORM test.assert_raises('test.assert_greater_than(8, ''abc''::text)',
270    'function test.assert_greater_than(integer, text) does not exist', '42883');
271 
272  RAISE EXCEPTION '[OK]';
273END;
274$$ LANGUAGE plpgsql;
275
276
277CREATE OR REPLACE FUNCTION test.test_assert_greater_than_or_equal() RETURNS VOID AS $$
278-- Assert the correct operation of test.assert_greater_than_or_equal
279-- module: test_asserts
280DECLARE
281  retval    text;
282BEGIN
283  -- assert_greater_than_or_equal() MUST return VOID if a >= b.
284  PERFORM test.assert_void('test.assert_greater_than_or_equal(2, 1);');
285  PERFORM test.assert_void('test.assert_greater_than_or_equal(1, 1);');
286  PERFORM test.assert_void('test.assert_greater_than_or_equal(''xyz''::text, ''abc'');');
287  PERFORM test.assert_void('test.assert_greater_than_or_equal(''abc''::text, ''abc'');');
288 
289  -- assert_greater_than_or_equal() MUST raise an exception if a < b.
290  PERFORM test.assert_raises('test.assert_greater_than_or_equal(1, 2)', '1 not >= 2', 'P0001');
291  PERFORM test.assert_raises('test.assert_greater_than_or_equal(''abc''::text, ''xyz'')',
292    'abc not >= xyz', 'P0001');
293 
294  -- assert_greater_than_or_equal() MUST raise an exception if either arg is NULL.
295  PERFORM test.assert_raises('test.assert_greater_than_or_equal(NULL::int, NULL)',
296    'Assertion arguments may not be NULL.', 'P0001');
297 
298  -- assert_greater_than_or_equal() will raise an undefined_function exception if the args have different types.
299  -- It would be nice to find a way around this (without writing M x N overloaded funcs).
300  PERFORM test.assert_raises('test.assert_greater_than_or_equal(8, ''abc''::text)',
301    'function test.assert_greater_than_or_equal(integer, text) does not exist', '42883');
302 
303  RAISE EXCEPTION '[OK]';
304END;
305$$ LANGUAGE plpgsql;
306
307
308CREATE OR REPLACE FUNCTION test.test_assert_rows() RETURNS VOID AS $$
309-- Assert the correct operation of test.assert_rows
310-- module: test_asserts
311DECLARE
312  failed     bool;
313BEGIN
314  -- Tautology
315  PERFORM test.assert_rows(
316    'SELECT oid, proname FROM pg_proc',
317    'SELECT oid, proname FROM pg_proc');
318  -- Almost a tautology ;)
319  -- Note the trailing semicolon in the first arg.
320  PERFORM test.assert_rows(
321    'SELECT tablename FROM pg_tables;',
322    'SELECT relname FROM pg_class where relkind = ''r''');
323  -- SELECT-less argument
324  PERFORM test.assert_rows(
325    'SELECT adrelid, adnum, adbin, adsrc FROM pg_attrdef',
326    'pg_attrdef');
327 
328  -- ...and an assertion that should fail
329  failed := false;
330  BEGIN
331    PERFORM test.assert_rows(
332      'generate_series(1, 10)',
333      'SELECT * FROM generate_series(1, 5)');
334  EXCEPTION WHEN OTHERS THEN
335    failed := true;
336    IF SQLERRM = 'Record: (6) from: generate_series(1, 10) not found in: SELECT * FROM generate_series(1, 5)' THEN
337      NULL;
338    ELSE
339      RAISE EXCEPTION 'test.assert_rows() did not raise the correct error. Raised: %', SQLERRM;
340    END IF;
341  END;
342  IF NOT failed THEN
343    PERFORM test.fail('test.assert_rows() did not fail.');
344  END IF;
345 
346  PERFORM test.pass();
347END;
348$$ LANGUAGE plpgsql;
349
350
351CREATE OR REPLACE FUNCTION test.test_assert_column() RETURNS VOID AS $$
352-- Assert the correct operation of test.assert_column
353-- module: test_asserts
354DECLARE
355  failed     bool;
356BEGIN
357  -- Test an assertion that should pass
358  PERFORM test.assert_column(
359    'generate_series(1, 10);',
360    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
361  -- explicit colname version
362  PERFORM test.assert_column(
363    'generate_series(1, 10);',
364    ARRAY[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
365    'generate_series');
366  -- text fields
367  PERFORM test.assert_column(
368    'pg_namespace WHERE nspname IN (''public'', ''test'') ORDER BY nspname',
369    ARRAY['public', 'test'],
370    'nspname');
371  -- timestamp fields. This also tests omitted colname
372  PERFORM test.assert_column(
373    'SELECT ''2007-04-13 09:28:54.132132''::timestamptz',
374    ARRAY['2007-04-13 09:28:54.132132'::timestamptz]);
375 
376  -- ...and an assertion that should fail
377  failed := false;
378  BEGIN
379    PERFORM test.assert_column('generate_series(1, 10);', ARRAY[1, 2]);
380  EXCEPTION WHEN OTHERS THEN
381    failed := true;
382    IF SQLERRM = '[FAIL] record: 3 not found in array: 1, 2' THEN
383      NULL;
384    ELSE
385      RAISE EXCEPTION 'test.assert_column() did not raise the correct error. Raised: %', SQLERRM;
386    END IF;
387  END;
388  IF NOT failed THEN
389    PERFORM test.fail('test.assert_column() did not fail.');
390  END IF;
391 
392  RAISE EXCEPTION '[OK]';
393END;
394$$ LANGUAGE plpgsql;
395
396
397CREATE OR REPLACE FUNCTION test.test_assert_empty() RETURNS VOID AS $$
398-- Assert the correct operation of test.assert_empty
399-- module: test_asserts
400DECLARE
401  failed     bool;
402BEGIN
403  -- Test an assertion that should pass
404  CREATE TEMP TABLE testtemp (a int);
405  PERFORM test.assert_empty('testtemp');
406  -- array version
407  CREATE TEMP TABLE testtemp2 (a int);
408  PERFORM test.assert_empty('{testtemp, testtemp2}'::text[]);
409  PERFORM test.assert_empty(ARRAY['testtemp', 'testtemp2']);
410 
411  -- ...and an assertion that should fail
412  failed := false;
413  BEGIN
414    PERFORM test.assert_empty('{pg_type,pg_proc}'::text[]);
415  EXCEPTION WHEN OTHERS THEN
416    failed := true;
417    IF SQLERRM = '[FAIL] The calls "pg_type", "pg_proc" are not empty.' THEN
418      NULL;
419    ELSE
420      RAISE EXCEPTION 'test.assert_empty() did not raise the correct error. Raised: %', SQLERRM;
421    END IF;
422  END;
423  IF NOT failed THEN
424    PERFORM test.fail('test.assert_empty() did not fail.');
425  END IF;
426 
427  RAISE EXCEPTION '[OK]';
428END;
429$$ LANGUAGE plpgsql;
430
431
432CREATE OR REPLACE FUNCTION test.test_assert_not_empty() RETURNS VOID AS $$
433-- Assert the correct operation of test.assert_not_empty
434-- module: test_asserts
435DECLARE
436  failed     bool;
437BEGIN
438  -- Test an assertion that should pass
439  CREATE TEMP TABLE testtemp AS SELECT * FROM generate_series(1, 10);
440  PERFORM test.assert_not_empty('testtemp');
441  -- array version
442  CREATE TEMP TABLE testtemp2 AS SELECT * FROM generate_series(1, 5);
443  PERFORM test.assert_not_empty('{testtemp, testtemp2}'::text[]);
444  PERFORM test.assert_not_empty(ARRAY['testtemp', 'testtemp2']);
445 
446  -- ...and an assertion that should fail
447  CREATE TEMP TABLE testtemp3 (a int);
448  failed := false;
449  BEGIN
450    PERFORM test.assert_not_empty('testtemp3');
451  EXCEPTION WHEN OTHERS THEN
452    failed := true;
453    IF SQLERRM = '[FAIL] The call "testtemp3" is empty.' THEN
454      NULL;
455    ELSE
456      RAISE EXCEPTION 'test.assert_not_empty() did not raise the correct error. Raised: %', SQLERRM;
457    END IF;
458  END;
459  IF NOT failed THEN
460    PERFORM test.fail('test.assert_not_empty() did not fail.');
461  END IF;
462 
463  RAISE EXCEPTION '[OK]';
464END;
465$$ LANGUAGE plpgsql;
Note: See TracBrowser for help on using the browser.