/*
 * Test constraints and foreign keys on defs.natural_resources
 */
BEGIN;
	
	/* We need a few strings to be used when creating resource definitions. */
	\i utils/strings.sql
	SELECT _create_test_strings( 4 );
	
	/*
	 * We need a a pair of resource definitions to be used when creating
	 * natural resources.
	 */
	INSERT INTO defs.resources (
		resource_name_id , resource_description_id , resource_weight
	) VALUES ( _get_string( 'test1' ) , _get_string( 'test2' ) , 1 );
	INSERT INTO defs.resources (
		resource_name_id , resource_description_id , resource_weight
	) VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , 2 );

	
	/****** TESTS BEGIN HERE ******/
	SELECT plan( 33 );


	SELECT diag_test_name( 'Valid natural resource definition' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources (
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT lives_ok( '_test_this' );

	SELECT diag_test_name( 'Duplicate natural resource definition' );
	SELECT throws_ok( '_test_this' , 23505 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Distinct natural resource definitions' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources (
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test3' ) , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.natural_resources;


	SELECT diag_test_name( 'Natural resource definition with missing resource definition' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test2' ) , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23503 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with NULL resource definition' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			NULL , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;

	
	SELECT diag_test_name( 'Natural resource definition with presence probability <= 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with presence probability >= 1' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 1 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with NULL presence probability' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , NULL , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;


	SELECT diag_test_name( 'Natural resource definition with avg. quantity <= 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 0 , 1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with NULL avg. quantity' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , NULL , 1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;



	SELECT diag_test_name( 'Natural resource definition with quantity dev. < 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , -1 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with NULL quantity dev.' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , NULL , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with quantity dev. = avg. quantity' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 100 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with quantity dev. = 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 0 , 0.5 , 0.05 , 0.5 , 0.05
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.natural_resources;



	SELECT diag_test_name( 'Natural resource definition with avg. difficulty = 1' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 1 , 0 , 0.5 , 0.05
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.natural_resources;

	SELECT diag_test_name( 'Natural resource definition with avg. difficulty = 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0 , 0 , 0.5 , 0.05
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.natural_resources;

	SELECT diag_test_name( 'Natural resource definition with avg. difficulty > 1' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 1.0001 , 0 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;


	SELECT diag_test_name( 'Natural resource definition with avg. difficulty < 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , -0.0001 , 0 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with NULL avg. difficulty' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , NULL , 0.05 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;



	SELECT diag_test_name( 'Natural resource definition with difficulty dev. < 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , -1 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with avg. difficulty - difficulty dev. < 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.25 , 0.5 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with difficulty dev. + avg. difficulty > 1' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.75 , 0.5 , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with NULL difficulty dev.' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , NULL , 0.5 , 0.05
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;


	SELECT diag_test_name( 'Natural resource definition with avg. recovery = 1' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
			1 , 0
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.natural_resources;

	SELECT diag_test_name( 'Natural resource definition with avg. recovery = 0+' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
			0.0001 , 0
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.natural_resources;

	SELECT diag_test_name( 'Natural resource definition with avg. recovery > 1' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
			1.1 , 0
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with avg. recovery <= 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
			0 , 0
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with NULL avg. recovery' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
			NULL , 0.05
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;


	SELECT diag_test_name( 'Natural resource definition with recovery dev. < 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
			0.5 , -1
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with avg.recovery - recovery dev. <= 0' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with avg.recovery - recovery dev. > 1' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
			0.75 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Natural resource definition with NULL recovery dev.' );
	PREPARE _test_this AS
		INSERT INTO defs.natural_resources(
			resource_name_id , natres_p_presence , natres_quantity_avg ,
			natres_quantity_dev , natres_difficulty_avg ,
			natres_difficulty_dev , natres_recovery_avg ,
			natres_recovery_dev
		) VALUES (
			_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
			0.5 , NULL
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;


	SELECT diag_test_name( 'Deletion of the base definition for a natural resource' );
	INSERT INTO defs.natural_resources(
		resource_name_id , natres_p_presence , natres_quantity_avg ,
		natres_quantity_dev , natres_difficulty_avg ,
		natres_difficulty_dev , natres_recovery_avg ,
		natres_recovery_dev
	) VALUES (
		_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
	);
	PREPARE _test_this AS
		DELETE FROM defs.resources WHERE resource_name_id = _get_string( 'test1' );
	SELECT throws_ok( '_test_this' , 23503 );

	SELECT * FROM finish( );
ROLLBACK;