/*
 * Make sure that inserting technology dependencies results in new entries in
 * the cache, and that the amount of entries in the cache grows according to
 * predictions.
 */
BEGIN;
	\i utils/strings.sql

	-- Make the columns we don't use in the technology definition table NULL-able
	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;

	-- Create a string to use as the technologies' names
	SELECT _create_test_strings( 4 , 'tech' );
	
	-- Insert the technologies
	INSERT INTO defs.technologies ( technology_name_id )
		VALUES ( _get_string( 'tech1' ) ) , ( _get_string( 'tech2' ) ) ,
				( _get_string( 'tech3' ) ) , ( _get_string( 'tech4' ) );

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

	INSERT INTO defs.technology_dependencies ( technology_name_id , technology_name_id_depends )
		VALUES ( _get_string( 'tech2' ) , _get_string( 'tech1' ) );
	SELECT diag_test_name( 'defs.technology_dependencies - Dependencies create cache entries' );
	SELECT set_eq( $$
		SELECT technology_name_id , tdcache_reverse ,
				( tdcache_id_parent IS NULL ) AS no_parent ,
				tdcache_depth , technology_name_id_copyof ,
				( tdcache_id_copyof IS NULL ) AS not_a_copy ,
				( techdep_id IS NULL ) AS not_a_dependency
			FROM defs.techdep_cache
			WHERE technology_name_id IN ( _get_string( 'tech1' ) , _get_string( 'tech2' ) );
	$$ , $$ VALUES (
			_get_string( 'tech1' ) , FALSE , TRUE , 0 , _get_string( 'tech1' ) , TRUE , TRUE
		) , (
			_get_string( 'tech1' ) , TRUE , TRUE , 0 , _get_string( 'tech1' ) , TRUE , TRUE
		) , (
			_get_string( 'tech1' ) , TRUE , FALSE , 1 , _get_string( 'tech2' ) , FALSE , FALSE
		) , (
			_get_string( 'tech2' ) , TRUE , TRUE , 0 , _get_string( 'tech2' ) , TRUE , TRUE
		) , (
			_get_string( 'tech2' ) , FALSE , TRUE , 0 , _get_string( 'tech2' ) , TRUE , TRUE
		) , (
			_get_string( 'tech2' ) , FALSE , FALSE , 1 , _get_string( 'tech1' ) , FALSE , FALSE
	) $$ );


	DELETE FROM defs.technology_dependencies WHERE technology_name_id = _get_string( 'tech2' );
	SELECT diag_test_name( 'defs.technologies - Cache entries are deleted along with dependencies' );
	SELECT is_empty( $$
		SELECT * FROM defs.techdep_cache
			WHERE technology_name_id IN ( _get_string( 'tech1' ) , _get_string( 'tech2' ) )
				AND tdcache_depth > 0;
	$$ );

	
	/* Now insert the following dependencies:
	 *	tech4 -> {tech3 , tech2}
	 *	tech3 -> {tech1}
	 *	tech2 -> {tech1}
	 */
	INSERT INTO defs.technology_dependencies ( technology_name_id , technology_name_id_depends )
		VALUES ( _get_string( 'tech4' ) , _get_string( 'tech3' ) ) ,
				( _get_string( 'tech4' ) , _get_string( 'tech2' ) ) ,
				( _get_string( 'tech3' ) , _get_string( 'tech1' ) ) ,
				( _get_string( 'tech2' ) , _get_string( 'tech1' ) );

	/* Then check the amount of cache entries, as described below:
	 *
	 *	technology		reverse			entries / copies
	 * ------------------------------------------------
	 *	tech1			yes				5 / 4
	 *	tech1			no				1 / 0
	 *	tech2			yes				2 / 1
	 *	tech2			no				2 / 1
	 *	tech3			yes				2 / 1
	 *	tech3			no				2 / 1
	 *	tech4			yes				1 / 0
	 *	tech4			no				5 / 4
	 */
	SELECT set_eq( $$
		SELECT technology_name_id , tdcache_reverse ,
				COUNT(*) AS total , COUNT( tdcache_id_copyof ) AS copies
			FROM defs.techdep_cache
			WHERE technology_name_id IN (
					_get_string( 'tech1' ) , _get_string( 'tech2' ) ,
					_get_string( 'tech3' ) , _get_string( 'tech4' )
				)
			GROUP BY technology_name_id , tdcache_reverse;
	$$ , $$ VALUES (
			_get_string( 'tech1' ) , TRUE , 5 , 4
		) , (
			_get_string( 'tech1' ) , FALSE , 1 , 0
		) , (
			_get_string( 'tech2' ) , TRUE , 2 , 1
		) , (
			_get_string( 'tech2' ) , FALSE , 2 , 1
		) , (
			_get_string( 'tech3' ) , TRUE , 2 , 1
		) , (
			_get_string( 'tech3' ) , FALSE , 2 , 1
		) , (
			_get_string( 'tech4' ) , TRUE , 1 , 0
		) , (
			_get_string( 'tech4' ) , FALSE , 5 , 4
	) $$ );

	SELECT * FROM finish( );
ROLLBACK;