From: Dagfinn Ilmari Mannsåker Date: Mon, 25 Aug 2014 12:57:38 +0000 (+0300) Subject: Clean up Pg batch alter test X-Git-Tag: v0.11019~1^2~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4d4d68c0266941415382ae8651a1cf4abc01b54f;p=dbsrgits%2FSQL-Translator.git Clean up Pg batch alter test - Only use Test::PostgreSQL if DBICTEST_PG_DSN isn't set - Use sqlt_test_ prefix for tables we create - Drop the tables we created on exit - Use lives_ok for the SQL we're testing --- diff --git a/t/postgresql-rename-table-and-field.t b/t/postgresql-rename-table-and-field.t index 48a81b1..887f023 100644 --- a/t/postgresql-rename-table-and-field.t +++ b/t/postgresql-rename-table-and-field.t @@ -4,21 +4,30 @@ use strict; use warnings; use Test::More; +use Test::Exception; use Test::SQL::Translator; use SQL::Translator; use SQL::Translator::Diff; -maybe_plan(10, 'DBD::Pg', 'Test::PostgreSQL'); +maybe_plan(undef, 'DBD::Pg'); -my ( $pgsql, $dbh , $ddl, $ret ); +my ( $pgsql, $ddl, $ret, $dsn, $user, $pass ); +if ($ENV{DBICTEST_PG_DSN}) { + ($dsn, $user, $pass) = map { $ENV{"DBICTEST_PG_$_"} } qw(DSN USER PASS); +} +else { + no warnings 'once'; + maybe_plan(undef, 'Test::PostgreSQL'); + $pgsql = Test::PostgreSQL->new + or die "Can't create test database: $Test::PostgreSQL::errstr"; + $dsn = $pgsql->dsn; +}; -no warnings "once"; -$pgsql = Test::PostgreSQL->new() or plan skip_all => $Test::PostgreSQL::errstr; -$dbh = DBI->connect($pgsql->dsn,'','', { RaiseError => 1 }) or plan skip_all => $DBI::errstr; -use warnings "once"; +my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1, AutoCommit => 1 }); +$dbh->do('SET client_min_messages=warning'); my $source_ddl = <do($source_ddl), "create table" ); -ok( $ret = $dbh->do(q| INSERT INTO foo (bar) VALUES ('buzz') |), "insert data" ); +ok( $ret = $dbh->do(q| INSERT INTO sqlt_test_foo (bar) VALUES ('buzz') |), "insert data" ); cmp_ok( $ret, '==', 1, "one row inserted" ); my $target_ddl = <new( parser => 'SQL::Translator::Parser::PostgreSQL', )->translate(\$target_ddl); -my $table = $target_sqlt->get_table('fluff'); -$table->extra( renamed_from => 'foo' ); +my $table = $target_sqlt->get_table('sqlt_test_fluff'); +$table->extra( renamed_from => 'sqlt_test_foo' ); my $field = $table->get_field('biff'); $field->extra( renamed_from => 'bar' ); @@ -61,11 +70,22 @@ my @diff = SQL::Translator::Diff->new({ foreach my $line (@diff) { $line =~ s/\n//g; next if $line =~ /^--/; - ok( $dbh->do($line), "$line" ); + lives_ok { $dbh->do($line) } "$line"; } -ok ( $ret = $dbh->selectall_arrayref(q(SELECT biff FROM fluff), { Slice => {} }), "query DB for data" ); +ok ( $ret = $dbh->selectall_arrayref(q(SELECT biff FROM sqlt_test_fluff), { Slice => {} }), "query DB for data" ); cmp_ok( scalar(@$ret), '==', 1, "Got 1 row"); cmp_ok( $ret->[0]->{biff}, 'eq', 'buzz', "col biff has value buzz" ); + +# Make sure Test::PostgreSQL can kill Pg +undef $dbh if $pgsql; + +END { + if ($dbh && !$pgsql) { + $dbh->do("drop table if exists sqlt_test_$_") foreach qw(foo fluff); + } +} + +done_testing;