/*
 * Tests for the emp.get_planet_resources() function
 */
BEGIN;
	/*
	 * We need two planets, one being owned by some empire, the other being
	 * neutral. Both planets' resource records must exist. Both planets will
	 * also include resource providers which will serve as tests for the
	 * various rounding which takes place.
	 * 
	 * To avoid having to define actual natural resources, we disable the
	 * foreign keys on resource providers and mining settings. We can't do
	 * that for the empire, tho: we need an actual account as the translations
	 * must be looked up.
	 */

	\i utils/strings.sql
	SELECT _create_test_strings( 3 , 'resource' , 'Resource name ' );
	SELECT _create_test_strings( 3 , 'rDesc' , 'Resource description ' );
	SELECT _create_test_strings( 1 , 'rCat' , 'Resource category ' );
	
	INSERT INTO defs.resources (
			resource_name_id , resource_description_id ,
			resource_category_id , resource_weight
		) VALUES (
			_get_string( 'resource1' ) , _get_string( 'rDesc1' ) ,
			_get_string( 'rCat1' ) , 2
		) , (
			_get_string( 'resource2' ) , _get_string( 'rDesc2' ) ,
			NULL , 1
		) , (
			_get_string( 'resource3' ) , _get_string( 'rDesc3' ) ,
			NULL , 3
		);

	\i utils/accounts.sql
	\i utils/naming.sql
	SELECT _create_emp_names( 1 , 'emp' );
	INSERT INTO emp.empires( name_id , cash )
		VALUES( _get_emp_name( 'emp1' ) , 0 );

	ALTER TABLE emp.mining_settings DROP CONSTRAINT fk_empmset_resource;
	INSERT INTO emp.mining_settings ( empire_id , resource_name_id )
		SELECT _get_emp_name( 'emp1' ) , resource_name_id
			FROM defs.resources;

	\i utils/universe.sql
	SELECT _create_raw_planets( 2 , 'planet' );
	
	INSERT INTO verse.planet_resources (
			planet_id , resource_name_id , pres_income , pres_upkeep
		) VALUES (
			_get_map_name( 'planet1' ) , _get_string( 'resource1' ) ,
			99.4 / 720.0 , 99.4 / 720.0
		) , (
			_get_map_name( 'planet1' ) , _get_string( 'resource2' ) ,
			99.5 / 720.0 , 99.5 / 720.0
		) , (
			_get_map_name( 'planet1' ) , _get_string( 'resource3' ) ,
			99.6 / 720.0 , 99.6 / 720.0
		);
	INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
		SELECT _get_map_name( 'planet2' ) , resource_name_id
			FROM defs.resources;

	ALTER TABLE verse.resource_providers DROP CONSTRAINT fk_resprov_resource;
	INSERT INTO verse.resource_providers(
			planet_id , resource_name_id , resprov_quantity_max ,
			resprov_quantity , resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'planet1' ) , _get_string( 'resource1' ) , 99.4 ,
			99.4 , 0.494 , 0.5
		) ,  (
			_get_map_name( 'planet1' ) , _get_string( 'resource2' ) , 99.5 ,
			99.5 , 0.495 , 0.5
		) ,  (
			_get_map_name( 'planet2' ) , _get_string( 'resource1' ) , 100 ,
			100 , 0.5 , 0.5
		);

	INSERT INTO emp.planets ( empire_id , planet_id )
		VALUES ( _get_emp_name( 'emp1' ) , _get_map_name( 'planet1' ) );


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

	SELECT diag_test_name( 'emp.get_planet_resources() - No results on missing planets' );
	SELECT is_empty( $$ SELECT * FROM emp.get_planet_resources( _get_bad_map_name( ) ) $$ );

	SELECT diag_test_name( 'emp.get_planet_resources() - No results on neutral planets' );
	SELECT is_empty( $$ SELECT * FROM emp.get_planet_resources( _get_map_name( 'planet2' ) ) $$ );
	
	SELECT diag_test_name( 'emp.get_planet_resources() - One row per resource type' );
	SELECT is( COUNT(*)::INT , 3 ) FROM emp.get_planet_resources( _get_map_name( 'planet1' ) );
	
	SELECT diag_test_name( 'emp.get_planet_resources() - Row ordering' );
	SELECT set_eq(
		$$ SELECT resource_identifier , row_number() OVER ( )
			FROM emp.get_planet_resources( _get_map_name( 'planet1' ) ) $$ ,
		$$ VALUES (
			'resource1' , 2
		) , (
			'resource2' , 1 
		) , (
			'resource3' , 3 
		) $$
	);
	
	SELECT diag_test_name( 'emp.get_planet_resources() - Name translation' );
	SELECT is_empty( $$
		SELECT *
			FROM emp.get_planet_resources( _get_map_name( 'planet1' ) )
			WHERE resource_name NOT LIKE 'Resource name %'
	$$ );
	
	SELECT diag_test_name( 'emp.get_planet_resources() - Description translation' );
	SELECT is_empty( $$
		SELECT *
			FROM emp.get_planet_resources( _get_map_name( 'planet1' ) )
			WHERE resource_description NOT LIKE 'Resource description %'
	$$ );
	
	SELECT diag_test_name( 'emp.get_planet_resources() - Category translation' );
	SELECT is_empty( $$
		SELECT *
			FROM emp.get_planet_resources( _get_map_name( 'planet1' ) )
			WHERE resource_identifier = 'resource1'
				AND resource_category NOT LIKE 'Resource category %'
	$$ );
	
	SELECT diag_test_name( 'emp.get_planet_resources() - NULL category -> NULL translation' );
	SELECT is_empty( $$
		SELECT *
			FROM emp.get_planet_resources( _get_map_name( 'planet1' ) )
			WHERE resource_identifier <> 'resource1'
				AND resource_category IS NOT NULL
	$$ );
	
	SELECT diag_test_name( 'emp.get_planet_resources() - Upkeep is valid and rounded up' );
	SELECT is_empty( $$
		SELECT *
			FROM emp.get_planet_resources( _get_map_name( 'planet1' ) )
			WHERE pres_upkeep IS NULL OR pres_upkeep <> 100
	$$ );
	
	SELECT diag_test_name( 'emp.get_planet_resources() - Income is valid and rounded down' );
	SELECT is_empty( $$
		SELECT * FROM emp.get_planet_resources( _get_map_name( 'planet1' ) )
			WHERE pres_income IS NULL OR pres_income <> 99
	$$ );
	
	SELECT diag_test_name( 'emp.get_planet_resources() - No mining-related fields when there is no resource provider' );
	SELECT is_empty( $$
		SELECT * FROM emp.get_planet_resources( _get_map_name( 'planet1' ) )
			WHERE resource_identifier = 'resource3' AND NOT (
					resprov_capacity IS NULL
					AND resprov_quantity IS NULL 
					AND resprov_difficulty IS NULL
					AND mset_weight IS NULL
				);
	$$ );
	
	SELECT diag_test_name( 'emp.get_planet_resources() - Resource provider fields are present' );
	SELECT is_empty( $$
		SELECT * FROM emp.get_planet_resources( _get_map_name( 'planet1' ) )
			WHERE resource_identifier <> 'resource3' AND (
					resprov_capacity IS NULL
					OR resprov_quantity IS NULL 
					OR resprov_difficulty IS NULL
					OR mset_weight IS NULL
				);
	$$ );

	SELECT diag_test_name( 'emp.get_planet_resources() - Resource provider values' );
	SELECT set_eq( $$
		SELECT resource_identifier , resprov_capacity , resprov_quantity , resprov_difficulty
			FROM emp.get_planet_resources( _get_map_name( 'planet1' ) )
			WHERE resource_identifier <> 'resource3'
	$$ , $$ VALUES (
		'resource1' , 99 , 99 , 49
	) , (
		'resource2' , 100 , 100 , 50 
	) $$ );

	SELECT *  FROM finish( );
ROLLBACK;