Skip to content

Commit

Permalink
Add the has_pk(:schema, :table) variant
Browse files Browse the repository at this point in the history
Resolves #287
  • Loading branch information
theory committed Sep 23, 2023
1 parent d663c04 commit 7231edd
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 89 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Revision history for pgTAP
* `domain_type_isnt()`
* `has_operator()`
* `hasnt_operator()`
* Added the `has_pk( :schema, :table )` variant, which requires that `:table` be
cast to `name` to avoid confusion with the `has_pk( :schema, :description )`
variant. Thanks to Adrian Klaver for the report (#287)!

1.3.0 2023-08-14T22:14:20Z
--------------------------
Expand Down
6 changes: 6 additions & 0 deletions doc/pgtap.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -4636,6 +4636,7 @@ the diagnostics will tell you that, too. But you use `has_column()` and
### `has_pk()` ###

SELECT has_pk( :schema, :table, :description );
SELECT has_pk( :schema, :table );
SELECT has_pk( :table, :description );
SELECT has_pk( :table );

Expand All @@ -4657,6 +4658,11 @@ test description is omitted, it will be set to "Table `:table` should have a
primary key". Note that this test will fail if the table in question does not
exist.

If you find that the function call confuses the table name for a
description, cast the table to the `NAME` type:

SELECT has_pk( 'myschema', 'mytable'::name );

### `hasnt_pk()` ###

SELECT hasnt_pk( :schema, :table, :description );
Expand Down
6 changes: 6 additions & 0 deletions sql/pgtap.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,12 @@ RETURNS TEXT AS $$
SELECT ok( _hasc( $1, $2, 'p' ), $3 );
$$ LANGUAGE sql;

-- has_pk( schema, table )
CREATE OR REPLACE FUNCTION has_pk ( NAME, NAME )
RETURNS TEXT AS $$
SELECT has_pk( $1, $2, 'Table ' || quote_ident($1) || '.' || quote_ident($2) || ' should have a primary key' );
$$ LANGUAGE sql;

-- has_pk( table, description )
CREATE OR REPLACE FUNCTION has_pk ( NAME, TEXT )
RETURNS TEXT AS $$
Expand Down
182 changes: 94 additions & 88 deletions test/expected/pktap.out
Original file line number Diff line number Diff line change
@@ -1,92 +1,98 @@
\unset ECHO
1..90
1..96
ok 1 - has_pk( schema, table, description ) should pass
ok 2 - has_pk( schema, table, description ) should have the proper description
ok 3 - has_pk( schema, table, description ) should have the proper diagnostics
ok 4 - has_pk( hideschema, hidetable, description ) should pass
ok 5 - has_pk( hideschema, hidetable, description ) should have the proper description
ok 6 - has_pk( hideschema, hidetable, description ) should have the proper diagnostics
ok 7 - has_pk( table, description ) should pass
ok 8 - has_pk( table, description ) should have the proper description
ok 9 - has_pk( table, description ) should have the proper diagnostics
ok 10 - has_pk( hidetable, description ) fail should fail
ok 11 - has_pk( hidetable, description ) fail should have the proper description
ok 12 - has_pk( hidetable, description ) fail should have the proper diagnostics
ok 13 - has_pk( table ) should pass
ok 14 - has_pk( table ) should have the proper description
ok 15 - has_pk( table ) should have the proper diagnostics
ok 16 - has_pk( schema, table, description ) fail should fail
ok 17 - has_pk( schema, table, description ) fail should have the proper description
ok 18 - has_pk( schema, table, description ) fail should have the proper diagnostics
ok 19 - has_pk( table, description ) fail should fail
ok 20 - has_pk( table, description ) fail should have the proper description
ok 21 - has_pk( table, description ) fail should have the proper diagnostics
ok 22 - hasnt_pk( schema, table, description ) should fail
ok 23 - hasnt_pk( schema, table, description ) should have the proper description
ok 24 - hasnt_pk( schema, table, description ) should have the proper diagnostics
ok 25 - hasnt_pk( table, description ) should fail
ok 26 - hasnt_pk( table, description ) should have the proper description
ok 27 - hasnt_pk( table, description ) should have the proper diagnostics
ok 28 - hasnt_pk( table ) should fail
ok 29 - hasnt_pk( table ) should have the proper description
ok 30 - hasnt_pk( table ) should have the proper diagnostics
ok 31 - hasnt_pk( schema, table, description ) pass should pass
ok 32 - hasnt_pk( schema, table, description ) pass should have the proper description
ok 33 - hasnt_pk( schema, table, description ) pass should have the proper diagnostics
ok 34 - hasnt_pk( table, description ) pass should pass
ok 35 - hasnt_pk( table, description ) pass should have the proper description
ok 36 - hasnt_pk( table, description ) pass should have the proper diagnostics
ok 37 - col_is_pk( schema, table, column, description ) should pass
ok 38 - col_is_pk( schema, table, column, description ) should have the proper description
ok 39 - col_is_pk( schema, table, column, description ) should have the proper diagnostics
ok 40 - col_is_pk( schema, table, column ) should pass
ok 41 - col_is_pk( schema, table, column ) should have the proper description
ok 42 - col_is_pk( schema, table, column ) should have the proper diagnostics
ok 43 - col_is_pk( table, column, description ) should pass
ok 44 - col_is_pk( table, column, description ) should have the proper description
ok 45 - col_is_pk( table, column, description ) should have the proper diagnostics
ok 46 - col_is_pk( table, column ) should pass
ok 47 - col_is_pk( table, column ) should have the proper description
ok 48 - col_is_pk( table, column ) should have the proper diagnostics
ok 49 - col_is_pk( schema, table, column, description ) fail should fail
ok 50 - col_is_pk( schema, table, column, description ) fail should have the proper description
ok 51 - col_is_pk( schema, table, column, description ) fail should have the proper diagnostics
ok 52 - col_is_pk( table, column, description ) fail should fail
ok 53 - col_is_pk( table, column, description ) fail should have the proper description
ok 54 - col_is_pk( table, column, description ) fail should have the proper diagnostics
ok 55 - col_is_pk( schema, table, column[], description ) should pass
ok 56 - col_is_pk( schema, table, column[], description ) should have the proper description
ok 57 - col_is_pk( schema, table, column[], description ) should have the proper diagnostics
ok 58 - col_is_pk( schema, table, column[] ) should pass
ok 59 - col_is_pk( schema, table, column[] ) should have the proper description
ok 60 - col_is_pk( schema, table, column[] ) should have the proper diagnostics
ok 61 - col_is_pk( table, column[], description ) should pass
ok 62 - col_is_pk( table, column[], description ) should have the proper description
ok 63 - col_is_pk( table, column[], description ) should have the proper diagnostics
ok 64 - col_is_pk( table, column[] ) should pass
ok 65 - col_is_pk( table, column[] ) should have the proper description
ok 66 - col_is_pk( table, column[] ) should have the proper diagnostics
ok 67 - col_isnt_pk( schema, table, column, description ) should fail
ok 68 - col_isnt_pk( schema, table, column, description ) should have the proper description
ok 69 - col_isnt_pk( schema, table, column, description ) should have the proper diagnostics
ok 70 - col_isnt_pk( table, column, description ) should fail
ok 71 - col_isnt_pk( table, column, description ) should have the proper description
ok 72 - col_isnt_pk( table, column, description ) should have the proper diagnostics
ok 73 - col_isnt_pk( table, column ) should fail
ok 74 - col_isnt_pk( table, column ) should have the proper description
ok 75 - col_isnt_pk( table, column ) should have the proper diagnostics
ok 76 - col_isnt_pk( schema, table, column, description ) pass should pass
ok 77 - col_isnt_pk( schema, table, column, description ) pass should have the proper description
ok 78 - col_isnt_pk( schema, table, column, description ) pass should have the proper diagnostics
ok 79 - col_isnt_pk( table, column, description ) pass should pass
ok 80 - col_isnt_pk( table, column, description ) pass should have the proper description
ok 81 - col_isnt_pk( table, column, description ) pass should have the proper diagnostics
ok 82 - col_isnt_pk( schema, table, column[], description ) should pass
ok 83 - col_isnt_pk( schema, table, column[], description ) should have the proper description
ok 84 - col_isnt_pk( schema, table, column[], description ) should have the proper diagnostics
ok 85 - col_isnt_pk( table, column[], description ) should pass
ok 86 - col_isnt_pk( table, column[], description ) should have the proper description
ok 87 - col_isnt_pk( table, column[], description ) should have the proper diagnostics
ok 88 - col_isnt_pk( table, column[] ) should pass
ok 89 - col_isnt_pk( table, column[] ) should have the proper description
ok 90 - col_isnt_pk( table, column[] ) should have the proper diagnostics
ok 4 - has_pk( schema, table ) should pass
ok 5 - has_pk( schema, table ) should have the proper description
ok 6 - has_pk( schema, table ) should have the proper diagnostics
ok 7 - has_pk( hideschema, hidetable, description ) should pass
ok 8 - has_pk( hideschema, hidetable, description ) should have the proper description
ok 9 - has_pk( hideschema, hidetable, description ) should have the proper diagnostics
ok 10 - has_pk( hideschema, hidetable ) should pass
ok 11 - has_pk( hideschema, hidetable ) should have the proper description
ok 12 - has_pk( hideschema, hidetable ) should have the proper diagnostics
ok 13 - has_pk( table, description ) should pass
ok 14 - has_pk( table, description ) should have the proper description
ok 15 - has_pk( table, description ) should have the proper diagnostics
ok 16 - has_pk( hidetable, description ) fail should fail
ok 17 - has_pk( hidetable, description ) fail should have the proper description
ok 18 - has_pk( hidetable, description ) fail should have the proper diagnostics
ok 19 - has_pk( table ) should pass
ok 20 - has_pk( table ) should have the proper description
ok 21 - has_pk( table ) should have the proper diagnostics
ok 22 - has_pk( schema, table, description ) fail should fail
ok 23 - has_pk( schema, table, description ) fail should have the proper description
ok 24 - has_pk( schema, table, description ) fail should have the proper diagnostics
ok 25 - has_pk( table, description ) fail should fail
ok 26 - has_pk( table, description ) fail should have the proper description
ok 27 - has_pk( table, description ) fail should have the proper diagnostics
ok 28 - hasnt_pk( schema, table, description ) should fail
ok 29 - hasnt_pk( schema, table, description ) should have the proper description
ok 30 - hasnt_pk( schema, table, description ) should have the proper diagnostics
ok 31 - hasnt_pk( table, description ) should fail
ok 32 - hasnt_pk( table, description ) should have the proper description
ok 33 - hasnt_pk( table, description ) should have the proper diagnostics
ok 34 - hasnt_pk( table ) should fail
ok 35 - hasnt_pk( table ) should have the proper description
ok 36 - hasnt_pk( table ) should have the proper diagnostics
ok 37 - hasnt_pk( schema, table, description ) pass should pass
ok 38 - hasnt_pk( schema, table, description ) pass should have the proper description
ok 39 - hasnt_pk( schema, table, description ) pass should have the proper diagnostics
ok 40 - hasnt_pk( table, description ) pass should pass
ok 41 - hasnt_pk( table, description ) pass should have the proper description
ok 42 - hasnt_pk( table, description ) pass should have the proper diagnostics
ok 43 - col_is_pk( schema, table, column, description ) should pass
ok 44 - col_is_pk( schema, table, column, description ) should have the proper description
ok 45 - col_is_pk( schema, table, column, description ) should have the proper diagnostics
ok 46 - col_is_pk( schema, table, column ) should pass
ok 47 - col_is_pk( schema, table, column ) should have the proper description
ok 48 - col_is_pk( schema, table, column ) should have the proper diagnostics
ok 49 - col_is_pk( table, column, description ) should pass
ok 50 - col_is_pk( table, column, description ) should have the proper description
ok 51 - col_is_pk( table, column, description ) should have the proper diagnostics
ok 52 - col_is_pk( table, column ) should pass
ok 53 - col_is_pk( table, column ) should have the proper description
ok 54 - col_is_pk( table, column ) should have the proper diagnostics
ok 55 - col_is_pk( schema, table, column, description ) fail should fail
ok 56 - col_is_pk( schema, table, column, description ) fail should have the proper description
ok 57 - col_is_pk( schema, table, column, description ) fail should have the proper diagnostics
ok 58 - col_is_pk( table, column, description ) fail should fail
ok 59 - col_is_pk( table, column, description ) fail should have the proper description
ok 60 - col_is_pk( table, column, description ) fail should have the proper diagnostics
ok 61 - col_is_pk( schema, table, column[], description ) should pass
ok 62 - col_is_pk( schema, table, column[], description ) should have the proper description
ok 63 - col_is_pk( schema, table, column[], description ) should have the proper diagnostics
ok 64 - col_is_pk( schema, table, column[] ) should pass
ok 65 - col_is_pk( schema, table, column[] ) should have the proper description
ok 66 - col_is_pk( schema, table, column[] ) should have the proper diagnostics
ok 67 - col_is_pk( table, column[], description ) should pass
ok 68 - col_is_pk( table, column[], description ) should have the proper description
ok 69 - col_is_pk( table, column[], description ) should have the proper diagnostics
ok 70 - col_is_pk( table, column[] ) should pass
ok 71 - col_is_pk( table, column[] ) should have the proper description
ok 72 - col_is_pk( table, column[] ) should have the proper diagnostics
ok 73 - col_isnt_pk( schema, table, column, description ) should fail
ok 74 - col_isnt_pk( schema, table, column, description ) should have the proper description
ok 75 - col_isnt_pk( schema, table, column, description ) should have the proper diagnostics
ok 76 - col_isnt_pk( table, column, description ) should fail
ok 77 - col_isnt_pk( table, column, description ) should have the proper description
ok 78 - col_isnt_pk( table, column, description ) should have the proper diagnostics
ok 79 - col_isnt_pk( table, column ) should fail
ok 80 - col_isnt_pk( table, column ) should have the proper description
ok 81 - col_isnt_pk( table, column ) should have the proper diagnostics
ok 82 - col_isnt_pk( schema, table, column, description ) pass should pass
ok 83 - col_isnt_pk( schema, table, column, description ) pass should have the proper description
ok 84 - col_isnt_pk( schema, table, column, description ) pass should have the proper diagnostics
ok 85 - col_isnt_pk( table, column, description ) pass should pass
ok 86 - col_isnt_pk( table, column, description ) pass should have the proper description
ok 87 - col_isnt_pk( table, column, description ) pass should have the proper diagnostics
ok 88 - col_isnt_pk( schema, table, column[], description ) should pass
ok 89 - col_isnt_pk( schema, table, column[], description ) should have the proper description
ok 90 - col_isnt_pk( schema, table, column[], description ) should have the proper diagnostics
ok 91 - col_isnt_pk( table, column[], description ) should pass
ok 92 - col_isnt_pk( table, column[], description ) should have the proper description
ok 93 - col_isnt_pk( table, column[], description ) should have the proper diagnostics
ok 94 - col_isnt_pk( table, column[] ) should pass
ok 95 - col_isnt_pk( table, column[] ) should have the proper description
ok 96 - col_isnt_pk( table, column[] ) should have the proper diagnostics
19 changes: 18 additions & 1 deletion test/sql/pktap.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
\unset ECHO
\i test/setup.sql
-- \i sql/pgtap.sql

SELECT plan(90);
SELECT plan(96);
--SELECT * FROM no_plan();

-- This will be rolled back. :-)
Expand Down Expand Up @@ -37,6 +38,14 @@ SELECT * FROM check_test(
''
);

SELECT * FROM check_test(
has_pk( 'public', 'sometab'::name ),
true,
'has_pk( schema, table )',
'Table public.sometab should have a primary key',
''
);

SELECT * FROM check_test(
has_pk( 'hide', 'hidesometab', 'hide.sometab should have a pk' ),
true,
Expand All @@ -45,6 +54,14 @@ SELECT * FROM check_test(
''
);

SELECT * FROM check_test(
has_pk( 'hide', 'hidesometab'::name ),
true,
'has_pk( hideschema, hidetable )',
'Table hide.hidesometab should have a primary key',
''
);

SELECT * FROM check_test(
has_pk( 'sometab', 'sometab should have a pk' ),
true,
Expand Down

0 comments on commit 7231edd

Please sign in to comment.