/*
 * Tests for emp.planet_resources_view
 */
BEGIN;
	/*
	 * Create two empires, one with 2 planets, the other without. Add 2 planet
	 * resources records.
	 * 
	 * Disable all foreign keys to avoid a lot of work.
	 */

	ALTER TABLE emp.planets
		DROP CONSTRAINT fk_eplanets_empire ,
		DROP CONSTRAINT fk_eplanets_planet;
	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 , 1 , 2 ) , ( 1 , 2 , 3 , 4 ) ,
			( 2 , 1 , 3 , 4 ) , ( 2 , 2 , 5 , 6 ) ,
			( 3 , 1 , 0.1 / 720 , 0.4 / 720 ) ,
			( 3 , 2 , 0.9 / 720, 0.9 / 720 );
	INSERT INTO emp.planets( empire_id , planet_id )
		VALUES ( 1 , 1 ) , ( 1 , 2 ) , ( 2 , 3 );


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

	SELECT diag_test_name( 'emp.planet_resources_view - Sums' );
	SELECT set_eq(
		$$ SELECT * FROM emp.planet_resources_view WHERE empire_id = 1 $$ ,
		$$ VALUES ( 1 , 1 , 4 * 720 , 6 * 720 ) , ( 1 , 2 , 8 * 720 , 10 * 720 ) $$
	);

	SELECT diag_test_name( 'emp.planet_resources_view - Incomes are rounded down' );
	SELECT set_eq(
		$$ SELECT resource_name_id , planets_income
			FROM emp.planet_resources_view
			WHERE empire_id = 2 $$ ,
		$$ VALUES ( 1 , 0 ) , ( 2 , 0 ) $$
	);

	SELECT diag_test_name( 'emp.planet_resources_view - Upkeeps are rounded up' );
	SELECT set_eq(
		$$ SELECT resource_name_id , planets_upkeep
			FROM emp.planet_resources_view
			WHERE empire_id = 2 $$ ,
		$$ VALUES ( 1 , 1 ) , ( 2 , 1 ) $$
	);

	SELECT * FROM finish( );
ROLLBACK;