SQL privileges fix and unit tests

* Fixed privileges on both variants of defs.uoc_natural_resource()

* Added user unit tests which check execution privileges on all (new)
stored procedures and INSERT/UPDATE/SELECT/DELETE privileges on all
(new) tables
This commit is contained in:
Emmanuel BENOîT 2012-01-03 09:35:57 +01:00
parent d4945d8e21
commit e7d2072813
19 changed files with 258 additions and 2 deletions

View file

@ -0,0 +1,225 @@
/*
* 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;

View file

@ -0,0 +1,474 @@
/*
* Test constraints and foreign keys on defs.natural_resources
*/
BEGIN;
/* We need a few strings to be used when creating resource definitions. */
\i utils/strings.sql
SELECT _create_test_strings( 4 );
/*
* We need a a pair of resource definitions to be used when creating
* natural resources.
*/
INSERT INTO defs.resources (
resource_name_id , resource_description_id , resource_weight
) VALUES ( _get_string( 'test1' ) , _get_string( 'test2' ) , 1 );
INSERT INTO defs.resources (
resource_name_id , resource_description_id , resource_weight
) VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , 2 );
/****** TESTS BEGIN HERE ******/
SELECT plan( 33 );
SELECT diag_test_name( 'Valid natural resource definition' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources (
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT lives_ok( '_test_this' );
SELECT diag_test_name( 'Duplicate natural resource definition' );
SELECT throws_ok( '_test_this' , 23505 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Distinct natural resource definitions' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources (
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test3' ) , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM defs.natural_resources;
SELECT diag_test_name( 'Natural resource definition with missing resource definition' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test2' ) , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with NULL resource definition' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
NULL , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with presence probability <= 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with presence probability >= 1' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 1 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with NULL presence probability' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , NULL , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with avg. quantity <= 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 0 , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with NULL avg. quantity' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , NULL , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with quantity dev. < 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , -1 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with NULL quantity dev.' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , NULL , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with quantity dev. = avg. quantity' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 100 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with quantity dev. = 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 0 , 0.5 , 0.05 , 0.5 , 0.05
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM defs.natural_resources;
SELECT diag_test_name( 'Natural resource definition with avg. difficulty = 1' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 1 , 0 , 0.5 , 0.05
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM defs.natural_resources;
SELECT diag_test_name( 'Natural resource definition with avg. difficulty = 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0 , 0 , 0.5 , 0.05
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM defs.natural_resources;
SELECT diag_test_name( 'Natural resource definition with avg. difficulty > 1' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 1.0001 , 0 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with avg. difficulty < 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , -0.0001 , 0 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with NULL avg. difficulty' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , NULL , 0.05 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with difficulty dev. < 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , -1 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with avg. difficulty - difficulty dev. < 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.25 , 0.5 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with difficulty dev. + avg. difficulty > 1' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.75 , 0.5 , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with NULL difficulty dev.' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , NULL , 0.5 , 0.05
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with avg. recovery = 1' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
1 , 0
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM defs.natural_resources;
SELECT diag_test_name( 'Natural resource definition with avg. recovery = 0+' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
0.0001 , 0
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM defs.natural_resources;
SELECT diag_test_name( 'Natural resource definition with avg. recovery > 1' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
1.1 , 0
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with avg. recovery <= 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
0 , 0
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with NULL avg. recovery' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
NULL , 0.05
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with recovery dev. < 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
0.5 , -1
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with avg.recovery - recovery dev. <= 0' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with avg.recovery - recovery dev. > 1' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
0.75 , 0.5
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Natural resource definition with NULL recovery dev.' );
PREPARE _test_this AS
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 ,
0.5 , NULL
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Deletion of the base definition for a natural resource' );
INSERT INTO defs.natural_resources(
resource_name_id , natres_p_presence , natres_quantity_avg ,
natres_quantity_dev , natres_difficulty_avg ,
natres_difficulty_dev , natres_recovery_avg ,
natres_recovery_dev
) VALUES (
_get_string( 'test1' ) , 0.5 , 100 , 1 , 0.5 , 0.05 , 0.5 , 0.05
);
PREPARE _test_this AS
DELETE FROM defs.resources WHERE resource_name_id = _get_string( 'test1' );
SELECT throws_ok( '_test_this' , 23503 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,331 @@
/*
* Test constraints and foreign keys on verse.resource_providers
*/
BEGIN;
/* We need a pair of resource definitions and a pair of planets. */
\i utils/strings.sql
\i utils/resources.sql
\i utils/accounts.sql
\i utils/naming.sql
\i utils/universe.sql
SELECT _create_natural_resources( 2 , 'testResource' );
SELECT _create_raw_planets( 2 , 'testPlanet' );
/****** TESTS BEGIN HERE ******/
SELECT plan( 22 );
SELECT diag_test_name( 'Valid resource provider' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
0.5 , 0.5
);
SELECT lives_ok( '_test_this' );
SELECT diag_test_name( 'Duplicate resource provider' );
SELECT throws_ok( '_test_this' , 23505 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with same planet but different type' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource2' ) ,
100 , 50 ,
0.5 , 0.5
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with same type but different planet' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet2' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
0.5 , 0.5
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM verse.resource_providers;
SELECT diag_test_name( 'Resource provider with NULL planet identifier' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
NULL , _get_string( 'testResource1' ) ,
100 , 50 ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with invalid planet identifier' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_bad_map_name( ) , _get_string( 'testResource1' ) ,
100 , 50 ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with NULL resource identifier' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , NULL ,
100 , 50 ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with invalid resource identifier' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_bad_string( ) ,
100 , 50 ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with NULL maximal quantity' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
NULL , 50 ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with maximal quantity <= 0' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
0 , 0 ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with NULL quantity' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , NULL ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with quantity < 0' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , -1 ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with quantity > max. quantity' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 101 ,
0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with NULL difficulty' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
NULL , 0.5
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with difficulty < 0' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
-0.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with difficulty > 1' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
1.5 , 0.5
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with difficulty = 0' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
0 , 0.5
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM verse.resource_providers;
SELECT diag_test_name( 'Resource provider with difficulty = 1' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
1 , 0.5
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM verse.resource_providers;
SELECT diag_test_name( 'Resource provider with NULL recovery' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
0.5 , NULL
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with recovery = 0' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
0.5 , 0
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with recovery > 1' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
0.5 , 1.5
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Resource provider with recovery = 1' );
PREPARE _test_this AS
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) ,
100 , 50 ,
0.5 , 1
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM verse.resource_providers;
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,191 @@
/*
* Test constraints and foreign keys on emp.resources
*/
BEGIN;
/* We need to create a pair of resources and a pair of empires */
\i utils/strings.sql
\i utils/resources.sql
\i utils/accounts.sql
\i utils/naming.sql
SELECT _create_natural_resources( 2 , 'testResource' );
SELECT _create_emp_names( 2 , 'testUser' );
INSERT INTO emp.empires ( name_id , cash )
SELECT id , 0 FROM naming.empire_names;
/****** TESTS BEGIN HERE ******/
SELECT plan( 16 );
SELECT diag_test_name( 'Valid empire resources record' );
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed , empres_owed
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) ,
1 , 1
);
SELECT lives_ok( '_test_this' );
DELETE FROM emp.resources;
DEALLOCATE ALL;
INSERT INTO emp.resources (
empire_id , resource_name_id
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' )
);
SELECT diag_test_name( 'Default possessed value in empire resources record' );
SELECT results_eq(
$$ SELECT empres_possessed FROM emp.resources $$ ,
$$ VALUES ( 0::DOUBLE PRECISION ) $$
);
SELECT diag_test_name( 'Default owed value in empire resources record' );
SELECT results_eq(
$$ SELECT empres_owed FROM emp.resources $$ ,
$$ VALUES ( 0::DOUBLE PRECISION ) $$
);
DELETE FROM emp.resources;
SELECT diag_test_name( 'NULL empire identifier in empire resources record' );
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed , empres_owed
) VALUES (
NULL , _get_string( 'testResource1' ) ,
1 , 1
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Invalid empire identifier in empire resources record' );
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed , empres_owed
) VALUES (
_get_bad_emp_name( ) , _get_string( 'testResource1' ) ,
1 , 1
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'NULL resource identifier in empire resources record' );
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed , empres_owed
) VALUES (
_get_emp_name( 'testUser1' ) , NULL ,
1 , 1
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Invalid resource identifier in empire resources record' );
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed , empres_owed
) VALUES (
_get_emp_name( 'testUser1' ) , _get_bad_string( ) ,
1 , 1
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Duplicate empire resources record' );
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed , empres_owed
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) ,
1 , 1
);
EXECUTE _test_this;
SELECT throws_ok( '_test_this' , 23505 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire resources record with same empire but different types of resources' );
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed , empres_owed
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource2' ) ,
1 , 1
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire resources record with different empires but same type of resources' );
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed , empres_owed
) VALUES (
_get_emp_name( 'testUser2' ) , _get_string( 'testResource1' ) ,
1 , 1
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM emp.resources;
INSERT INTO emp.resources (
empire_id , resource_name_id
) VALUES (
_get_emp_name( 'testUser2' ) , _get_string( 'testResource1' )
);
SELECT diag_test_name( 'Empire deletion succeeds when empire resources records are present' );
PREPARE _test_this AS
DELETE FROM emp.empires WHERE name_id = _get_emp_name( 'testUser2' );
SELECT lives_ok( '_test_this' );
SELECT diag_test_name( 'Empire deletion causes empire resources record deletion' );
SELECT is_empty( 'SELECT * FROM emp.resources' );
DEALLOCATE ALL;
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) , NULL
);
SELECT diag_test_name( 'NULL possessed value in empire resources record' );
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_possessed
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) , -1
);
SELECT diag_test_name( 'Negative possessed value in empire resources record' );
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_owed
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) , NULL
);
SELECT diag_test_name( 'NULL owed value in empire resources record' );
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
PREPARE _test_this AS
INSERT INTO emp.resources (
empire_id , resource_name_id , empres_owed
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) , -1
);
SELECT diag_test_name( 'Negative owed value in empire resources record' );
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,119 @@
/*
* Test constraints and foreign keys on emp.mining_settings
*/
BEGIN;
/* We need to create a pair of resources and a pair of empires */
\i utils/strings.sql
\i utils/resources.sql
\i utils/accounts.sql
\i utils/naming.sql
SELECT _create_natural_resources( 2 , 'testResource' );
SELECT _create_emp_names( 2 , 'testUser' );
INSERT INTO emp.empires ( name_id , cash )
SELECT id , 0 FROM naming.empire_names;
/****** TESTS BEGIN HERE ******/
SELECT plan( 10 );
SELECT diag_test_name( 'Valid empire mining settings record' );
PREPARE _test_this AS
INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) , 0
);
SELECT lives_ok( '_test_this' );
SELECT diag_test_name( 'Duplicate empire mining settings record' );
SELECT throws_ok( '_test_this' , 23505 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire mining settings records with same empire but different types' );
PREPARE _test_this AS
INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource2' ) , 0
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire mining settings records with same type but different empires' );
PREPARE _test_this AS
INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight
) VALUES (
_get_emp_name( 'testUser2' ) , _get_string( 'testResource1' ) , 0
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM emp.mining_settings;
SELECT diag_test_name( 'Empire mining settings record with NULL empire identifier' );
PREPARE _test_this AS
INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight
) VALUES (
NULL , _get_string( 'testResource1' ) , 0
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire mining settings record with invalid empire identifier' );
PREPARE _test_this AS
INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight
) VALUES (
_get_bad_emp_name( ) , _get_string( 'testResource1' ) , 0
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire mining settings record with NULL resource identifier' );
PREPARE _test_this AS
INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , NULL , 0
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire mining settings record with invalid resource identifier' );
PREPARE _test_this AS
INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_bad_string( ) , 0
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire mining settings record with NULL weight' );
PREPARE _test_this AS
INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) , NULL
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire mining settings record with weight < 0' );
PREPARE _test_this AS
INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) , -1
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,193 @@
/*
* Test constraints and foreign keys on emp.mining_settings
*/
BEGIN;
/* We need to create a pair of resources, a pair of empires, a pair of planets, and resource providers */
\i utils/strings.sql
\i utils/resources.sql
\i utils/accounts.sql
\i utils/naming.sql
\i utils/universe.sql
SELECT _create_natural_resources( 2 , 'testResource' );
SELECT _create_emp_names( 2 , 'testUser' );
INSERT INTO emp.empires ( name_id , cash )
SELECT id , 0 FROM naming.empire_names;
SELECT _create_raw_planets( 2 , 'testPlanet' );
SELECT _create_resource_provider( 'testPlanet1' , 'testResource1' );
SELECT _create_resource_provider( 'testPlanet1' , 'testResource2' );
SELECT _create_resource_provider( 'testPlanet2' , 'testResource1' );
-- No provider for testResource2 on testPlanet2
/****** TESTS BEGIN HERE ******/
SELECT plan( 14 );
SELECT diag_test_name( 'Valid empire planet mining settings record' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet1' ) ,
_get_string( 'testResource1' ) , 0
);
SELECT lives_ok( '_test_this' );
SELECT diag_test_name( 'Duplicate empire planet mining settings record' );
SELECT throws_ok( '_test_this' , 23505 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining settings records with different types' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet1' ) ,
_get_string( 'testResource2' ) , 0
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining settings records with different empires' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser2' ) , _get_map_name( 'testPlanet1' ) ,
_get_string( 'testResource1' ) , 0
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining settings records with different planets' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet2' ) ,
_get_string( 'testResource1' ) , 0
);
SELECT lives_ok( '_test_this' );
DEALLOCATE ALL;
DELETE FROM emp.mining_settings;
SELECT diag_test_name( 'Empire planet mining setting record with NULL empire identifier' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
NULL , _get_map_name( 'testPlanet2' ) ,
_get_string( 'testResource1' ) , 0
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining setting record with invalid empire identifier' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_bad_emp_name( ) , _get_map_name( 'testPlanet2' ) ,
_get_string( 'testResource1' ) , 0
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining setting record with NULL planet identifier' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , NULL ,
_get_string( 'testResource1' ) , 0
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining setting record with invalid planet identifier' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_bad_map_name( ) ,
_get_string( 'testResource1' ) , 0
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining setting record with NULL resource identifier' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet2' ) ,
NULL , 0
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining setting record with invalid resource identifier' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet2' ) ,
_get_bad_string( ) , 0
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining setting record with invalid resource provider identifier' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet2' ) ,
_get_string( 'testResource2' ) , 0
);
SELECT throws_ok( '_test_this' , 23503 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining setting record with NULL weight' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet1' ) ,
_get_string( 'testResource1' ) , NULL
);
SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL;
SELECT diag_test_name( 'Empire planet mining setting record with weight < 0' );
PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings(
empire_id , planet_id ,
resource_name_id , emppmset_weight
) VALUES (
_get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet1' ) ,
_get_string( 'testResource1' ) , -1
);
SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL;
SELECT * FROM finish( );
ROLLBACK;