/* * Test the emp.mining_compute_extraction() function */ BEGIN; /* Define the necessary constant */ SELECT sys.uoc_constant( 'game.resources.extraction' , '(test)' , 'Resources' , 10 ); SELECT sys.uoc_constant( 'game.happiness.strike' , '(test)' , 'Resources' , 0.5 ); /* Make sure the functions are not immutable during the tests */ ALTER FUNCTION sys.get_constant( TEXT ) VOLATILE; ALTER FUNCTION emp.mining_compute_extraction( emp.planet_mining_type ) VOLATILE; /* Replace verse.get_raw_production() with a function that returns a * value from a "temporary" table. */ CREATE TABLE fake_mining_production( production REAL ); CREATE OR REPLACE FUNCTION verse.get_raw_production( pid INT , pt building_output_type ) RETURNS REAL LANGUAGE SQL VOLATILE AS 'SELECT production FROM fake_mining_production'; INSERT INTO fake_mining_production VALUES ( 100 ); /* Create a table that is used as the input for tests */ CREATE TABLE tests_input OF emp.planet_mining_type; -- ***** TESTS BEGIN HERE ***** SELECT plan( 9 ); INSERT INTO tests_input VALUES ( 0 , 0 , 1000 , 1000 , 0 , NULL , NULL , NULL , NULL ) , ( 1 , 1 , 10000 , 10000 , 0 , NULL , NULL , NULL , NULL ) , ( 2 , 2 , 100000 , 100000 , 0 , NULL , NULL , NULL , NULL ); SELECT diag_test_name( 'emp.mining_compute_extraction() - No empire -> no extraction' ); SELECT is_empty( $$ SELECT * FROM tests_input WHERE emp.mining_compute_extraction( ROW( planet , resource , quantity , quantity_max , difficulty , empire , happiness , weight , total_weight ) ) <> 0 $$ ); DELETE FROM tests_input; INSERT INTO tests_input VALUES ( 0 , 0 , 1000 , 1000 , 0 , 1 , 1 , 1 , 1 ) , ( 1 , 1 , 10000 , 10000 , 0 , 1 , 1 , 1 , 1 ) , ( 2 , 2 , 100000 , 100000 , 0 , 1 , 1 , 1 , 1 ); SELECT diag_test_name( 'emp.mining_compute_extraction() - Extracted quantity > 0 if empire and resources are present' ); SELECT is_empty( $$ SELECT * FROM tests_input WHERE emp.mining_compute_extraction( ROW( planet , resource , quantity , quantity_max , difficulty , empire , happiness , weight , total_weight ) ) = 0 $$ ); SELECT diag_test_name( 'emp.mining_compute_extraction() - Extracted quantity does not change for same initial ratio' ); SELECT is( COUNT( DISTINCT emp.mining_compute_extraction( ROW( planet , resource , quantity , quantity_max , difficulty , empire , happiness , weight , total_weight ) ) )::INT , 1 ) FROM tests_input; UPDATE tests_input SET weight = 1 + planet , total_weight = 3; SELECT diag_test_name( 'emp.mining_compute_extraction() - Result increases when weight/total weight ratio increases' ); SELECT set_eq( $$ SELECT rank() OVER ( ORDER BY weight ) AS r1 , rank() OVER( ORDER BY emp.mining_compute_extraction( ROW( planet , resource , quantity , quantity_max , difficulty , empire , happiness , weight , total_weight ) ) ) AS r2 FROM tests_input $$ , $$ VALUES ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) $$ ); UPDATE tests_input SET weight = total_weight , happiness = 0.5 + 0.25 * planet; SELECT diag_test_name( 'emp.mining_compute_extraction() - Happiness does not affect result when greater than strike level' ); SELECT is( COUNT( DISTINCT emp.mining_compute_extraction( ROW( planet , resource , quantity , quantity_max , difficulty , empire , happiness , weight , total_weight ) ) )::INT , 1 ) FROM tests_input; UPDATE tests_input SET weight = total_weight , happiness = 0.2 * planet; SELECT diag_test_name( 'emp.mining_compute_extraction() - Result increases with happiness when lower than strike level' ); SELECT set_eq( $$ SELECT rank() OVER ( ORDER BY happiness ) AS r1 , rank() OVER( ORDER BY emp.mining_compute_extraction( ROW( planet , resource , quantity , quantity_max , difficulty , empire , happiness , weight , total_weight ) ) ) AS r2 FROM tests_input $$ , $$ VALUES ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) $$ ); UPDATE fake_mining_production SET production = 10000; UPDATE tests_input SET quantity = 1 , quantity_max = 1 , happiness = 1; SELECT diag_test_name( 'emp.mining_compute_extraction() - Result <= quantity' ); SELECT is_empty( $$ SELECT * FROM tests_input WHERE quantity < emp.mining_compute_extraction( ROW( planet , resource , quantity , quantity_max , difficulty , empire , happiness , weight , total_weight ) ) $$ ); UPDATE fake_mining_production SET production = 1; UPDATE tests_input SET quantity = 100 * planet , quantity_max = 200; SELECT diag_test_name( 'emp.mining_compute_extraction() - Result decreases when quantity/capacity ratio decreases' ); SELECT set_eq( $$ SELECT rank() OVER ( ORDER BY quantity ) AS r1 , rank() OVER( ORDER BY emp.mining_compute_extraction( ROW( planet , resource , quantity , quantity_max , difficulty , empire , happiness , weight , total_weight ) ) ) AS r2 FROM tests_input $$ , $$ VALUES ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) $$ ); UPDATE tests_input SET quantity = quantity_max , difficulty = 0.33 * planet; SELECT diag_test_name( 'emp.mining_compute_extraction() - Result decreases when difficulty increases' ); SELECT set_eq( $$ SELECT rank() OVER ( ORDER BY difficulty DESC ) AS r1 , rank() OVER( ORDER BY emp.mining_compute_extraction( ROW( planet , resource , quantity , quantity_max , difficulty , empire , happiness , weight , total_weight ) ) ) AS r2 FROM tests_input $$ , $$ VALUES ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) $$ ); SELECT * FROM finish( ); ROLLBACK;