Dirty SQL test system

* Added a system that allows "dirty" tests to be written. These tests
actually need to change the database, so a temporary test database must
be created to run them.
This commit is contained in:
Emmanuel BENOîT 2012-01-14 09:02:24 +01:00
parent 04e550709a
commit c18bdc2d1f
5 changed files with 114 additions and 17 deletions
legacyworlds-server-data/db-structure/tests/dirty

View file

@ -0,0 +1,23 @@
/*
* Dirty test system self-check
*
* Insert an address, it should exist during the main test. Also create a
* function and a table which are used to synchronise execution.
*/
BEGIN;
INSERT INTO users.addresses ( address )
VALUES ( 'prepare@example.org' );
CREATE TABLE _barriers( _barrier BIGINT );
CREATE FUNCTION _synchronise_tests( _lock BIGINT )
RETURNS VOID
AS $$
BEGIN
WHILE pg_try_advisory_lock( _lock )
LOOP
PERFORM pg_advisory_unlock( _lock );
END LOOP;
END;
$$ LANGUAGE PLPGSQL;
COMMIT;

View file

@ -0,0 +1,17 @@
/*
* Dirty test system self-check
*
* Insert an address, it should exist during the main test.
*/
BEGIN;
SELECT no_plan( );
SELECT pg_advisory_lock( 1 );
SELECT diag_test_name( 'Dirty test system self-check - prepare.sql was executed' );
SELECT is( COUNT(*)::INT , 1 )
FROM users.addresses
WHERE address = 'prepare@example.org';
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,37 @@
#!/bin/bash
if [ -z "$TEST_DATABASE" ]; then
echo "Missing TEST_DATABASE environment variable"
exit 1;
fi
echo
echo 'Running "dirty" database tests'
echo
find -mindepth 1 -maxdepth 1 -type d | sort | while read testdir; do
[ -f "$testdir/run-test.sql" ] || continue
echo "Dirty test $testdir"
echo "--------------------------------------------"
echo
echo "Creating temporary database ..."
echo "DROP DATABASE IF EXISTS dirty_tests; CREATE DATABASE dirty_tests TEMPLATE $TEST_DATABASE;" \
| psql -vQUIET=1 -vON_ERROR_STOP=1 $TEST_DATABASE || exit 1
if [ -f "$testdir/prepare.sql" ]; then
echo "Preparing database ..."
( cd .. && exec psql -vQUIET=1 -vON_ERROR_STOP=1 --file "dirty/$testdir/prepare.sql" dirty_tests ) || exit 1
fi
echo "Running main test ..."
( cd .. && pg_prove -d dirty_tests dirty/$testdir/run-test.sql ) || exit 1
echo "Dropping temporary database ..."
echo -e "DROP DATABASE IF EXISTS dirty_tests;" \
| psql -vQUIET=1 -vON_ERROR_STOP=1 $TEST_DATABASE || exit 1
echo
done