/*
 * Test the verse.create_resource_provider( ) function
 */
BEGIN;

	/* Before any actual testing, we need to drop FK constraints on the RP
	 * table and create a table which will contain the results of the
	 * function.
	 */

	ALTER TABLE verse.resource_providers
		DROP CONSTRAINT fk_resprov_planet ,
		DROP CONSTRAINT fk_resprov_resource;

	CREATE TEMPORARY TABLE test_result(
			_providers_left	INT ,
			_tot_quantity	DOUBLE PRECISION ,
			_tot_difficulty	DOUBLE PRECISION ,
			_tot_recovery	DOUBLE PRECISION
	) ON COMMIT DROP;

	/* Now we call the function using a crafted resource statistics row which
	 * will make it easy to check the results.
	 */
	INSERT INTO test_result
		SELECT * FROM verse.create_resource_provider( 12 ,
			ROW( 34 , 0.0 , 0.0 , 0.0 , 0.0 , 5.0 , 0.0 , 0.0 , 0.75 , 0.0 , 0.0 , 0.5 , 0 ) ,
			2 , 10 , 1.5 , 1 );


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


	SELECT diag_test_name( 'verse.create_resource_provider( ) - Output exists' );
	SELECT is( COUNT(*)::INT , 1 ) FROM test_result;
	SELECT diag_test_name( 'verse.create_resource_provider( ) - _providers_left updated' );
	SELECT is( _providers_left , 1 ) FROM test_result;
	SELECT diag_test_name( 'verse.create_resource_provider( ) - _tot_quantity updated' );
	SELECT is( _tot_quantity::NUMERIC , 5.0 ) FROM test_result;
	SELECT diag_test_name( 'verse.create_resource_provider( ) - _tot_difficulty updated' );
	SELECT is( _tot_difficulty::NUMERIC , 0.75 ) FROM test_result;
	SELECT diag_test_name( 'verse.create_resource_provider( ) - _tot_recovery updated' );
	SELECT is( _tot_recovery::NUMERIC , 0.5 ) FROM test_result;

	SELECT diag_test_name( 'verse.create_resource_provider( ) - Resource provider exists' );
	SELECT is( COUNT(*)::INT , 1 ) FROM verse.resource_providers;
	SELECT diag_test_name( 'verse.create_resource_provider( ) - Resource provider primary key' );
	SELECT is( COUNT(*)::INT , 1 ) FROM verse.resource_providers
		WHERE planet_id = 12 AND resource_name_id = 34;
	SELECT diag_test_name( 'verse.create_resource_provider( ) - Resource provider is full' );
	SELECT is( COUNT(*)::INT , 1 ) FROM verse.resource_providers
		WHERE resprov_quantity = resprov_quantity_max;
	SELECT diag_test_name( 'verse.create_resource_provider( ) - Resource provider maximal quantity' );
	SELECT is( resprov_quantity_max::NUMERIC , 5.0 ) FROM verse.resource_providers;
	SELECT diag_test_name( 'verse.create_resource_provider( ) - Resource provider extraction difficulty' );
	SELECT is( resprov_difficulty::NUMERIC , 0.75 ) FROM verse.resource_providers;
	SELECT diag_test_name( 'verse.create_resource_provider( ) - Resource provider recovery rate' );
	SELECT is( resprov_recovery::NUMERIC , 0.5 ) FROM verse.resource_providers;

	SELECT * FROM finish( );
ROLLBACK;