Changeset 5

Show
Ignore:
Timestamp:
08/16/08 12:38:29 (4 years ago)
Author:
fumanchu
Message:

New assertion methods: assert_void, assert_less_than, assert_greater_than, assert_greater_than_or_equal.

Location:
trunk/epic
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/epic/epic.sql

    r4 r5  
    9595        to assert anything that can be evaluated to a boolean. For example, 
    9696        PERFORM test.assert(substring(a from b), b||" not found in "||a); 
    97     * test.assert_equal(1 anyelement, 2 anyelement) 
    98     * test.assert_not_equal(1 anyelement, 2 anyelement) 
    99     * test.assert_less_than(1 anyelement, 2 anyelement) 
     97    * test.assert_void(call text) 
     98    * test.assert_equal(elem_1 anyelement, elem_2 anyelement) 
     99    * test.assert_not_equal(elem_1 anyelement, elem_2 anyelement) 
     100    * test.assert_greater_than(elem_1 anyelement, elem_2 anyelement) 
     101    * test.assert_greater_than_or_equal(elem_1 anyelement, elem_2 anyelement) 
     102    * test.assert_less_than(elem_1 anyelement, elem_2 anyelement) 
     103    * test.assert_less_than_or_equal(elem_1 anyelement, elem_2 anyelement) 
    100104    * test.assert_values(column text, source text, expected anyarray): 
    101105        Raises an exception if SELECT column FROM source != expected. 
    102106     
    103107    * test.assert_raises(call text, errm text, state text): Raises an 
    104       exception if 'SELECT * FROM [call];' does not raise errm. 
     108        exception if 'SELECT * FROM [call];' does not raise errm 
     109        (if provided) or state (if provided). 
    105110 
    106111Some return dynamic SQL: 
     
    290295 
    291296 
     297CREATE OR REPLACE FUNCTION test.assert_void(call text) RETURNS VOID AS $$ 
     298-- Raises an exception if SELECT * FROM call != void. 
     299DECLARE 
     300  retval    text; 
     301BEGIN 
     302  EXECUTE ('SELECT * FROM ' || call || ';') INTO retval; 
     303  IF retval != '' THEN 
     304    RAISE EXCEPTION 'Call: ''%'' did not return void. Got ''%'' instead.', call, retval; 
     305  END IF; 
     306END; 
     307$$ LANGUAGE plpgsql; 
     308 
     309 
    292310CREATE OR REPLACE FUNCTION test.assert(assertion boolean, msg text) RETURNS VOID AS $$ 
    293311-- Raises an exception (msg) if assertion is false. 
     
    334352 
    335353 
     354CREATE OR REPLACE FUNCTION test.assert_less_than(elem_1 anyelement, elem_2 anyelement) RETURNS VOID AS $$ 
     355-- Raises an exception if elem_1 >= elem_2 
     356--  
     357-- The two arguments must be of the same type. If they are not, 
     358-- you will receive "ERROR:  invalid input syntax ..." 
     359BEGIN 
     360  IF (elem_1 IS NULL or elem_2 IS NULL) THEN 
     361    RAISE EXCEPTION 'Assertion arguments may not be NULL.'; 
     362  END IF; 
     363  IF NOT (elem_1 < elem_2) THEN 
     364    RAISE EXCEPTION '% not < %', elem_1, elem_2; 
     365  END IF; 
     366END; 
     367$$ LANGUAGE plpgsql; 
     368 
     369 
    336370CREATE OR REPLACE FUNCTION test.assert_less_than_or_equal(elem_1 anyelement, elem_2 anyelement) RETURNS VOID AS $$ 
    337371-- Raises an exception if elem_1 > elem_2 
     
    345379  IF NOT (elem_1 <= elem_2) THEN 
    346380    RAISE EXCEPTION '% not <= %', elem_1, elem_2; 
     381  END IF; 
     382END; 
     383$$ LANGUAGE plpgsql; 
     384 
     385 
     386CREATE OR REPLACE FUNCTION test.assert_greater_than(elem_1 anyelement, elem_2 anyelement) RETURNS VOID AS $$ 
     387-- Raises an exception if elem_1 <= elem_2 
     388--  
     389-- The two arguments must be of the same type. If they are not, 
     390-- you will receive "ERROR:  invalid input syntax ..." 
     391BEGIN 
     392  IF (elem_1 IS NULL or elem_2 IS NULL) THEN 
     393    RAISE EXCEPTION 'Assertion arguments may not be NULL.'; 
     394  END IF; 
     395  IF NOT (elem_1 > elem_2) THEN 
     396    RAISE EXCEPTION '% not > %', elem_1, elem_2; 
     397  END IF; 
     398END; 
     399$$ LANGUAGE plpgsql; 
     400 
     401 
     402CREATE OR REPLACE FUNCTION test.assert_greater_than_or_equal(elem_1 anyelement, elem_2 anyelement) RETURNS VOID AS $$ 
     403-- Raises an exception if elem_1 < elem_2 
     404--  
     405-- The two arguments must be of the same type. If they are not, 
     406-- you will receive "ERROR:  invalid input syntax ..." 
     407BEGIN 
     408  IF (elem_1 IS NULL or elem_2 IS NULL) THEN 
     409    RAISE EXCEPTION 'Assertion arguments may not be NULL.'; 
     410  END IF; 
     411  IF NOT (elem_1 >= elem_2) THEN 
     412    RAISE EXCEPTION '% not >= %', elem_1, elem_2; 
    347413  END IF; 
    348414END; 
  • trunk/epic/test/test_asserts.sql

    r4 r5  
     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 
    14SET search_path = test, public, pg_catalog; 
    25 
     
    9497 
    9598 
     99CREATE OR REPLACE FUNCTION test.test_assert_void() RETURNS VOID AS $$ 
     100-- Assert the correct operation of test.assert_void 
     101-- module: test_asserts 
     102DECLARE 
     103  retval    text; 
     104BEGIN 
     105  -- assert_void() MUST return VOID if the given call returns VOID. 
     106  SELECT INTO retval * FROM test.assert_void('pg_sleep(0.1)'); 
     107  IF retval != '' THEN 
     108    RAISE EXCEPTION 'assert_void did not itself return void. Got ''%'' instead.', retval; 
     109  END IF; 
     110   
     111  -- assert_void() MUST raise an exception if the given call does not return void. 
     112  PERFORM test.assert_raises('test.assert_void(''pg_namespace'')',  
     113    'Call: ''pg_namespace'' did not return void. Got ''pg_toast'' instead.', 
     114    'P0001'); 
     115   
     116  RAISE EXCEPTION '[OK]'; 
     117END; 
     118$$ LANGUAGE plpgsql; 
     119 
     120 
    96121CREATE OR REPLACE FUNCTION test.test_assert_equal() RETURNS VOID AS $$ 
    97122-- Assert the correct operation of test.assert_equal 
     
    101126BEGIN 
    102127  -- assert_equal() MUST return VOID if the given assertion holds. 
    103   SELECT INTO retval * FROM test.assert_equal(1, 1); 
    104   IF retval != '' THEN 
    105     RAISE EXCEPTION 'test.assert_equal() did not return void for (1, 1).'; 
    106   END IF; 
    107   SELECT INTO retval * FROM test.assert_equal('abc'::text, 'abc'); 
    108   IF retval != '' THEN 
    109     RAISE EXCEPTION 'test.assert_equal() did not return void for (''abc'', ''abc'').'; 
    110   END IF; 
     128  PERFORM test.assert_void('test.assert_equal(1, 1)'); 
     129  PERFORM test.assert_void('test.assert_equal(''abc''::text, ''abc'');'); 
     130   
     131  -- assert_equal() MUST return VOID if both args are null. 
     132  PERFORM test.assert_void('test.assert_equal(NULL::int, NULL);'); 
    111133   
    112134  -- assert_equal() MUST raise an exception if the given assertion does not hold. 
    113135  PERFORM test.assert_raises('test.assert_equal(1, 2)', '1 != 2', 'P0001'); 
    114    
    115136  PERFORM test.assert_raises('test.assert_equal(''abc''::text, ''xyz'')', 'abc != xyz', 'P0001'); 
    116    
    117   -- assert_equal() MUST return VOID if both args are null. 
    118   SELECT INTO retval * FROM test.assert_equal(NULL::int, NULL); 
    119   IF retval != '' THEN 
    120     RAISE EXCEPTION 'test.assert_equal() did not return void for (null, null).'; 
    121   END IF; 
    122137   
    123138  -- assert_equal() MUST raise an exception if only one arg is NULL. 
     
    142157BEGIN 
    143158  -- assert_not_equal() MUST return VOID if the given assertion does not hold. 
    144   SELECT INTO retval * FROM test.assert_not_equal(1, 2); 
    145   IF retval != '' THEN 
    146     RAISE EXCEPTION 'test.assert_not_equal() did not return void for (1, 2).'; 
    147   END IF; 
    148   SELECT INTO retval * FROM test.assert_not_equal('abc'::text, 'xyz'); 
    149   IF retval != '' THEN 
    150     RAISE EXCEPTION 'test.assert_not_equal() did not return void for (''abc'', ''xyz'').'; 
    151   END IF; 
     159  PERFORM test.assert_void('test.assert_not_equal(1, 2);'); 
     160  PERFORM test.assert_void('test.assert_not_equal(''abc''::text, ''xyz'');'); 
     161   
     162  -- assert_not_equal() MUST return VOID if only one arg is NULL. 
     163  PERFORM test.assert_void('test.assert_not_equal(8, NULL);'); 
     164  PERFORM test.assert_void('test.assert_not_equal(NULL, 7);'); 
    152165   
    153166  -- assert_not_equal() MUST raise an exception if the given assertion holds. 
     
    155168  PERFORM test.assert_raises('test.assert_not_equal(''abc''::text, ''abc'')', 'abc = abc', 'P0001'); 
    156169   
    157   -- assert_not_equal() MUST return VOID if only one arg is NULL. 
    158   SELECT INTO retval * FROM test.assert_not_equal(8, NULL); 
    159   IF retval != '' THEN 
    160     RAISE EXCEPTION 'test.assert_not_equal() did not return void for (8, null).'; 
    161   END IF; 
    162   SELECT INTO retval * FROM test.assert_not_equal(NULL, 7); 
    163   IF retval != '' THEN 
    164     RAISE EXCEPTION 'test.assert_not_equal() did not return void for (null, 7).'; 
    165   END IF; 
    166    
    167170  -- assert_not_equal() MUST raise an exception if both args are NULL. 
    168171  PERFORM test.assert_raises('test.assert_not_equal(NULL::int, NULL)', '<NULL> = <NULL>', 'P0001'); 
     
    178181 
    179182 
     183CREATE OR REPLACE FUNCTION test.test_assert_less_than() RETURNS VOID AS $$ 
     184-- Assert the correct operation of test.assert_less_than 
     185-- module: test_asserts 
     186DECLARE 
     187  retval    text; 
     188BEGIN 
     189  -- assert_less_than() MUST return VOID if a < b. 
     190  PERFORM test.assert_void('test.assert_less_than(1, 2);'); 
     191  PERFORM test.assert_void('test.assert_less_than(''abc''::text, ''xyz'');'); 
     192   
     193  -- assert_less_than() MUST raise an exception if a >= b. 
     194  PERFORM test.assert_raises('test.assert_less_than(2, 1)', '2 not < 1', 'P0001'); 
     195  PERFORM test.assert_raises('test.assert_less_than(1, 1)', '1 not < 1', 'P0001'); 
     196  PERFORM test.assert_raises('test.assert_less_than(''abc''::text, ''abc'')',  
     197    'abc not < abc', 'P0001'); 
     198  PERFORM test.assert_raises('test.assert_less_than(''xyz''::text, ''abc'')',  
     199    'xyz not < abc', 'P0001'); 
     200   
     201  -- assert_less_than() MUST raise an exception if either arg is NULL. 
     202  PERFORM test.assert_raises('test.assert_less_than(NULL::int, NULL)',  
     203    'Assertion arguments may not be NULL.', 'P0001'); 
     204   
     205  -- assert_less_than() will raise an undefined_function exception if the args have different types. 
     206  -- It would be nice to find a way around this (without writing M x N overloaded funcs). 
     207  PERFORM test.assert_raises('test.assert_less_than(8, ''abc''::text)',  
     208    'function test.assert_less_than(integer, text) does not exist', '42883'); 
     209   
     210  RAISE EXCEPTION '[OK]'; 
     211END; 
     212$$ LANGUAGE plpgsql; 
     213 
     214 
    180215CREATE OR REPLACE FUNCTION test.test_assert_less_than_or_equal() RETURNS VOID AS $$ 
    181216-- Assert the correct operation of test.assert_less_than_or_equal 
     
    185220BEGIN 
    186221  -- assert_less_than_or_equal() MUST return VOID if a <= b. 
    187   SELECT INTO retval * FROM test.assert_less_than_or_equal(1, 2); 
    188   IF retval != '' THEN 
    189     RAISE EXCEPTION 'test.assert_less_than_or_equal() did not return void for (1, 2).'; 
    190   END IF; 
    191   SELECT INTO retval * FROM test.assert_less_than_or_equal(1, 1); 
    192   IF retval != '' THEN 
    193     RAISE EXCEPTION 'test.assert_less_than_or_equal() did not return void for (1, 1).'; 
    194   END IF; 
    195   SELECT INTO retval * FROM test.assert_less_than_or_equal('abc'::text, 'xyz'); 
    196   IF retval != '' THEN 
    197     RAISE EXCEPTION 'test.assert_less_than_or_equal() did not return void for (''abc'', ''xyz'').'; 
    198   END IF; 
    199   SELECT INTO retval * FROM test.assert_less_than_or_equal('abc'::text, 'abc'); 
    200   IF retval != '' THEN 
    201     RAISE EXCEPTION 'test.assert_less_than_or_equal() did not return void for (''abc'', ''abc'').'; 
    202   END IF; 
     222  PERFORM test.assert_void('test.assert_less_than_or_equal(1, 2);'); 
     223  PERFORM test.assert_void('test.assert_less_than_or_equal(1, 1);'); 
     224  PERFORM test.assert_void('test.assert_less_than_or_equal(''abc''::text, ''xyz'');'); 
     225  PERFORM test.assert_void('test.assert_less_than_or_equal(''abc''::text, ''abc'');'); 
    203226   
    204227  -- assert_less_than_or_equal() MUST raise an exception if a > b. 
     
    215238  PERFORM test.assert_raises('test.assert_less_than_or_equal(8, ''abc''::text)',  
    216239    'function test.assert_less_than_or_equal(integer, text) does not exist', '42883'); 
     240   
     241  RAISE EXCEPTION '[OK]'; 
     242END; 
     243$$ LANGUAGE plpgsql; 
     244 
     245 
     246CREATE OR REPLACE FUNCTION test.test_assert_greater_than() RETURNS VOID AS $$ 
     247-- Assert the correct operation of test.assert_greater_than 
     248-- module: test_asserts 
     249DECLARE 
     250  retval    text; 
     251BEGIN 
     252  -- assert_greater_than() MUST return VOID if a > b. 
     253  PERFORM test.assert_void('test.assert_greater_than(2, 1);'); 
     254  PERFORM test.assert_void('test.assert_greater_than(''xyz''::text, ''abc'');'); 
     255   
     256  -- assert_greater_than() MUST raise an exception if a <= b. 
     257  PERFORM test.assert_raises('test.assert_greater_than(1, 2)', '1 not > 2', 'P0001'); 
     258  PERFORM test.assert_raises('test.assert_greater_than(1, 1)', '1 not > 1', 'P0001'); 
     259  PERFORM test.assert_raises('test.assert_greater_than(''abc''::text, ''abc'')',  
     260    'abc not > abc', 'P0001'); 
     261  PERFORM test.assert_raises('test.assert_greater_than(''abc''::text, ''xyz'')',  
     262    'abc not > xyz', 'P0001'); 
     263   
     264  -- assert_greater_than() MUST raise an exception if either arg is NULL. 
     265  PERFORM test.assert_raises('test.assert_greater_than(NULL::int, NULL)',  
     266    'Assertion arguments may not be NULL.', 'P0001'); 
     267   
     268  -- assert_greater_than() will raise an undefined_function exception if the args have different types. 
     269  -- It would be nice to find a way around this (without writing M x N overloaded funcs). 
     270  PERFORM test.assert_raises('test.assert_greater_than(8, ''abc''::text)',  
     271    'function test.assert_greater_than(integer, text) does not exist', '42883'); 
     272   
     273  RAISE EXCEPTION '[OK]'; 
     274END; 
     275$$ LANGUAGE plpgsql; 
     276 
     277 
     278CREATE OR REPLACE FUNCTION test.test_assert_greater_than_or_equal() RETURNS VOID AS $$ 
     279-- Assert the correct operation of test.assert_greater_than_or_equal 
     280-- module: test_asserts 
     281DECLARE 
     282  retval    text; 
     283BEGIN 
     284  -- assert_greater_than_or_equal() MUST return VOID if a >= b. 
     285  PERFORM test.assert_void('test.assert_greater_than_or_equal(2, 1);'); 
     286  PERFORM test.assert_void('test.assert_greater_than_or_equal(1, 1);'); 
     287  PERFORM test.assert_void('test.assert_greater_than_or_equal(''xyz''::text, ''abc'');'); 
     288  PERFORM test.assert_void('test.assert_greater_than_or_equal(''abc''::text, ''abc'');'); 
     289   
     290  -- assert_greater_than_or_equal() MUST raise an exception if a < b. 
     291  PERFORM test.assert_raises('test.assert_greater_than_or_equal(1, 2)', '1 not >= 2', 'P0001'); 
     292  PERFORM test.assert_raises('test.assert_greater_than_or_equal(''abc''::text, ''xyz'')',  
     293    'abc not >= xyz', 'P0001'); 
     294   
     295  -- assert_greater_than_or_equal() MUST raise an exception if either arg is NULL. 
     296  PERFORM test.assert_raises('test.assert_greater_than_or_equal(NULL::int, NULL)',  
     297    'Assertion arguments may not be NULL.', 'P0001'); 
     298   
     299  -- assert_greater_than_or_equal() will raise an undefined_function exception if the args have different types. 
     300  -- It would be nice to find a way around this (without writing M x N overloaded funcs). 
     301  PERFORM test.assert_raises('test.assert_greater_than_or_equal(8, ''abc''::text)',  
     302    'function test.assert_greater_than_or_equal(integer, text) does not exist', '42883'); 
    217303   
    218304  RAISE EXCEPTION '[OK]';