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