| 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 |
BEGIN { |
| 9 |
eval "use Cache::MemoryCache"; |
| 10 |
plan skip_all => "needs Cache::Cache for testing" if $@; |
| 11 |
eval { require DBD::Pg; }; |
| 12 |
plan skip_all => 'needs DBD::Pg for testing' if $@; |
| 13 |
} |
| 14 |
|
| 15 |
use lib 't/cdbi-t/testlib'; |
| 16 |
use Binary; |
| 17 |
|
| 18 |
eval { Binary->CONSTRUCT; }; |
| 19 |
if ($@) { |
| 20 |
diag <<SKIP; |
| 21 |
Pg connection failed ($@). Set env variables DBD_PG_DBNAME, DBD_PG_USER, |
| 22 |
DBD_PG_PASSWD to enable testing. |
| 23 |
SKIP |
| 24 |
plan skip_all => 'Pg connection failed.'; |
| 25 |
} |
| 26 |
|
| 27 |
plan tests => 40; |
| 28 |
|
| 29 |
Binary->data_type(bin => DBI::SQL_BINARY); |
| 30 |
Binary->db_Main->{ AutoCommit } = 0; |
| 31 |
|
| 32 |
SKIP: { |
| 33 |
for my $id (1 .. 10) { |
| 34 |
my $bin = "foo\0$id"; |
| 35 |
my $obj = eval { Binary->insert( |
| 36 |
{ |
| 37 |
# id => $id, |
| 38 |
bin => $bin, |
| 39 |
} |
| 40 |
) }; |
| 41 |
skip $@, 40 if $@; |
| 42 |
isa_ok $obj, 'Binary'; |
| 43 |
is $obj->id, $id, "id is $id"; |
| 44 |
is $obj->bin, $bin, "insert: bin ok"; |
| 45 |
|
| 46 |
$obj->bin("bar\0$id"); |
| 47 |
$obj->update; |
| 48 |
|
| 49 |
if ($obj->id % 2) { |
| 50 |
$obj->dbi_commit; |
| 51 |
my $new_obj = $obj->retrieve($obj->id); |
| 52 |
is $obj->bin, "bar\0$id", "update: bin ok"; |
| 53 |
} else { |
| 54 |
$obj->dbi_rollback; |
| 55 |
my $new_obj = $obj->retrieve($obj->id); |
| 56 |
is $new_obj, undef, "Rolled back"; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|