Skip to content
binodnp edited this page Feb 24, 2014 · 2 revisions

Fails the test. This function should always follow a return statement.

Annotation

CREATE FUNCTION assert.fail(message text)
RETURNS text

Parameters

Parameter Parameter Name Data Type Description
$1 message text A message explaining why the test failed.

#Example 1

This example fails the test intentionally.

DROP FUNCTION IF EXISTS unit_tests.assert_fail_example1();

CREATE FUNCTION unit_tests.assert_fail_example1()
RETURNS test_result
AS
$$
	DECLARE message test_result;
BEGIN
	IF 1 = 1 THEN
		SELECT assert.fail('This failed intentionally.') INTO message;
		RETURN message;
	END IF;

	SELECT assert.ok('End of test.') INTO message;  
	RETURN message; 
END
$$
LANGUAGE plpgsql;

/*Use transaction if your test contains DML query.*/
--BEGIN TRANSACTION;
SELECT * FROM unit_tests.begin();
--ROLLBACK TRANSACTION;

#Example 2 This example fails the test if PostgreSQL Server version is less than 9.0.

DROP FUNCTION IF EXISTS unit_tests.assert_fail_example2();

CREATE FUNCTION unit_tests.assert_fail_example2()
RETURNS test_result
AS
$$
	DECLARE message test_result;
	DECLARE db_version integer;
BEGIN
	
	SELECT setting::integer
	INTO db_version
	FROM pg_settings 
	WHERE name='server_version_num'; 

	IF db_version <  90000 THEN
		SELECT assert.fail('PostgreSQL Server version 9.0 or higher is required.') INTO message;
		RETURN message;
	END IF;

	SELECT assert.ok('End of test.') INTO message;  
	RETURN message; 
END
$$
LANGUAGE plpgsql;

/*Use transaction if your test contains DML query.*/
--BEGIN TRANSACTION;
SELECT * FROM unit_tests.begin();
--ROLLBACK TRANSACTION;

#Example 3 This example fails the test if schema "core" is not found.

DROP FUNCTION IF EXISTS unit_tests.assert_fail_example3();

CREATE FUNCTION unit_tests.assert_fail_example3()
RETURNS test_result
AS
$$
	DECLARE message test_result;
BEGIN

	IF NOT EXISTS(SELECT 1 FROM information_schema.schemata WHERE schema_name = 'core') THEN
		SELECT assert.fail('Schema ''core'' was not found.') INTO message;
		RETURN message;
	END IF;

	SELECT assert.ok('End of test.') INTO message;  
	RETURN message; 
END
$$
LANGUAGE plpgsql;

/*Use transaction if your test contains DML query.*/
--BEGIN TRANSACTION;
SELECT * FROM unit_tests.begin();
--ROLLBACK TRANSACTION;

#Example 4

This example contains multiple fail assertions. The test fails if the database was not vacuumed and analyzed in the last 24 hours.

DROP FUNCTION IF EXISTS unit_tests.assert_fail_example4();

CREATE FUNCTION unit_tests.assert_fail_example4()
RETURNS test_result
AS
$$
	DECLARE message test_result;
BEGIN

	IF NOT EXISTS(SELECT MAX(last_vacuum) FROM pg_stat_user_tables HAVING MAX(last_vacuum) > NOW() - interval '24 hours') THEN
		SELECT assert.fail('Vacuum was not run since last 24 hours.') INTO message;
		RETURN message;
	END IF;

	IF NOT EXISTS(SELECT MAX(last_analyze) FROM pg_stat_user_tables HAVING MAX(last_analyze) > NOW() - interval '24 hours') THEN
		SELECT assert.fail('Analyze was not run since last 24 hours.') INTO message;
		RETURN message;
	END IF;

	SELECT assert.ok('End of test.') INTO message;  
	RETURN message; 
END
$$
LANGUAGE plpgsql;

/*Use transaction if your test contains DML query.*/
--BEGIN TRANSACTION;
SELECT * FROM unit_tests.begin();
--ROLLBACK TRANSACTION;
Clone this wiki locally