DBIx-Custom / t / dbix-custom-transaction-manager.t /
Newer Older
53 lines | 1.25kb
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1
use Test::More;
2

            
3
eval {require DBIx::TransactionManager; 1}
4
  or plan skip_all => 'required DBIx::TransactionManager';
5

            
6
plan 'no_plan';
7

            
8
use DBIx::Custom;
9

            
10
# Function for test name
11
my $test;
12
sub test {$test = shift }
13

            
14
# Constant varialbes for test
15
my $CREATE_TABLE = {
16
    0 => 'create table table1 (key1 char(255), key2 char(255));',
17
};
18

            
19
my $NEW_ARGS = {
20
    0 => {data_source => 'dbi:SQLite:dbname=:memory:'}
21
};
22

            
23
# Variables
24
my $dbi;
25
my $result;
26
my $txn;
27

            
28
test 'transaction';
29
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
30
$dbi->execute($CREATE_TABLE->{0});
31
{
32
    my $txn = $dbi->txn_scope;
33
    $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
34
    $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
35
    $txn->commit;
36
}
37
$result = $dbi->select(table => 'table1');
38
is_deeply(scalar $result->fetch_hash_all, [{key1 => 1, key2 => 2}, {key1 => 2, key2 => 3}],
39
          "$test : commit");
40

            
41
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
42
$dbi->execute($CREATE_TABLE->{0});
43

            
44
{
45
    local $SIG{__WARN__} = sub {};
46
    {
47
        my $txn = $dbi->txn_scope;
48
        $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
49
    }
50
}
51
$result = $dbi->select(table => 'table1');
52
ok(! $result->fetch_first, "$test: rollback");
53