savepoints for Informix
[dbsrgits/DBIx-Class.git] / t / 748informix.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_INFORMIX_${_}" } qw/DSN USER PASS/};
10
11 #warn "$dsn $user $pass";
12
13 plan skip_all => 'Set $ENV{DBICTEST_INFORMIX_DSN}, _USER and _PASS to run this test'
14   unless $dsn;
15
16 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
17   auto_savepoint => 1
18 });
19
20 my $dbh = $schema->storage->dbh;
21
22 eval { $dbh->do("DROP TABLE artist") };
23
24 $dbh->do("CREATE TABLE artist (artistid SERIAL, name VARCHAR(255), charfield CHAR(10), rank INTEGER DEFAULT 13);");
25
26 my $ars = $schema->resultset('Artist');
27 is ( $ars->count, 0, 'No rows at first' );
28
29 # test primary key handling
30 my $new = $ars->create({ name => 'foo' });
31 ok($new->artistid, "Auto-PK worked");
32
33 # test explicit key spec
34 $new = $ars->create ({ name => 'bar', artistid => 66 });
35 is($new->artistid, 66, 'Explicit PK worked');
36 $new->discard_changes;
37 is($new->artistid, 66, 'Explicit PK assigned');
38
39 # test populate
40 lives_ok (sub {
41   my @pop;
42   for (1..2) {
43     push @pop, { name => "Artist_$_" };
44   }
45   $ars->populate (\@pop);
46 });
47
48 # test populate with explicit key
49 lives_ok (sub {
50   my @pop;
51   for (1..2) {
52     push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
53   }
54   $ars->populate (\@pop);
55 });
56
57 # count what we did so far
58 is ($ars->count, 6, 'Simple count works');
59
60 # test LIMIT support
61 my $lim = $ars->search( {},
62   {
63     rows => 3,
64     offset => 4,
65     order_by => 'artistid'
66   }
67 );
68 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
69 is( $lim->all, 2, 'Number of ->all objects matches count' );
70
71 # test iterator
72 $lim->reset;
73 is( $lim->next->artistid, 101, "iterator->next ok" );
74 is( $lim->next->artistid, 102, "iterator->next ok" );
75 is( $lim->next, undef, "next past end of resultset ok" );
76
77 # test savepoints
78 eval {
79   $schema->txn_do(sub {
80     eval {
81       $schema->txn_do(sub {
82         $ars->create({ name => 'in_savepoint' });
83         die "rolling back savepoint";
84       });
85     };
86     ok ((not $ars->search({ name => 'in_savepoint' })->first),
87       'savepoint rolled back');
88     $ars->create({ name => 'in_outer_txn' });
89     die "rolling back outer txn";
90   });
91 };
92
93 like $@, qr/rolling back outer txn/,
94   'correct exception for rollback';
95
96 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
97   'outer txn rolled back');
98
99 done_testing;
100
101 # clean up our mess
102 END {
103     my $dbh = eval { $schema->storage->_dbh };
104     $dbh->do("DROP TABLE artist") if $dbh;
105 }