Changeset 13 for trunk/epic
- Timestamp:
- 08/20/08 14:05:58 (4 years ago)
- Location:
- trunk/epic
- Files:
-
- 2 modified
-
epic.sql (modified) (1 diff)
-
test/test_asserts.sql (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/epic/epic.sql
r12 r13 632 632 END; 633 633 $$ LANGUAGE plpgsql; 634 635 636 CREATE OR REPLACE FUNCTION test.assert_empty(tablenames text[]) RETURNS VOID AS $$ 637 -- Raises an exception if the given tables have any rows. 638 DECLARE 639 result bool; 640 failed text[]; 641 failed_len int; 642 i int; 643 BEGIN 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; 658 END; 659 $$ LANGUAGE plpgsql; 660 661 CREATE OR REPLACE FUNCTION test.assert_empty(tablename text) RETURNS VOID AS $$ 662 -- Raises an exception if the given table has any rows. 663 DECLARE 664 result bool; 665 BEGIN 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; 675 END; 676 $$ LANGUAGE plpgsql; 677 -
trunk/epic/test/test_asserts.sql
r10 r13 103 103 104 104 -- 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.', 107 107 'P0001'); 108 108 … … 374 374 END; 375 375 $$ LANGUAGE plpgsql; 376 377 378 CREATE OR REPLACE FUNCTION test.test_assert_empty() RETURNS VOID AS $$ 379 -- Assert the correct operation of test.assert_empty 380 -- module: test_asserts 381 DECLARE 382 failed bool; 383 BEGIN 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]'; 409 END; 410 $$ LANGUAGE plpgsql;
