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