/*
 * Make sure that inserting new technologies create corresponding dependency
 * cache entries, and that these entries are deleted along with the
 * technologies.
 */
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 technology's name
	SELECT _create_test_strings( 1 , 'tech' );
	
	-- Insert the new technology
	INSERT INTO defs.technologies ( technology_name_id )
		VALUES ( _get_string( 'tech1' ) );

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

	SELECT diag_test_name( 'defs.technologies - Inserting creates cache entries' );
	SELECT set_eq( $$
		SELECT 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 = _get_string( 'tech1' );
	$$ , $$ VALUES (
			FALSE , TRUE , 0 , _get_string( 'tech1' ) , TRUE , TRUE
		) , (
			TRUE , TRUE , 0 , _get_string( 'tech1' ) , TRUE , TRUE
	) $$ );

	SELECT diag_test_name( 'defs.technologies - Cache entries are deleted along with technologies' );
	DELETE FROM defs.technologies WHERE technology_name_id = _get_string( 'tech1' );
	SELECT is_empty( $$
		SELECT * FROM defs.techdep_cache
				WHERE technology_name_id = _get_string( 'tech1' );
	$$ );

	SELECT * FROM finish( );
ROLLBACK;