/*
 * Test constraints and foreign keys on verse.planet_resources
 */
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( 16 );
	
	SELECT diag_test_name( 'verse.planet_resources - Valid record' );
	SELECT lives_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id , pres_income , pres_upkeep
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			0 , 0
		); $$
	);
	DELETE FROM verse.planet_resources;

	SELECT diag_test_name( 'verse.planet_resources - Inserting with default values' );
	SELECT lives_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' )
		); $$
	);
	SELECT diag_test_name( 'verse.planet_resources - Default income = 0' );
	SELECT is( pres_income , 0::DOUBLE PRECISION )
		FROM verse.planet_resources;
	SELECT diag_test_name( 'verse.planet_resources - Default upkeep = 0' );
	SELECT is( pres_upkeep , 0::DOUBLE PRECISION )
		FROM verse.planet_resources;
	
	SELECT diag_test_name( 'verse.planet_resources - Duplicate record' );
	SELECT throws_ok($$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' )
		); $$ , 23505 );
	
	SELECT diag_test_name( 'verse.planet_resources - Different planet, same resource' );
	SELECT lives_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id
		) VALUES (
			_get_map_name( 'testPlanet2' ) , _get_string( 'testResource1' )
		); $$
	);
	
	SELECT diag_test_name( 'verse.planet_resources - Different resource, same planet' );
	SELECT lives_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource2' )
		); $$
	);

	SELECT diag_test_name( 'verse.planet_resources - Different planet and resource' );
	SELECT lives_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id
		) VALUES (
			_get_map_name( 'testPlanet2' ) , _get_string( 'testResource2' )
		); $$
	);
	DELETE FROM verse.planet_resources;

	SELECT diag_test_name( 'verse.planet_resources - NULL planet identifier' );
	SELECT throws_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id
		) VALUES (
			NULL , _get_string( 'testResource1' )
		); $$ , 23502
	);

	SELECT diag_test_name( 'verse.planet_resources - NULL resource identifier' );
	SELECT throws_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id
		) VALUES (
			_get_map_name( 'testPlanet1' ) , NULL
		); $$ , 23502
	);

	SELECT diag_test_name( 'verse.planet_resources - NULL income' );
	SELECT throws_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id , pres_income
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			NULL
		); $$ , 23502
	);

	SELECT diag_test_name( 'verse.planet_resources - NULL upkeep' );
	SELECT throws_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id , pres_upkeep
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			NULL
		); $$ , 23502
	);

	SELECT diag_test_name( 'verse.planet_resources - Invalid planet identifier' );
	SELECT throws_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id
		) VALUES (
			_get_bad_map_name( ) , _get_string( 'testResource1' )
		); $$ , 23503
	);

	SELECT diag_test_name( 'verse.planet_resources - Invalid resource identifier' );
	SELECT throws_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_bad_string( )
		); $$ , 23503
	);

	SELECT diag_test_name( 'verse.planet_resources - Income < 0' );
	SELECT throws_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id , pres_income
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			-1
		); $$ , 23514
	);

	SELECT diag_test_name( 'verse.planet_resources - Upkeep > 0' );
	SELECT throws_ok( $$
		INSERT INTO verse.planet_resources(
			planet_id , resource_name_id , pres_upkeep
		) VALUES (
			_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
			-1
		); $$ , 23514
	);

	SELECT * FROM finish( );
ROLLBACK;