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

	/* Create three empires (easier to use as keys) */
	SELECT _create_emp_names( 3 , 'emp' );
	INSERT INTO emp.empires ( name_id , cash )
		SELECT id , 200.0 FROM naming.empire_names;

	/* Create a technology after disabling unused fields */
	ALTER TABLE defs.technologies
		ALTER technology_discovery_id DROP NOT NULL;
	SELECT _create_test_strings( 1 , 'tech' );
	SELECT _create_test_strings( 1 , 'techCategory' );
	SELECT _create_test_strings( 1 , 'techDescription' );
	INSERT INTO defs.technologies (
			technology_name_id , technology_category_id , technology_description_id ,
			technology_price , technology_points
		) VALUES (
			_get_string( 'tech1' ) , _get_string( 'techCategory1' ) , _get_string( 'techDescription1' ) ,
			123 , 456
		);

	/* 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 both the visibility and dependencies views with plain SELECT's
	 * from tables.
	 */
	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;
	CREATE TABLE _fake_deps(
		technology_name_id		INT ,
		technology_dependencies	TEXT
	);
	CREATE OR REPLACE VIEW defs.technology_dependencies_view
		AS SELECT * FROM _fake_deps;

	/* Insert empire states and data for fake views */
	INSERT INTO emp.technologies (
			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( 'emp2' ) , _get_string( 'tech1' ) ,
			'RESEARCH' , 228.9 , 0
		) , (
			_get_emp_name( 'emp3' ) , _get_string( 'tech1' ) ,
			'RESEARCH' , 114 , 1
		);
	INSERT INTO _fake_visibility VALUES(
		_get_emp_name( 'emp1' ) , _get_string( 'tech1' ) , TRUE 
	) , (
		_get_emp_name( 'emp2' ) , _get_string( 'tech1' ) , TRUE 
	) , (
		_get_emp_name( 'emp3' ) , _get_string( 'tech1' ) , FALSE 
	);
	INSERT INTO _fake_deps VALUES( _get_string( 'tech1' ) , 'deps are here' );
	
	-- ***** TESTS BEGIN HERE *****
	SELECT plan( 3 );
	
	SELECT diag_test_name( 'emp.technologies_view - Known technology' );
	SELECT set_eq( $$
		SELECT emptech_id , emptech_state::TEXT , emptech_visible ,
				technology_category , technology_name , technology_description ,
				emptech_points , emptech_priority IS NULL AS ep_null ,
				emptech_ratio IS NULL AS er_null ,
				technology_price , technology_dependencies
			FROM emp.technologies_view
			WHERE empire_id = _get_emp_name( 'emp1' )
	$$ , $$ VALUES(
		_get_emp_name( 'emp1' ) || ',tech1,true' , 'KNOWN' , TRUE ,
		'techCategory1' , 'tech1' , 'techDescription1' ,
		456 ,  TRUE , TRUE , 123 , 'deps are here'
	) $$ );
	
	SELECT diag_test_name( 'emp.technologies_view - In-progress, visible technology' );
	SELECT set_eq( $$
		SELECT emptech_id , emptech_state::TEXT , emptech_visible ,
				technology_category , technology_name , technology_description ,
				emptech_points , emptech_priority , emptech_ratio ,
				technology_price , technology_dependencies
			FROM emp.technologies_view
			WHERE empire_id = _get_emp_name( 'emp2' )
	$$ , $$ VALUES(
		_get_emp_name( 'emp2' ) || ',tech1,true' , 'RESEARCH' , TRUE ,
		'techCategory1' , 'tech1' , 'techDescription1' ,
		228 ,  0 , 50 , 123 , 'deps are here'
	) $$ );

	SELECT diag_test_name( 'emp.technologies_view - In-progress, unknown technology' );
	SELECT set_eq( $$
		SELECT emptech_id , emptech_state::TEXT , emptech_visible ,
				technology_category , technology_name IS NULL AS n1 , technology_description IS NULL AS n2 ,
				emptech_points IS NULL AS n3 , emptech_priority , emptech_ratio ,
				technology_price IS NULL AS n4, technology_dependencies
			FROM emp.technologies_view
			WHERE empire_id = _get_emp_name( 'emp3' )
	$$ , $$ VALUES(
		_get_emp_name( 'emp3' ) || ',tech1,false' , 'RESEARCH' , FALSE ,
		'techCategory1' , TRUE, TRUE ,
		TRUE , 1 , 25 , TRUE , 'deps are here'
	) $$ );
	SELECT * FROM finish( );
ROLLBACK;