/*
 * Test the defs.techdep_add() function
 */
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 strings to use as the technologies' names
	SELECT _create_test_strings( 5 , 'tech' );
	
	-- Insert the technologies
	INSERT INTO defs.technologies ( technology_name_id )
		VALUES ( _get_string( 'tech1' ) ) ,
				( _get_string( 'tech2' ) ) ,
				( _get_string( 'tech3' ) ) ,
				( _get_string( 'tech4' ) );

	-- Add a few dependencies
	INSERT INTO defs.technology_dependencies(
			technology_name_id , technology_name_id_depends
		) VALUES ( _get_string( 'tech3' ) , _get_string( 'tech2' ) ) , 
			( _get_string( 'tech2' ) , _get_string( 'tech1' ) );


	-- ***** TESTS BEGIN HERE *****
	SELECT plan( 8 );
	
	SELECT diag_test_name( 'defs.techdep_add() - Bad dependent technology name' );
	SELECT is( defs.techdep_add( 'does not exist' , 'tech2' )::TEXT , 'BAD_STRINGS' );

	SELECT diag_test_name( 'defs.techdep_add() - Bad dependency name' );
	SELECT is( defs.techdep_add( 'tech1' , 'does not exist' )::TEXT , 'BAD_STRINGS' );
	
	SELECT diag_test_name( 'defs.techdep_add() - Valid name that is not a technology' );
	SELECT is( defs.techdep_add( 'tech5' , 'tech2' )::TEXT , 'BAD_STRINGS' );

	SELECT diag_test_name( 'defs.techdep_add() - Duplicate dependency' );
	SELECT is( defs.techdep_add( 'tech3' , 'tech2' )::TEXT , 'REDUNDANT' );

	SELECT diag_test_name( 'defs.techdep_add() - Cyclic dependency' );
	SELECT is( defs.techdep_add( 'tech2' , 'tech3' )::TEXT , 'CYCLE' );

	SELECT diag_test_name( 'defs.techdep_add() - Redundant dependency' );
	SELECT is( defs.techdep_add( 'tech3' , 'tech1' )::TEXT , 'REDUNDANT' );

	SELECT diag_test_name( 'defs.techdep_add() - Success - Return value' );
	SELECT is( defs.techdep_add( 'tech4' , 'tech1' )::TEXT , 'CREATED' );

	SELECT diag_test_name( 'defs.techdep_add() - Success - Table entries' );
	SELECT set_eq( $$
		SELECT technology_name_id_depends
			FROM defs.technology_dependencies
			WHERE technology_name_id = _get_string( 'tech4' );
	$$ , $$ VALUES(
		_get_string( 'tech1' )
	) $$ );

	SELECT * FROM finish( );
ROLLBACK;