Permalink
Browse files

Add language_owner_is()`.

Ref #40.
  • Loading branch information...
1 parent 13fcfa4 commit 6e101fc06b4f79c48c2a0ce9e56b0a890becf635 @theory committed Jan 25, 2013
Showing with 153 additions and 2 deletions.
  1. +1 −0 Changes
  2. +35 −0 doc/pgtap.mmd
  3. +34 −0 sql/pgtap--0.92.0--0.93.0.sql
  4. +34 −0 sql/pgtap.sql.in
  5. +13 −1 test/expected/ownership.out
  6. +36 −1 test/sql/ownership.sql
View
@@ -7,6 +7,7 @@ Revision history for pgTAP
+ `tablespace_owner_is()`
+ `schema_owner_is()`
+ `index_owner_is()`
+ + `language_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
@@ -5651,6 +5651,41 @@ diagnostics will look something like:
# have: postgres
# want: root
+### `language_owner_is ()` ###
+
+ SELECT language_owner_is ( :languagename, :user, :description );
+ SELECT language_owner_is ( :languagename, :user );
+
+**Parameters**
+
+`:languagename`
+: Name of a language.
+
+`:user`
+: Name of a user.
+
+`:description`
+: A short description of the test.
+
+Tests the ownership of a procedural language. If the `:description` argument
+is omitted, an appropriate description will be created. Examples:
+
+ SELECT language_owner_is( 'plpgsql', 'larry', 'Larry should own plpgsql' );
+ SELECT language_owner_is( 'plperl', current_user );
+
+In the event that the test fails because the language in question does not
+actually exist, you will see an appropriate diagnostic such as:
+
+ # Failed test 16: "Language pllolcode should be owned by meow"
+ # Language pllolcode does not exist
+
+If the test fails because the language is not owned by the specified user,
+the diagnostics will look something like:
+
+ # Failed test 17: "Language plruby should be owned by mats"
+ # have: postgres
+ # want: mats
+
Prvileged Access
----------------
@@ -470,3 +470,37 @@ RETURNS TEXT AS $$
|| quote_ident($1) || ' should be owned by ' || quote_ident($3)
);
$$ LANGUAGE sql;
+
+-- _get_language_owner( language )
+CREATE OR REPLACE FUNCTION _get_language_owner( NAME )
+RETURNS NAME AS $$
+ SELECT pg_catalog.pg_get_userbyid(lanowner)
+ FROM pg_catalog.pg_language
+ WHERE lanname = $1;
+$$ LANGUAGE SQL;
+
+-- language_owner_is ( language, user, description )
+CREATE OR REPLACE FUNCTION language_owner_is ( NAME, NAME, TEXT )
+RETURNS TEXT AS $$
+DECLARE
+ owner NAME := _get_language_owner($1);
+BEGIN
+ -- Make sure the language exists.
+ IF owner IS NULL THEN
+ RETURN ok(FALSE, $3) || E'\n' || diag(
+ E' Language ' || quote_ident($1) || ' does not exist'
+ );
+ END IF;
+
+ RETURN is(owner, $2, $3);
+END;
+$$ LANGUAGE plpgsql;
+
+-- language_owner_is ( language, user )
+CREATE OR REPLACE FUNCTION language_owner_is ( NAME, NAME )
+RETURNS TEXT AS $$
+ SELECT language_owner_is(
+ $1, $2,
+ 'Language ' || quote_ident($1) || ' should be owned by ' || quote_ident($2)
+ );
+$$ LANGUAGE sql;
View
@@ -8056,6 +8056,40 @@ RETURNS TEXT AS $$
);
$$ LANGUAGE sql;
+-- _get_language_owner( language )
+CREATE OR REPLACE FUNCTION _get_language_owner( NAME )
+RETURNS NAME AS $$
+ SELECT pg_catalog.pg_get_userbyid(lanowner)
+ FROM pg_catalog.pg_language
+ WHERE lanname = $1;
+$$ LANGUAGE SQL;
+
+-- language_owner_is ( language, user, description )
+CREATE OR REPLACE FUNCTION language_owner_is ( NAME, NAME, TEXT )
+RETURNS TEXT AS $$
+DECLARE
+ owner NAME := _get_language_owner($1);
+BEGIN
+ -- Make sure the language exists.
+ IF owner IS NULL THEN
+ RETURN ok(FALSE, $3) || E'\n' || diag(
+ E' Language ' || quote_ident($1) || ' does not exist'
+ );
+ END IF;
+
+ RETURN is(owner, $2, $3);
+END;
+$$ LANGUAGE plpgsql;
+
+-- language_owner_is ( language, user )
+CREATE OR REPLACE FUNCTION language_owner_is ( NAME, NAME )
+RETURNS TEXT AS $$
+ SELECT language_owner_is(
+ $1, $2,
+ 'Language ' || quote_ident($1) || ' should be owned by ' || quote_ident($2)
+ );
+$$ LANGUAGE sql;
+
CREATE OR REPLACE FUNCTION _assets_are ( text, text[], text[], TEXT )
RETURNS TEXT AS $$
SELECT _areni(
@@ -1,5 +1,5 @@
\unset ECHO
-1..288
+1..300
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
@@ -288,3 +288,15 @@ ok 285 - index_owner_is(table, non-index, user) should have the proper diagnosti
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
+ok 289 - language_owner_is(language, user, desc) should pass
+ok 290 - language_owner_is(language, user, desc) should have the proper description
+ok 291 - language_owner_is(language, user, desc) should have the proper diagnostics
+ok 292 - language_owner_is(language, user) should pass
+ok 293 - language_owner_is(language, user) should have the proper description
+ok 294 - language_owner_is(language, user) should have the proper diagnostics
+ok 295 - language_owner_is(non-language, user) should fail
+ok 296 - language_owner_is(non-language, user) should have the proper description
+ok 297 - language_owner_is(non-language, user) should have the proper diagnostics
+ok 298 - language_owner_is(language, non-user) should fail
+ok 299 - language_owner_is(language, non-user) should have the proper description
+ok 300 - language_owner_is(language, non-user) should have the proper diagnostics
View
@@ -1,7 +1,7 @@
\unset ECHO
\i test/setup.sql
-SELECT plan(288);
+SELECT plan(300);
--SELECT * FROM no_plan();
-- This will be rolled back. :-)
@@ -933,6 +933,41 @@ SELECT * FROM check_test(
);
/****************************************************************************/
+-- Test language_owner_is().
+SELECT * FROM check_test(
+ language_owner_is('plpgsql', _get_language_owner('plpgsql'), 'mumble'),
+ true,
+ 'language_owner_is(language, user, desc)',
+ 'mumble',
+ ''
+);
+
+SELECT * FROM check_test(
+ language_owner_is('plpgsql', _get_language_owner('plpgsql')),
+ true,
+ 'language_owner_is(language, user)',
+ 'Language ' || quote_ident('plpgsql') || ' should be owned by ' || _get_language_owner('plpgsql'),
+ ''
+);
+
+SELECT * FROM check_test(
+ language_owner_is('__not__' || 'plpgsql', _get_language_owner('plpgsql'), 'mumble'),
+ false,
+ 'language_owner_is(non-language, user)',
+ 'mumble',
+ ' Language __not__' || 'plpgsql' || ' does not exist'
+);
+
+SELECT * FROM check_test(
+ language_owner_is('plpgsql', '__not__' || _get_language_owner('plpgsql'), 'mumble'),
+ false,
+ 'language_owner_is(language, non-user)',
+ 'mumble',
+ ' have: ' || _get_language_owner('plpgsql') || '
+ want: __not__' || _get_language_owner('plpgsql')
+);
+
+/****************************************************************************/
-- Finish the tests and clean up.
SELECT * FROM finish();
ROLLBACK;

0 comments on commit 6e101fc

Please sign in to comment.