/*
 * Test the sys.gu_pmc_update_resource() function
 */
BEGIN;
	/*
	 * We need to create a set of both resource providers and planet resource
	 * records that will be updated by the function when it is called. We
	 * disable foreign keys on both tables, as it makes things simpler. We
	 * also replace verse.get_raw_production(), verse.get_extraction_factor( )
	 * and verse.adjust_production() so that they return fixed values, and we
	 * need to set the game.resources.extraction constant.
	 */
	ALTER TABLE verse.resource_providers
		DROP CONSTRAINT fk_resprov_planet ,
		DROP CONSTRAINT fk_resprov_resource;
	ALTER TABLE verse.planet_resources
		DROP CONSTRAINT fk_pres_planet ,
		DROP CONSTRAINT fk_pres_resource;

	INSERT INTO verse.resource_providers(
			planet_id , resource_name_id , resprov_quantity_max, resprov_quantity ,
			resprov_difficulty , resprov_recovery
		) VALUES (
			1 , 1 , 1000 , 1000 , 0 , 0.5
		);

	INSERT INTO verse.planet_resources(
			planet_id , resource_name_id , pres_income , pres_upkeep
		) VALUES (
			1 , 1 , 0 , 0
		);

	CREATE OR REPLACE FUNCTION verse.get_raw_production( pid INT , pt building_output_type )
		RETURNS REAL
	AS $$
		SELECT 1.0::REAL;
	$$ LANGUAGE SQL;

	CREATE OR REPLACE FUNCTION verse.adjust_production( prod REAL , happiness REAL )
		RETURNS REAL
	AS $$
		SELECT 1.0::REAL;
	$$ LANGUAGE SQL;

	CREATE OR REPLACE FUNCTION verse.get_extraction_factor( _fill_ratio DOUBLE PRECISION , _difficulty DOUBLE PRECISION )
		RETURNS DOUBLE PRECISION
	AS $$
		SELECT ( CASE WHEN $1 = 1.0 AND $2 = 0 THEN 0.002 ELSE 0.0 END )::DOUBLE PRECISION;
	$$ LANGUAGE SQL;

	SELECT sys.uoc_constant( 'game.resources.extraction' , '(test)' , 'Resources' , 1440.0 );
	ALTER FUNCTION sys.get_constant( TEXT ) VOLATILE;


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

	SELECT sys.gu_pmc_update_resource( ROW(
		1 , 1 , 1000.0 , 1000.0 , 0.0 , NULL , 1.0 , 0.5 , 1.0
	) );
	
	SELECT diag_test_name( 'sys.gu_pmc_update_resource( ) - Provider udpated' );
	SELECT is( resprov_quantity , 999.0::DOUBLE PRECISION )
		FROM verse.resource_providers
		WHERE planet_id = 1 AND resource_name_id = 1;
	
	SELECT diag_test_name( 'sys.gu_pmc_update_resource( ) - Planet resources income udpated' );
	SELECT is( pres_income , 1.0::DOUBLE PRECISION )
		FROM verse.planet_resources
		WHERE planet_id = 1 AND resource_name_id = 1;

	UPDATE verse.resource_providers SET resprov_quantity = resprov_quantity_max;
	UPDATE sys.constant_definitions
		SET c_value = 14400000.0
		WHERE name = 'game.resources.extraction';
	SELECT sys.gu_pmc_update_resource( ROW(
		1 , 1 , 1000.0 , 1000.0 , 0.0 , NULL , 1.0 , 0.5 , 1.0
	) );

	SELECT diag_test_name( 'sys.gu_pmc_update_resource( ) - Bounded extraction quantity (1/2)' );
	SELECT is( resprov_quantity , 0.0::DOUBLE PRECISION )
		FROM verse.resource_providers
		WHERE planet_id = 1 AND resource_name_id = 1;

	SELECT diag_test_name( 'sys.gu_pmc_update_resource( ) - Bounded extraction quantity (2/2)' );
	SELECT is( pres_income , 1000.0::DOUBLE PRECISION )
		FROM verse.planet_resources
		WHERE planet_id = 1 AND resource_name_id = 1;

	SELECT * FROM finish( );
ROLLBACK;