'undef =>' isn't what one would want
[dbsrgits/DBIx-Class-Historic.git] / t / 748informix.t
CommitLineData
cb551b07 1use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_informix';
2
835cdc8d 3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
8use lib qw(t/lib);
9use DBICTest;
10
11my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_INFORMIX_${_}" } qw/DSN USER PASS/};
12
9fb04139 13my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
14 auto_savepoint => 1
15});
835cdc8d 16
17my $dbh = $schema->storage->dbh;
18
19eval { $dbh->do("DROP TABLE artist") };
835cdc8d 20$dbh->do("CREATE TABLE artist (artistid SERIAL, name VARCHAR(255), charfield CHAR(10), rank INTEGER DEFAULT 13);");
d3774d9b 21eval { $dbh->do("DROP TABLE cd") };
22$dbh->do(<<EOS);
23CREATE 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)
31EOS
32eval { $dbh->do("DROP TABLE track") };
33$dbh->do(<<EOS);
34CREATE 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)
43EOS
835cdc8d 44
45my $ars = $schema->resultset('Artist');
46is ( $ars->count, 0, 'No rows at first' );
47
48# test primary key handling
49my $new = $ars->create({ name => 'foo' });
50ok($new->artistid, "Auto-PK worked");
51
52# test explicit key spec
53$new = $ars->create ({ name => 'bar', artistid => 66 });
54is($new->artistid, 66, 'Explicit PK worked');
55$new->discard_changes;
56is($new->artistid, 66, 'Explicit PK assigned');
57
58# test populate
59lives_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
68lives_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
77is ($ars->count, 6, 'Simple count works');
78
79# test LIMIT support
80my $lim = $ars->search( {},
81 {
82 rows => 3,
83 offset => 4,
84 order_by => 'artistid'
85 }
86);
87is( $lim->count, 2, 'ROWS+OFFSET count ok' );
88is( $lim->all, 2, 'Number of ->all objects matches count' );
89
90# test iterator
91$lim->reset;
92is( $lim->next->artistid, 101, "iterator->next ok" );
93is( $lim->next->artistid, 102, "iterator->next ok" );
94is( $lim->next, undef, "next past end of resultset ok" );
95
9fb04139 96# test savepoints
b9889595 97throws_ok {
9fb04139 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 });
b9889595 110} qr/rolling back outer txn/,
9fb04139 111 'correct exception for rollback';
112
113ok ((not $ars->search({ name => 'in_outer_txn' })->first),
114 'outer txn rolled back');
835cdc8d 115
d3774d9b 116######## test with_deferred_fk_checks
117lives_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
128is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
8273e845 129 'code in with_deferred_fk_checks worked';
d3774d9b 130
131throws_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
835cdc8d 137done_testing;
138
139# clean up our mess
140END {
65d35121 141 my $dbh = eval { $schema->storage->_dbh };
142 $dbh->do("DROP TABLE artist") if $dbh;
143 undef $schema;
835cdc8d 144}