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

	/* Create a single empire */
	SELECT _create_emp_names( 1 , 'emp' );
	INSERT INTO emp.empires ( name_id , cash )
		VALUES ( _get_emp_name( 'emp1' ) , 200 );

	/*
	 * Set visibility thresholds to either 50% or 100 points.
	 */
	SELECT sys.uoc_constant( 'game.research.visibility.points' , '(test)' , 'Test' , 100.0 );
	SELECT sys.uoc_constant( 'game.research.visibility.ratio' , '(test)' , 'Test' , 0.5 );
	
	/* Create 6 technology definitions that will be used to test various
	 * states: pending, known, 4 variants of research in progress (points
	 * and ratio below thresholds, points below threshold, ratio below
	 * threshold, both points and ratio above thresholds).
	 * 
	 * Disable all unused fields from the definition table.
	 */
	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;
	SELECT _create_test_strings( 6 , 'tech' );
	INSERT INTO defs.technologies( technology_name_id , technology_points )
		VALUES ( _get_string( 'tech1' ) , 100 ) ,
			( _get_string( 'tech2' ) , 100 ) ,
			( _get_string( 'tech3' ) , 100 ) ,
			( _get_string( 'tech4' ) , 100 ) ,
			( _get_string( 'tech5' ) , 1000 ) ,
			( _get_string( 'tech6' ) , 1000 );

	/* Insert empire state */
	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' ) ,
			'PENDING' , NULL , NULL
		) , (
			_get_emp_name( 'emp1' ) , _get_string( 'tech3' ) ,
			'RESEARCH' , 10.0 , 2
		) , (
			_get_emp_name( 'emp1' ) , _get_string( 'tech4' ) ,
			'RESEARCH' , 51.0 , 2
		) , (
			_get_emp_name( 'emp1' ) , _get_string( 'tech5' ) ,
			'RESEARCH' , 101.0 , 2
		) , (
			_get_emp_name( 'emp1' ) , _get_string( 'tech6' ) ,
			'RESEARCH' , 501.0 , 2
		);

	-- ***** TESTS BEGIN HERE *****
	SELECT plan( 7 );

	SELECT diag_test_name( 'emp.technology_visibility_view - All technologies are listed' );
	SELECT is( COUNT( * )::INT , 6 )
		FROM emp.technology_visibility_view
		WHERE empire_id = _get_emp_name( 'emp1' );

	SELECT diag_test_name( 'emp.technology_visibility_view - Known technologies are visible' );
	SELECT ok( emptech_visible )
		FROM emp.technology_visibility_view
		WHERE technology_name_id = _get_string( 'tech1' )
			AND empire_id = _get_emp_name( 'emp1' );

	SELECT diag_test_name( 'emp.technology_visibility_view - Pending technologies are visible' );
	SELECT ok( emptech_visible )
		FROM emp.technology_visibility_view
		WHERE technology_name_id = _get_string( 'tech2' )
			AND empire_id = _get_emp_name( 'emp1' );

	SELECT diag_test_name( 'emp.technology_visibility_view - In-progress technologies with both points and ratios below thresholds are not visible' );
	SELECT ok( NOT emptech_visible )
		FROM emp.technology_visibility_view
		WHERE technology_name_id = _get_string( 'tech3' )
			AND empire_id = _get_emp_name( 'emp1' );

	SELECT diag_test_name( 'emp.technology_visibility_view - In-progress technologies with points below threshold are visible' );
	SELECT ok( emptech_visible )
		FROM emp.technology_visibility_view
		WHERE technology_name_id = _get_string( 'tech4' )
			AND empire_id = _get_emp_name( 'emp1' );

	SELECT diag_test_name( 'emp.technology_visibility_view - In-progress technologies with ratio below threshold are visible' );
	SELECT ok( emptech_visible )
		FROM emp.technology_visibility_view
		WHERE technology_name_id = _get_string( 'tech5' )
			AND empire_id = _get_emp_name( 'emp1' );

	SELECT diag_test_name( 'emp.technology_visibility_view - In-progress technologies with both points and ratios above threshold are visible' );
	SELECT ok( emptech_visible )
		FROM emp.technology_visibility_view
		WHERE technology_name_id = _get_string( 'tech6' )
			AND empire_id = _get_emp_name( 'emp1' );


	SELECT * FROM finish( );
ROLLBACK;