Permalink
Browse files

Add index_owner_is()`.

Ref #40.
  • Loading branch information...
1 parent b838eb0 commit 13fcfa43eac58b6c93f80dc2caf49d521a42b8b5 @theory committed Jan 25, 2013
Showing with 351 additions and 2 deletions.
  1. +1 −0 Changes
  2. +47 −0 doc/pgtap.mmd
  3. +79 −0 sql/pgtap--0.92.0--0.93.0.sql
  4. +79 −0 sql/pgtap.sql.in
  5. +37 −1 test/expected/ownership.out
  6. +108 −1 test/sql/ownership.sql
View
@@ -6,6 +6,7 @@ Revision history for pgTAP
* Added additional ownership-testing functions:
+ `tablespace_owner_is()`
+ `schema_owner_is()`
+ + `index_owner_is()`
* Fixed misselling of "constraint" in constraint test diagnostic output.
* Fixed `fk_ok()` so that the table must be visible in the search path when
the schema name is not specified.
View
@@ -5557,6 +5557,53 @@ diagnostics will look something like:
# have: postgres
# want: root
+### `index_owner_is ()` ###
+
+ SELECT index_owner_is ( :schema, :table, :index, :user, :description );
+ SELECT index_owner_is ( :table, :index, :user, :description );
+ SELECT index_owner_is ( :schema, :table, :index, :user );
+ SELECT index_owner_is ( :table, :index, :user );
+
+**Parameters**
+
+`:schema`
+: Name of a schema in which to find the `:table, :index`.
+
+`:table`
+: Name of a table.
+
+`:table`
+: Name of an index on the table.
+
+`:user`
+: Name of a user.
+
+`:description`
+: A short description of the test.
+
+Tests the ownership of an index. If the `:description` argument is omitted, an
+appropriate description will be created. Examples:
+
+ SELECT index_owner_is(
+ 'public', 'mytable', 'idx_name', 'someuser',
+ 'Index "idx_name" on mytable should be owned by someuser'
+ );
+ SELECT index_owner_is( 'widgets', 'widgets_pkey', current_user );
+
+In the event that the test fails because the index in question does not
+actually exist, or the table or schema it's on does not exist or is not
+visible, you will see an appropriate diagnostic such as:
+
+ # Failed test 16: "Index idx_foo should be owned by root"
+ # Index idx_foo on table darfoo not found
+
+If the test fails because the table is not owned by the specified user, the
+diagnostics will look something like:
+
+ # Failed test 17: "Index idx_foo on table bar should be owned by bob"
+ # have: postgres
+ # want: bob
+
### `function_owner_is ()` ###
SELECT function_owner_is ( :schema, :function, :args, :user, :description );
@@ -391,3 +391,82 @@ RETURNS BOOLEAN AS $$
AND pg_catalog.pg_table_is_visible(ct.oid)
);
$$ LANGUAGE sql;
+
+CREATE OR REPLACE FUNCTION _get_index_owner( NAME, NAME, NAME )
+RETURNS NAME AS $$
+ SELECT pg_catalog.pg_get_userbyid(ci.relowner)
+ FROM pg_catalog.pg_index x
+ JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
+ JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
+ JOIN pg_catalog.pg_namespace n ON n.oid = ct.relnamespace
+ WHERE n.nspname = $1
+ AND ct.relname = $2
+ AND ci.relname = $3;
+$$ LANGUAGE sql;
+
+CREATE OR REPLACE FUNCTION _get_index_owner( NAME, NAME )
+RETURNS NAME AS $$
+ SELECT pg_catalog.pg_get_userbyid(ci.relowner)
+ FROM pg_catalog.pg_index x
+ JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
+ JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
+ WHERE ct.relname = $1
+ AND ci.relname = $2
+ AND pg_catalog.pg_table_is_visible(ct.oid);
+$$ LANGUAGE sql;
+
+-- index_owner_is ( schema, table, index, user, description )
+CREATE OR REPLACE FUNCTION index_owner_is ( NAME, NAME, NAME, NAME, TEXT )
+RETURNS TEXT AS $$
+DECLARE
+ owner NAME := _get_index_owner($1, $2, $3);
+BEGIN
+ -- Make sure the index exists.
+ IF owner IS NULL THEN
+ RETURN ok(FALSE, $5) || E'\n' || diag(
+ E' Index ' || quote_ident($3) || ' ON '
+ || quote_ident($1) || '.' || quote_ident($2) || ' not found'
+ );
+ END IF;
+
+ RETURN is(owner, $4, $5);
+END;
+$$ LANGUAGE plpgsql;
+
+-- index_owner_is ( schema, table, index, user )
+CREATE OR REPLACE FUNCTION index_owner_is ( NAME, NAME, NAME, NAME )
+RETURNS TEXT AS $$
+ SELECT index_owner_is(
+ $1, $2, $3, $4,
+ 'Index ' || quote_ident($3) || ' ON '
+ || quote_ident($1) || '.' || quote_ident($2)
+ || ' should be owned by ' || quote_ident($4)
+ );
+$$ LANGUAGE sql;
+
+-- index_owner_is ( table, index, user, description )
+CREATE OR REPLACE FUNCTION index_owner_is ( NAME, NAME, NAME, TEXT )
+RETURNS TEXT AS $$
+DECLARE
+ owner NAME := _get_index_owner($1, $2);
+BEGIN
+ -- Make sure the index exists.
+ IF owner IS NULL THEN
+ RETURN ok(FALSE, $4) || E'\n' || diag(
+ E' Index ' || quote_ident($2) || ' ON ' || quote_ident($1) || ' not found'
+ );
+ END IF;
+
+ RETURN is(owner, $3, $4);
+END;
+$$ LANGUAGE plpgsql;
+
+-- index_owner_is ( table, index, user )
+CREATE OR REPLACE FUNCTION index_owner_is ( NAME, NAME, NAME )
+RETURNS TEXT AS $$
+ SELECT index_owner_is(
+ $1, $2, $3,
+ 'Index ' || quote_ident($2) || ' ON '
+ || quote_ident($1) || ' should be owned by ' || quote_ident($3)
+ );
+$$ LANGUAGE sql;
View
@@ -7977,6 +7977,85 @@ RETURNS TEXT AS $$
);
$$ LANGUAGE sql;
+CREATE OR REPLACE FUNCTION _get_index_owner( NAME, NAME, NAME )
+RETURNS NAME AS $$
+ SELECT pg_catalog.pg_get_userbyid(ci.relowner)
+ FROM pg_catalog.pg_index x
+ JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
+ JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
+ JOIN pg_catalog.pg_namespace n ON n.oid = ct.relnamespace
+ WHERE n.nspname = $1
+ AND ct.relname = $2
+ AND ci.relname = $3;
+$$ LANGUAGE sql;
+
+CREATE OR REPLACE FUNCTION _get_index_owner( NAME, NAME )
+RETURNS NAME AS $$
+ SELECT pg_catalog.pg_get_userbyid(ci.relowner)
+ FROM pg_catalog.pg_index x
+ JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
+ JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
+ WHERE ct.relname = $1
+ AND ci.relname = $2
+ AND pg_catalog.pg_table_is_visible(ct.oid);
+$$ LANGUAGE sql;
+
+-- index_owner_is ( schema, table, index, user, description )
+CREATE OR REPLACE FUNCTION index_owner_is ( NAME, NAME, NAME, NAME, TEXT )
+RETURNS TEXT AS $$
+DECLARE
+ owner NAME := _get_index_owner($1, $2, $3);
+BEGIN
+ -- Make sure the index exists.
+ IF owner IS NULL THEN
+ RETURN ok(FALSE, $5) || E'\n' || diag(
+ E' Index ' || quote_ident($3) || ' ON '
+ || quote_ident($1) || '.' || quote_ident($2) || ' not found'
+ );
+ END IF;
+
+ RETURN is(owner, $4, $5);
+END;
+$$ LANGUAGE plpgsql;
+
+-- index_owner_is ( schema, table, index, user )
+CREATE OR REPLACE FUNCTION index_owner_is ( NAME, NAME, NAME, NAME )
+RETURNS TEXT AS $$
+ SELECT index_owner_is(
+ $1, $2, $3, $4,
+ 'Index ' || quote_ident($3) || ' ON '
+ || quote_ident($1) || '.' || quote_ident($2)
+ || ' should be owned by ' || quote_ident($4)
+ );
+$$ LANGUAGE sql;
+
+-- index_owner_is ( table, index, user, description )
+CREATE OR REPLACE FUNCTION index_owner_is ( NAME, NAME, NAME, TEXT )
+RETURNS TEXT AS $$
+DECLARE
+ owner NAME := _get_index_owner($1, $2);
+BEGIN
+ -- Make sure the index exists.
+ IF owner IS NULL THEN
+ RETURN ok(FALSE, $4) || E'\n' || diag(
+ E' Index ' || quote_ident($2) || ' ON ' || quote_ident($1) || ' not found'
+ );
+ END IF;
+
+ RETURN is(owner, $3, $4);
+END;
+$$ LANGUAGE plpgsql;
+
+-- index_owner_is ( table, index, user )
+CREATE OR REPLACE FUNCTION index_owner_is ( NAME, NAME, NAME )
+RETURNS TEXT AS $$
+ SELECT index_owner_is(
+ $1, $2, $3,
+ 'Index ' || quote_ident($2) || ' ON '
+ || quote_ident($1) || ' should be owned by ' || quote_ident($3)
+ );
+$$ LANGUAGE sql;
+
CREATE OR REPLACE FUNCTION _assets_are ( text, text[], text[], TEXT )
RETURNS TEXT AS $$
SELECT _areni(
@@ -1,5 +1,5 @@
\unset ECHO
-1..252
+1..288
ok 1 - db_owner_is(db, user, desc) should pass
ok 2 - db_owner_is(db, user, desc) should have the proper description
ok 3 - db_owner_is(db, user, desc) should have the proper diagnostics
@@ -252,3 +252,39 @@ ok 249 - tablespace_owner_is(non-tablespace, user) should have the proper diagno
ok 250 - tablespace_owner_is(tablespace, non-user) should fail
ok 251 - tablespace_owner_is(tablespace, non-user) should have the proper description
ok 252 - tablespace_owner_is(tablespace, non-user) should have the proper diagnostics
+ok 253 - index_owner_is(schema, table, index, user, desc) should pass
+ok 254 - index_owner_is(schema, table, index, user, desc) should have the proper description
+ok 255 - index_owner_is(schema, table, index, user, desc) should have the proper diagnostics
+ok 256 - index_owner_is(schema, table, index, user) should pass
+ok 257 - index_owner_is(schema, table, index, user) should have the proper description
+ok 258 - index_owner_is(schema, table, index, user) should have the proper diagnostics
+ok 259 - index_owner_is(schema, table, non-index, user, desc) should fail
+ok 260 - index_owner_is(schema, table, non-index, user, desc) should have the proper description
+ok 261 - index_owner_is(schema, table, non-index, user, desc) should have the proper diagnostics
+ok 262 - index_owner_is(schema, non-table, index, user, desc) should fail
+ok 263 - index_owner_is(schema, non-table, index, user, desc) should have the proper description
+ok 264 - index_owner_is(schema, non-table, index, user, desc) should have the proper diagnostics
+ok 265 - index_owner_is(non-schema, table, index, user, desc) should fail
+ok 266 - index_owner_is(non-schema, table, index, user, desc) should have the proper description
+ok 267 - index_owner_is(non-schema, table, index, user, desc) should have the proper diagnostics
+ok 268 - index_owner_is(schema, table, index, non-user, desc) should fail
+ok 269 - index_owner_is(schema, table, index, non-user, desc) should have the proper description
+ok 270 - index_owner_is(schema, table, index, non-user, desc) should have the proper diagnostics
+ok 271 - index_owner_is(invisible-table, index, user, desc) should fail
+ok 272 - index_owner_is(invisible-table, index, user, desc) should have the proper description
+ok 273 - index_owner_is(invisible-table, index, user, desc) should have the proper diagnostics
+ok 274 - index_owner_is(table, index, user, desc) should pass
+ok 275 - index_owner_is(table, index, user, desc) should have the proper description
+ok 276 - index_owner_is(table, index, user, desc) should have the proper diagnostics
+ok 277 - index_owner_is(table, index, user) should pass
+ok 278 - index_owner_is(table, index, user) should have the proper description
+ok 279 - index_owner_is(table, index, user) should have the proper diagnostics
+ok 280 - index_owner_is(non-table, index, user) should fail
+ok 281 - index_owner_is(non-table, index, user) should have the proper description
+ok 282 - index_owner_is(non-table, index, user) should have the proper diagnostics
+ok 283 - index_owner_is(table, non-index, user) should fail
+ok 284 - index_owner_is(table, non-index, user) should have the proper description
+ok 285 - index_owner_is(table, non-index, user) should have the proper diagnostics
+ok 286 - index_owner_is(table, index, non-user) should fail
+ok 287 - index_owner_is(table, index, non-user) should have the proper description
+ok 288 - index_owner_is(table, index, non-user) should have the proper diagnostics
Oops, something went wrong.

0 comments on commit 13fcfa4

Please sign in to comment.