Changeset 19 for trunk

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

New test.assert_not_empty functions, plus fixed some array bounds.

Location:
trunk/epic
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/epic/epic.sql

    r18 r19  
    662662--  
    663663DECLARE 
    664   i             integer; 
    665664  record1       record; 
    666665  record2       record; 
     
    690689  -- doesn't get cached and re-used (or subsequent calls will fail). 
    691690  EXECUTE 'CREATE TEMPORARY TABLE _test_assert_column_expected (LIKE _test_assert_column_base);'; 
    692   FOR i IN array_lower(expected, 1)..array_upper(expected, 1) 
    693   LOOP 
    694     IF expected[i] IS NULL THEN 
    695       EXECUTE 'INSERT INTO _test_assert_column_expected (_assert_column_result) VALUES (NULL);'; 
    696     ELSEIF typename(expected[i]) IN ('text', 'varchar', 'char', 'bytea', 'date', 'timestamp', 'timestamptz', 'time', 'timetz') THEN 
    697       EXECUTE 'INSERT INTO _test_assert_column_expected (_assert_column_result) VALUES (' 
    698               || quote_literal(expected[i]) || ');'; 
    699     ELSE 
    700       EXECUTE 'INSERT INTO _test_assert_column_expected (_assert_column_result) VALUES (' 
    701               || expected[i] || ');'; 
    702     END IF; 
    703   END LOOP; 
     691  IF array_lower(expected, 1) iS NOT NULL THEN 
     692    FOR i IN array_lower(expected, 1)..array_upper(expected, 1) 
     693    LOOP 
     694      IF expected[i] IS NULL THEN 
     695        EXECUTE 'INSERT INTO _test_assert_column_expected (_assert_column_result) VALUES (NULL);'; 
     696      ELSEIF typename(expected[i]) IN ('text', 'varchar', 'char', 'bytea', 'date', 'timestamp', 'timestamptz', 'time', 'timetz') THEN 
     697        EXECUTE 'INSERT INTO _test_assert_column_expected (_assert_column_result) VALUES (' 
     698                || quote_literal(expected[i]) || ');'; 
     699      ELSE 
     700        EXECUTE 'INSERT INTO _test_assert_column_expected (_assert_column_result) VALUES (' 
     701                || expected[i] || ');'; 
     702      END IF; 
     703    END LOOP; 
     704  END IF; 
    704705   
    705706  -- Compare the two tables in order. 
     
    720721          EXIT; 
    721722        ELSE 
    722           PERFORM test.fail('record: ' || record1._assert_column_result || ' not found in array: ' || array_to_string(expected)); 
     723          PERFORM test.fail('record: ' || record1._assert_column_result || ' not found in array: ' || array_to_string(expected, ', ')); 
    723724        END IF; 
    724725      END IF; 
     
    768769  failed      text[]; 
    769770  failed_len  int; 
    770   i           int; 
    771 BEGIN 
    772   FOR i in array_lower(tablenames, 1)..array_upper(tablenames, 1) 
    773   LOOP 
    774     EXECUTE 'SELECT (EXISTS (SELECT 1 FROM ' || tablenames[i] || '));' INTO result; 
    775     IF result THEN 
    776       failed := failed || ('"' || btrim(tablenames[i]) || '"'); 
     771BEGIN 
     772  IF array_lower(tablenames, 1) IS NOT NULL THEN 
     773    FOR i in array_lower(tablenames, 1)..array_upper(tablenames, 1) 
     774    LOOP 
     775      EXECUTE 'SELECT (EXISTS (SELECT 1 FROM ' || tablenames[i] || '));' INTO result; 
     776      IF result THEN 
     777        failed := failed || ('"' || btrim(tablenames[i]) || '"'); 
     778      END IF; 
     779    END LOOP; 
     780  END IF; 
     781   
     782  IF array_lower(failed, 1) IS NOT NULL THEN 
     783    failed_len := (array_upper(failed, 1) - array_lower(failed, 1)) + 1; 
     784    IF failed_len = 1 THEN 
     785      PERFORM test.fail('The table ' || array_to_string(failed, ', ') || ' is not empty.'); 
     786    ELSEIF failed_len > 1 THEN 
     787      PERFORM test.fail('The tables ' || array_to_string(failed, ', ') || ' are not empty.'); 
    777788    END IF; 
    778   END LOOP; 
    779    
    780   failed_len := (array_upper(failed, 1) - array_lower(failed, 1)) + 1; 
    781   IF failed_len = 1 THEN 
    782     PERFORM test.fail('The table ' || array_to_string(failed, ', ') || ' is not empty.'); 
    783   ELSEIF failed_len > 1 THEN 
    784     PERFORM test.fail('The tables ' || array_to_string(failed, ', ') || ' are not empty.'); 
    785789  END IF; 
    786790END; 
     
    804808$$ LANGUAGE plpgsql; 
    805809 
     810 
     811CREATE OR REPLACE FUNCTION test.assert_not_empty(tablenames text[]) RETURNS VOID AS $$ 
     812-- Raises an exception if the given tables have no rows. 
     813DECLARE 
     814  result      bool; 
     815  failed      text[]; 
     816  failed_len  int; 
     817BEGIN 
     818  IF array_lower(tablenames, 1) IS NOT NULL THEN 
     819    FOR i in array_lower(tablenames, 1)..array_upper(tablenames, 1) 
     820    LOOP 
     821      EXECUTE 'SELECT (EXISTS (SELECT 1 FROM ' || tablenames[i] || '));' INTO result; 
     822      IF NOT result THEN 
     823        failed := failed || ('"' || btrim(tablenames[i]) || '"'); 
     824      END IF; 
     825    END LOOP; 
     826  END IF; 
     827   
     828  IF array_lower(failed, 1) IS NULL THEN 
     829    -- failed is an empty array (no failures). 
     830    NULL; 
     831  ELSE 
     832    failed_len := (array_upper(failed, 1) - array_lower(failed, 1)) + 1; 
     833    IF failed_len = 1 THEN 
     834      PERFORM test.fail('The table ' || array_to_string(failed, ', ') || ' is empty.'); 
     835    ELSEIF failed_len > 1 THEN 
     836      PERFORM test.fail('The tables ' || array_to_string(failed, ', ') || ' are empty.'); 
     837    END IF; 
     838  END IF; 
     839END; 
     840$$ LANGUAGE plpgsql; 
     841 
     842CREATE OR REPLACE FUNCTION test.assert_not_empty(tablename text) RETURNS VOID AS $$ 
     843-- Raises an exception if the given table has no rows. 
     844DECLARE 
     845  result    bool; 
     846BEGIN 
     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; 
     853  IF NOT result THEN 
     854    PERFORM test.fail('The table "' || tablename || '" is empty.'); 
     855  END IF; 
     856END; 
     857$$ LANGUAGE plpgsql; 
     858 
  • trunk/epic/test/test_asserts.sql

    r18 r19  
    374374  EXCEPTION WHEN OTHERS THEN 
    375375    failed := true; 
    376     IF SQLERRM = '[FAIL] record: 3 not found in array: 1,2' THEN 
     376    IF SQLERRM = '[FAIL] record: 3 not found in array: 1, 2' THEN 
    377377      NULL; 
    378378    ELSE