2011-12-19 16:00:01 +01:00
|
|
|
/*
|
|
|
|
* 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 ******/
|
2012-01-10 12:30:47 +01:00
|
|
|
SELECT plan( 17 );
|
2011-12-19 16:00:01 +01:00
|
|
|
|
|
|
|
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' );
|
2012-01-10 10:48:56 +01:00
|
|
|
|
2012-01-10 12:30:47 +01:00
|
|
|
/* Reset resources, create temporary table, replace
|
|
|
|
* defs.add_resource_records() to make sure it's called when needed (and
|
|
|
|
* only then)
|
|
|
|
*/
|
2012-01-10 10:48:56 +01:00
|
|
|
DELETE FROM defs.natural_resources;
|
|
|
|
DELETE FROM defs.resources;
|
2012-01-10 12:30:47 +01:00
|
|
|
CREATE TEMPORARY TABLE arr_called( on_id INT ) ON COMMIT DROP;
|
|
|
|
CREATE OR REPLACE FUNCTION defs.add_resource_records( _resource INT )
|
|
|
|
RETURNS VOID
|
|
|
|
STRICT VOLATILE
|
|
|
|
SECURITY INVOKER
|
|
|
|
AS $$
|
|
|
|
INSERT INTO arr_called VALUES( $1 );
|
|
|
|
$$ LANGUAGE SQL;
|
2012-01-10 10:48:56 +01:00
|
|
|
ALTER TABLE emp.empires
|
|
|
|
DROP CONSTRAINT fk_empires_name;
|
|
|
|
INSERT INTO emp.empires ( name_id , cash )
|
|
|
|
VALUES ( 1 , 0 );
|
|
|
|
|
2012-01-10 12:30:47 +01:00
|
|
|
SELECT diag_test_name( 'defs.uoc_resource_internal() - defs.add_resource_record() called on new resource' );
|
|
|
|
SELECT defs.uoc_resource_internal( 'test1' , 'test4' , NULL , 1 );
|
2012-01-10 10:48:56 +01:00
|
|
|
SELECT is( COUNT(*)::INT , 1 )
|
2012-01-10 12:30:47 +01:00
|
|
|
FROM arr_called
|
|
|
|
WHERE on_id = _get_string( 'test1' );
|
|
|
|
DELETE FROM arr_called;
|
|
|
|
|
|
|
|
SELECT diag_test_name( 'defs.uoc_resource_internal() - defs.add_resource_record() not called on resource update' );
|
|
|
|
SELECT defs.uoc_resource_internal( 'test1' , 'test3' , NULL , 1 );
|
|
|
|
SELECT is( COUNT(*)::INT , 0 )
|
|
|
|
FROM arr_called
|
|
|
|
WHERE on_id = _get_string( 'test1' );
|
|
|
|
DELETE FROM arr_called;
|
2011-12-19 16:00:01 +01:00
|
|
|
|
|
|
|
SELECT * FROM finish( );
|
|
|
|
ROLLBACK;
|