savepoints for Informix
[dbsrgits/DBIx-Class.git] / t / 748informix.t
CommitLineData
835cdc8d 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
9my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_INFORMIX_${_}" } qw/DSN USER PASS/};
10
11#warn "$dsn $user $pass";
12
13plan skip_all => 'Set $ENV{DBICTEST_INFORMIX_DSN}, _USER and _PASS to run this test'
88704779 14 unless $dsn;
835cdc8d 15
9fb04139 16my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
17 auto_savepoint => 1
18});
835cdc8d 19
20my $dbh = $schema->storage->dbh;
21
22eval { $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
26my $ars = $schema->resultset('Artist');
27is ( $ars->count, 0, 'No rows at first' );
28
29# test primary key handling
30my $new = $ars->create({ name => 'foo' });
31ok($new->artistid, "Auto-PK worked");
32
33# test explicit key spec
34$new = $ars->create ({ name => 'bar', artistid => 66 });
35is($new->artistid, 66, 'Explicit PK worked');
36$new->discard_changes;
37is($new->artistid, 66, 'Explicit PK assigned');
38
39# test populate
40lives_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
49lives_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
58is ($ars->count, 6, 'Simple count works');
59
60# test LIMIT support
61my $lim = $ars->search( {},
62 {
63 rows => 3,
64 offset => 4,
65 order_by => 'artistid'
66 }
67);
68is( $lim->count, 2, 'ROWS+OFFSET count ok' );
69is( $lim->all, 2, 'Number of ->all objects matches count' );
70
71# test iterator
72$lim->reset;
73is( $lim->next->artistid, 101, "iterator->next ok" );
74is( $lim->next->artistid, 102, "iterator->next ok" );
75is( $lim->next, undef, "next past end of resultset ok" );
76
9fb04139 77# test savepoints
78eval {
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
93like $@, qr/rolling back outer txn/,
94 'correct exception for rollback';
95
96ok ((not $ars->search({ name => 'in_outer_txn' })->first),
97 'outer txn rolled back');
835cdc8d 98
99done_testing;
100
101# clean up our mess
102END {
103 my $dbh = eval { $schema->storage->_dbh };
104 $dbh->do("DROP TABLE artist") if $dbh;
105}