fix and regression test for RT #62642
[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") };
835cdc8d 23$dbh->do("CREATE TABLE artist (artistid SERIAL, name VARCHAR(255), charfield CHAR(10), rank INTEGER DEFAULT 13);");
d3774d9b 24eval { $dbh->do("DROP TABLE cd") };
25$dbh->do(<<EOS);
26CREATE 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)
34EOS
35eval { $dbh->do("DROP TABLE track") };
36$dbh->do(<<EOS);
37CREATE 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)
46EOS
835cdc8d 47
48my $ars = $schema->resultset('Artist');
49is ( $ars->count, 0, 'No rows at first' );
50
51# test primary key handling
52my $new = $ars->create({ name => 'foo' });
53ok($new->artistid, "Auto-PK worked");
54
55# test explicit key spec
56$new = $ars->create ({ name => 'bar', artistid => 66 });
57is($new->artistid, 66, 'Explicit PK worked');
58$new->discard_changes;
59is($new->artistid, 66, 'Explicit PK assigned');
60
61# test populate
62lives_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
71lives_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
80is ($ars->count, 6, 'Simple count works');
81
82# test LIMIT support
83my $lim = $ars->search( {},
84 {
85 rows => 3,
86 offset => 4,
87 order_by => 'artistid'
88 }
89);
90is( $lim->count, 2, 'ROWS+OFFSET count ok' );
91is( $lim->all, 2, 'Number of ->all objects matches count' );
92
93# test iterator
94$lim->reset;
95is( $lim->next->artistid, 101, "iterator->next ok" );
96is( $lim->next->artistid, 102, "iterator->next ok" );
97is( $lim->next, undef, "next past end of resultset ok" );
98
9fb04139 99# test savepoints
b9889595 100throws_ok {
9fb04139 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 });
b9889595 113} qr/rolling back outer txn/,
9fb04139 114 'correct exception for rollback';
115
116ok ((not $ars->search({ name => 'in_outer_txn' })->first),
117 'outer txn rolled back');
835cdc8d 118
d3774d9b 119######## test with_deferred_fk_checks
120lives_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
131is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
132 'code in with_deferred_fk_checks worked';
133
134throws_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
835cdc8d 140done_testing;
141
142# clean up our mess
143END {
144 my $dbh = eval { $schema->storage->_dbh };
145 $dbh->do("DROP TABLE artist") if $dbh;
146}