Changeset 20 for trunk/epic

Show
Ignore:
Timestamp:
08/28/08 16:50:51 (3 years ago)
Author:
fumanchu
Message:

Made assert_empty more generic; now takes call(s) not just tablename(s). Also ordered the output of run_module and run_all, and added a test for assert_not_empty.

Location:
trunk/epic
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/epic/epic.sql

    r19 r20  
    346346  output_record   test.results%ROWTYPE; 
    347347BEGIN 
    348   FOR testname IN SELECT name FROM test.testnames WHERE module = modulename 
     348  FOR testname IN SELECT name FROM test.testnames WHERE module = modulename ORDER BY name ASC 
    349349  LOOP 
    350350    SELECT INTO output_record * FROM test.run_test(testname); 
     
    364364  FOR modulename in SELECT DISTINCT module FROM test.testnames ORDER BY module ASC 
    365365  LOOP 
    366     FOR testname IN SELECT name FROM test.testnames WHERE module = modulename 
     366    FOR testname IN SELECT name FROM test.testnames WHERE module = modulename ORDER BY name ASC 
    367367    LOOP 
    368368      SELECT INTO output_record * FROM test.run_test(testname); 
     
    763763 
    764764 
    765 CREATE OR REPLACE FUNCTION test.assert_empty(tablenames text[]) RETURNS VOID AS $$ 
    766 -- Raises an exception if the given tables have any rows. 
     765CREATE OR REPLACE FUNCTION test.assert_empty(calls text[]) RETURNS VOID AS $$ 
     766-- Raises an exception if the given calls have any rows. 
    767767DECLARE 
    768768  result      bool; 
     
    770770  failed_len  int; 
    771771BEGIN 
    772   IF array_lower(tablenames, 1) IS NOT NULL THEN 
    773     FOR i in array_lower(tablenames, 1)..array_upper(tablenames, 1) 
     772  IF array_lower(calls, 1) IS NOT NULL THEN 
     773    FOR i in array_lower(calls, 1)..array_upper(calls, 1) 
    774774    LOOP 
    775       EXECUTE 'SELECT (EXISTS (SELECT 1 FROM ' || tablenames[i] || '));' INTO result; 
     775      EXECUTE 'SELECT EXISTS (' || test.statement(calls[i]) || ');' INTO result; 
    776776      IF result THEN 
    777         failed := failed || ('"' || btrim(tablenames[i]) || '"'); 
     777        failed := failed || ('"' || btrim(calls[i]) || '"'); 
    778778      END IF; 
    779779    END LOOP; 
     
    783783    failed_len := (array_upper(failed, 1) - array_lower(failed, 1)) + 1; 
    784784    IF failed_len = 1 THEN 
    785       PERFORM test.fail('The table ' || array_to_string(failed, ', ') || ' is not empty.'); 
     785      PERFORM test.fail('The call ' || array_to_string(failed, ', ') || ' is not empty.'); 
    786786    ELSEIF failed_len > 1 THEN 
    787       PERFORM test.fail('The tables ' || array_to_string(failed, ', ') || ' are not empty.'); 
     787      PERFORM test.fail('The calls ' || array_to_string(failed, ', ') || ' are not empty.'); 
    788788    END IF; 
    789789  END IF; 
     
    791791$$ LANGUAGE plpgsql; 
    792792 
    793 CREATE OR REPLACE FUNCTION test.assert_empty(tablename text) RETURNS VOID AS $$ 
    794 -- Raises an exception if the given table has any rows. 
     793CREATE OR REPLACE FUNCTION test.assert_empty(call text) RETURNS VOID AS $$ 
     794-- Raises an exception if the given call returns any rows. 
    795795DECLARE 
    796796  result    bool; 
    797797BEGIN 
    798   IF tablename LIKE '%,%' THEN 
    799     PERFORM test.assert_empty(string_to_array(tablename, ',')); 
    800     RETURN; 
    801   END IF; 
    802    
    803   EXECUTE 'SELECT (EXISTS (SELECT 1 FROM ' || tablename || '));' INTO result; 
     798  EXECUTE 'SELECT EXISTS (' || test.statement(call) || ');' INTO result; 
    804799  IF result THEN 
    805     PERFORM test.fail('The table "' || tablename || '" is not empty.'); 
    806   END IF; 
    807 END; 
    808 $$ LANGUAGE plpgsql; 
    809  
    810  
    811 CREATE OR REPLACE FUNCTION test.assert_not_empty(tablenames text[]) RETURNS VOID AS $$ 
    812 -- Raises an exception if the given tables have no rows. 
     800    PERFORM test.fail('The call "' || call || '" is not empty.'); 
     801  END IF; 
     802END; 
     803$$ LANGUAGE plpgsql; 
     804 
     805 
     806CREATE OR REPLACE FUNCTION test.assert_not_empty(calls text[]) RETURNS VOID AS $$ 
     807-- Raises an exception if the given calls have no rows. 
    813808DECLARE 
    814809  result      bool; 
     
    816811  failed_len  int; 
    817812BEGIN 
    818   IF array_lower(tablenames, 1) IS NOT NULL THEN 
    819     FOR i in array_lower(tablenames, 1)..array_upper(tablenames, 1) 
     813  IF array_lower(calls, 1) IS NOT NULL THEN 
     814    FOR i in array_lower(calls, 1)..array_upper(calls, 1) 
    820815    LOOP 
    821       EXECUTE 'SELECT (EXISTS (SELECT 1 FROM ' || tablenames[i] || '));' INTO result; 
     816      EXECUTE 'SELECT EXISTS (' || test.statement(calls[i]) || ');' INTO result; 
    822817      IF NOT result THEN 
    823         failed := failed || ('"' || btrim(tablenames[i]) || '"'); 
     818        failed := failed || ('"' || btrim(calls[i]) || '"'); 
    824819      END IF; 
    825820    END LOOP; 
     
    832827    failed_len := (array_upper(failed, 1) - array_lower(failed, 1)) + 1; 
    833828    IF failed_len = 1 THEN 
    834       PERFORM test.fail('The table ' || array_to_string(failed, ', ') || ' is empty.'); 
     829      PERFORM test.fail('The call ' || array_to_string(failed, ', ') || ' is empty.'); 
    835830    ELSEIF failed_len > 1 THEN 
    836       PERFORM test.fail('The tables ' || array_to_string(failed, ', ') || ' are empty.'); 
     831      PERFORM test.fail('The calls ' || array_to_string(failed, ', ') || ' are empty.'); 
    837832    END IF; 
    838833  END IF; 
     
    840835$$ LANGUAGE plpgsql; 
    841836 
    842 CREATE OR REPLACE FUNCTION test.assert_not_empty(tablename text) RETURNS VOID AS $$ 
     837CREATE OR REPLACE FUNCTION test.assert_not_empty(call text) RETURNS VOID AS $$ 
    843838-- Raises an exception if the given table has no rows. 
    844839DECLARE 
    845840  result    bool; 
    846841BEGIN 
    847   IF tablename LIKE '%,%' THEN 
    848     PERFORM test.assert_not_empty(string_to_array(tablename, ',')); 
    849     RETURN; 
    850   END IF; 
    851    
    852   EXECUTE 'SELECT (EXISTS (SELECT 1 FROM ' || tablename || '));' INTO result; 
     842  EXECUTE 'SELECT EXISTS (' || test.statement(call) || ');' INTO result; 
    853843  IF NOT result THEN 
    854     PERFORM test.fail('The table "' || tablename || '" is empty.'); 
    855   END IF; 
    856 END; 
    857 $$ LANGUAGE plpgsql; 
    858  
     844    PERFORM test.fail('The call "' || call || '" is empty.'); 
     845  END IF; 
     846END; 
     847$$ LANGUAGE plpgsql; 
     848 
  • trunk/epic/test/test_asserts.sql

    r19 r20  
    400400  -- array version 
    401401  CREATE TEMP TABLE testtemp2 (a int); 
    402   PERFORM test.assert_empty('testtemp, testtemp2'); 
     402  PERFORM test.assert_empty('{testtemp, testtemp2}'::text[]); 
    403403  PERFORM test.assert_empty(ARRAY['testtemp', 'testtemp2']); 
    404404   
     
    406406  failed := false; 
    407407  BEGIN 
    408     PERFORM test.assert_empty('pg_type,pg_proc'); 
     408    PERFORM test.assert_empty('{pg_type,pg_proc}'::text[]); 
    409409  EXCEPTION WHEN OTHERS THEN 
    410410    failed := true; 
    411     IF SQLERRM = '[FAIL] The tables "pg_type", "pg_proc" are not empty.' THEN 
     411    IF SQLERRM = '[FAIL] The calls "pg_type", "pg_proc" are not empty.' THEN 
    412412      NULL; 
    413413    ELSE 
     
    422422END; 
    423423$$ LANGUAGE plpgsql; 
     424 
     425 
     426CREATE OR REPLACE FUNCTION test.test_assert_not_empty() RETURNS VOID AS $$ 
     427-- Assert the correct operation of test.assert_not_empty 
     428-- module: test_asserts 
     429DECLARE 
     430  failed     bool; 
     431BEGIN 
     432  -- Test an assertion that should pass 
     433  CREATE TEMP TABLE testtemp AS SELECT * FROM generate_series(1, 10); 
     434  PERFORM test.assert_not_empty('testtemp'); 
     435  -- array version 
     436  CREATE TEMP TABLE testtemp2 AS SELECT * FROM generate_series(1, 5); 
     437  PERFORM test.assert_not_empty('{testtemp, testtemp2}'::text[]); 
     438  PERFORM test.assert_not_empty(ARRAY['testtemp', 'testtemp2']); 
     439   
     440  -- ...and an assertion that should fail 
     441  CREATE TEMP TABLE testtemp3 (a int); 
     442  failed := false; 
     443  BEGIN 
     444    PERFORM test.assert_not_empty('testtemp3'); 
     445  EXCEPTION WHEN OTHERS THEN 
     446    failed := true; 
     447    IF SQLERRM = '[FAIL] The call "testtemp3" is empty.' THEN 
     448      NULL; 
     449    ELSE 
     450      RAISE EXCEPTION 'test.assert_not_empty() did not raise the correct error. Raised: %', SQLERRM; 
     451    END IF; 
     452  END; 
     453  IF NOT failed THEN 
     454    PERFORM test.fail('test.assert_not_empty() did not fail.'); 
     455  END IF; 
     456   
     457  RAISE EXCEPTION '[OK]'; 
     458END; 
     459$$ LANGUAGE plpgsql;