| 1 |
use strict; |
| 2 |
use Test::More; |
| 3 |
use Class::DBI::Sweet; |
| 4 |
Class::DBI::Sweet->default_search_attributes({ use_resultset_cache => 0 }); |
| 5 |
Class::DBI::Sweet->cache(Cache::MemoryCache->new( |
| 6 |
{ namespace => "SweetTest", default_expires_in => 60 } ) ); |
| 7 |
|
| 8 |
#---------------------------------------------------------------------- |
| 9 |
# Test database failures |
| 10 |
#---------------------------------------------------------------------- |
| 11 |
|
| 12 |
BEGIN { |
| 13 |
eval "use Cache::MemoryCache"; |
| 14 |
plan skip_all => "needs Cache::Cache for testing" if $@; |
| 15 |
eval "use DBD::SQLite"; |
| 16 |
plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 7); |
| 17 |
} |
| 18 |
|
| 19 |
use lib 't/cdbi-t/testlib'; |
| 20 |
use Film; |
| 21 |
|
| 22 |
Film->create_test_film; |
| 23 |
|
| 24 |
{ |
| 25 |
my $btaste = Film->retrieve('Bad Taste'); |
| 26 |
isa_ok $btaste, 'Film', "We have Bad Taste"; |
| 27 |
{ |
| 28 |
no warnings 'redefine'; |
| 29 |
local *DBIx::ContextualFetch::st::execute = sub { die "Database died" }; |
| 30 |
eval { $btaste->delete }; |
| 31 |
::like $@, qr/Database died/s, "We failed"; |
| 32 |
} |
| 33 |
my $still = Film->retrieve('Bad Taste'); |
| 34 |
isa_ok $btaste, 'Film', "We still have Bad Taste"; |
| 35 |
} |
| 36 |
|
| 37 |
{ |
| 38 |
my $btaste = Film->retrieve('Bad Taste'); |
| 39 |
isa_ok $btaste, 'Film', "We have Bad Taste"; |
| 40 |
$btaste->numexplodingsheep(10); |
| 41 |
{ |
| 42 |
no warnings 'redefine'; |
| 43 |
local *DBIx::ContextualFetch::st::execute = sub { die "Database died" }; |
| 44 |
eval { $btaste->update }; |
| 45 |
::like $@, qr/update.*Database died/s, "We failed"; |
| 46 |
} |
| 47 |
$btaste->discard_changes; |
| 48 |
my $still = Film->retrieve('Bad Taste'); |
| 49 |
isa_ok $btaste, 'Film', "We still have Bad Taste"; |
| 50 |
is $btaste->numexplodingsheep, 1, "with 1 sheep"; |
| 51 |
} |
| 52 |
|
| 53 |
if (0) { |
| 54 |
my $sheep = Film->maximum_value_of('numexplodingsheep'); |
| 55 |
is $sheep, 1, "1 exploding sheep"; |
| 56 |
{ |
| 57 |
local *DBIx::ContextualFetch::st::execute = sub { die "Database died" }; |
| 58 |
my $sheep = eval { Film->maximum_value_of('numexplodingsheep') }; |
| 59 |
::like $@, qr/select.*Database died/s, |
| 60 |
"Handle database death in single value select"; |
| 61 |
} |
| 62 |
} |
| 63 |
|