X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F98savepoints.t;h=53d5d627a5b1ed7a5bfb226a6a3834e729323afb;hb=4e9fc3f33df616fb7340d05e304ff985b9cce9cb;hp=45fe577f187d240e49321316ddbc586b9c4f9f89;hpb=68de943862f06cabd397d2e74d12cd9cdc999779;p=dbsrgits%2FDBIx-Class.git diff --git a/t/98savepoints.t b/t/98savepoints.t index 45fe577..53d5d62 100644 --- a/t/98savepoints.t +++ b/t/98savepoints.t @@ -2,137 +2,148 @@ use strict; use warnings; use Test::More; +use DBIx::Class::Optional::Dependencies (); -my ($create_sql, $dsn, $user, $pass); +my $env2optdep = { + DBICTEST_PG => 'rdbms_pg', + DBICTEST_MYSQL => 'test_rdbms_mysql', +}; -if ($ENV{DBICTEST_PG_DSN}) { - ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}; - - $create_sql = "CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10))"; -} elsif ($ENV{DBICTEST_MYSQL_DSN}) { - ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; - - $create_sql = "CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10)) ENGINE=InnoDB"; -} else { - plan skip_all => 'Set DBICTEST_(PG|MYSQL)_DSN _USER and _PASS if you want to run savepoint tests'; -} +plan skip_all => join (' ', + 'Set $ENV{DBICTEST_PG_DSN} and/or $ENV{DBICTEST_MYSQL_DSN} _USER and _PASS to run these tests.', +) unless grep { $ENV{"${_}_DSN"} } keys %$env2optdep; use lib qw(t/lib); use DBICTest; use DBICTest::Stats; -plan tests => 16; - -my $schema = DBICTest::Schema->connect ($dsn,$user,$pass,{ auto_savepoint => 1 }); +my $schema; -my $stats = DBICTest::Stats->new; +for my $prefix (keys %$env2optdep) { SKIP: { + my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/; -$schema->storage->debugobj($stats); + skip ("Skipping tests with $prefix: set \$ENV{${prefix}_DSN} _USER and _PASS", 1) + unless $dsn; -$schema->storage->debug(1); + skip ("Testing with ${prefix}_DSN needs " . DBIx::Class::Optional::Dependencies->req_missing_for( $env2optdep->{$prefix} ), 1) + unless DBIx::Class::Optional::Dependencies->req_ok_for($env2optdep->{$prefix}); -{ - local $SIG{__WARN__} = sub {}; - $schema->storage->dbh->do ('DROP TABLE IF EXISTS artist'); - $schema->storage->dbh->do ($create_sql); -} - -$schema->resultset('Artist')->create({ name => 'foo' }); + $schema = DBICTest::Schema->connect ($dsn,$user,$pass,{ auto_savepoint => 1 }); -$schema->txn_begin; + my $create_sql; + $schema->storage->ensure_connected; + if ($schema->storage->isa('DBIx::Class::Storage::DBI::Pg')) { + $create_sql = "CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10))"; + $schema->storage->dbh->do('SET client_min_messages=WARNING'); + } + elsif ($schema->storage->isa('DBIx::Class::Storage::DBI::mysql')) { + $create_sql = "CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10)) ENGINE=InnoDB"; + } + else { + skip( 'Untested driver ' . $schema->storage, 1 ); + } -my $arty = $schema->resultset('Artist')->find(1); + note "Testing $prefix"; -my $name = $arty->name; + my $stats = DBICTest::Stats->new; + $schema->storage->debugobj($stats); + $schema->storage->debug(1); -# First off, test a generated savepoint name -$schema->svp_begin; + $schema->storage->dbh->do ('DROP TABLE IF EXISTS artist'); + $schema->storage->dbh->do ($create_sql); -cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled'); + $schema->resultset('Artist')->create({ name => 'foo' }); -$arty->update({ name => 'Jheephizzy' }); + $schema->txn_begin; -$arty->discard_changes; + my $arty = $schema->resultset('Artist')->find(1); -cmp_ok($arty->name, 'eq', 'Jheephizzy', 'Name changed'); + my $name = $arty->name; -# Rollback the generated name -# Active: 0 -$schema->svp_rollback; + # First off, test a generated savepoint name + $schema->svp_begin; -cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled'); + cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled'); -$arty->discard_changes; + $arty->update({ name => 'Jheephizzy' }); -cmp_ok($arty->name, 'eq', $name, 'Name rolled back'); + $arty->discard_changes; -$arty->update({ name => 'Jheephizzy'}); + cmp_ok($arty->name, 'eq', 'Jheephizzy', 'Name changed'); -# Active: 0 1 -$schema->svp_begin('testing1'); + # Rollback the generated name + # Active: 0 + $schema->svp_rollback; -$arty->update({ name => 'yourmom' }); + cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled'); -# Active: 0 1 2 -$schema->svp_begin('testing2'); + $arty->discard_changes; -$arty->update({ name => 'gphat' }); -$arty->discard_changes; -cmp_ok($arty->name, 'eq', 'gphat', 'name changed'); -# Active: 0 1 2 -# Rollback doesn't DESTROY the savepoint, it just rolls back to the value -# at it's conception -$schema->svp_rollback('testing2'); -$arty->discard_changes; -cmp_ok($arty->name, 'eq', 'yourmom', 'testing2 reverted'); + cmp_ok($arty->name, 'eq', $name, 'Name rolled back'); -# Active: 0 1 2 3 -$schema->svp_begin('testing3'); -$arty->update({ name => 'coryg' }); -# Active: 0 1 2 3 4 -$schema->svp_begin('testing4'); -$arty->update({ name => 'watson' }); + $arty->update({ name => 'Jheephizzy'}); -# Release 3, which implicitly releases 4 -# Active: 0 1 2 -$schema->svp_release('testing3'); -$arty->discard_changes; -cmp_ok($arty->name, 'eq', 'watson', 'release left data'); -# This rolls back savepoint 2 -# Active: 0 1 2 -$schema->svp_rollback; -$arty->discard_changes; -cmp_ok($arty->name, 'eq', 'yourmom', 'rolled back to 2'); + # Active: 0 1 + $schema->svp_begin('testing1'); -# Rollback the original savepoint, taking us back to the beginning, implicitly -# rolling back savepoint 1 and 2 -$schema->svp_rollback('savepoint_0'); -$arty->discard_changes; -cmp_ok($arty->name, 'eq', 'foo', 'rolled back to start'); + $arty->update({ name => 'yourmom' }); -$schema->txn_commit; + # Active: 0 1 2 + $schema->svp_begin('testing2'); -# And now to see if txn_do will behave correctly + $arty->update({ name => 'gphat' }); + $arty->discard_changes; + cmp_ok($arty->name, 'eq', 'gphat', 'name changed'); + # Active: 0 1 2 + # Rollback doesn't DESTROY the savepoint, it just rolls back to the value + # at it's conception + $schema->svp_rollback('testing2'); + $arty->discard_changes; + cmp_ok($arty->name, 'eq', 'yourmom', 'testing2 reverted'); -$schema->txn_do (sub { - $schema->txn_do (sub { - $arty->name ('Muff'); + # Active: 0 1 2 3 + $schema->svp_begin('testing3'); + $arty->update({ name => 'coryg' }); + # Active: 0 1 2 3 4 + $schema->svp_begin('testing4'); + $arty->update({ name => 'watson' }); - $arty->update; - }); + # Release 3, which implicitly releases 4 + # Active: 0 1 2 + $schema->svp_release('testing3'); + $arty->discard_changes; + cmp_ok($arty->name, 'eq', 'watson', 'release left data'); + # This rolls back savepoint 2 + # Active: 0 1 2 + $schema->svp_rollback; + $arty->discard_changes; + cmp_ok($arty->name, 'eq', 'yourmom', 'rolled back to 2'); - eval { - $schema->txn_do (sub { - $arty->name ('Moff'); + # Rollback the original savepoint, taking us back to the beginning, implicitly + # rolling back savepoint 1 and 2 + $schema->svp_rollback('savepoint_0'); + $arty->discard_changes; + cmp_ok($arty->name, 'eq', 'foo', 'rolled back to start'); - $arty->update; + $schema->txn_commit; - $arty->discard_changes; + # And now to see if txn_do will behave correctly + $schema->txn_do (sub { + my $artycp = $arty; - is($arty->name,'Moff','Value updated in nested transaction'); + $schema->txn_do (sub { + $artycp->name ('Muff'); + $artycp->update; + }); - $schema->storage->dbh->do ("GUARANTEED TO PHAIL"); - }); + eval { + $schema->txn_do (sub { + $artycp->name ('Moff'); + $artycp->update; + $artycp->discard_changes; + is($artycp->name,'Moff','Value updated in nested transaction'); + $schema->storage->dbh->do ("GUARANTEED TO PHAIL"); + }); }; ok ($@,'Nested transaction failed (good)'); @@ -146,15 +157,22 @@ $schema->txn_do (sub { $arty->update; }); -$arty->discard_changes; + $arty->discard_changes; -is($arty->name,'Miff','auto_savepoint worked'); + is($arty->name,'Miff','auto_savepoint worked'); -cmp_ok($stats->{'SVP_BEGIN'},'==',7,'Correct number of savepoints created'); + cmp_ok($stats->{'SVP_BEGIN'},'==',7,'Correct number of savepoints created'); -cmp_ok($stats->{'SVP_RELEASE'},'==',3,'Correct number of savepoints released'); + cmp_ok($stats->{'SVP_RELEASE'},'==',3,'Correct number of savepoints released'); -cmp_ok($stats->{'SVP_ROLLBACK'},'==',5,'Correct number of savepoint rollbacks'); + cmp_ok($stats->{'SVP_ROLLBACK'},'==',5,'Correct number of savepoint rollbacks'); -END { $schema->storage->dbh->do ("DROP TABLE artist") if defined $schema } + $schema->storage->dbh->do ("DROP TABLE artist"); +}} +done_testing; + +END { + eval { $schema->storage->dbh->do ("DROP TABLE artist") } if defined $schema; + undef $schema; +}