/*
 * Unit tests for emp.resprio_update_start()
 */
BEGIN;
	\i utils/strings.sql
	\i utils/accounts.sql
	\i utils/naming.sql

	/* Create two empires */
	SELECT _create_emp_names( 2 , 'emp' );
	INSERT INTO emp.empires ( name_id , cash )
		SELECT id , 200.0 FROM naming.empire_names;

	/* Create 3 technologies after disabling unused fields */
	ALTER TABLE defs.technologies
		ALTER technology_category_id DROP NOT NULL ,
		ALTER technology_discovery_id DROP NOT NULL ,
		ALTER technology_description_id DROP NOT NULL ,
		ALTER technology_price DROP NOT NULL ,
		ALTER technology_points DROP NOT NULL;
	SELECT _create_test_strings( 3 , 'tech' );
	INSERT INTO defs.technologies ( technology_name_id )
		VALUES ( _get_string( 'tech1' ) ) ,
			( _get_string( 'tech2' ) ) ,
			( _get_string( 'tech3' ) );

	/* Replace identifier function with something easier to check */
	CREATE OR REPLACE FUNCTION emp.technology_make_identifier(
				_empire INT , _technology TEXT , _visible BOOLEAN )
		RETURNS TEXT
		LANGUAGE SQL
		STRICT IMMUTABLE
		SECURITY DEFINER
	AS $technology_make_identifier$
		SELECT $1::TEXT || ',' || $2 || ',' || $3::TEXT;
	$technology_make_identifier$;

	/* Replace the visibility view with plain SELECT's from a table.
	 */
	CREATE TABLE _fake_visibility(
		empire_id			INT ,
		technology_name_id	INT ,
		emptech_visible		BOOLEAN
	);
	CREATE OR REPLACE VIEW emp.technology_visibility_view
		AS SELECT * FROM _fake_visibility;

	/* Insert empire state and data for fake views */
	INSERT INTO emp.technologies_v2 (
			empire_id , technology_name_id ,
			emptech_state , emptech_points , emptech_priority
		) VALUES (
			_get_emp_name( 'emp1' ) , _get_string( 'tech1' ) ,
			'KNOWN' , NULL , NULL
		) , (
			_get_emp_name( 'emp1' ) , _get_string( 'tech2' ) ,
			'RESEARCH' , 123 , 0
		) , (
			_get_emp_name( 'emp1' ) , _get_string( 'tech3' ) ,
			'RESEARCH' , 123 , 1
		);
	INSERT INTO _fake_visibility VALUES(
		_get_emp_name( 'emp1' ) , _get_string( 'tech1' ) , TRUE 
	) , (
		_get_emp_name( 'emp1' ) , _get_string( 'tech2' ) , TRUE 
	) , (
		_get_emp_name( 'emp1' ) , _get_string( 'tech3' ) , FALSE 
	);
	
	-- ***** TESTS BEGIN HERE *****
	SELECT plan( 9 );

	SELECT diag_test_name( 'emp.resprio_update_start() - Invalid empire - Return value' );
	SELECT ok( NOT emp.resprio_update_start( _get_bad_emp_name() ) );
	SELECT diag_test_name( 'emp.resprio_update_start() - Invalid empire - Temporary table exists' );
	SELECT has_table( 'rprio_update' );
	SELECT diag_test_name( 'emp.resprio_update_start() - Invalid empire - Temporary table is empty' );
	SELECT is_empty( $$ SELECT * FROM rprio_update $$ );
	DROP TABLE IF EXISTS rprio_update;

	SELECT diag_test_name( 'emp.resprio_update_start() - Empire with no research - Return value' );
	SELECT ok( NOT emp.resprio_update_start( _get_emp_name( 'emp2' ) ) );
	SELECT diag_test_name( 'emp.resprio_update_start() - Empire with no research - Temporary table exists' );
	SELECT has_table( 'rprio_update' );
	SELECT diag_test_name( 'emp.resprio_update_start() - Empire with no research - Temporary table is empty' );
	SELECT is_empty( $$ SELECT * FROM rprio_update $$ );
	DROP TABLE IF EXISTS rprio_update;

	SELECT diag_test_name( 'emp.resprio_update_start() - Empire with in-progress research - Return value' );
	SELECT ok( emp.resprio_update_start( _get_emp_name( 'emp1' ) ) );
	SELECT diag_test_name( 'emp.resprio_update_start() - Empire with in-progress research - Temporary table exists' );
	SELECT has_table( 'rprio_update' );
	SELECT diag_test_name( 'emp.resprio_update_start() - Empire with in-progress research - Temporary table contents' );
	SELECT set_eq( $$
		SELECT _empire_id , _technology_name_id , _emptech_id , _emptech_priority
			FROM rprio_update
	$$ , $$ VALUES(
		_get_emp_name( 'emp1' ) , _get_string( 'tech2' ) , _get_emp_name( 'emp1' ) || ',tech2,true' , 0
	) , (
		_get_emp_name( 'emp1' ) , _get_string( 'tech3' ) , _get_emp_name( 'emp1' ) || ',tech3,false' , 1
	) $$ );
	DROP TABLE IF EXISTS rprio_update;

	SELECT * FROM finish( );
ROLLBACK;