From: Brandon L. Black Date: Thu, 27 Jul 2006 02:31:19 +0000 (+0000) Subject: add a couple of tests for the new exception stuff X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4ffbc1d6d4ea0462ca64dca0a178fbc219db77a0;p=dbsrgits%2FDBIx-Class-Historic.git add a couple of tests for the new exception stuff --- diff --git a/t/33storage_reconnect.t b/t/33storage_reconnect.t new file mode 100644 index 0000000..6e82b13 --- /dev/null +++ b/t/33storage_reconnect.t @@ -0,0 +1,26 @@ +use strict; +use warnings; + +use Test::More; +use lib qw(t/lib); +use DBICTest; + +plan tests => 2; + +# Set up the "usual" sqlite for DBICTest +my $schema = DBICTest->init_schema; + +# Make sure we're connected by doing something +my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); +cmp_ok(@art, '==', 3, "Three artists returned"); + +# Disconnect the dbh, and be sneaky about it +$schema->storage->_dbh->disconnect; + +# Try the operation again - What should happen here is: +# 1. S::DBI blindly attempts the SELECT, which throws an exception +# 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state... +# 3. Reconnects, and retries the operation +# 4. Success! +my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); +cmp_ok(@art_two, '==', 3, "Three artists returned"); diff --git a/t/34exception_action.t b/t/34exception_action.t new file mode 100644 index 0000000..dd54be1 --- /dev/null +++ b/t/34exception_action.t @@ -0,0 +1,64 @@ +use strict; +use warnings; + +use Test::More; +use lib qw(t/lib); +use DBICTest; + +plan tests => 6; + +# Set up the "usual" sqlite for DBICTest +my $schema = DBICTest->init_schema; + +# This is how we're generating exceptions in the rest of these tests, +# which might need updating at some future time to be some other +# exception-generating statement: + +sub throwex { $schema->resultset("Artist")->search(1,1,1); } +my $ex_regex = qr/Odd number of arguments to search/; + +# Basic check, normal exception +eval { throwex }; +like($@, $ex_regex); + +# Now lets rethrow via exception_action +$schema->exception_action(sub { die @_ }); +eval { throwex }; +like($@, $ex_regex); + +# Now lets suppress the error +$schema->exception_action(sub { 1 }); +eval { throwex }; +ok(!$@, "Suppress exception"); + +# Now lets fall through and let croak take back over +$schema->exception_action(sub { return }); +eval { throwex }; +like($@, $ex_regex); + +# Whacky useless exception class +{ + package DBICTest::Exception; + use overload '""' => \&stringify, fallback => 1; + sub new { + my $class = shift; + bless { msg => shift }, $class; + } + sub throw { + my $self = shift; + die $self if ref $self eq __PACKAGE__; + die $self->new(shift); + } + sub stringify { + "DBICTest::Exception is handling this: " . shift->{msg}; + } +} + +# Try the exception class +$schema->exception_action(sub { DBICTest::Exception->throw(@_) }); +eval { throwex }; +like($@, qr/DBICTest::Exception is handling this: $ex_regex/); + +# While we're at it, lets throw a custom exception through Storage::DBI +eval { DBICTest->schema->storage->throw_exception('floob') }; +like($@, qr/DBICTest::Exception is handling this: floob/);