/*
 * Test constraints and foreign keys on defs.resources
 */
BEGIN;
	
	/* We need a few strings to be used when creating resource definitions. */
	\i utils/strings.sql
	SELECT _create_test_strings( 6 );

	/****** TESTS BEGIN HERE ******/
	SELECT plan( 17 );

	
	/* Valid resource definition, no category */
	SELECT diag_test_name( 'Valid resource definition without category' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			_get_string( 'test1' ) , _get_string( 'test2' ) , 1
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.resources;

	/* Resource with valid fields, including a category */
	SELECT diag_test_name( 'Valid resource definition with category' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id ,
			resource_category_id , resource_weight
		) VALUES (
			_get_string( 'test1' ) , _get_string( 'test2' ) ,
			_get_string( 'test3' ) , 1
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.resources;


	/* Resource definition with an invalid name */
	SELECT diag_test_name( 'Resource definition with an invalid name' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			_get_bad_string( ) , _get_string( 'test2' ) , 1
		);
	SELECT throws_ok( '_test_this' , 23503 );
	DEALLOCATE ALL;

	/* Resource definition with a NULL name */
	SELECT diag_test_name( 'Resource definition with a NULL name' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			NULL , _get_string( 'test2' ) , 1
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;


	/* Resource definition with an invalid description */
	SELECT diag_test_name( 'Resource definition with an invalid description' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			_get_string( 'test1' ) , _get_bad_string( ) , 1
		);
	SELECT throws_ok( '_test_this' , 23503 );
	DEALLOCATE ALL;

	/* Resource definition with a NULL description */
	SELECT diag_test_name( 'Resource definition with a NULL description' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			_get_string( 'test1' ) , NULL , 1
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;


	/* Resource definition with an invalid category */
	SELECT diag_test_name( 'Resource definition with an invalid category' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id ,
			resource_category_id , resource_weight
		) VALUES (
			_get_string( 'test1' ) , _get_string( 'test2' ) ,
			_get_bad_string( ) , 1
		);
	SELECT throws_ok( '_test_this' , 23503 );
	DEALLOCATE ALL;


	/* Resource definition with an invalid weight */
	SELECT diag_test_name( 'Resource definition with an invalid weight' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			_get_string( 'test1' ) , _get_string( 'test2' ) , 0
		);
	SELECT throws_ok( '_test_this' , 23514 );
	DEALLOCATE ALL;

	/* Resource definition with a NULL weight */
	SELECT diag_test_name( 'Resource definition with a NULL weight' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			_get_string( 'test1' ) , _get_string( 'test2' ) , NULL
		);
	SELECT throws_ok( '_test_this' , 23502 );
	DEALLOCATE ALL;


	/* Resource definitions using the same name */
	INSERT INTO defs.resources (
		resource_name_id , resource_description_id , resource_weight
	) VALUES (
		_get_string( 'test1' ) , _get_string( 'test2' ) , 1
	);
	SELECT diag_test_name( 'Resource definitions using the same name' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			_get_string( 'test1' ) , _get_string( 'test3' ) , 1
		);
	SELECT throws_ok( '_test_this' , 23505 );
	DEALLOCATE ALL;

	/* Resource definitions using the same description */
	SELECT diag_test_name( 'Resource definitions using the same description' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			_get_string( 'test3' ) , _get_string( 'test2' ) , 1
		);
	SELECT throws_ok( '_test_this' , 23505 );
	DEALLOCATE ALL;

	/* Resources with distinct names and descriptions */
	SELECT diag_test_name( 'Resources with distinct names and descriptions' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id , resource_weight
		) VALUES (
			_get_string( 'test3' ) , _get_string( 'test4' ) , 1
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.resources;


	/* Resources with distinct categories */
	INSERT INTO defs.resources (
		resource_name_id , resource_description_id ,
		resource_category_id , resource_weight
	) VALUES (
		_get_string( 'test1' ) , _get_string( 'test2' ) ,
		_get_string( 'test3' ) , 1
	);
	SELECT diag_test_name( 'Resources with distinct categories' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id ,
			resource_category_id , resource_weight
		) VALUES (
			_get_string( 'test4' ) , _get_string( 'test5' ) ,
			_get_string( 'test6' ) , 1
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;
	DELETE FROM defs.resources WHERE resource_name_id = _get_string( 'test4' );

	/* Resources in the same category */
	SELECT diag_test_name( 'Resources in the same category' );
	PREPARE _test_this AS
		INSERT INTO defs.resources (
			resource_name_id , resource_description_id ,
			resource_category_id , resource_weight
		) VALUES (
			_get_string( 'test4' ) , _get_string( 'test5' ) ,
			_get_string( 'test3' ) , 1
		);
	SELECT lives_ok( '_test_this' );
	DEALLOCATE ALL;


	/* Resource definition name deletion impossible */
	SELECT diag_test_name( 'Resource definition name deletion impossible' );
	PREPARE _test_this AS
		DELETE FROM defs.strings WHERE id = _get_string( 'test1' );
	SELECT throws_ok( '_test_this' , 23503 );
	DEALLOCATE ALL;

	/* Resource definition description deletion impossible */
	SELECT diag_test_name( 'Resource definition description deletion impossible' );
	PREPARE _test_this AS
		DELETE FROM defs.strings WHERE id = _get_string( 'test2' );
	SELECT throws_ok( '_test_this' , 23503 );
	DEALLOCATE ALL;

	/*
	 * 17/ Make sure it is impossible to delete a string definition used as
	 *     a resource category
	 */
	SELECT diag_test_name( 'Resource definition description deletion impossible' );
	PREPARE _test_this AS
		DELETE FROM defs.strings WHERE id = _get_string( 'test3' );
	SELECT throws_ok( '_test_this' , 23503 );
	DEALLOCATE ALL;


	SELECT * FROM finish( );
ROLLBACK;