From: Marius Gavrilescu Date: Thu, 18 Jun 2015 12:55:12 +0000 (+0300) Subject: Set up test database automatically when testing X-Git-Tag: 5999.000_014~18 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=1ea1adc8a16e90ff6ba6a00655b3e02fe7aadfec;p=gruntmaster-data.git Set up test database automatically when testing --- diff --git a/db.sql b/db.sql index 46ddcdb..a9f03c6 100644 --- a/db.sql +++ b/db.sql @@ -1,4 +1,4 @@ -CREATE TABLE users ( +CREATE TABLE IF NOT EXISTS users ( id TEXT PRIMARY KEY, passphrase TEXT, -- NOT NULL, admin BOOLEAN NOT NULL DEFAULT FALSE, @@ -13,7 +13,7 @@ CREATE TABLE users ( since BIGINT DEFAULT CAST(EXTRACT(epoch from now()) AS bigint) ); -CREATE TABLE contests ( +CREATE TABLE IF NOT EXISTS contests ( id TEXT PRIMARY KEY, name TEXT NOT NULL, editorial TEXT, @@ -24,7 +24,7 @@ CREATE TABLE contests ( CONSTRAINT positive_duration CHECK (stop > start) ); -CREATE TABLE contest_status ( +CREATE TABLE IF NOT EXISTS contest_status ( contest TEXT NOT NULL REFERENCES contests ON DELETE CASCADE, owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE, score INT NOT NULL, @@ -33,7 +33,7 @@ CREATE TABLE contest_status ( PRIMARY KEY (owner, contest) ); -CREATE TABLE problems ( +CREATE TABLE IF NOT EXISTS problems ( id TEXT PRIMARY KEY, author TEXT, writer TEXT, @@ -58,13 +58,13 @@ CREATE TABLE problems ( versource TEXT ); -CREATE TABLE contest_problems ( +CREATE TABLE IF NOT EXISTS contest_problems ( contest TEXT REFERENCES contests ON DELETE CASCADE, problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE, PRIMARY KEY (contest, problem) ); -CREATE TABLE jobs ( +CREATE TABLE IF NOT EXISTS jobs ( id SERIAL PRIMARY KEY, contest TEXT REFERENCES contests ON DELETE CASCADE, daemon TEXT, @@ -82,7 +82,7 @@ CREATE TABLE jobs ( owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE ); -CREATE TABLE problem_status ( +CREATE TABLE IF NOT EXISTS problem_status ( problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE, owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE, job SERIAL NOT NULL REFERENCES jobs ON DELETE CASCADE, @@ -91,7 +91,7 @@ CREATE TABLE problem_status ( PRIMARY KEY (owner, problem) ); -CREATE TABLE opens ( +CREATE TABLE IF NOT EXISTS opens ( contest TEXT NOT NULL REFERENCES contests ON DELETE CASCADE, problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE, owner TEXT NOT NULL REFERENCES users ON DELETE CASCADE, @@ -99,7 +99,7 @@ CREATE TABLE opens ( PRIMARY KEY (contest, problem, owner) ); -CREATE TABLE limits ( +CREATE TABLE IF NOT EXISTS limits ( problem TEXT NOT NULL REFERENCES problems ON DELETE CASCADE, format TEXT NOT NULL, timeout REAL NOT NULL, diff --git a/t/Gruntmaster-Data.t b/t/Gruntmaster-Data.t index d7e2d7f..54861af 100644 --- a/t/Gruntmaster-Data.t +++ b/t/Gruntmaster-Data.t @@ -9,10 +9,15 @@ use Test::More; BEGIN { eval { Gruntmaster::Data::init 'dbi:Pg:dbname=gmtest'; 1; - } or plan skip_all => 'Cannot connect to test database. Create it by running make_test_db.sh before running this test. '. "Error: $@"; + } or plan skip_all => 'Cannot connect to test database. Create it by running createdb gmtest before running this test. '. "Error: $@"; plan tests => 37; } +note 'Setting up test database'; +$ENV{PGOPTIONS} = '-c client_min_messages=WARNING'; +system 'psql', 'gmtest', '-qf', 'db.sql'; +system 'psql', 'gmtest', '-qf', 'testdata.sql'; + note 'Running update_status'; update_status; diff --git a/testdata.sql b/testdata.sql index 5cf0565..bc00077 100644 --- a/testdata.sql +++ b/testdata.sql @@ -1,7 +1,9 @@ -CREATE FUNCTION tm() RETURNS BIGINT IMMUTABLE LANGUAGE SQL AS $$ +CREATE OR REPLACE FUNCTION tm() RETURNS BIGINT IMMUTABLE LANGUAGE SQL AS $$ SELECT EXTRACT(epoch FROM NOW())::bigint $$; +TRUNCATE users, contests, problems, jobs RESTART IDENTITY CASCADE; + -- USERS INSERT INTO users (id, admin) VALUES ('MGV', TRUE);