Game updates improvements

* Added a set of tables which define game updates and their targets.
These definitions replace the old enumerate type. Added a set of
triggers which automatically create specific update tables, insert
missing entries, etc... when game update types are being manipulated.

* Removed manual insertion of game updates from empire creation
function and universe generator.

* Added registration of core update targets (i.e. planets and empires),
updated all existing game update processing functions and added type
registrations

* Created Maven project for game updates control components, moved
existing components from the -simple project, rewritten most of what
they contained, added new components for server-side update batch
processing
This commit is contained in:
Emmanuel BENOîT 2012-02-03 16:25:03 +01:00
parent ba6a1e2b41
commit 56eddcc4f0
93 changed files with 4004 additions and 578 deletions

View file

@ -0,0 +1,70 @@
/*
* Tests of the update target definitions
*/
BEGIN;
-- Make sure we get rid of all existing definitions first
DELETE FROM sys.update_types;
DELETE FROM sys.update_targets;
-- Create a test schema & table which will be used as the target
CREATE SCHEMA test
CREATE TABLE test(
test_id SERIAL NOT NULL PRIMARY KEY
)
CREATE TABLE test2(
test_id SERIAL NOT NULL PRIMARY KEY
);
-- ***** TESTS BEGIN HERE *****
SELECT plan( 8 );
SELECT diag_test_name( 'Update target definitions - Failure if schema/table do not exist' );
SELECT throws_ok(
$$ INSERT INTO sys.update_targets(
updtgt_name , updtgt_schema , updtgt_table
) VALUES (
'Test' , 'test' , 'does_not_exist'
); $$ ,
23503
);
SELECT diag_test_name( 'Update target definitions - Successful insertion' );
SELECT lives_ok( $$
INSERT INTO sys.update_targets(
updtgt_name , updtgt_schema , updtgt_table
) VALUES (
'Test' , 'test' , 'test'
);
$$ );
SELECT diag_test_name( 'Update target definitions - Specific update table - Exists after insertion' );
SELECT has_table( 'test' , 'test_updates' , 'No specific update table' );
SELECT diag_test_name( 'Update target definitions - Specific update table - Primary key' );
SELECT index_is_primary( 'test' , 'test_updates' , 'test_updates_pkey' );
SELECT diag_test_name( 'Update target definitions - Specific update table - Primary key definition' );
SELECT has_index( 'test' , 'test_updates' , 'test_updates_pkey' ,
ARRAY[ 'updtgt_id' , 'updtype_id' , 'update_id' , 'test_id' ] );
SELECT diag_test_name( 'Update target definitions - Duplicate name' );
SELECT throws_ok( $$
INSERT INTO sys.update_targets(
updtgt_name , updtgt_schema , updtgt_table
) VALUES (
'Test' , 'test' , 'test2'
);
$$ , 23505 );
SELECT diag_test_name( 'Update target definitions - Duplicate table reference' );
SELECT throws_ok( $$
INSERT INTO sys.update_targets(
updtgt_name , updtgt_schema , updtgt_table
) VALUES (
'Test 2' , 'test' , 'test'
);
$$ , 23505 );
SELECT diag_test_name( 'Update target definitions - Deletion drops the specific update table' );
DELETE FROM sys.update_targets;
SELECT hasnt_table( 'test' , 'test_updates' , 'Specific update table still exists' );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,150 @@
/*
* Tests of the update type definitions
*/
BEGIN;
-- Make sure we get rid of all existing definitions first
DELETE FROM sys.update_types;
DELETE FROM sys.update_targets;
-- Create a test schema & tables which will be used as targets
CREATE SCHEMA test
CREATE TABLE test(
test_id SERIAL NOT NULL PRIMARY KEY
);
-- Create target definitions
INSERT INTO sys.update_targets(
updtgt_id , updtgt_name , updtgt_schema , updtgt_table
) VALUES (
1 , 'Test 1' , 'test' , 'test'
);
-- Create a dummy stored procedure in the sys schema
CREATE FUNCTION sys.dummy_update_func( c_tick BIGINT )
RETURNS INT LANGUAGE SQL AS 'SELECT 1;';
-- ***** TESTS BEGIN HERE *****
SELECT plan( 14 );
SELECT diag_test_name( 'Update types - Definition with invalid target' );
SELECT throws_ok( $$
INSERT INTO sys.update_types(
updtype_id , updtgt_id , updtype_name , updtype_description , updtype_ordering
) VALUES (
1 , 5 , 'Test' , '' , 1
);
$$ , 23503 );
SELECT diag_test_name( 'Update types - Definition with invalid stored procedure name' );
SELECT throws_ok( $$
INSERT INTO sys.update_types(
updtype_id , updtgt_id , updtype_name , updtype_description , updtype_ordering , updtype_proc_name
) VALUES (
1 , 5 , 'Test' , '' , 1 , 'does_not_exist'
);
$$ , 'P0001' );
SELECT diag_test_name( 'Update types - Successful insertion with no procedure name' );
SELECT lives_ok( $$
INSERT INTO sys.update_types(
updtype_id , updtgt_id , updtype_name , updtype_description , updtype_ordering
) VALUES (
1 , 1 , 'Test' , '' , 1
);
$$ );
DELETE FROM sys.update_types;
SELECT diag_test_name( 'Update types - Successful insertion with a procedure name' );
SELECT lives_ok( $$
INSERT INTO sys.update_types(
updtype_id , updtgt_id , updtype_name , updtype_description , updtype_ordering , updtype_proc_name
) VALUES (
1 , 1 , 'Test' , '' , 1 , 'dummy_update_func'
);
$$ );
SELECT diag_test_name( 'Update types - Setting an existing row''s stored procedure to something invalid fails' );
SELECT throws_ok( $$
UPDATE sys.update_types
SET updtype_proc_name = 'does_not_exist'
WHERE updtype_id = 1
$$ , 'P0001' );
SELECT diag_test_name( 'Update types - Failure on duplicate name' );
SELECT throws_ok( $$
INSERT INTO sys.update_types(
updtype_id , updtgt_id , updtype_name , updtype_description , updtype_ordering
) VALUES (
2 , 1 , 'Test' , '' , 1
);
$$ , 23505 );
SELECT diag_test_name( 'Update types - Failure on duplicate stored procedure' );
SELECT throws_ok( $$
INSERT INTO sys.update_types(
updtype_id , updtgt_id , updtype_name , updtype_description , updtype_ordering , updtype_proc_name
) VALUES (
3 , 1 , 'Test 2' , '' , 1 , 'dummy_update_func'
);
$$ , 23505 );
SELECT diag_test_name( 'Update types - Target deletion fails if type definitions exist' );
SELECT throws_ok( $$ DELETE FROM sys.update_targets WHERE updtgt_id = 1 $$ , 23503 );
DELETE FROM sys.update_types;
INSERT INTO sys.update_types(
updtype_id , updtgt_id , updtype_name , updtype_description , updtype_ordering
) VALUES (
1 , 1 , 'Test 1' , '' , 1
) , (
2 , 1 , 'Test 3' , '' , 75
) , (
3 , 1 , 'Test 2' , '' , 2
);
SELECT diag_test_name( 'Update types - Ordering column is updated' );
SELECT set_eq(
$$ SELECT updtype_id , updtype_ordering FROM sys.update_types $$ ,
$$ VALUES ( 1 , 2 ) , ( 3 , 4 ) , ( 2 , 6 ) $$
);
DELETE FROM sys.update_types;
INSERT INTO test.test VALUES ( 1 ) , ( 2 );
INSERT INTO sys.update_types(
updtype_id , updtgt_id , updtype_name , updtype_description , updtype_ordering
) VALUES (
1 , 1 , 'Test 1' , '' , 1
);
SELECT diag_test_name( 'Update types - Type creation adds specific rows for missing items' );
SELECT set_eq(
$$ SELECT updtgt_id , updtype_id , test_id FROM test.test_updates $$ ,
$$ VALUES ( 1 , 1 , 1 ) , ( 1 , 1 , 2 ) $$
);
SELECT diag_test_name( 'Update types - Type creation adds genertic rows for missing items' );
SELECT set_eq(
$$ SELECT test_id , update_state , update_last
FROM test.test_updates
INNER JOIN sys.updates USING ( updtgt_id , updtype_id , update_id ) $$ ,
$$ VALUES ( 1 , 'FUTURE'::sys.update_state_type , -1 ) ,
( 2 , 'FUTURE'::sys.update_state_type , '-1' ) $$
);
INSERT INTO test.test VALUES ( 3 );
SELECT diag_test_name( 'Update types - New items have automatically inserted update rows' );
SELECT set_eq(
$$ SELECT test_id , updtype_id , update_state , update_last
FROM test.test_updates
INNER JOIN sys.updates USING ( updtgt_id , updtype_id , update_id )
WHERE test_id = 3 $$ ,
$$ VALUES ( 3 , 1 , 'FUTURE'::sys.update_state_type , -1 ) $$
);
DELETE FROM sys.update_types WHERE updtype_id = 1;
SELECT diag_test_name( 'Update types - Type deletion cascades to specific rows' );
SELECT is_empty( $$ SELECT * FROM test.test_updates $$ );
SELECT diag_test_name( 'Update types - Type deletion cascades to generic rows' );
SELECT is_empty( $$ SELECT * FROM sys.updates $$ );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -16,7 +16,7 @@ BEGIN;
SELECT _create_emp_names( 1 , 'testEmp' );
/***** TESTS BEGIN HERE *****/
SELECT plan( 8 );
SELECT plan( 7 );
SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) ,
_get_map_name( 'testPlanet1' ) ,
@ -57,16 +57,5 @@ BEGIN;
FROM emp.resources
WHERE empire_id = _get_emp_name( 'testEmp1' );
SELECT diag_test_name( 'emp.create_empire() - Empire update records' );
SELECT is( _eur.quantity , _utv.quantity)
FROM (
SELECT COUNT(*) AS quantity FROM emp.updates
WHERE empire_id = _get_emp_name( 'testEmp1' )
) AS _eur , (
SELECT COUNT(*) AS quantity
FROM unnest( enum_range( NULL::update_type ) ) AS _row
WHERE _row::TEXT LIKE 'EMPIRE_%'
) AS _utv;
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,53 @@
/*
* Test the sys.start_tick() function
*/
BEGIN;
-- Delete all registered update types and targets
DELETE FROM sys.update_types;
DELETE FROM sys.update_targets;
-- Create a new update target / type which will be used in tests
CREATE SCHEMA test
CREATE TABLE test( test_id INT NOT NULL PRIMARY KEY );
INSERT INTO sys.update_targets VALUES ( 1 , 'Test' , 'test' , 'test' );
INSERT INTO sys.update_types VALUES ( 1 , 1 , 'Test' , 2 , '' , NULL , NULL );
-- Insert a few update rows
INSERT INTO test.test VALUES ( 1 ) , ( 2 ) , ( 3 );
-- ***** TESTS BEGIN HERE *****
SELECT plan( 5 );
UPDATE sys.status SET maintenance_start = now( ) , current_tick = NULL;
SELECT diag_test_name( 'sys.start_tick() - Ticks do not start if maintenance mode is active' );
SELECT ok( sys.start_tick( ) IS NULL );
UPDATE sys.status SET maintenance_start = NULL , current_tick = next_tick;
SELECT diag_test_name( 'sys.start_tick() - Ticks do not start if current_tick is set' );
SELECT ok( sys.start_tick( ) IS NULL );
UPDATE sys.status SET current_tick = NULL , next_tick = 2;
UPDATE sys.updates SET update_state = 'PROCESSED' , update_last = -1;
UPDATE sys.updates su SET update_last = 514
FROM test.test_updates tu
WHERE su.update_id = tu.update_id
AND tu.test_id = 3;
SELECT diag_test_name( 'sys.start_tick() - Returns next tick identifier when ticks can start' );
SELECT is( sys.start_tick( ) , 2::BIGINT );
SELECT diag_test_name( 'sys.start_tick() - System status is updated' );
SELECT set_eq(
$$ SELECT current_tick , next_tick FROM sys.status $$ ,
$$ VALUES ( 2 , 3 ) $$
);
SELECT diag_test_name( 'sys.start_tick() - Update rows are marked for processing when necessary' );
SELECT set_eq(
$$ SELECT test_id
FROM test.test_updates
INNER JOIN sys.updates
USING ( update_id , updtgt_id , updtype_id )
WHERE update_state = 'FUTURE' AND update_last = 2 $$ ,
$$ VALUES ( 1 ) , ( 2 ) $$
);
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,60 @@
/*
* Tests of the sys.check_stuck_tick() function
*/
BEGIN;
-- Delete all registered update types and targets
DELETE FROM sys.update_types;
DELETE FROM sys.update_targets;
-- Create a new update target / type which will be used in tests
CREATE SCHEMA test
CREATE TABLE test( test_id INT NOT NULL PRIMARY KEY );
INSERT INTO sys.update_targets VALUES ( 1 , 'Test' , 'test' , 'test' );
INSERT INTO sys.update_types VALUES ( 1 , 1 , 'Test' , 2 , '' , NULL , NULL );
-- Insert a few update rows
INSERT INTO test.test VALUES ( 1 ) , ( 2 ) , ( 3 );
-- Replace sys.end_tick() with a function that inserts into some table
CREATE TABLE end_tick_calls( tick_id BIGINT NOT NULL );
CREATE OR REPLACE FUNCTION sys.end_tick( tick_id BIGINT )
RETURNS VOID LANGUAGE SQL
AS 'INSERT INTO end_tick_calls VALUES( $1 );';
-- ***** TESTS BEGIN HERE *****
SELECT plan( 8 );
UPDATE sys.status SET maintenance_start = NULL , current_tick = NULL;
SELECT diag_test_name( 'sys.check_stuck_tick() - Returns NULL if no tick is in progress' );
SELECT ok( sys.check_stuck_tick() IS NULL );
SELECT diag_test_name( 'sys.check_stuck_tick() - Does not call sys.end_tick() if no tick is in progress' );
SELECT is_empty( $$ SELECT * FROM end_tick_calls $$ );
DELETE FROM end_tick_calls;
UPDATE sys.status SET maintenance_start = now( ) , current_tick = 2;
SELECT diag_test_name( 'sys.check_stuck_tick() - Returns NULL if maintenance mode is enabled' );
SELECT ok( sys.check_stuck_tick() IS NULL );
SELECT diag_test_name( 'sys.check_stuck_tick() - Does not call sys.end_tick() if maintenance mode is enabled' );
SELECT is_empty( $$ SELECT * FROM end_tick_calls $$ );
DELETE FROM end_tick_calls;
UPDATE sys.status SET maintenance_start = NULL , current_tick = 2;
UPDATE sys.updates SET update_state = 'PROCESSED' , update_last = 2;
SELECT diag_test_name( 'sys.check_stuck_tick() - Returns NULL if a tick is in progress but there are no updates left' );
SELECT ok( sys.check_stuck_tick() IS NULL );
SELECT diag_test_name( 'sys.check_stuck_tick() - Calls sys.end_tick() if a tick is in progress but there are no updates left' );
SELECT set_eq(
$$ SELECT * FROM end_tick_calls $$ ,
$$ VALUES ( 2 ) $$
);
DELETE FROM end_tick_calls;
UPDATE sys.status SET maintenance_start = NULL , current_tick = 2;
UPDATE sys.updates SET update_state = 'FUTURE' , update_last = 2;
SELECT diag_test_name( 'sys.check_stuck_tick() - Returns tick identifier if a tick is in progress and there are updates left' );
SELECT is( sys.check_stuck_tick() , 2::BIGINT );
SELECT diag_test_name( 'sys.check_stuck_tick() - Does not call sys.end_tick() if a tick is in progress and there are updates left' );
SELECT is_empty( $$ SELECT * FROM end_tick_calls $$ );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,179 @@
/*
* Tests for the sys.process_updates() function
*/
BEGIN;
-- Delete all registered update types and targets
DELETE FROM sys.update_types;
DELETE FROM sys.update_targets;
-- Create a new update target / type which will be used in tests
CREATE SCHEMA test
CREATE TABLE test( test_id INT NOT NULL PRIMARY KEY );
INSERT INTO sys.update_targets VALUES ( 1 , 'Test' , 'test' , 'test' );
INSERT INTO sys.update_types VALUES ( 1 , 1 , 'Test 1' , 2 , '' , NULL , NULL );
INSERT INTO sys.update_types VALUES ( 2 , 1 , 'Test 2' , 4 , '' , NULL , NULL );
-- Replace sys.end_tick() with a function that inserts into some table
CREATE TABLE end_tick_calls( tick_id BIGINT NOT NULL );
CREATE OR REPLACE FUNCTION sys.end_tick( tick_id BIGINT )
RETURNS VOID LANGUAGE SQL
AS 'INSERT INTO end_tick_calls VALUES( $1 );';
-- Create a sys.process_dummy_update() function that inserts into some other table
CREATE TABLE process_update_calls( tick_id BIGINT NOT NULL );
CREATE OR REPLACE FUNCTION sys.process_dummy_update( tick_id BIGINT )
RETURNS VOID LANGUAGE SQL
AS 'INSERT INTO process_update_calls VALUES( $1 );';
-- Set the general batch size to 1
SELECT sys.uoc_constant( 'game.batchSize' , '(test)' , 'Game updates' , 1 );
-- Insert a few entries in the test table
INSERT INTO test.test
SELECT * FROM generate_series( 1 , 10 );
-- ***** TESTS BEGIN HERE *****
SELECT plan( 21 );
/*
* Case:
* Updates have not been processed yet,
*
* Expected results:
* 1) New updates of the first type have been marked for processing,
* 2) The quantity of updates marked for processing corresponds to game.batchSize
*/
UPDATE sys.updates
SET update_state = 'FUTURE' , update_last = 1;
SELECT diag_test_name( 'sys.process_updates() - General batch size - Return value' );
SELECT ok( sys.process_updates( 1 ) = ROW( TRUE , 'Test 1'::TEXT ) );
SELECT diag_test_name( 'sys.process_updates() - General batch size - Update type' );
SELECT set_eq(
$$ SELECT DISTINCT updtype_id FROM sys.updates WHERE update_state = 'PROCESSING'; $$ ,
$$ VALUES ( 1 ) $$
);
SELECT diag_test_name( 'sys.process_updates() - General batch size - Update count' );
SELECT is( COUNT(*)::INT , 1 ) FROM sys.updates WHERE update_state = 'PROCESSING';
SELECT diag_test_name( 'sys.process_updates() - General batch size - sys.end_tick() not called' );
SELECT is_empty( $$ SELECT * FROM end_tick_calls $$ );
DELETE FROM end_tick_calls;
/*
* Case:
* Updates have not been processed yet,
* Update type has a specific batch size,
*
* Expected results:
* 1) New updates have been marked for processing,
* 2) The quantity of updates marked for processing corresponds to the specific batch size
* 3) sys.end_tick() has not been called
*/
UPDATE sys.update_types SET updtype_batch_size = 2 WHERE updtype_id = 1;
UPDATE sys.updates SET update_state = 'FUTURE' , update_last = 1;
SELECT diag_test_name( 'sys.process_updates() - Specific batch size - Return value' );
SELECT ok( sys.process_updates( 1 ) = ROW( TRUE , 'Test 1'::TEXT ) );
SELECT diag_test_name( 'sys.process_updates() - Specific batch size - Update type' );
SELECT set_eq(
$$ SELECT DISTINCT updtype_id FROM sys.updates WHERE update_state = 'PROCESSING'; $$ ,
$$ VALUES ( 1 ) $$
);
SELECT diag_test_name( 'sys.process_updates() - Specific batch size - Update count' );
SELECT is( COUNT(*)::INT , 2 ) FROM sys.updates WHERE update_state = 'PROCESSING';
SELECT diag_test_name( 'sys.process_updates() - Specific batch size - sys.end_tick() not called' );
SELECT is_empty( $$ SELECT * FROM end_tick_calls $$ );
DELETE FROM end_tick_calls;
/*
* Case:
* Some but not all updates of the first type have been processed,
* Batch size is greater than the quantity of updates left to process for the type.
*
* Expected results:
* 1) All updates of the first type are marked either for processing or as processed.
* 2) sys.end_tick() has not been called
*/
UPDATE sys.update_types SET updtype_batch_size = 2 WHERE updtype_id = 1;
UPDATE sys.updates SET update_state = 'FUTURE' , update_last = 1;
UPDATE sys.updates
SET update_state = 'PROCESSED'
WHERE update_id IN (
SELECT update_id FROM sys.updates WHERE updtype_id = 1 LIMIT 9
);
UPDATE sys.update_types SET updtype_batch_size = 2 WHERE updtype_id = 1;
SELECT diag_test_name( 'sys.process_updates() - End of type updates - Return value' );
SELECT ok( sys.process_updates( 1 ) = ROW( TRUE , 'Test 1'::TEXT ) );
SELECT diag_test_name( 'sys.process_updates() - End of type updates - Update type' );
SELECT set_eq(
$$ SELECT DISTINCT updtype_id FROM sys.updates WHERE update_state <> 'FUTURE'; $$ ,
$$ VALUES ( 1 ) $$
);
SELECT diag_test_name( 'sys.process_updates() - End of type updates - Update count' );
SELECT is( COUNT(*)::INT , 10 ) FROM sys.updates WHERE update_state <> 'FUTURE';
SELECT diag_test_name( 'sys.process_updates() - End of type updates - sys.end_tick() not called' );
SELECT is_empty( $$ SELECT * FROM end_tick_calls $$ );
DELETE FROM end_tick_calls;
/*
* Case:
* All updates of the first type have been processed.
* No updates of the second type have been processed.
*
* Expected results:
* 1) Some updates from the second type have been marked for processing.
* 2) sys.end_tick() has not been called
*/
UPDATE sys.updates SET update_state = 'FUTURE' , update_last = 1;
UPDATE sys.updates
SET update_state = 'PROCESSED'
WHERE updtype_id = 1;
SELECT diag_test_name( 'sys.process_updates() - Update type transition - Return value' );
SELECT ok( sys.process_updates( 1 ) = ROW( TRUE , 'Test 2'::TEXT ) );
SELECT diag_test_name( 'sys.process_updates() - Update type transition - Update type' );
SELECT set_eq(
$$ SELECT DISTINCT updtype_id FROM sys.updates WHERE update_state = 'PROCESSING'; $$ ,
$$ VALUES ( 2 ) $$
);
SELECT diag_test_name( 'sys.process_updates() - Update type transition - Update count' );
SELECT is( COUNT(*)::INT , 1 ) FROM sys.updates WHERE update_state = 'PROCESSING';
SELECT diag_test_name( 'sys.process_updates() - Update type transition - sys.end_tick() not called' );
SELECT is_empty( $$ SELECT * FROM end_tick_calls $$ );
DELETE FROM end_tick_calls;
/*
* Case:
* There no updates to process
* Updates are marked as 'PROCESSING'
*
* Expected results:
* 1) _has_more return value is FALSE,
* 2) All updates are marked as 'PROCESSED'
* 3) sys.end_tick() has been called.
*/
UPDATE sys.updates SET update_state = 'PROCESSING' , update_last = 1;
SELECT diag_test_name( 'sys.process_updates() - No updates left - Return value' );
SELECT ok( sys.process_updates( 1 ) = ROW( FALSE , NULL::TEXT ) );
SELECT diag_test_name( 'sys.process_updates() - No updates left - All updates processed' );
SELECT is_empty( $$ SELECT * FROM sys.updates WHERE update_state <> 'PROCESSED' $$ );
SELECT diag_test_name( 'sys.process_updates() - No updates left - sys.end_tick() called' );
SELECT set_eq( $$ SELECT * FROM end_tick_calls $$ , $$ VALUES ( 1 ) $$ );
DELETE FROM end_tick_calls;
/*
* Case:
* The update type has an in-base processing function
*
* Expected result:
* 1) _process_externally return value is NULL
* 2) The processing function has been called.
*/
UPDATE sys.updates SET update_state = 'FUTURE' , update_last = 1;
UPDATE sys.update_types
SET updtype_batch_size = NULL , updtype_proc_name = 'process_dummy_update'
WHERE updtype_id = 1;
SELECT diag_test_name( 'sys.process_updates() - In-base processing - Return value' );
SELECT ok( sys.process_updates( 1 ) = ROW( TRUE , NULL::TEXT ) );
SELECT diag_test_name( 'sys.process_updates() - In-base processing - Processing function called' );
SELECT set_eq( $$ SELECT * FROM process_update_calls $$ , $$ VALUES ( 1 ) $$ );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,17 @@
/*
* Check that core update targets have been defined correctly
*/
BEGIN;
SELECT plan( 3 );
SELECT diag_test_name( 'Update targets - Definitions present' );
SELECT is( COUNT(*)::INT , 2 ) FROM sys.update_targets;
SELECT diag_test_name( 'Update targets - verse.planets_updates exists' );
SELECT has_table( 'verse' , 'planets_updates' , 'Missing planets update table' );
SELECT diag_test_name( 'Update targets - emp.empires_updates exists' );
SELECT has_table( 'emp' , 'empires_updates' , 'Missing empires update table' );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -2,6 +2,12 @@
* Test the sys.process_empire_resources_updates() function
*/
BEGIN;
/* Start by deleting all update types except for the one
* we're interested in.
*/
DELETE FROM sys.update_types
WHERE updtype_name <> 'EmpireResources';
/* We need:
* - three types of resources,
* - two empire with resources records, and the corresponding update
@ -35,14 +41,15 @@ BEGIN;
/* Make sure update 0 is set to be processed for testEmp1 */
UPDATE sys.updates
SET status = 'PROCESSING' , last_tick = 0;
SET update_state = 'PROCESSING' , update_last = 0;
/* Create testEmp2 empire, make sure it will not be processed */
SELECT emp.create_empire( _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) , 1 );
UPDATE sys.updates _su
SET status = 'PROCESSED' , last_tick = 0
FROM emp.updates _eu
WHERE _eu.update_id = _su.id AND _eu.empire_id = _get_emp_name( 'testEmp2' );
SET update_state = 'PROCESSED' , update_last = 0
FROM emp.empires_updates _eu
WHERE _eu.update_id = _su.update_id
AND _eu.name_id = _get_emp_name( 'testEmp2' );
/****** TESTS BEGIN HERE ******/
SELECT plan( 19 );

View file

@ -2,6 +2,11 @@
* Test for the sys.process_planet_res_regen_updates() function
*/
BEGIN;
/* Start by deleting all update types except for the one
* we're interested in.
*/
DELETE FROM sys.update_types
WHERE updtype_name <> 'ResourceRegeneration';
/* We need to create a natural resource and a pair of planets */
\i utils/strings.sql
@ -34,23 +39,19 @@ BEGIN;
0.5 , 0.5
);
/* Insert update records for the planets. Planets 1 and 2 will be processed,
/* Modify update records for the planets. Planets 1 and 2 will be processed,
* planet testPlanet3 will not.
*/
INSERT INTO sys.updates ( id , gu_type , status , last_tick )
VALUES ( 1 , 'PLANET_RES_REGEN' , 'PROCESSING' , 0 );
INSERT INTO verse.updates ( update_id , planet_id )
VALUES ( 1 , _get_map_name( 'testPlanet1' ) );
INSERT INTO sys.updates ( id , gu_type , status , last_tick )
VALUES ( 2 , 'PLANET_RES_REGEN' , 'PROCESSING' , 0 );
INSERT INTO verse.updates ( update_id , planet_id )
VALUES ( 2 , _get_map_name( 'testPlanet2' ) );
INSERT INTO sys.updates ( id , gu_type , status , last_tick )
VALUES ( 3 , 'PLANET_RES_REGEN' , 'PROCESSED' , 0 );
INSERT INTO verse.updates ( update_id , planet_id )
VALUES ( 3 , _get_map_name( 'testPlanet3' ) );
UPDATE sys.updates su
SET update_state = 'PROCESSING' , update_last = 0
FROM verse.planets_updates vu
WHERE vu.update_id = su.update_id
AND vu.name_id <> _get_map_name( 'testPlanet3' );
UPDATE sys.updates su
SET update_state = 'PROCESSED' , update_last = 0
FROM verse.planets_updates vu
WHERE vu.update_id = su.update_id
AND vu.name_id = _get_map_name( 'testPlanet3' );
/* Helper function that gets a provider's current quantity */
CREATE FUNCTION _get_quantity( _planet TEXT )
@ -65,7 +66,7 @@ BEGIN;
SELECT plan( 6 );
SELECT sys.process_planet_res_regen_updates( 10 );
SELECT sys.process_res_regen_updates( 10 );
SELECT diag_test_name( 'PLANET_RES_REGEN update - Wrong update identifier (1/3)' );
SELECT ok( _get_quantity( 'testPlanet1' ) = 50 );
SELECT diag_test_name( 'PLANET_RES_REGEN update - Wrong update identifier (2/3)' );
@ -74,7 +75,7 @@ BEGIN;
SELECT ok( _get_quantity( 'testPlanet3' ) = 50 );
SELECT sys.process_planet_res_regen_updates( 0 );
SELECT sys.process_res_regen_updates( 0 );
SELECT diag_test_name( 'PLANET_RES_REGEN update - Processed planet with quantity < max.' );
SELECT ok( _get_quantity( 'testPlanet1' ) > 50 );
SELECT diag_test_name( 'PLANET_RES_REGEN update - Processed planet with quantity = max.' );

View file

@ -0,0 +1,28 @@
/*
* Test privileges on sys.update_targets
*/
BEGIN;
SELECT plan( 4 );
SELECT diag_test_name( 'sys.update_targets - No INSERT privilege' );
SELECT throws_ok(
$$ INSERT INTO sys.update_targets DEFAULT VALUES; $$ ,
42501 );
SELECT diag_test_name( 'sys.update_targets - No UPDATE privilege' );
SELECT throws_ok(
$$ UPDATE sys.update_targets SET updtgt_id = 42; $$ ,
42501 );
SELECT diag_test_name( 'sys.update_targets - No SELECT privilege' );
SELECT throws_ok(
$$ SELECT * FROM sys.update_targets; $$ ,
42501 );
SELECT diag_test_name( 'sys.update_targets - No DELETE privilege' );
SELECT throws_ok(
$$ DELETE FROM sys.update_targets; $$ ,
42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,28 @@
/*
* Test privileges on sys.update_types
*/
BEGIN;
SELECT plan( 4 );
SELECT diag_test_name( 'sys.update_types - No INSERT privilege' );
SELECT throws_ok(
$$ INSERT INTO sys.update_types DEFAULT VALUES; $$ ,
42501 );
SELECT diag_test_name( 'sys.update_types - No UPDATE privilege' );
SELECT throws_ok(
$$ UPDATE sys.update_types SET updtgt_id = 42; $$ ,
42501 );
SELECT diag_test_name( 'sys.update_types - No SELECT privilege' );
SELECT throws_ok(
$$ SELECT * FROM sys.update_types; $$ ,
42501 );
SELECT diag_test_name( 'sys.update_types - No DELETE privilege' );
SELECT throws_ok(
$$ DELETE FROM sys.update_types; $$ ,
42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,28 @@
/*
* Test privileges on sys.updates
*/
BEGIN;
SELECT plan( 4 );
SELECT diag_test_name( 'sys.updates - No INSERT privilege' );
SELECT throws_ok(
$$ INSERT INTO sys.updates DEFAULT VALUES; $$ ,
42501 );
SELECT diag_test_name( 'sys.updates - No UPDATE privilege' );
SELECT throws_ok(
$$ UPDATE sys.updates SET updtgt_id = 42; $$ ,
42501 );
SELECT diag_test_name( 'sys.updates - No SELECT privilege' );
SELECT throws_ok(
$$ SELECT * FROM sys.updates; $$ ,
42501 );
SELECT diag_test_name( 'sys.updates - No DELETE privilege' );
SELECT throws_ok(
$$ DELETE FROM sys.updates; $$ ,
42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.get_table_pkey()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.get_table_pkey() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.get_table_pkey( 'a' , 'b' ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.tgf_speupd_before_insert()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.tgf_speupd_before_insert() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.tgf_speupd_before_insert( ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.tgf_speupd_after_delete()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.tgf_speupd_after_delete() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.tgf_speupd_after_delete( ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.insert_missing_updates()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.insert_missing_updates() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.insert_missing_updates( 1 ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.tgf_insert_missing_updates()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.tgf_insert_missing_updates() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.tgf_insert_missing_updates( ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.tgf_updtype_after_insert_row()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.tgf_updtype_after_insert_row() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.tgf_updtype_after_insert_row( ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.tgf_check_update_type_proc()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.tgf_check_update_type_proc() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.tgf_check_update_type_proc( ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.tgf_reorder_update_types()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.tgf_reorder_update_types() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.tgf_reorder_update_types( ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.tgf_updtgt_before_insert()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.tgf_updtgt_before_insert() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.tgf_updtgt_before_insert( ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.tgf_updtgt_after_insert()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.tgf_updtgt_after_insert() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.tgf_updtgt_after_insert( ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.tgf_updtgt_after_delete()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.tgf_updtgt_after_delete() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.tgf_updtgt_after_delete( ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.register_update_type()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.register_update_type() - No EXECUTE privilege' );
SELECT throws_ok( $$ SELECT sys.register_update_type( '' , '' , '' , 'x' ) $$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.start_tick()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.start_tick() - EXECUTE privilege' );
SELECT lives_ok( 'SELECT sys.start_tick( )' );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.check_stuck_tick()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.check_stuck_tick() - EXECUTE privilege' );
SELECT lives_ok( 'SELECT sys.check_stuck_tick( )' );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.process_updates()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.process_updates() - EXECUTE privilege' );
SELECT lives_ok( 'SELECT sys.process_updates( 1 )' );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -1,11 +0,0 @@
/*
* Test privileges on sys.process_planet_res_regen_updates()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.process_planet_res_regen_updates() - Privileges' );
SELECT throws_ok( 'SELECT sys.process_planet_res_regen_updates( 1 )' , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on sys.process_res_regen_updates()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.process_res_regen_updates() - Privileges' );
SELECT throws_ok( 'SELECT sys.process_res_regen_updates( 1 )' , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -21,13 +21,13 @@
* - Finally, we need a planet that matches the criterias but isn't
* scheduled for an update at the time.
*
* FIXME: for now, locking is NOT tested. I simply have no idea how to do
* it, at least not without creating an extra database and that
* kind of stuff.
*
* FIXME: cannot test where the happiness column comes from until values
* all use DOUBLE PRECISION.
*/
DELETE FROM sys.update_types
WHERE updtype_name <> 'PlanetMining';
\i utils/strings.sql
\i utils/resources.sql
\i utils/accounts.sql
@ -41,17 +41,7 @@ SELECT _create_emp_names( 4 , 'empire' );
INSERT INTO emp.empires ( name_id , cash )
SELECT id , 0 FROM naming.empire_names;
/* Planet #1 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
VALUES ( 1 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
VALUES ( 1 , _get_map_name( 'planet1' ) );
/* Planet #2 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
VALUES ( 2 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
VALUES ( 2 , _get_map_name( 'planet2' ) );
INSERT INTO verse.resource_providers (
planet_id , resource_name_id , resprov_quantity_max ,
resprov_quantity , resprov_difficulty , resprov_recovery
@ -64,10 +54,6 @@ INSERT INTO verse.resource_providers (
);
/* Planet #3 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
VALUES ( 3 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
VALUES ( 3 , _get_map_name( 'planet3' ) );
INSERT INTO verse.resource_providers (
planet_id , resource_name_id , resprov_quantity_max ,
resprov_quantity , resprov_difficulty , resprov_recovery
@ -90,10 +76,6 @@ INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight )
);
/* Planet #4 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
VALUES ( 4 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
VALUES ( 4 , _get_map_name( 'planet4' ) );
INSERT INTO verse.resource_providers (
planet_id , resource_name_id , resprov_quantity_max ,
resprov_quantity , resprov_difficulty , resprov_recovery
@ -123,10 +105,6 @@ INSERT INTO emp.planet_mining_settings(
);
/* Planet #5 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
VALUES ( 5 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
VALUES ( 5 , _get_map_name( 'planet5' ) );
INSERT INTO verse.planet_happiness ( planet_id , current , target )
VALUES ( _get_map_name( 'planet5' ) , 0.5 , 0.5 );
INSERT INTO emp.planets ( empire_id , planet_id )
@ -139,10 +117,6 @@ INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight )
);
/* Planet #6 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
VALUES ( 6 , 'PLANET_MINING' , 'PROCESSED' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
VALUES ( 6 , _get_map_name( 'planet6' ) );
INSERT INTO verse.resource_providers (
planet_id , resource_name_id , resprov_quantity_max ,
resprov_quantity , resprov_difficulty , resprov_recovery
@ -168,3 +142,16 @@ INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight )
INSERT INTO verse.planet_resources ( planet_id , resource_name_id , pres_income , pres_upkeep )
SELECT p.name_id , r.resource_name_id , 42 , 0
FROM verse.planets p CROSS JOIN defs.resources r;
/* Modify update records for the planets. Planet 6 is not to be processed.
*/
UPDATE sys.updates su
SET update_state = 'PROCESSING' , update_last = 0
FROM verse.planets_updates vu
WHERE vu.update_id = su.update_id
AND vu.name_id <> _get_map_name( 'planet6' );
UPDATE sys.updates su
SET update_state = 'PROCESSED' , update_last = 0
FROM verse.planets_updates vu
WHERE vu.update_id = su.update_id
AND vu.name_id = _get_map_name( 'planet6' );