/*
 * Test constraints and foreign keys on verse.resource_providers
 */
BEGIN;
	
	/* We need a pair of resource definitions and a pair of planets. */
	\i utils/strings.sql
	\i utils/resources.sql
	\i utils/accounts.sql
	\i utils/naming.sql
	\i utils/universe.sql
	SELECT _create_natural_resources( 2 , 'testResource' );
	SELECT _create_raw_planets( 2 , 'testPlanet' );
	
	
	/****** TESTS BEGIN HERE ******/
	SELECT plan( 22 );

	
	SELECT diag_test_name( 'Valid resource provider' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			0.5 , 0.5
		);
	SELECT lives_ok( '_test_this' );

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

	SELECT diag_test_name( 'Resource provider with same planet but different type' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource2' ) ,
			100 , 50 ,
			0.5 , 0.5
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Resource provider with same type but different planet' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet2' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			0.5 , 0.5
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM verse.resource_providers;


	SELECT diag_test_name( 'Resource provider with NULL planet identifier' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			NULL , _get_string( 'testResource1' ) ,
			100 , 50 ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;
	
	SELECT diag_test_name( 'Resource provider with invalid planet identifier' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_bad_map_name( ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23503 );
	DEALLOCATE ALL;

	
	SELECT diag_test_name( 'Resource provider with NULL resource identifier' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery

		) VALUES (
			_get_map_name( 'testPlanet1' ) , NULL ,
			100 , 50 ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;
	
	SELECT diag_test_name( 'Resource provider with invalid resource identifier' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_bad_string( ) ,
			100 , 50 ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23503 );
	DEALLOCATE ALL;


	SELECT diag_test_name( 'Resource provider with NULL maximal quantity' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery

		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			NULL , 50 ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Resource provider with maximal quantity <= 0' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			0 , 0 ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;


	SELECT diag_test_name( 'Resource provider with NULL quantity' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , NULL ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Resource provider with quantity < 0' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , -1 ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Resource provider with quantity > max. quantity' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 101 ,
			0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;


	SELECT diag_test_name( 'Resource provider with NULL difficulty' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			NULL , 0.5
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Resource provider with difficulty < 0' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			-0.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Resource provider with difficulty > 1' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			1.5 , 0.5
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;
	
	SELECT diag_test_name( 'Resource provider with difficulty = 0' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			0 , 0.5
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM verse.resource_providers;

	SELECT diag_test_name( 'Resource provider with difficulty = 1' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			1 , 0.5
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM verse.resource_providers;


	SELECT diag_test_name( 'Resource provider with NULL recovery' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			0.5 , NULL
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Resource provider with recovery = 0' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			0.5 , 0
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Resource provider with recovery > 1' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			0.5 , 1.5
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	SELECT diag_test_name( 'Resource provider with recovery = 1' );
	PREPARE _test_this AS
		INSERT INTO verse.resource_providers(
				planet_id , resource_name_id ,
				resprov_quantity_max , resprov_quantity ,
				resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			100 , 50 ,
			0.5 , 1
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM verse.resource_providers;


	SELECT * FROM finish( );
ROLLBACK;