Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Started making changes to supports PostgreSQL 8.0:
 * Older versions of PostgreSQL cannot cast `name[]` to text. So we pass such variables through `array_to_string()`, instead. I think that the output is probably more appropriate, anyway.
 * Replaced all but one usage of `regexp_replace()`, since it's not available in older versions of PostgreSQL. That last one will be replaced before long, too.
 * Switched to using `FOR record IN EXECUTE` to fetch a value. It's an ugly hack, but older versions of PostgreSQL require it. I'll probably have to do the same thing in a few other places.
 * Booleans don't case to text very well in 8.0. So use a CASE statement, instead. Thanks to David Westerbrook for the patch.
 * Fixed the raising of an exception so that its variable argument is not dynamically generated, which 8.0 doesn't seem to like.
  • Loading branch information
theory committed Sep 16, 2008
1 parent 1afcf1a commit 4eab0fc
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 42 deletions.
50 changes: 28 additions & 22 deletions pgtap.sql.in
Expand Up @@ -69,10 +69,12 @@ $$ LANGUAGE plpgsql strict;
CREATE OR REPLACE FUNCTION _get ( text )
RETURNS integer AS $$
DECLARE
ret integer;
rec RECORD;
BEGIN
EXECUTE 'SELECT value FROM __tcache__ WHERE label = ' || quote_literal($1) || ' LIMIT 1' INTO ret;
RETURN ret;
FOR rec IN EXECUTE 'SELECT value FROM __tcache__ WHERE label = ' || quote_literal($1) || ' LIMIT 1' LOOP
RETURN rec.value;
END LOOP;
RETURN NULL;
END;
$$ LANGUAGE plpgsql strict;

Expand Down Expand Up @@ -111,8 +113,11 @@ CREATE OR REPLACE FUNCTION add_result ( bool, bool, text, text, text )
RETURNS integer AS $$
BEGIN
EXECUTE 'INSERT INTO __tresults__ ( ok, aok, descr, type, reason )
VALUES( ' || $1 || ', ' || $2 || ', ' || quote_literal(COALESCE($3, '')) || ', '
|| quote_literal($4) || ', ' || quote_literal($5) || ' )';
VALUES( ' || quote_literal(CASE WHEN $1 THEN 't' ELSE 'f' END) || ', '
|| quote_literal(CASE WHEN $2 THEN 't' ELSE 'f' END) || ', '
|| quote_literal(COALESCE($3, '')) || ', '
|| quote_literal($4) || ', '
|| quote_literal($5) || ' )';
RETURN currval('__tresults___numb_seq');
END;
$$ LANGUAGE plpgsql;
Expand Down Expand Up @@ -141,7 +146,7 @@ BEGIN
plural := CASE exp_tests WHEN 1 THEN '' ELSE 's' END;

IF curr_test IS NULL THEN
RAISE EXCEPTION '%', '# No tests run!';
RAISE EXCEPTION '# No tests run!';
END IF;

IF exp_tests = 0 OR exp_tests IS NULL THEN
Expand Down Expand Up @@ -171,7 +176,8 @@ $$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION diag ( msg text )
RETURNS TEXT AS $$
BEGIN
RETURN regexp_replace( msg, '^', '# ', 'gn' );
-- RETURN regexp_replace( msg, '^', '# ', 'gn' );
RETURN '# ' || replace( replace( replace( msg, E'\r\n', E'\n# ' ), E'\n', E'\n# ' ), E'\r', E'\n# ');
END;
$$ LANGUAGE plpgsql strict;

Expand Down Expand Up @@ -200,8 +206,8 @@ BEGIN

RETURN (CASE aok WHEN TRUE THEN '' ELSE 'not ' END)
|| 'ok ' || _set( 'curr_test', test_num )
|| CASE descr WHEN '' THEN '' ELSE COALESCE( ' - ' || substr(regexp_replace( descr, '^', '# ', 'gn' ), 3), '' ) END
|| COALESCE( ' ' || regexp_replace( 'TODO ' || todo_why, '^', '# ', 'gn' ), '')
|| CASE descr WHEN '' THEN '' ELSE COALESCE( ' - ' || substr(diag( descr ), 3), '' ) END
|| COALESCE( ' ' || diag( 'TODO ' || todo_why ), '')
|| CASE aok WHEN TRUE THEN '' ELSE E'\n' ||
diag('Failed ' ||
CASE WHEN todo_why IS NULL THEN '' ELSE '(TODO) ' END ||
Expand Down Expand Up @@ -625,7 +631,7 @@ $$ LANGUAGE SQL;
-- has_column( table, column )
CREATE OR REPLACE FUNCTION has_column ( NAME, NAME )
RETURNS TEXT AS $$
SELECT has_column( $1, $2, 'Column ' || $1 || '.' || $2 || ' should exist' );
SELECT has_column( $1, $2, 'Column ' || $1 || '(' || $2 || ') should exist' );
$$ LANGUAGE SQL;

-- _col_is_null( schema, table, column, desc, null )
Expand Down Expand Up @@ -680,7 +686,7 @@ $$ LANGUAGE SQL;
-- col_not_null( table, column )
CREATE OR REPLACE FUNCTION col_not_null ( NAME, NAME )
RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, 'Column ' || $1 || '.' || $2 || ' should be NOT NULL', true );
SELECT _col_is_null( $1, $2, 'Column ' || $1 || '(' || $2 || ') should be NOT NULL', true );
$$ LANGUAGE SQL;

-- col_is_null( schema, table, column, description )
Expand All @@ -698,7 +704,7 @@ $$ LANGUAGE SQL;
-- col_is_null( table, column )
CREATE OR REPLACE FUNCTION col_is_null ( NAME, NAME )
RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, 'Column ' || $1 || '.' || $2 || ' should allow NULL', false );
SELECT _col_is_null( $1, $2, 'Column ' || $1 || '(' || $2 || ') should allow NULL', false );
$$ LANGUAGE SQL;

-- col_type_is( schema, table, column, type, description )
Expand Down Expand Up @@ -751,7 +757,7 @@ $$ LANGUAGE SQL;
-- col_type_is( table, column, type )
CREATE OR REPLACE FUNCTION col_type_is ( NAME, NAME, TEXT )
RETURNS TEXT AS $$
SELECT col_type_is( $1, $2, $3, 'Column ' || $1 || '.' || $2 || ' should be type ' || $3 );
SELECT col_type_is( $1, $2, $3, 'Column ' || $1 || '(' || $2 || ') should be type ' || $3 );
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION _def_is( TEXT, anyelement, TEXT )
Expand Down Expand Up @@ -812,7 +818,7 @@ CREATE OR REPLACE FUNCTION col_default_is ( NAME, NAME, TEXT )
RETURNS TEXT AS $$
SELECT col_default_is(
$1, $2, $3,
'Column ' || $1 || '.' || $2 || ' should default to ' || quote_literal($3)
'Column ' || $1 || '(' || $2 || ') should default to ' || quote_literal($3)
);
$$ LANGUAGE sql;

Expand Down Expand Up @@ -1000,7 +1006,7 @@ $$ LANGUAGE sql;
-- col_is_pk( table, column[] )
CREATE OR REPLACE FUNCTION col_is_pk ( NAME, NAME[] )
RETURNS TEXT AS $$
SELECT col_is_pk( $1, $2, 'Columns ' || $1 || '.' || $2::text || ' should be a primary key' );
SELECT col_is_pk( $1, $2, 'Columns ' || $1 || '(' || array_to_string($2, ', ') || ') should be a primary key' );
$$ LANGUAGE sql;

-- col_is_pk( schema, table, column, description )
Expand All @@ -1018,7 +1024,7 @@ $$ LANGUAGE sql;
-- col_is_pk( table, column )
CREATE OR REPLACE FUNCTION col_is_pk ( NAME, NAME )
RETURNS TEXT AS $$
SELECT col_is_pk( $1, $2, 'Column ' || $1 || '.' || $2 || ' should be a primary key' );
SELECT col_is_pk( $1, $2, 'Column ' || $1 || '(' || $2 || ') should be a primary key' );
$$ LANGUAGE sql;

-- has_fk( schema, table, description )
Expand Down Expand Up @@ -1114,7 +1120,7 @@ $$ LANGUAGE plpgsql;
-- col_is_fk( table, column[] )
CREATE OR REPLACE FUNCTION col_is_fk ( NAME, NAME[] )
RETURNS TEXT AS $$
SELECT col_is_fk( $1, $2, 'Columns ' || $1 || '.' || $2::text || ' should be a foreign key' );
SELECT col_is_fk( $1, $2, 'Columns ' || $1 || '(' || array_to_string($2, ', ') || ') should be a foreign key' );
$$ LANGUAGE sql;

-- col_is_fk( schema, table, column, description )
Expand All @@ -1132,7 +1138,7 @@ $$ LANGUAGE sql;
-- col_is_fk( table, column )
CREATE OR REPLACE FUNCTION col_is_fk ( NAME, NAME )
RETURNS TEXT AS $$
SELECT col_is_fk( $1, $2, 'Column ' || $1 || '.' || $2 || ' should be a foreign key' );
SELECT col_is_fk( $1, $2, 'Column ' || $1 || '(' || $2 || ') should be a foreign key' );
$$ LANGUAGE sql;

-- has_unique( schema, table, description )
Expand Down Expand Up @@ -1168,7 +1174,7 @@ $$ LANGUAGE sql;
-- col_is_unique( table, column[] )
CREATE OR REPLACE FUNCTION col_is_unique ( NAME, NAME[] )
RETURNS TEXT AS $$
SELECT col_is_unique( $1, $2, 'Columns ' || $1 || '.' || $2::text || ' should have a unique constraint' );
SELECT col_is_unique( $1, $2, 'Columns ' || $1 || '(' || array_to_string($2, ', ') || ') should have a unique constraint' );
$$ LANGUAGE sql;

-- col_is_unique( schema, table, column, description )
Expand All @@ -1186,7 +1192,7 @@ $$ LANGUAGE sql;
-- col_is_unique( table, column )
CREATE OR REPLACE FUNCTION col_is_unique ( NAME, NAME )
RETURNS TEXT AS $$
SELECT col_is_unique( $1, $2, 'Column ' || $1 || '.' || $2 || ' should have a unique constraint' );
SELECT col_is_unique( $1, $2, 'Column ' || $1 || '(' || $2 || ') should have a unique constraint' );
$$ LANGUAGE sql;

-- has_check( schema, table, description )
Expand Down Expand Up @@ -1222,7 +1228,7 @@ $$ LANGUAGE sql;
-- col_has_check( table, column[] )
CREATE OR REPLACE FUNCTION col_has_check ( NAME, NAME[] )
RETURNS TEXT AS $$
SELECT col_has_check( $1, $2, 'Columns ' || $1 || '.' || $2::text || ' should have a check constraint' );
SELECT col_has_check( $1, $2, 'Columns ' || $1 || '(' || array_to_string($2, ', ') || ') should have a check constraint' );
$$ LANGUAGE sql;

-- col_has_check( schema, table, column, description )
Expand All @@ -1240,7 +1246,7 @@ $$ LANGUAGE sql;
-- col_has_check( table, column )
CREATE OR REPLACE FUNCTION col_has_check ( NAME, NAME )
RETURNS TEXT AS $$
SELECT col_has_check( $1, $2, 'Column ' || $1 || '.' || $2 || ' should have a check constraint' );
SELECT col_has_check( $1, $2, 'Column ' || $1 || '(' || $2 || ') should have a check constraint' );
$$ LANGUAGE sql;

-- fk_ok( fk_schema, fk_table, fk_column[], pk_schema, pk_table, pk_column[], description )
Expand Down
4 changes: 2 additions & 2 deletions sql/check.sql
Expand Up @@ -100,7 +100,7 @@ SELECT is(
\echo ok 15 - test col_has_check( table, column )
SELECT is(
col_has_check( 'sometab', 'name' ),
'ok 15 - Column sometab.name should have a check constraint',
'ok 15 - Column sometab(name) should have a check constraint',
'col_has_check( table, column ) should work'
);

Expand Down Expand Up @@ -141,7 +141,7 @@ SELECT is(
\echo ok 25 - test col_has_check( table, column[], description )
SELECT is(
col_has_check( 'argh', ARRAY['id', 'name'] ),
'ok 25 - Columns argh.{id,name} should have a check constraint',
'ok 25 - Columns argh(id, name) should have a check constraint',
'col_has_check( table, column[] ) should work'
);

Expand Down
16 changes: 8 additions & 8 deletions sql/coltap.sql
Expand Up @@ -59,14 +59,14 @@ SELECT is(
\echo ok 5 - testing col_not_null( schema, table, column, desc )
SELECT is(
col_not_null( 'sometab', 'id' ),
'ok 5 - Column sometab.id should be NOT NULL',
'ok 5 - Column sometab(id) should be NOT NULL',
'col_not_null( table, column ) should work'
);
-- Make sure failure is correct.
\echo ok 7 - testing col_not_null( schema, table, column, desc )
SELECT is(
col_not_null( 'sometab', 'name' ),
E'not ok 7 - Column sometab.name should be NOT NULL\n# Failed test 7: "Column sometab.name should be NOT NULL"',
E'not ok 7 - Column sometab(name) should be NOT NULL\n# Failed test 7: "Column sometab(name) should be NOT NULL"',
'col_not_null( table, column ) should properly fail'
);
UPDATE __tresults__ SET ok = true, aok = true WHERE numb IN( 7 );
Expand All @@ -89,14 +89,14 @@ SELECT is(
\echo ok 13 - testing col_is_null( schema, table, column, desc )
SELECT is(
col_is_null( 'sometab', 'name' ),
'ok 13 - Column sometab.name should allow NULL',
'ok 13 - Column sometab(name) should allow NULL',
'col_is_null( table, column ) should work'
);
-- Make sure failure is correct.
\echo ok 15 - testing col_is_null( schema, table, column, desc )
SELECT is(
col_is_null( 'sometab', 'id' ),
E'not ok 15 - Column sometab.id should allow NULL\n# Failed test 15: "Column sometab.id should allow NULL"',
E'not ok 15 - Column sometab(id) should allow NULL\n# Failed test 15: "Column sometab(id) should allow NULL"',
'col_is_null( table, column ) should properly fail'
);
UPDATE __tresults__ SET ok = true, aok = true WHERE numb IN( 15 );
Expand All @@ -120,22 +120,22 @@ SELECT is(
\echo ok 21 - testing col_type_is( table, column, type )
SELECT is(
col_type_is( 'sometab', 'name', 'text' ),
'ok 21 - Column sometab.name should be type text',
'ok 21 - Column sometab(name) should be type text',
'col_type_is( table, column, type ) should work'
);

\echo ok 23 - testing col_type_is( table, column, type ) case-insensitively
SELECT is(
col_type_is( 'sometab', 'name', 'TEXT' ),
'ok 23 - Column sometab.name should be type TEXT',
'ok 23 - Column sometab(name) should be type TEXT',
'col_type_is( table, column, type ) should work case-insensitively'
);

-- Make sure failure is correct.
\echo ok 25 - testing col_type_is( table, column, type ) failure
SELECT is(
col_type_is( 'sometab', 'name', 'int4' ),
E'not ok 25 - Column sometab.name should be type int4\n# Failed test 25: "Column sometab.name should be type int4"\n# have: text\n# want: int4',
E'not ok 25 - Column sometab(name) should be type int4\n# Failed test 25: "Column sometab(name) should be type int4"\n# have: text\n# want: int4',
'col_type_is( table, column, type ) should fail with proper diagnostics'
);
UPDATE __tresults__ SET ok = true, aok = true WHERE numb IN( 25 );
Expand Down Expand Up @@ -187,7 +187,7 @@ SELECT is(
\echo ok 37 - col_default_is( table, column, default )
SELECT is(
col_default_is( 'sometab', 'name', '' ),
'ok 37 - Column sometab.name should default to ''''',
'ok 37 - Column sometab(name) should default to ''''',
'col_default_is( table, column, default ) should work'
);

Expand Down
8 changes: 4 additions & 4 deletions sql/fktap.sql
Expand Up @@ -124,7 +124,7 @@ SELECT * FROM check_test(
col_is_fk( 'fk', 'pk_id' ),
true,
'col_is_fk( table, column )',
'Column fk.pk_id should be a foreign key'
'Column fk(pk_id) should be a foreign key'
);

SELECT * FROM check_test(
Expand All @@ -151,7 +151,7 @@ SELECT * FROM check_test(
col_is_fk( 'fk3', 'pk_id' ),
true,
'multi-fk col_is_fk test',
'Column fk3.pk_id should be a foreign key'
'Column fk3(pk_id) should be a foreign key'
);

-- Check failure for table with no FKs.
Expand All @@ -167,7 +167,7 @@ SELECT * FROM check_test(
col_is_fk( 'pk', 'name' ),
false,
'col_is_fk with no FKs',
'Column pk.name should be a foreign key',
'Column pk(name) should be a foreign key',
' Table pk has no foreign key columns'
);

Expand All @@ -193,7 +193,7 @@ SELECT * FROM check_test(
col_is_fk( 'fk2', ARRAY['pk2_num', 'pk2_dot'] ),
true,
'col_is_fk( table, column[] )',
'Columns fk2.{pk2_num,pk2_dot} should be a foreign key'
'Columns fk2(pk2_num, pk2_dot) should be a foreign key'
);

/****************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions sql/hastap.sql
Expand Up @@ -126,7 +126,7 @@ SELECT is(
\echo ok 21 - has_column(table, column) fail
SELECT is(
has_column( '__SDFSDFD__', 'foo' ),
E'not ok 21 - Column __SDFSDFD__.foo should exist\n# Failed test 21: "Column __SDFSDFD__.foo should exist"',
E'not ok 21 - Column __SDFSDFD__(foo) should exist\n# Failed test 21: "Column __SDFSDFD__(foo) should exist"',
'has_column(table, column) should fail for non-existent table'
);

Expand All @@ -148,7 +148,7 @@ UPDATE __tresults__ SET ok = true, aok = true WHERE numb IN( 21, 23, 25 );
\echo ok 27 - has_column(table, column) pass
SELECT is(
has_column( 'sometab', 'id' ),
'ok 27 - Column sometab.id should exist',
'ok 27 - Column sometab(id) should exist',
'has_column(table, column) should pass for an existing column'
);

Expand Down
4 changes: 2 additions & 2 deletions sql/pktap.sql
Expand Up @@ -100,7 +100,7 @@ SELECT is(
\echo ok 15 - test col_is_pk( table, column )
SELECT is(
col_is_pk( 'sometab', 'id' ),
'ok 15 - Column sometab.id should be a primary key',
'ok 15 - Column sometab(id) should be a primary key',
'col_is_pk( table, column ) should work'
);

Expand Down Expand Up @@ -141,7 +141,7 @@ SELECT is(
\echo ok 25 - test col_is_pk( table, column[], description )
SELECT is(
col_is_pk( 'argh', ARRAY['id', 'name'] ),
'ok 25 - Columns argh.{id,name} should be a primary key',
'ok 25 - Columns argh(id, name) should be a primary key',
'col_is_pk( table, column[] ) should work'
);

Expand Down
4 changes: 2 additions & 2 deletions sql/unique.sql
Expand Up @@ -100,7 +100,7 @@ SELECT is(
\echo ok 15 - test col_is_unique( table, column )
SELECT is(
col_is_unique( 'sometab', 'name' ),
'ok 15 - Column sometab.name should have a unique constraint',
'ok 15 - Column sometab(name) should have a unique constraint',
'col_is_unique( table, column ) should work'
);

Expand Down Expand Up @@ -141,7 +141,7 @@ SELECT is(
\echo ok 25 - test col_is_unique( table, column[], description )
SELECT is(
col_is_unique( 'argh', ARRAY['id', 'name'] ),
'ok 25 - Columns argh.{id,name} should have a unique constraint',
'ok 25 - Columns argh(id, name) should have a unique constraint',
'col_is_unique( table, column[] ) should work'
);

Expand Down

0 comments on commit 4eab0fc

Please sign in to comment.