Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add index_owner_is()`.

Ref #40.
  • Loading branch information...
commit 13fcfa43eac58b6c93f80dc2caf49d521a42b8b5 1 parent b838eb0
David E. Wheeler authored
1  Changes
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.
47 doc/pgtap.mmd
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 );
79 sql/pgtap--0.92.0--0.93.0.sql
View
@@ -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;
79 sql/pgtap.sql.in
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(
38 test/expected/ownership.out
View
@@ -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
109 test/sql/ownership.sql
View
@@ -1,7 +1,7 @@
\unset ECHO
\i test/setup.sql
-SELECT plan(252);
+SELECT plan(288);
--SELECT * FROM no_plan();
-- This will be rolled back. :-)
@@ -13,6 +13,8 @@ CREATE TABLE public.sometab(
"myInt" NUMERIC(8)
);
+CREATE INDEX idx_hey ON public.sometab(numb);
+
CREATE VIEW public.someview AS SELECT * FROM public.sometab;
CREATE TYPE public.sometype AS (
@@ -23,6 +25,12 @@ CREATE TYPE public.sometype AS (
CREATE SEQUENCE public.someseq;
CREATE SCHEMA someschema;
+CREATE TABLE someschema.anothertab(
+ id INT PRIMARY KEY,
+ name TEXT DEFAULT ''
+);
+
+CREATE INDEX idx_name ON someschema.anothertab(name);
CREATE FUNCTION public.somefunction(int) RETURNS VOID LANGUAGE SQL AS '';
@@ -824,6 +832,105 @@ SELECT * FROM check_test(
want: __not__' || _get_tablespace_owner('pg_default')
);
+/****************************************************************************/
+-- Test index_owner_is().
+SELECT * FROM check_test(
+ index_owner_is('someschema', 'anothertab', 'idx_name', current_user, 'mumble'),
+ true,
+ 'index_owner_is(schema, table, index, user, desc)',
+ 'mumble',
+ ''
+);
+
+SELECT * FROM check_test(
+ index_owner_is('someschema', 'anothertab', 'idx_name', current_user),
+ true,
+ 'index_owner_is(schema, table, index, user)',
+ 'Index idx_name ON someschema.anothertab should be owned by ' || current_user,
+ ''
+);
+
+SELECT * FROM check_test(
+ index_owner_is('someschema', 'anothertab', 'idx_foo', current_user, 'mumble'),
+ false,
+ 'index_owner_is(schema, table, non-index, user, desc)',
+ 'mumble',
+ ' Index idx_foo ON someschema.anothertab not found'
+);
+
+SELECT * FROM check_test(
+ index_owner_is('someschema', 'nonesuch', 'idx_name', current_user, 'mumble'),
+ false,
+ 'index_owner_is(schema, non-table, index, user, desc)',
+ 'mumble',
+ ' Index idx_name ON someschema.nonesuch not found'
+);
+
+SELECT * FROM check_test(
+ index_owner_is('nonesuch', 'anothertab', 'idx_name', current_user, 'mumble'),
+ false,
+ 'index_owner_is(non-schema, table, index, user, desc)',
+ 'mumble',
+ ' Index idx_name ON nonesuch.anothertab not found'
+);
+
+SELECT * FROM check_test(
+ index_owner_is('someschema', 'anothertab', 'idx_name', '__noone', 'mumble'),
+ false,
+ 'index_owner_is(schema, table, index, non-user, desc)',
+ 'mumble',
+ ' have: ' || current_user || '
+ want: __noone'
+);
+
+SELECT * FROM check_test(
+ index_owner_is('anothertab', 'idx_name', current_user, 'mumble'),
+ false,
+ 'index_owner_is(invisible-table, index, user, desc)',
+ 'mumble',
+ ' Index idx_name ON anothertab not found'
+);
+
+SELECT * FROM check_test(
+ index_owner_is('sometab', 'idx_hey', current_user, 'mumble'),
+ true,
+ 'index_owner_is(table, index, user, desc)',
+ 'mumble',
+ ''
+);
+
+SELECT * FROM check_test(
+ index_owner_is('sometab', 'idx_hey', current_user),
+ true,
+ 'index_owner_is(table, index, user)',
+ 'Index idx_hey ON sometab should be owned by ' || current_user,
+ ''
+);
+
+SELECT * FROM check_test(
+ index_owner_is('notab', 'idx_hey', current_user, 'mumble'),
+ false,
+ 'index_owner_is(non-table, index, user)',
+ 'mumble',
+ ' Index idx_hey ON notab not found'
+);
+
+SELECT * FROM check_test(
+ index_owner_is('sometab', 'idx_foo', current_user, 'mumble'),
+ false,
+ 'index_owner_is(table, non-index, user)',
+ 'mumble',
+ ' Index idx_foo ON sometab not found'
+);
+
+SELECT * FROM check_test(
+ index_owner_is('sometab', 'idx_hey', '__no-one', 'mumble'),
+ false,
+ 'index_owner_is(table, index, non-user)',
+ 'mumble',
+ ' have: ' || current_user || '
+ want: __no-one'
+);
/****************************************************************************/
-- Finish the tests and clean up.
Please sign in to comment.
Something went wrong with that request. Please try again.