/*
 * Test the defs.uoc_natural_resource() functions
 */
BEGIN;
	
	/* We need a few strings to be used when creating resource definitions, and a basic resource. */
	\i utils/strings.sql
	\i utils/resources.sql
	SELECT _create_test_strings( 7 );
	SELECT _create_resources( 1 , 'basicRes' );

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


	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL name (no category)' );
	SELECT is( defs.uoc_natural_resource( NULL , 'test4' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL description (no category)' );
	SELECT is( defs.uoc_natural_resource( 'test1' , NULL , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL weight (no category)' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , NULL ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL presence (no category)' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 ,
		NULL , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average quantity (no category)' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 ,
		0.5 , NULL , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL quantity deviation (no category)' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 ,
		0.5 , 100 , NULL , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average difficulty (no category)' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 ,
		0.5 , 100 , 50 , NULL , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL difficulty deviation (no category)' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 ,
		0.5 , 100 , 50 , 0.5 , NULL , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average recovery (no category)' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , NULL , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL recovery deviation (no category)' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , NULL ) , NULL );

	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL name' );
	SELECT is( defs.uoc_natural_resource( NULL , 'test4' , 'test5' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL description' );
	SELECT is( defs.uoc_natural_resource( 'test1' , NULL , 'test5' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL category' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , NULL , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL weight' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , NULL ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL presence' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 ,
		NULL , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average quantity' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 ,
		0.5 , NULL , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL quantity deviation' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 ,
		0.5 , 100 , NULL , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average difficulty' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 ,
		0.5 , 100 , 50 , NULL , 0.05 , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL difficulty deviation' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 ,
		0.5 , 100 , 50 , 0.5 , NULL , 0.5 , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average recovery' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , NULL , 0.05 ) , NULL );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL recovery deviation' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , NULL ) , NULL );


	SELECT diag_test_name( 'defs.uoc_natural_resource() - creation without category' );
	SELECT is( defs.uoc_natural_resource( 'test3' , 'test4' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'CREATED' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - creation results without category - basic' );
	SELECT results_eq(
		$$ SELECT resource_name_id , resource_description_id , resource_weight
				FROM defs.resources
				WHERE resource_name_id = _get_string( 'test3' )
					AND resource_category_id IS NULL $$ ,
		$$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , 1 ) $$
	);
	SELECT diag_test_name( 'defs.uoc_natural_resource() - creation results without category - nat. res.' );
	SELECT results_eq(
		$$ SELECT * FROM defs.natural_resources
				WHERE resource_name_id = _get_string( 'test3' ) $$ ,
		$$ VALUES ( _get_string( 'test3' ) , 0.5::DOUBLE PRECISION , 100::DOUBLE PRECISION ,
			50::DOUBLE PRECISION , 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ,
			0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ) $$
	);

	SELECT diag_test_name( 'defs.uoc_natural_resource() - creation with category' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test6' , 'test7' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'CREATED' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - creation results with category - basic' );
	SELECT results_eq(
		$$ SELECT * FROM defs.resources
				WHERE resource_name_id = _get_string( 'test5' ) $$ ,
		$$ VALUES ( _get_string( 'test5' ) , _get_string( 'test6' ) , _get_string( 'test7' ) , 1 ) $$
	);
	SELECT diag_test_name( 'defs.uoc_natural_resource() - creation results with category - nat. res.' );
	SELECT results_eq(
		$$ SELECT * FROM defs.natural_resources
				WHERE resource_name_id = _get_string( 'test5' ) $$ ,
		$$ VALUES ( _get_string( 'test5' ) , 0.5::DOUBLE PRECISION , 100::DOUBLE PRECISION ,
			50::DOUBLE PRECISION , 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ,
			0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ) $$
	);

	SELECT diag_test_name( 'defs.uoc_natural_resource() - update without category' );
	SELECT is( defs.uoc_natural_resource( 'test3' , 'test7' , 2 ,
		0.3 , 300 , 30 , 0.3 , 0.03 , 0.3 , 0.03 ) , 'UPDATED' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - update results without category - basic' );
	SELECT results_eq(
		$$ SELECT resource_name_id , resource_description_id , resource_weight
				FROM defs.resources
				WHERE resource_name_id = _get_string( 'test3' )
					AND resource_category_id IS NULL $$ ,
		$$ VALUES ( _get_string( 'test3' ) , _get_string( 'test7' ) , 2 ) $$
	);
	SELECT diag_test_name( 'defs.uoc_natural_resource() - update results without category - nat. res.' );
	SELECT results_eq(
		$$ SELECT * FROM defs.natural_resources
				WHERE resource_name_id = _get_string( 'test3' ) $$ ,
		$$ VALUES ( _get_string( 'test3' ) , 0.3::DOUBLE PRECISION , 300::DOUBLE PRECISION ,
			30::DOUBLE PRECISION , 0.3::DOUBLE PRECISION , 0.03::DOUBLE PRECISION ,
			0.3::DOUBLE PRECISION , 0.03::DOUBLE PRECISION ) $$
	);

	SELECT diag_test_name( 'defs.uoc_natural_resource() - update with category' );
	SELECT is( defs.uoc_natural_resource( 'test3' , 'test4' , 'test7' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'UPDATED' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - update results with category' );
	SELECT results_eq(
		$$ SELECT * FROM defs.resources
				WHERE resource_name_id = _get_string( 'test3' ) $$ ,
		$$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , _get_string( 'test7' ) , 1 ) $$
	);
	SELECT diag_test_name( 'defs.uoc_natural_resource() - update results with category - nat. res.' );
	SELECT results_eq(
		$$ SELECT * FROM defs.natural_resources
				WHERE resource_name_id = _get_string( 'test3' ) $$ ,
		$$ VALUES ( _get_string( 'test3' ) , 0.5::DOUBLE PRECISION , 100::DOUBLE PRECISION ,
			50::DOUBLE PRECISION , 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ,
			0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ) $$
	);

	SELECT diag_test_name( 'defs.uoc_natural_resource() - incorrect name string' );
	SELECT is( defs.uoc_natural_resource( 'does-not-exist' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_STRINGS' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - incorrect description string' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'does-not-exist' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_STRINGS' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - incorrect category string' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test2' , 'does-not-exist' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_STRINGS' );

	SELECT diag_test_name( 'defs.uoc_natural_resource() - duplicate description on new resource' );
	SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'DUP_DESCR' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - duplicate description on existing resource' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test4' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'DUP_DESCR' );

	SELECT diag_test_name( 'defs.uoc_natural_resource() - update on basic resource' );
	SELECT is( defs.uoc_natural_resource( 'basicRes1' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_TYPE' );

	SELECT diag_test_name( 'defs.uoc_natural_resource() - weight <= 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 0 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' );

	SELECT diag_test_name( 'defs.uoc_natural_resource() - P(presence) <= 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - P(presence) >= 1' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' );

	SELECT diag_test_name( 'defs.uoc_natural_resource() - max. quantity <= 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 0 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - quantity dev. < 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , -1 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - quantity max. - dev. <= 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 100 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' );

	SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. = 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0 , 0 , 0.5 , 0.05 ) , 'UPDATED' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. < 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , -0.00001 , 0 , 0.5 , 0.05 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. = 1' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 1 , 0 , 0.5 , 0.05 ) , 'UPDATED' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. > 1' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 1.00001 , 0 , 0.5 , 0.05 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty dev. < 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , -1 , 0.5 , 0.05 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. - dev. < 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.25 , 0.5 , 0.5 , 0.05 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. + dev. > 1' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.75 , 0.5 , 0.5 , 0.05 ) , 'BAD_VALUE' );

	SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. <= 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0 , 0 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. = 1' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 1 , 0 ) , 'UPDATED' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. > 1' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 1.0001 , 0 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery dev. < 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , -0.25 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. - dev. <= 0' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.25 , 0.25 ) , 'BAD_VALUE' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. + dev. > 1' );
	SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.75 , 0.5 ) , 'BAD_VALUE' );

	/* Reset resources, create empire */
	DELETE FROM defs.natural_resources;
	DELETE FROM defs.resources;
	ALTER TABLE emp.empires
		DROP CONSTRAINT fk_empires_name;
	INSERT INTO emp.empires ( name_id , cash )
		VALUES ( 1 , 0 );

	SELECT diag_test_name( 'defs.uoc_natural_resource() - new resources are added to empires (no category)' );
	SELECT defs.uoc_natural_resource( 'test1' , 'test2' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 );
	SELECT is( COUNT(*)::INT , 1 )
		FROM emp.resources
		WHERE empire_id = 1
			AND resource_name_id = _get_string( 'test1' );
	SELECT diag_test_name( 'defs.uoc_natural_resource() - new resources are added to empires' );
	SELECT defs.uoc_natural_resource( 'test3' , 'test4' , 'test5' , 1 ,
		0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 );
	SELECT is( COUNT(*)::INT , 1 )
		FROM emp.resources
		WHERE empire_id = 1
			AND resource_name_id = _get_string( 'test3' );

	SELECT * FROM finish( );
ROLLBACK;