Changeset 13 for trunk/epic

Show
Ignore:
Timestamp:
08/20/08 14:05:58 (4 years ago)
Author:
fumanchu
Message:

New test.assert_empty functions.

Location:
trunk/epic
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/epic/epic.sql

    r12 r13  
    632632END; 
    633633$$ LANGUAGE plpgsql; 
     634 
     635 
     636CREATE OR REPLACE FUNCTION test.assert_empty(tablenames text[]) RETURNS VOID AS $$ 
     637-- Raises an exception if the given tables have any rows. 
     638DECLARE 
     639  result      bool; 
     640  failed      text[]; 
     641  failed_len  int; 
     642  i           int; 
     643BEGIN 
     644  FOR i in array_lower(tablenames, 1)..array_upper(tablenames, 1) 
     645  LOOP 
     646    EXECUTE 'SELECT (EXISTS (SELECT 1 FROM ' || tablenames[i] || '));' INTO result; 
     647    IF result THEN 
     648      failed := failed || ('"' || btrim(tablenames[i]) || '"'); 
     649    END IF; 
     650  END LOOP; 
     651   
     652  failed_len := (array_upper(failed, 1) - array_lower(failed, 1)) + 1; 
     653  IF failed_len = 1 THEN 
     654    PERFORM test.fail('The table ' || array_to_string(failed, ', ') || ' is not empty.'); 
     655  ELSEIF failed_len > 1 THEN 
     656    PERFORM test.fail('The tables ' || array_to_string(failed, ', ') || ' are not empty.'); 
     657  END IF; 
     658END; 
     659$$ LANGUAGE plpgsql; 
     660 
     661CREATE OR REPLACE FUNCTION test.assert_empty(tablename text) RETURNS VOID AS $$ 
     662-- Raises an exception if the given table has any rows. 
     663DECLARE 
     664  result    bool; 
     665BEGIN 
     666  IF tablename LIKE '%,%' THEN 
     667    PERFORM test.assert_empty(string_to_array(tablename, ',')); 
     668    RETURN; 
     669  END IF; 
     670   
     671  EXECUTE 'SELECT (EXISTS (SELECT 1 FROM ' || tablename || '));' INTO result; 
     672  IF result THEN 
     673    PERFORM test.fail('The table "' || tablename || '" is not empty.'); 
     674  END IF; 
     675END; 
     676$$ LANGUAGE plpgsql; 
     677 
  • trunk/epic/test/test_asserts.sql

    r10 r13  
    103103   
    104104  -- assert_void() MUST raise an exception if the given call does not return void. 
    105   PERFORM test.assert_raises('test.assert_void(''pg_namespace'')',  
    106     'Call: ''pg_namespace'' did not return void. Got ''pg_toast'' instead.', 
     105  PERFORM test.assert_raises('test.assert_void(''pg_namespace WHERE nspname = ''''pg_catalog'''''')',  
     106    'Call: ''pg_namespace WHERE nspname = ''pg_catalog'''' did not return void. Got ''pg_catalog'' instead.', 
    107107    'P0001'); 
    108108   
     
    374374END; 
    375375$$ LANGUAGE plpgsql; 
     376 
     377 
     378CREATE OR REPLACE FUNCTION test.test_assert_empty() RETURNS VOID AS $$ 
     379-- Assert the correct operation of test.assert_empty 
     380-- module: test_asserts 
     381DECLARE 
     382  failed     bool; 
     383BEGIN 
     384  -- Test an assertion that should pass 
     385  CREATE TEMP TABLE testtemp (a int); 
     386  PERFORM test.assert_empty('testtemp'); 
     387  -- array version 
     388  CREATE TEMP TABLE testtemp2 (a int); 
     389  PERFORM test.assert_empty('testtemp, testtemp2'); 
     390  PERFORM test.assert_empty(ARRAY['testtemp', 'testtemp2']); 
     391   
     392  -- ...and an assertion that should fail 
     393  failed := false; 
     394  BEGIN 
     395    PERFORM test.assert_empty('pg_type,pg_proc'); 
     396  EXCEPTION WHEN OTHERS THEN 
     397    failed := true; 
     398    IF SQLERRM = '[FAIL] The tables "pg_type", "pg_proc" are not empty.' THEN 
     399      NULL; 
     400    ELSE 
     401      RAISE EXCEPTION 'test.assert_empty() did not raise the correct error. Raised: %', SQLERRM; 
     402    END IF; 
     403  END; 
     404  IF NOT failed THEN 
     405    PERFORM test.fail('test.assert_empty() did not fail.'); 
     406  END IF; 
     407   
     408  RAISE EXCEPTION '[OK]'; 
     409END; 
     410$$ LANGUAGE plpgsql;