diff options
-rw-r--r-- | t/02_same_schema.t | 14 | ||||
-rw-r--r-- | t/CP_Testing.pm | 34 |
2 files changed, 44 insertions, 4 deletions
diff --git a/t/02_same_schema.t b/t/02_same_schema.t index 4eea20cc3..ddcf05730 100644 --- a/t/02_same_schema.t +++ b/t/02_same_schema.t @@ -10,11 +10,12 @@ use Test::More tests => 35; use lib 't','.'; use CP_Testing; -use vars qw/$dbh1 $dbh2 $SQL $t $stdargs/; +use vars qw/$dbh1 $dbh2 $SQL $t/; my $cp1 = CP_Testing->new({ default_action => 'same_schema' }); my $cp2 = CP_Testing->new({ default_action => 'same_schema', dbnum => 2}); +## Setup both database handles, and create a testing user $dbh1 = $cp1->test_database_handle(); $dbh1->{AutoCommit} = 1; eval { $dbh1->do(q{CREATE USER alternate_owner}, { RaiseError => 0, PrintError => 0 }); }; @@ -22,8 +23,7 @@ $dbh2 = $cp2->test_database_handle(); $dbh2->{AutoCommit} = 1; eval { $dbh2->do(q{CREATE USER alternate_owner}, { RaiseError => 0, PrintError => 0 }); }; -$stdargs = qq{--dbhost2=$cp2->{shorthost} --dbuser2=$cp2->{testuser}}; - +my $stdargs = qq{--dbhost2=$cp2->{shorthost} --dbuser2=$cp2->{testuser}}; my $S = q{Action 'same_schema'}; my $label = 'POSTGRES_SAME_SCHEMA'; @@ -31,13 +31,19 @@ $t = qq{$S fails when called with an invalid option}; like ($cp1->run('foobar=12'), qr{^\s*Usage:}, $t); +## Because other tests may have left artifacts around, we want to recreate the databases +$dbh1 = $cp1->recreate_database($dbh1); +$dbh2 = $cp2->recreate_database($dbh2); + $t = qq{$S succeeds with two empty databases}; -#local($CP_Testing::DEBUG) = 1; like ($cp1->run($stdargs), qr{^$label OK}, $t); #/////////// Users +$dbh1->{AutoCommit} = 1; +$dbh2->{AutoCommit} = 1; + $t = qq{$S fails when first schema has an extra user}; $dbh1->do(q{CREATE USER user_1_only}); like ($cp1->run($stdargs), diff --git a/t/CP_Testing.pm b/t/CP_Testing.pm index a9cafd5fc..3f3faa2f9 100644 --- a/t/CP_Testing.pm +++ b/t/CP_Testing.pm @@ -344,6 +344,40 @@ sub test_database_handle { } ## end of test_database_handle +sub recreate_database { + + ## Given a database handle, comepletely recreate the current database + + my ($self,$dbh) = @_; + + my $dbname = $dbh->{pg_db}; + + $dbname eq 'template1' and die qq{Cannot recreate from template1!\n}; + + my $user = $dbh->{pg_user}; + my $host = $dbh->{pg_host}; + my $port = $dbh->{pg_port}; + + $dbh->disconnect(); + + my $dsn = "DBI:Pg:dbname=template1;port=$port;host=$host"; + + $dbh = DBI->connect($dsn, $user, '', {AutoCommit=>1, RaiseError=>1, PrintError=>0}); + + $dbh->do("DROP DATABASE $dbname"); + $dbh->do("CREATE DATABASE $dbname"); + + $dbh->disconnect(); + + $dsn = "DBI:Pg:dbname=$dbname;port=$port;host=$host"; + + $dbh = DBI->connect($dsn, $user, '', {AutoCommit=>0, RaiseError=>1, PrintError=>0}); + + return $dbh; + +} ## end of recreate_database + + sub get_command { return run('get_command', @_); } |