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