/**
 * Test for view pg_role_infos
 *
 * Every test does raise division by zero if it failes
 *
 * @author: "Stefanie Janine Stölting"<stefanie@proopensource.eu>
 * @license: PostgreSQL https://opensource.org/licenses/postgresql
 */

DO $$
DECLARE
function_exists BOOLEAN;
view_source TEXT;
BEGIN

SELECT count(*) = 1 AS function_exists
INTO function_exists
FROM pg_catalog.pg_proc AS f
WHERE f.proname = 'role_inheritance'
	AND f.pronargs = 1
;

IF function_exists THEN
		-- Check view existence
		view_source :=
$string$
WITH test AS
	(
		SELECT COUNT(*) AS exist
			, 0 AS zero
		FROM pg_catalog.pg_views
		WHERE viewname = 'pg_role_infos'
	)
SELECT
	CASE
		WHEN 1 / test.exist = 1 THEN
			TRUE
		ELSE
			(1 / zero)::BOOLEAN
	END AS res
FROM test
;
$string$
;

		EXECUTE view_source;

		-- Test if the view runs without errors
		view_source :=
$string$
SELECT count(*)
FROM pg_role_infos
;
$string$
;

		EXECUTE view_source;
	END IF;

END $$;
