/**
 * Test for function role_inheritance
 *
 * 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
 	version_greater_13 BOOLEAN;
	function_source TEXT;
	test_result BOOLEAN;
 BEGIN
 	SELECT to_number((string_to_array(version(), ' '))[2], '999.99') >= 14
	INTO version_greater_13;

	-- Test if the function exists
	WITH test AS
		(
			SELECT COUNT(*) AS exist
				, 0 AS zero
			FROM pg_catalog.pg_proc
			WHERE proname = 'role_inheritance'
		)
	SELECT
		CASE
			WHEN test.exist > 0 THEN
				TRUE
			ELSE
				(1 / zero)::BOOLEAN
		END AS res
		INTO test_result
	FROM test
	;

 	IF version_greater_13 THEN
 		-- Only usable with default roles in PostgreSQL >= 14

		-- Test function result
		WITH test AS
			(
				SELECT count (*) AS rows
					, 0 AS zero
				FROM role_inheritance('pg_database_owner')
			)
		SELECT
			CASE
				WHEN rows > 0 THEN
					TRUE
				ELSE
					(1 / zero)::BOOLEAN
			END AS res
			INTO test_result
		FROM test
		;
	END IF;
END
$$;
