/*
 * Test the sys.process_planet_mining_updates() function
 */
BEGIN;
	/*
	 * Create a fake planet resource record which will be updated by the
	 * function (dropping the foreign key is therefore needed).
	 */
	ALTER TABLE verse.planet_resources
		DROP CONSTRAINT fk_pres_planet ,
		DROP CONSTRAINT fk_pres_resource;

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

	/*
	 * Create a table which contains the values which will be returned by
	 * the fake sys.gu_pmc_get_data( ).
	 */
	CREATE TABLE _fake_update_data OF sys.gu_pmc_data_type;
	INSERT INTO _fake_update_data VALUES (
		1 , 1 , 1000.0 , 1000.0 , 0.5 , NULL , NULL , NULL , NULL ) ,
		( 2 , 1 , 1000.0 , 1000.0 , 0.5 , 1 , 0.5 , 1.0 , 1.0 );
	CREATE OR REPLACE FUNCTION sys.gu_pmc_get_data( _tick BIGINT )
		RETURNS SETOF sys.gu_pmc_data_type
	AS $$
		SELECT * FROM _fake_update_data;
	$$ LANGUAGE SQL;

	/*
	 * Replace sys.gu_pmc_update_resource() so it deletes records from the
	 * table.
	 */
	CREATE OR REPLACE FUNCTION sys.gu_pmc_update_resource( _input sys.gu_pmc_data_type )
		RETURNS VOID
	AS $$
		DELETE FROM _fake_update_data WHERE planet = $1.planet AND resource = $1.resource;
	$$ LANGUAGE SQL;
	

	/***** TESTS BEGIN HERE *****/
	SELECT no_plan( );
	
	SELECT sys.process_planet_mining_updates( 0 );

	SELECT diag_test_name( 'sys.process_planet_mining_updates() - Neutral planets updated' );
	SELECT is( pres_income , 0::DOUBLE PRECISION ) FROM verse.planet_resources;

	SELECT diag_test_name( 'sys.process_planet_mining_updates() - Owned planets updated' );
	SELECT is_empty(
		$$ SELECT * FROM _fake_update_data WHERE planet = 2 $$
	);

	SELECT * FROM finish( );
ROLLBACK;