/*
 * Test the emp.mining_settings_view view
 */
BEGIN;
	/* Create 1 natural resource, 3 planets and 2 empire names */
	\i utils/strings.sql
	\i utils/resources.sql
	\i utils/accounts.sql
	\i utils/naming.sql
	\i utils/universe.sql
	SELECT _create_natural_resources( 1 , 'testResource' );
	SELECT _create_raw_planets( 3 , 'testPlanet' );
	SELECT _create_emp_names( 2 , 'testEmp' );

	/*
	 * Create an empire which possesses a planet, but has no planet-specific
	 * mining settings. Modify the default empire-wide settings so we can
	 * check the results.
	 * 
	 * The planet only has a resource provider for one type of resource.
	 */
	SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet1' ) , 1 );
	UPDATE emp.mining_settings
		SET empmset_weight = 3
		WHERE empire_id = _get_emp_name( 'testEmp1' )
			AND resource_name_id = _get_string( 'testResource1' );
	INSERT INTO verse.resource_providers ( planet_id , resource_name_id ,
			resprov_quantity_max , resprov_quantity ,
			resprov_difficulty , resprov_recovery )
		VALUES (
			 _get_map_name( 'testPlanet1' ) ,  _get_string( 'testResource1' ) ,
			 100 , 50 ,
			 0.5 , 0.5
		);

	/*
	 * Add a resource provider to the second planet; create an empire which
	 * owns the planet and has planet-specific settings for the resource
	 * provider.
	 * 
	 * This empire also has settings for the testPlanet1 planet.
	 */
	INSERT INTO verse.resource_providers ( planet_id , resource_name_id ,
			resprov_quantity_max , resprov_quantity ,
			resprov_difficulty , resprov_recovery )
		VALUES (
			 _get_map_name( 'testPlanet2' ) ,  _get_string( 'testResource1' ) ,
			 100 , 50 ,
			 0.5 , 0.5
		);
	SELECT emp.create_empire( _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) , 1 );
	INSERT INTO emp.planet_mining_settings(
			empire_id , planet_id , resource_name_id , emppmset_weight
		) VALUES (
			_get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) ,
			_get_string( 'testResource1' ) , 0
		) , (
			_get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet1' ) ,
			_get_string( 'testResource1' ) , 0
		);

	/*
	 * While the last planet is not owned by any empire, make sure it has a
	 * resource provider.
	 */
	INSERT INTO verse.resource_providers ( planet_id , resource_name_id ,
			resprov_quantity_max , resprov_quantity ,
			resprov_difficulty , resprov_recovery )
		VALUES (
			 _get_map_name( 'testPlanet3' ) ,  _get_string( 'testResource1' ) ,
			 100 , 50 ,
			 0.5 , 0.5
		);


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

	SELECT diag_test_name( 'emp.mining_settings_view - Ignore neutral planets' );
	SELECT is( COUNT(*)::INT , 0 )
		FROM emp.mining_settings_view
		WHERE planet_id = _get_map_name( 'testPlanet3' );

	SELECT diag_test_name( 'emp.mining_settings_view - Only display provided resources' );
	SELECT is( COUNT(*)::INT , 1 )
		FROM emp.mining_settings_view
		WHERE planet_id = _get_map_name( 'testPlanet1' );
	SELECT diag_test_name( 'emp.mining_settings_view - Settings from empire-wide table' );
	SELECT ok( NOT mset_specific )
		FROM emp.mining_settings_view
		WHERE planet_id = _get_map_name( 'testPlanet1' );
	SELECT diag_test_name( 'emp.mining_settings_view - Value from empire-wide table' );
	SELECT is( mset_weight , 3 )
		FROM emp.mining_settings_view
		WHERE planet_id = _get_map_name( 'testPlanet1' );
		
	SELECT diag_test_name( 'emp.mining_settings_view - Settings from planet-specific table' );
	SELECT ok( mset_specific )
		FROM emp.mining_settings_view
		WHERE planet_id = _get_map_name( 'testPlanet2' );
	SELECT diag_test_name( 'emp.mining_settings_view - Value from planet-specific table' );
	SELECT is( mset_weight , 0 )
		FROM emp.mining_settings_view
		WHERE planet_id = _get_map_name( 'testPlanet2' );

	SELECT * FROM finish( );
ROLLBACK;