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