Move the Subclassing POD under Examples
[dbsrgits/DBIx-Class.git] / t / 748informix.t
CommitLineData
835cdc8d 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
199fbc45 6use DBIx::Class::Optional::Dependencies ();
835cdc8d 7use lib qw(t/lib);
8use DBICTest;
9
199fbc45 10plan 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
835cdc8d 13my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_INFORMIX_${_}" } qw/DSN USER PASS/};
14
15#warn "$dsn $user $pass";
16
17plan skip_all => 'Set $ENV{DBICTEST_INFORMIX_DSN}, _USER and _PASS to run this test'
88704779 18 unless $dsn;
835cdc8d 19
9fb04139 20my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
21 auto_savepoint => 1
22});
835cdc8d 23
24my $dbh = $schema->storage->dbh;
25
26eval { $dbh->do("DROP TABLE artist") };
835cdc8d 27$dbh->do("CREATE TABLE artist (artistid SERIAL, name VARCHAR(255), charfield CHAR(10), rank INTEGER DEFAULT 13);");
d3774d9b 28eval { $dbh->do("DROP TABLE cd") };
29$dbh->do(<<EOS);
30CREATE 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)
38EOS
39eval { $dbh->do("DROP TABLE track") };
40$dbh->do(<<EOS);
41CREATE 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)
50EOS
835cdc8d 51
52my $ars = $schema->resultset('Artist');
53is ( $ars->count, 0, 'No rows at first' );
54
55# test primary key handling
56my $new = $ars->create({ name => 'foo' });
57ok($new->artistid, "Auto-PK worked");
58
59# test explicit key spec
60$new = $ars->create ({ name => 'bar', artistid => 66 });
61is($new->artistid, 66, 'Explicit PK worked');
62$new->discard_changes;
63is($new->artistid, 66, 'Explicit PK assigned');
64
65# test populate
66lives_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
75lives_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
84is ($ars->count, 6, 'Simple count works');
85
86# test LIMIT support
87my $lim = $ars->search( {},
88 {
89 rows => 3,
90 offset => 4,
91 order_by => 'artistid'
92 }
93);
94is( $lim->count, 2, 'ROWS+OFFSET count ok' );
95is( $lim->all, 2, 'Number of ->all objects matches count' );
96
97# test iterator
98$lim->reset;
99is( $lim->next->artistid, 101, "iterator->next ok" );
100is( $lim->next->artistid, 102, "iterator->next ok" );
101is( $lim->next, undef, "next past end of resultset ok" );
102
9fb04139 103# test savepoints
b9889595 104throws_ok {
9fb04139 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 });
b9889595 117} qr/rolling back outer txn/,
9fb04139 118 'correct exception for rollback';
119
120ok ((not $ars->search({ name => 'in_outer_txn' })->first),
121 'outer txn rolled back');
835cdc8d 122
d3774d9b 123######## test with_deferred_fk_checks
124lives_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
135is eval { $schema->resultset('Track')->find(999)->title }, 'deferred FK track',
8273e845 136 'code in with_deferred_fk_checks worked';
d3774d9b 137
138throws_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
835cdc8d 144done_testing;
145
146# clean up our mess
147END {
65d35121 148 my $dbh = eval { $schema->storage->_dbh };
149 $dbh->do("DROP TABLE artist") if $dbh;
150 undef $schema;
835cdc8d 151}