Not sure what I was thinking, but 18637ebb never worked :/
[dbsrgits/DBIx-Class.git] / t / 748informix.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_informix';
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 use lib qw(t/lib);
9 use DBICTest;
10
11 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_INFORMIX_${_}" } qw/DSN USER PASS/};
12
13 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
14   auto_savepoint => 1
15 });
16
17 my $dbh = $schema->storage->dbh;
18
19 eval { $dbh->do("DROP TABLE artist") };
20 $dbh->do("CREATE TABLE artist (artistid SERIAL, name VARCHAR(255), charfield CHAR(10), rank INTEGER DEFAULT 13);");
21 eval { $dbh->do("DROP TABLE cd") };
22 $dbh->do(<<EOS);
23 CREATE TABLE cd (
24   cdid int PRIMARY KEY,
25   artist int,
26   title varchar(255),
27   year varchar(4),
28   genreid int,
29   single_track int
30 )
31 EOS
32 eval { $dbh->do("DROP TABLE track") };
33 $dbh->do(<<EOS);
34 CREATE TABLE track (
35   trackid int,
36   cd int REFERENCES cd(cdid),
37   position int,
38   title varchar(255),
39   last_updated_on date,
40   last_updated_at date,
41   small_dt date
42 )
43 EOS
44
45 my $ars = $schema->resultset('Artist');
46 is ( $ars->count, 0, 'No rows at first' );
47
48 # test primary key handling
49 my $new = $ars->create({ name => 'foo' });
50 ok($new->artistid, "Auto-PK worked");
51
52 # test explicit key spec
53 $new = $ars->create ({ name => 'bar', artistid => 66 });
54 is($new->artistid, 66, 'Explicit PK worked');
55 $new->discard_changes;
56 is($new->artistid, 66, 'Explicit PK assigned');
57
58 # test populate
59 lives_ok (sub {
60   my @pop;
61   for (1..2) {
62     push @pop, { name => "Artist_$_" };
63   }
64   $ars->populate (\@pop);
65 });
66
67 # test populate with explicit key
68 lives_ok (sub {
69   my @pop;
70   for (1..2) {
71     push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
72   }
73   $ars->populate (\@pop);
74 });
75
76 # count what we did so far
77 is ($ars->count, 6, 'Simple count works');
78
79 # test LIMIT support
80 my $lim = $ars->search( {},
81   {
82     rows => 3,
83     offset => 4,
84     order_by => 'artistid'
85   }
86 );
87 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
88 is( $lim->all, 2, 'Number of ->all objects matches count' );
89
90 # test iterator
91 $lim->reset;
92 is( $lim->next->artistid, 101, "iterator->next ok" );
93 is( $lim->next->artistid, 102, "iterator->next ok" );
94 is( $lim->next, undef, "next past end of resultset ok" );
95
96 # test savepoints
97 throws_ok {
98   $schema->txn_do(sub {
99     eval {
100       $schema->txn_do(sub {
101         $ars->create({ name => 'in_savepoint' });
102         die "rolling back savepoint";
103       });
104     };
105     ok ((not $ars->search({ name => 'in_savepoint' })->first),
106       'savepoint rolled back');
107     $ars->create({ name => 'in_outer_txn' });
108     die "rolling back outer txn";
109   });
110 } qr/rolling back outer txn/,
111   'correct exception for rollback';
112
113 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
114   'outer txn rolled back');
115
116 ######## test with_deferred_fk_checks
117 lives_ok {
118   $schema->storage->with_deferred_fk_checks(sub {
119     $schema->resultset('Track')->create({
120       trackid => 999, cd => 999, position => 1, title => 'deferred FK track'
121     });
122     $schema->resultset('CD')->create({
123       artist => 1, cdid => 999, year => '2003', title => 'deferred FK cd'
124     });
125   });
126 } 'with_deferred_fk_checks code survived';
127
128 is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
129  'code in with_deferred_fk_checks worked';
130
131 throws_ok {
132   $schema->resultset('Track')->create({
133     trackid => 1, cd => 9999, position => 1, title => 'Track1'
134   });
135 } qr/constraint/i, 'with_deferred_fk_checks is off';
136
137 done_testing;
138
139 # clean up our mess
140 END {
141   my $dbh = eval { $schema->storage->_dbh };
142   $dbh->do("DROP TABLE artist") if $dbh;
143   undef $schema;
144 }