From bed784a8e1881189b700a93d1a8a48babe08e061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Mon, 19 Dec 2011 16:00:01 +0100 Subject: [PATCH] Resource management functions * Added defs.uoc_resources() set of functions which create or update basic resources. * Added defs.uoc_natural_resources() set of functions which create or update natural resources. --- .../db-structure/parts/020-functions.sql | 1 + .../functions/025-resources-functions.sql | 467 ++++++++++++++++++ .../defs/02500-uoc-resource-internal.sql | 74 +++ .../functions/defs/02501-uoc-resource.sql | 90 ++++ .../defs/02502-uoc-natres-internal.sql | 175 +++++++ .../defs/02503-uoc-natural-resource.sql | 242 +++++++++ legacyworlds/doc/TODO.txt | 8 +- 7 files changed, 1056 insertions(+), 1 deletion(-) create mode 100644 legacyworlds-server-data/db-structure/parts/functions/025-resources-functions.sql create mode 100644 legacyworlds-server-data/db-structure/tests/admin/functions/defs/02500-uoc-resource-internal.sql create mode 100644 legacyworlds-server-data/db-structure/tests/admin/functions/defs/02501-uoc-resource.sql create mode 100644 legacyworlds-server-data/db-structure/tests/admin/functions/defs/02502-uoc-natres-internal.sql create mode 100644 legacyworlds-server-data/db-structure/tests/admin/functions/defs/02503-uoc-natural-resource.sql diff --git a/legacyworlds-server-data/db-structure/parts/020-functions.sql b/legacyworlds-server-data/db-structure/parts/020-functions.sql index 5a55389..d97d3cd 100644 --- a/legacyworlds-server-data/db-structure/parts/020-functions.sql +++ b/legacyworlds-server-data/db-structure/parts/020-functions.sql @@ -11,6 +11,7 @@ \i parts/functions/005-logs-functions.sql \i parts/functions/010-constants-functions.sql \i parts/functions/020-naming-functions.sql +\i parts/functions/025-resources-functions.sql \i parts/functions/030-tech-functions.sql \i parts/functions/035-users-view.sql \i parts/functions/040-empire-functions.sql diff --git a/legacyworlds-server-data/db-structure/parts/functions/025-resources-functions.sql b/legacyworlds-server-data/db-structure/parts/functions/025-resources-functions.sql new file mode 100644 index 0000000..b401eb0 --- /dev/null +++ b/legacyworlds-server-data/db-structure/parts/functions/025-resources-functions.sql @@ -0,0 +1,467 @@ +-- LegacyWorlds Beta 6 +-- PostgreSQL database scripts +-- +-- Resource definitions management functions +-- +-- Copyright(C) 2004-2011, DeepClone Development +-- -------------------------------------------------------- + +/* + * Return codes for resource creation or update functions. + */ +DROP TYPE IF EXISTS defs.resource_update_result CASCADE; +CREATE TYPE defs.resource_update_result + AS ENUM( + /* The resource definition was created */ + 'CREATED' , + + /* The resource definition was updated */ + 'UPDATED' , + + /* The resource definition already existed, and was either a basic + * resource definition while the update required a natural resource, + * or a natural resource definition when the update required a basic + * resource. + */ + 'BAD_TYPE' , + + /* The name, description or category string identifiers were not valid + * string identifiers. + */ + 'BAD_STRINGS' , + + /* One (or more) of the numeric parameters is invalid */ + 'BAD_VALUE' , + + /* The specified description was in use by another resource */ + 'DUP_DESCR' + ); + + + +/* + * Create or update a basic resource + * + * /!\ INTERNAL FUNCTION /!\ + * + * This function is called by the variants of defs.uoc_resource() to actually + * update or create the resource. It will make sure that all string + * identifiers exist, then try to insert the resource. If that fails because + * the resource already exists, make sure it's a basic resource then update + * it. + * + * Parameters: + * _name the identifier of the resource's name + * _description the identifier of the resource's description + * _category the identifier of the resource's category, or NULL if + * the resource does not belong to a category + * _weight the resource's ordering weight + * + * Returns: + * ? the result code for the operation + */ +CREATE OR REPLACE FUNCTION defs.uoc_resource_internal( + _name TEXT , + _description TEXT , + _category TEXT , + _weight INT ) + RETURNS defs.resource_update_result + CALLED ON NULL INPUT + VOLATILE + SECURITY INVOKER + AS $$ +DECLARE + _ret defs.resource_update_result; + _name_id INT; + _desc_id INT; + _cat_id INT; +BEGIN + -- Get all string identifiers + SELECT INTO _name_id id FROM defs.strings WHERE name = _name; + IF NOT FOUND THEN + RETURN 'BAD_STRINGS'; + END IF; + SELECT INTO _desc_id id FROM defs.strings WHERE name = _description; + IF NOT FOUND THEN + RETURN 'BAD_STRINGS'; + END IF; + IF _category IS NULL THEN + _cat_id := NULL; + ELSE + SELECT INTO _cat_id id FROM defs.strings WHERE name = _category; + IF NOT FOUND THEN + RETURN 'BAD_STRINGS'; + END IF; + END IF; + + -- Try inserting the record + BEGIN + INSERT INTO defs.resources ( + resource_name_id , resource_description_id , + resource_category_id , resource_weight + ) VALUES ( + _name_id , _desc_id , _cat_id , _weight + ); + RETURN 'CREATED'; + EXCEPTION + WHEN unique_violation THEN + IF SQLERRM LIKE '%_description_%' THEN + RETURN 'DUP_DESCR'; + END IF; + END; + + -- Insertion failed, make sure the resource is a basic resource + PERFORM * + FROM defs.resources basic_res + LEFT OUTER JOIN defs.natural_resources nat_res + USING ( resource_name_id ) + WHERE basic_res.resource_name_id = _name_id + AND nat_res.resource_name_id IS NULL + FOR UPDATE OF basic_res; + IF NOT FOUND THEN + RETURN 'BAD_TYPE'; + END IF; + + -- Update the resource + BEGIN + UPDATE defs.resources + SET resource_description_id = _desc_id , + resource_category_id = _cat_id , + resource_weight = _weight + WHERE resource_name_id = _name_id; + RETURN 'UPDATED'; + EXCEPTION + WHEN unique_violation THEN + RETURN 'DUP_DESCR'; + END; +EXCEPTION + WHEN check_violation THEN + RETURN 'BAD_VALUE'; +END; +$$ LANGUAGE PLPGSQL; + +REVOKE EXECUTE + ON FUNCTION defs.uoc_resource_internal( TEXT , TEXT , TEXT , INT ) + FROM PUBLIC; + + + +/* + * Update or create a basic resource definition with no category + * + * Parameters: + * _name the identifier of the resource's name + * _description the identifier of the resource's description + * _weight the resource's ordering weight + * + * Returns: + * ? the result code for the operation + */ +CREATE OR REPLACE FUNCTION defs.uoc_resource( + _name TEXT , + _description TEXT , + _weight INT ) + RETURNS defs.resource_update_result + STRICT + VOLATILE + SECURITY DEFINER +AS $$ + SELECT defs.uoc_resource_internal( $1 , $2 , NULL , $3 ); +$$ LANGUAGE SQL; + +REVOKE EXECUTE + ON FUNCTION defs.uoc_resource( TEXT , TEXT , INT ) + FROM PUBLIC; +GRANT EXECUTE + ON FUNCTION defs.uoc_resource( TEXT , TEXT , INT ) + TO :dbuser; + + + +/* + * Update or create a basic resource definition with a category + * + * Parameters: + * _name the identifier of the resource's name + * _description the identifier of the resource's description + * _category the identifier of the resource's category + * _weight the resource's ordering weight + * + * Returns: + * ? the result code for the operation + */ +CREATE OR REPLACE FUNCTION defs.uoc_resource( + _name TEXT , + _description TEXT , + _category TEXT , + _weight INT ) + RETURNS defs.resource_update_result + STRICT + VOLATILE + SECURITY DEFINER +AS $$ + SELECT defs.uoc_resource_internal( $1 , $2 , $3 , $4 ); +$$ LANGUAGE SQL; + +REVOKE EXECUTE + ON FUNCTION defs.uoc_resource( TEXT , TEXT , TEXT , INT ) + FROM PUBLIC; +GRANT EXECUTE + ON FUNCTION defs.uoc_resource( TEXT , TEXT , TEXT , INT ) + TO :dbuser; + + + +/* + * Create or update a natural resource + * + * /!\ INTERNAL FUNCTION /!\ + * + * This function is called by the variants of defs.uoc_natural_resource() to + * actually update or create the resource. It will make sure that all string + * identifiers exist, then try to insert the resource. If that fails because + * the resource already exists, make sure it's a natural resource then update + * it. + * + * Parameters: + * _name the identifier of the resource's name + * _description the identifier of the resource's description + * _category the identifier of the resource's category, or NULL if + * the resource does not belong to a category + * _weight the resource's ordering weight + * _presence the presence probability + * _quantity_avg the average quantity + * _quantity_dev the deviation from the average quantity + * _difficulty_avg the average extraction difficulty + * _difficulty_dev the deviation from the average extraction difficulty + * _recovery_avg the average recovery rate + * _recovery_dev the deviation from the average recovery rate + * + * Returns: + * ? the result code for the operation + */ +CREATE OR REPLACE FUNCTION defs.uoc_natres_internal( + _name TEXT , + _description TEXT , + _category TEXT , + _weight INT , + _presence DOUBLE PRECISION , + _quantity_avg DOUBLE PRECISION , + _quantity_dev DOUBLE PRECISION , + _difficulty_avg DOUBLE PRECISION , + _difficulty_dev DOUBLE PRECISION , + _recovery_avg DOUBLE PRECISION , + _recovery_dev DOUBLE PRECISION ) + RETURNS defs.resource_update_result + CALLED ON NULL INPUT + VOLATILE + SECURITY INVOKER + AS $$ +DECLARE + _ret defs.resource_update_result; + _name_id INT; + _desc_id INT; + _cat_id INT; + _inserted BOOLEAN; +BEGIN + -- Get all string identifiers + SELECT INTO _name_id id FROM defs.strings WHERE name = _name; + IF NOT FOUND THEN + RETURN 'BAD_STRINGS'; + END IF; + SELECT INTO _desc_id id FROM defs.strings WHERE name = _description; + IF NOT FOUND THEN + RETURN 'BAD_STRINGS'; + END IF; + IF _category IS NULL THEN + _cat_id := NULL; + ELSE + SELECT INTO _cat_id id FROM defs.strings WHERE name = _category; + IF NOT FOUND THEN + RETURN 'BAD_STRINGS'; + END IF; + END IF; + + -- Try inserting the basic record + BEGIN + INSERT INTO defs.resources ( + resource_name_id , resource_description_id , + resource_category_id , resource_weight + ) VALUES ( + _name_id , _desc_id , _cat_id , _weight + ); + _inserted := TRUE; + EXCEPTION + WHEN unique_violation THEN + IF SQLERRM LIKE '%_description_%' THEN + RETURN 'DUP_DESCR'; + END IF; + _inserted := FALSE; + END; + + -- If insertion succeeded, insert the rest of the record + IF _inserted THEN + 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 ( + _name_id , _presence , + _quantity_avg , _quantity_dev , + _difficulty_avg , _difficulty_dev , + _recovery_avg , _recovery_dev + ); + RETURN 'CREATED'; + END IF; + + -- Insertion failed, make sure it is a natural resource + PERFORM * + FROM defs.resources basic_res + INNER JOIN defs.natural_resources nat_res + USING ( resource_name_id ) + WHERE basic_res.resource_name_id = _name_id + FOR UPDATE; + IF NOT FOUND THEN + RETURN 'BAD_TYPE'; + END IF; + + -- Update the resource + BEGIN + UPDATE defs.resources + SET resource_description_id = _desc_id , + resource_category_id = _cat_id , + resource_weight = _weight + WHERE resource_name_id = _name_id; + UPDATE defs.natural_resources + SET natres_p_presence = _presence , + natres_quantity_avg = _quantity_avg , + natres_quantity_dev = _quantity_dev , + natres_difficulty_avg = _difficulty_avg , + natres_difficulty_dev = _difficulty_dev , + natres_recovery_avg = _recovery_avg , + natres_recovery_dev = _recovery_dev + WHERE resource_name_id = _name_id; + RETURN 'UPDATED'; + EXCEPTION + WHEN unique_violation THEN + RETURN 'DUP_DESCR'; + END; +EXCEPTION + WHEN check_violation THEN + RETURN 'BAD_VALUE'; +END; +$$ LANGUAGE PLPGSQL; + +REVOKE EXECUTE + ON FUNCTION defs.uoc_natres_internal( TEXT , TEXT , TEXT , INT , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION ) + FROM PUBLIC; + + + +/* + * Create or update a natural resource with no category + * + * Parameters: + * _name the identifier of the resource's name + * _description the identifier of the resource's description + * _weight the resource's ordering weight + * _presence the presence probability + * _quantity_avg the average quantity + * _quantity_dev the deviation from the average quantity + * _difficulty_avg the average extraction difficulty + * _difficulty_dev the deviation from the average extraction difficulty + * _recovery_avg the average recovery rate + * _recovery_dev the deviation from the average recovery rate + * + * Returns: + * ? the result code for the operation + */ +CREATE OR REPLACE FUNCTION defs.uoc_natural_resource( + _name TEXT , + _description TEXT , + _weight INT , + _presence DOUBLE PRECISION , + _quantity_avg DOUBLE PRECISION , + _quantity_dev DOUBLE PRECISION , + _difficulty_avg DOUBLE PRECISION , + _difficulty_dev DOUBLE PRECISION , + _recovery_avg DOUBLE PRECISION , + _recovery_dev DOUBLE PRECISION ) + RETURNS defs.resource_update_result + STRICT VOLATILE + SECURITY INVOKER +AS $$ + SELECT defs.uoc_natres_internal( $1 , $2 , NULL , $3 , $4 , $5 , $6 , $7 , + $8 , $9 , $10 ); +$$ LANGUAGE SQL; + +REVOKE EXECUTE + ON FUNCTION defs.uoc_natural_resource( TEXT , TEXT , INT , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION ) + FROM PUBLIC; +GRANT EXECUTE + ON FUNCTION defs.uoc_natural_resource( TEXT , TEXT , INT , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION ) + TO :dbuser; + + + +/* + * Create or update a natural resource with a category + * + * Parameters: + * _name the identifier of the resource's name + * _description the identifier of the resource's description + * _category the identifier of the resource's category + * _weight the resource's ordering weight + * _presence the presence probability + * _quantity_avg the average quantity + * _quantity_dev the deviation from the average quantity + * _difficulty_avg the average extraction difficulty + * _difficulty_dev the deviation from the average extraction difficulty + * _recovery_avg the average recovery rate + * _recovery_dev the deviation from the average recovery rate + * + * Returns: + * ? the result code for the operation + */ +CREATE OR REPLACE FUNCTION defs.uoc_natural_resource( + _name TEXT , + _description TEXT , + _category TEXT , + _weight INT , + _presence DOUBLE PRECISION , + _quantity_avg DOUBLE PRECISION , + _quantity_dev DOUBLE PRECISION , + _difficulty_avg DOUBLE PRECISION , + _difficulty_dev DOUBLE PRECISION , + _recovery_avg DOUBLE PRECISION , + _recovery_dev DOUBLE PRECISION ) + RETURNS defs.resource_update_result + STRICT VOLATILE + SECURITY INVOKER +AS $$ + SELECT defs.uoc_natres_internal( $1 , $2 , $3 , $4 , $5 , $6 , $7 , $8 , + $9 , $10 , $11 ); +$$ LANGUAGE SQL; + +REVOKE EXECUTE + ON FUNCTION defs.uoc_natural_resource( TEXT , TEXT , TEXT , INT , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION ) + FROM PUBLIC; +GRANT EXECUTE + ON FUNCTION defs.uoc_natural_resource( TEXT , TEXT , TEXT , INT , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION , + DOUBLE PRECISION ) + TO :dbuser; diff --git a/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02500-uoc-resource-internal.sql b/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02500-uoc-resource-internal.sql new file mode 100644 index 0000000..10bee48 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02500-uoc-resource-internal.sql @@ -0,0 +1,74 @@ +/* + * Test the defs.uoc_resource_internal() function + */ +BEGIN; + + /* We need a few strings to be used when creating resource definitions, and a natural resource. */ + \i utils/strings.sql + \i utils/resources.sql + SELECT _create_test_strings( 7 ); + SELECT _create_natural_resources( 1 , 'natRes' ); + + /****** TESTS BEGIN HERE ******/ + SELECT plan( 15 ); + + SELECT diag_test_name( 'defs.uoc_resource_internal() - creation without category' ); + SELECT is( defs.uoc_resource_internal( 'test3' , 'test4' , NULL , 1 ) , 'CREATED' ); + SELECT diag_test_name( 'defs.uoc_resource_internal() - creation results without category' ); + SELECT results_eq( + $$ SELECT resource_name_id , resource_description_id , resource_weight + FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) + AND resource_category_id IS NULL $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , 1 ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_resource_internal() - creation with category' ); + SELECT is( defs.uoc_resource_internal( 'test5' , 'test6' , 'test7' , 1 ) , 'CREATED' ); + SELECT diag_test_name( 'defs.uoc_resource_internal() - creation results with category' ); + SELECT results_eq( + $$ SELECT * FROM defs.resources + WHERE resource_name_id = _get_string( 'test5' ) $$ , + $$ VALUES ( _get_string( 'test5' ) , _get_string( 'test6' ) , _get_string( 'test7' ) , 1 ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_resource_internal() - update without category' ); + SELECT is( defs.uoc_resource_internal( 'test3' , 'test7' , NULL , 2 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_resource_internal() - update results without category' ); + SELECT results_eq( + $$ SELECT resource_name_id , resource_description_id , resource_weight + FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) + AND resource_category_id IS NULL $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test7' ) , 2 ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_resource_internal() - update with category' ); + SELECT is( defs.uoc_resource_internal( 'test3' , 'test4' , 'test7' , 1 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_resource_internal() - update results with category' ); + SELECT results_eq( + $$ SELECT * FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , _get_string( 'test7' ) , 1 ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_resource_internal() - incorrect name string' ); + SELECT is( defs.uoc_resource_internal( 'does-not-exist' , 'test2' , NULL , 1 ) , 'BAD_STRINGS' ); + SELECT diag_test_name( 'defs.uoc_resource_internal() - incorrect description string' ); + SELECT is( defs.uoc_resource_internal( 'test1' , 'does-not-exist' , NULL , 1 ) , 'BAD_STRINGS' ); + SELECT diag_test_name( 'defs.uoc_resource_internal() - incorrect category string' ); + SELECT is( defs.uoc_resource_internal( 'test1' , 'test2' , 'does-not-exist' , 1 ) , 'BAD_STRINGS' ); + + SELECT diag_test_name( 'defs.uoc_resource_internal() - duplicate description on new resource' ); + SELECT is( defs.uoc_resource_internal( 'test1' , 'test4' , NULL , 1 ) , 'DUP_DESCR' ); + SELECT diag_test_name( 'defs.uoc_resource_internal() - duplicate description on existing resource' ); + SELECT is( defs.uoc_resource_internal( 'test5' , 'test4' , NULL , 1 ) , 'DUP_DESCR' ); + + SELECT diag_test_name( 'defs.uoc_resource_internal() - update on natural resource' ); + SELECT is( defs.uoc_resource_internal( 'natRes1' , 'test2' , NULL , 1 ) , 'BAD_TYPE' ); + + SELECT diag_test_name( 'defs.uoc_resource_internal() - weight <= 0' ); + SELECT is( defs.uoc_resource_internal( 'test1' , 'test2' , NULL , 0 ) , 'BAD_VALUE' ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02501-uoc-resource.sql b/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02501-uoc-resource.sql new file mode 100644 index 0000000..113634d --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02501-uoc-resource.sql @@ -0,0 +1,90 @@ +/* + * Test the defs.uoc_resource() functions + */ +BEGIN; + + /* We need a few strings to be used when creating resource definitions, and a natural resource. */ + \i utils/strings.sql + \i utils/resources.sql + SELECT _create_test_strings( 7 ); + SELECT _create_natural_resources( 1 , 'natRes' ); + + /****** TESTS BEGIN HERE ******/ + SELECT plan( 22 ); + + SELECT diag_test_name( 'defs.uoc_resource() - NULL name (no category)' ); + SELECT is( defs.uoc_resource( NULL , 'test2' , 1 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_resource() - NULL description (no category)' ); + SELECT is( defs.uoc_resource( 'test1' , NULL , 1 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_resource() - NULL weight (no category)' ); + SELECT is( defs.uoc_resource( 'test1' , 'test2' , NULL ) , NULL ); + + SELECT diag_test_name( 'defs.uoc_resource() - NULL name (with category)' ); + SELECT is( defs.uoc_resource( NULL , 'test2' , 'test3' , 1 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_resource() - NULL description (with category)' ); + SELECT is( defs.uoc_resource( 'test1' , NULL , 'test3' , 1 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_resource() - NULL category' ); + SELECT is( defs.uoc_resource( 'test1' , 'test2' , NULL , 1 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_resource() - NULL weight (with category)' ); + SELECT is( defs.uoc_resource( 'test1' , 'test2' , 'test3' , NULL ) , NULL ); + + SELECT diag_test_name( 'defs.uoc_resource() - creation without category' ); + SELECT is( defs.uoc_resource( 'test3' , 'test4' , 1 ) , 'CREATED' ); + SELECT diag_test_name( 'defs.uoc_resource() - creation results without category' ); + SELECT results_eq( + $$ SELECT resource_name_id , resource_description_id , resource_weight + FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) + AND resource_category_id IS NULL $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , 1 ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_resource() - creation with category' ); + SELECT is( defs.uoc_resource( 'test5' , 'test6' , 'test7' , 1 ) , 'CREATED' ); + SELECT diag_test_name( 'defs.uoc_resource() - creation results with category' ); + SELECT results_eq( + $$ SELECT * FROM defs.resources + WHERE resource_name_id = _get_string( 'test5' ) $$ , + $$ VALUES ( _get_string( 'test5' ) , _get_string( 'test6' ) , _get_string( 'test7' ) , 1 ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_resource() - update without category' ); + SELECT is( defs.uoc_resource( 'test3' , 'test7', 2 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_resource() - update results without category' ); + SELECT results_eq( + $$ SELECT resource_name_id , resource_description_id , resource_weight + FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) + AND resource_category_id IS NULL $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test7' ) , 2 ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_resource() - update with category' ); + SELECT is( defs.uoc_resource( 'test3' , 'test4' , 'test7' , 1 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_resource() - update results with category' ); + SELECT results_eq( + $$ SELECT * FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , _get_string( 'test7' ) , 1 ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_resource() - incorrect name string' ); + SELECT is( defs.uoc_resource( 'does-not-exist' , 'test2' , 1 ) , 'BAD_STRINGS' ); + SELECT diag_test_name( 'defs.uoc_resource() - incorrect description string' ); + SELECT is( defs.uoc_resource( 'test1' , 'does-not-exist' , 1 ) , 'BAD_STRINGS' ); + SELECT diag_test_name( 'defs.uoc_resource() - incorrect category string' ); + SELECT is( defs.uoc_resource( 'test1' , 'test2' , 'does-not-exist' , 1 ) , 'BAD_STRINGS' ); + + SELECT diag_test_name( 'defs.uoc_resource() - duplicate description on new resource' ); + SELECT is( defs.uoc_resource( 'test1' , 'test4' , 1 ) , 'DUP_DESCR' ); + SELECT diag_test_name( 'defs.uoc_resource() - duplicate description on existing resource' ); + SELECT is( defs.uoc_resource( 'test5' , 'test4' , 1 ) , 'DUP_DESCR' ); + + SELECT diag_test_name( 'defs.uoc_resource() - update on natural resource' ); + SELECT is( defs.uoc_resource( 'natRes1' , 'test2' , 1 ) , 'BAD_TYPE' ); + + SELECT diag_test_name( 'defs.uoc_resource() - weight <= 0' ); + SELECT is( defs.uoc_resource( 'natRes1' , 'test2' , 0 ) , 'BAD_VALUE' ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02502-uoc-natres-internal.sql b/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02502-uoc-natres-internal.sql new file mode 100644 index 0000000..d442020 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02502-uoc-natres-internal.sql @@ -0,0 +1,175 @@ +/* + * Test the defs.uoc_natres_internal() function + */ +BEGIN; + + /* We need a few strings to be used when creating resource definitions, and a basic resource. */ + \i utils/strings.sql + \i utils/resources.sql + SELECT _create_test_strings( 7 ); + SELECT _create_resources( 1 , 'basicRes' ); + + /****** TESTS BEGIN HERE ******/ + SELECT plan( 37 ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - creation without category' ); + SELECT is( defs.uoc_natres_internal( 'test3' , 'test4' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'CREATED' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - creation results without category - basic' ); + SELECT results_eq( + $$ SELECT resource_name_id , resource_description_id , resource_weight + FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) + AND resource_category_id IS NULL $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , 1 ) $$ + ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - creation results without category - nat. res.' ); + SELECT results_eq( + $$ SELECT * FROM defs.natural_resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , 0.5::DOUBLE PRECISION , 100::DOUBLE PRECISION , + 50::DOUBLE PRECISION , 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION , + 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - creation with category' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test6' , 'test7' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'CREATED' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - creation results with category - basic' ); + SELECT results_eq( + $$ SELECT * FROM defs.resources + WHERE resource_name_id = _get_string( 'test5' ) $$ , + $$ VALUES ( _get_string( 'test5' ) , _get_string( 'test6' ) , _get_string( 'test7' ) , 1 ) $$ + ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - creation results with category - nat. res.' ); + SELECT results_eq( + $$ SELECT * FROM defs.natural_resources + WHERE resource_name_id = _get_string( 'test5' ) $$ , + $$ VALUES ( _get_string( 'test5' ) , 0.5::DOUBLE PRECISION , 100::DOUBLE PRECISION , + 50::DOUBLE PRECISION , 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION , + 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - update without category' ); + SELECT is( defs.uoc_natres_internal( 'test3' , 'test7' , NULL , 2 , + 0.3 , 300 , 30 , 0.3 , 0.03 , 0.3 , 0.03 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - update results without category - basic' ); + SELECT results_eq( + $$ SELECT resource_name_id , resource_description_id , resource_weight + FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) + AND resource_category_id IS NULL $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test7' ) , 2 ) $$ + ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - update results without category - nat. res.' ); + SELECT results_eq( + $$ SELECT * FROM defs.natural_resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , 0.3::DOUBLE PRECISION , 300::DOUBLE PRECISION , + 30::DOUBLE PRECISION , 0.3::DOUBLE PRECISION , 0.03::DOUBLE PRECISION , + 0.3::DOUBLE PRECISION , 0.03::DOUBLE PRECISION ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - update with category' ); + SELECT is( defs.uoc_natres_internal( 'test3' , 'test4' , 'test7' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - update results with category' ); + SELECT results_eq( + $$ SELECT * FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , _get_string( 'test7' ) , 1 ) $$ + ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - update results with category - nat. res.' ); + SELECT results_eq( + $$ SELECT * FROM defs.natural_resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , 0.5::DOUBLE PRECISION , 100::DOUBLE PRECISION , + 50::DOUBLE PRECISION , 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION , + 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - incorrect name string' ); + SELECT is( defs.uoc_natres_internal( 'does-not-exist' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_STRINGS' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - incorrect description string' ); + SELECT is( defs.uoc_natres_internal( 'test1' , 'does-not-exist' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_STRINGS' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - incorrect category string' ); + SELECT is( defs.uoc_natres_internal( 'test1' , 'test2' , 'does-not-exist' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_STRINGS' ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - duplicate description on new resource' ); + SELECT is( defs.uoc_natres_internal( 'test1' , 'test4' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'DUP_DESCR' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - duplicate description on existing resource' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test4' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'DUP_DESCR' ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - update on basic resource' ); + SELECT is( defs.uoc_natres_internal( 'basicRes1' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_TYPE' ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - weight <= 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 0 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - P(presence) <= 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - P(presence) >= 1' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - max. quantity <= 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 0 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - quantity dev. < 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , -1 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - quantity max. - dev. <= 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 100 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - difficulty max. = 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0 , 0 , 0.5 , 0.05 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - difficulty max. < 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , -0.00001 , 0 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - difficulty max. = 1' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 1 , 0 , 0.5 , 0.05 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - difficulty max. > 1' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 1.00001 , 0 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - difficulty dev. < 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , -1 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - difficulty max. - dev. < 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.25 , 0.5 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - difficulty max. + dev. > 1' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.75 , 0.5 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - recovery max. <= 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0 , 0 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - recovery max. = 1' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 1 , 0 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - recovery max. > 1' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 1.0001 , 0 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - recovery dev. < 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , -0.25 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - recovery max. - dev. <= 0' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.25 , 0.25 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natres_internal() - recovery max. + dev. > 1' ); + SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.75 , 0.5 ) , 'BAD_VALUE' ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02503-uoc-natural-resource.sql b/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02503-uoc-natural-resource.sql new file mode 100644 index 0000000..3f8f119 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/functions/defs/02503-uoc-natural-resource.sql @@ -0,0 +1,242 @@ +/* + * Test the defs.uoc_natural_resource() functions + */ +BEGIN; + + /* We need a few strings to be used when creating resource definitions, and a basic resource. */ + \i utils/strings.sql + \i utils/resources.sql + SELECT _create_test_strings( 7 ); + SELECT _create_resources( 1 , 'basicRes' ); + + /****** TESTS BEGIN HERE ******/ + SELECT plan( 58 ); + + + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL name (no category)' ); + SELECT is( defs.uoc_natural_resource( NULL , 'test4' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL description (no category)' ); + SELECT is( defs.uoc_natural_resource( 'test1' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL weight (no category)' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , NULL , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL presence (no category)' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 , + NULL , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average quantity (no category)' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 , + 0.5 , NULL , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL quantity deviation (no category)' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 , + 0.5 , 100 , NULL , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average difficulty (no category)' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 , + 0.5 , 100 , 50 , NULL , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL difficulty deviation (no category)' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 , + 0.5 , 100 , 50 , 0.5 , NULL , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average recovery (no category)' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , NULL , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL recovery deviation (no category)' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , NULL ) , NULL ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL name' ); + SELECT is( defs.uoc_natural_resource( NULL , 'test4' , 'test5' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL description' ); + SELECT is( defs.uoc_natural_resource( 'test1' , NULL , 'test5' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL category' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL weight' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , NULL , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL presence' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 , + NULL , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average quantity' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 , + 0.5 , NULL , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL quantity deviation' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 , + 0.5 , 100 , NULL , 0.5 , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average difficulty' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 , + 0.5 , 100 , 50 , NULL , 0.05 , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL difficulty deviation' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 , + 0.5 , 100 , 50 , 0.5 , NULL , 0.5 , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL average recovery' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , NULL , 0.05 ) , NULL ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL recovery deviation' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 'test5' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , NULL ) , NULL ); + + + SELECT diag_test_name( 'defs.uoc_natural_resource() - creation without category' ); + SELECT is( defs.uoc_natural_resource( 'test3' , 'test4' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'CREATED' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - creation results without category - basic' ); + SELECT results_eq( + $$ SELECT resource_name_id , resource_description_id , resource_weight + FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) + AND resource_category_id IS NULL $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , 1 ) $$ + ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - creation results without category - nat. res.' ); + SELECT results_eq( + $$ SELECT * FROM defs.natural_resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , 0.5::DOUBLE PRECISION , 100::DOUBLE PRECISION , + 50::DOUBLE PRECISION , 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION , + 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - creation with category' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test6' , 'test7' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'CREATED' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - creation results with category - basic' ); + SELECT results_eq( + $$ SELECT * FROM defs.resources + WHERE resource_name_id = _get_string( 'test5' ) $$ , + $$ VALUES ( _get_string( 'test5' ) , _get_string( 'test6' ) , _get_string( 'test7' ) , 1 ) $$ + ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - creation results with category - nat. res.' ); + SELECT results_eq( + $$ SELECT * FROM defs.natural_resources + WHERE resource_name_id = _get_string( 'test5' ) $$ , + $$ VALUES ( _get_string( 'test5' ) , 0.5::DOUBLE PRECISION , 100::DOUBLE PRECISION , + 50::DOUBLE PRECISION , 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION , + 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - update without category' ); + SELECT is( defs.uoc_natural_resource( 'test3' , 'test7' , 2 , + 0.3 , 300 , 30 , 0.3 , 0.03 , 0.3 , 0.03 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - update results without category - basic' ); + SELECT results_eq( + $$ SELECT resource_name_id , resource_description_id , resource_weight + FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) + AND resource_category_id IS NULL $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test7' ) , 2 ) $$ + ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - update results without category - nat. res.' ); + SELECT results_eq( + $$ SELECT * FROM defs.natural_resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , 0.3::DOUBLE PRECISION , 300::DOUBLE PRECISION , + 30::DOUBLE PRECISION , 0.3::DOUBLE PRECISION , 0.03::DOUBLE PRECISION , + 0.3::DOUBLE PRECISION , 0.03::DOUBLE PRECISION ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - update with category' ); + SELECT is( defs.uoc_natural_resource( 'test3' , 'test4' , 'test7' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - update results with category' ); + SELECT results_eq( + $$ SELECT * FROM defs.resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , _get_string( 'test4' ) , _get_string( 'test7' ) , 1 ) $$ + ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - update results with category - nat. res.' ); + SELECT results_eq( + $$ SELECT * FROM defs.natural_resources + WHERE resource_name_id = _get_string( 'test3' ) $$ , + $$ VALUES ( _get_string( 'test3' ) , 0.5::DOUBLE PRECISION , 100::DOUBLE PRECISION , + 50::DOUBLE PRECISION , 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION , + 0.5::DOUBLE PRECISION , 0.05::DOUBLE PRECISION ) $$ + ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - incorrect name string' ); + SELECT is( defs.uoc_natural_resource( 'does-not-exist' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_STRINGS' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - incorrect description string' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'does-not-exist' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_STRINGS' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - incorrect category string' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test2' , 'does-not-exist' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_STRINGS' ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - duplicate description on new resource' ); + SELECT is( defs.uoc_natural_resource( 'test1' , 'test4' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'DUP_DESCR' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - duplicate description on existing resource' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test4' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'DUP_DESCR' ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - update on basic resource' ); + SELECT is( defs.uoc_natural_resource( 'basicRes1' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_TYPE' ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - weight <= 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 0 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - P(presence) <= 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - P(presence) >= 1' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - max. quantity <= 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 0 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - quantity dev. < 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , -1 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - quantity max. - dev. <= 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 100 , 0.5 , 0.05 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. = 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0 , 0 , 0.5 , 0.05 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. < 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , -0.00001 , 0 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. = 1' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 1 , 0 , 0.5 , 0.05 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. > 1' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 1.00001 , 0 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty dev. < 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , -1 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. - dev. < 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.25 , 0.5 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - difficulty max. + dev. > 1' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.75 , 0.5 , 0.5 , 0.05 ) , 'BAD_VALUE' ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. <= 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0 , 0 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. = 1' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 1 , 0 ) , 'UPDATED' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. > 1' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 1.0001 , 0 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery dev. < 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , -0.25 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. - dev. <= 0' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.25 , 0.25 ) , 'BAD_VALUE' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - recovery max. + dev. > 1' ); + SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.75 , 0.5 ) , 'BAD_VALUE' ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds/doc/TODO.txt b/legacyworlds/doc/TODO.txt index 448622a..6c64d19 100644 --- a/legacyworlds/doc/TODO.txt +++ b/legacyworlds/doc/TODO.txt @@ -16,7 +16,13 @@ SERVER & DATABASE: ! Add some form of database version control to allow easier updates -> existing options were investigated, they are unsatisfactory - * Replace all single-precision reals with double precision reals + ! SQL code clean-up: + * Replace all single-precision reals with double precision reals + * Make sure internal functions cannot be called by the main user + * Make sure functions that are supposed to be executed by the main + user are not public + * Rename all views to v_* + * Rename all table fields to use a prefix * Add a tool to initialise the database