Bring out the big-paranoia-harness - make describe_env infallible
[dbsrgits/DBIx-Class.git] / t / 750firebird.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3a8aae80 3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
199fbc45 8use DBIx::Class::Optional::Dependencies ();
bbf6a9a5 9use DBIx::Class::_Util 'scope_guard';
2c649faf 10use List::Util 'shuffle';
5346c8ff 11use Try::Tiny;
c0329273 12
3a8aae80 13use DBICTest;
14
49b3a264 15my $env2optdep = {
16 DBICTEST_FIREBIRD => 'test_rdbms_firebird',
17 DBICTEST_FIREBIRD_INTERBASE => 'test_rdbms_firebird_interbase',
18 DBICTEST_FIREBIRD_ODBC => 'test_rdbms_firebird_odbc',
19};
20
21plan skip_all => join (' ',
22 'Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_INTERBASE_DSN}',
23 'and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN},',
24 '_USER and _PASS to run these tests.',
25
26 'WARNING: this test creates and drops the tables "artist", "bindtype_test" and',
27 '"sequence_test"; the generators "gen_artist_artistid", "pkid1_seq", "pkid2_seq"',
28 'and "nonpkid_seq" and the trigger "artist_bi".',
29) unless grep { $ENV{"${_}_DSN"} } keys %$env2optdep;
3a8aae80 30
f7e5fd18 31# Example DSNs:
f7e5fd18 32# dbi:Firebird:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
199fbc45 33# dbi:InterBase:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
f7e5fd18 34
35# Example ODBC DSN:
36# dbi:ODBC:Driver=Firebird;Dbname=/var/lib/firebird/2.5/data/hlaghdb.fdb
37
145b2a3d 38my $schema;
3a8aae80 39
2c649faf 40for my $prefix (shuffle keys %$env2optdep) { SKIP: {
49b3a264 41
3cff955a 42 DBIx::Class::Optional::Dependencies->skip_without( $env2optdep->{$prefix} );
49b3a264 43
63af9ced 44 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
63af9ced 45 note "Testing with ${prefix}_DSN";
46
c7e85630 47 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
dd2109ee 48 auto_savepoint => 1,
5346c8ff 49 quote_names => 1,
e46df41a 50 ($dsn !~ /ODBC/ ? (on_connect_call => 'use_softcommit') : ()),
32323fc2 51 });
3a8aae80 52 my $dbh = $schema->storage->dbh;
53
bbf6a9a5 54 my $sg = scope_guard { cleanup($schema) };
3a8aae80 55
930689e6 56 eval { $dbh->do(q[DROP TABLE "artist"]) };
3a8aae80 57 $dbh->do(<<EOF);
930689e6 58 CREATE TABLE "artist" (
59 "artistid" INT PRIMARY KEY,
60 "name" VARCHAR(255),
61 "charfield" CHAR(10),
62 "rank" INT DEFAULT 13
3a8aae80 63 )
64EOF
930689e6 65 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
66 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
67 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
3a8aae80 68 $dbh->do(<<EOF);
930689e6 69 CREATE TRIGGER "artist_bi" FOR "artist"
3a8aae80 70 ACTIVE BEFORE INSERT POSITION 0
71 AS
72 BEGIN
930689e6 73 IF (NEW."artistid" IS NULL) THEN
74 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
3a8aae80 75 END
76EOF
e1958268 77 eval { $dbh->do('DROP TABLE "sequence_test"') };
78 $dbh->do(<<EOF);
79 CREATE TABLE "sequence_test" (
80 "pkid1" INT NOT NULL,
81 "pkid2" INT NOT NULL,
82 "nonpkid" INT,
83 "name" VARCHAR(255)
84 )
85EOF
86 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
87 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
07cda1c5 88 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
e1958268 89 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
90 $dbh->do('CREATE GENERATOR "pkid1_seq"');
07cda1c5 91 $dbh->do('CREATE GENERATOR pkid2_seq');
92 $dbh->do('SET GENERATOR pkid2_seq TO 9');
e1958268 93 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
94 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
3a8aae80 95
96 my $ars = $schema->resultset('Artist');
97 is ( $ars->count, 0, 'No rows at first' );
98
99# test primary key handling
100 my $new = $ars->create({ name => 'foo' });
101 ok($new->artistid, "Auto-PK worked");
102
e1958268 103# test auto increment using generators WITHOUT triggers
104 for (1..5) {
105 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
106 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
107 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
108 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
109 }
110 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
111 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
112
e02b39b4 113# test transaction commit
114 $schema->txn_do(sub {
115 $ars->create({ name => 'in_transaction' });
116 });
117 ok (($ars->search({ name => 'in_transaction' })->first),
118 'transaction committed');
119 is $schema->storage->_dbh->{AutoCommit}, 1,
120 '$dbh->{AutoCommit} is correct after transaction commit';
121
122 $ars->search({ name => 'in_transaction' })->delete;
123
32323fc2 124# test savepoints
b9889595 125 throws_ok {
a499b173 126 $schema->txn_do(sub {
e78660c7 127 my ($schema, $ars) = @_;
a499b173 128 eval {
129 $schema->txn_do(sub {
130 $ars->create({ name => 'in_savepoint' });
131 die "rolling back savepoint";
132 });
133 };
134 ok ((not $ars->search({ name => 'in_savepoint' })->first),
135 'savepoint rolled back');
136 $ars->create({ name => 'in_outer_txn' });
137 die "rolling back outer txn";
e78660c7 138 }, $schema, $ars);
b9889595 139 } qr/rolling back outer txn/,
5c6ed0b5 140 'correct exception for rollback';
141
e02b39b4 142 is $schema->storage->_dbh->{AutoCommit}, 1,
143 '$dbh->{AutoCommit} is correct after transaction rollback';
144
a499b173 145 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
146 'outer txn rolled back');
32323fc2 147
3a8aae80 148# test explicit key spec
149 $new = $ars->create ({ name => 'bar', artistid => 66 });
150 is($new->artistid, 66, 'Explicit PK worked');
151 $new->discard_changes;
152 is($new->artistid, 66, 'Explicit PK assigned');
153
e1958268 154# row update
babd3d8e 155 lives_ok {
156 $new->update({ name => 'baz' })
157 } 'update survived';
158 $new->discard_changes;
159 is $new->name, 'baz', 'row updated';
160
3a8aae80 161# test populate
162 lives_ok (sub {
163 my @pop;
164 for (1..2) {
165 push @pop, { name => "Artist_$_" };
166 }
167 $ars->populate (\@pop);
168 });
169
170# test populate with explicit key
171 lives_ok (sub {
172 my @pop;
173 for (1..2) {
174 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
175 }
17d59d97 176 $ars->populate (\@pop);
3a8aae80 177 });
178
179# count what we did so far
180 is ($ars->count, 6, 'Simple count works');
181
e1958268 182# test ResultSet UPDATE
17d59d97 183 lives_and {
184 $ars->search({ name => 'foo' })->update({ rank => 4 });
185
dd2109ee 186 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
72537d31 187 } 'Can update a column';
188
189 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
dd2109ee 190 is eval { $updated->rank }, 4, 'and the update made it to the database';
72537d31 191
3a8aae80 192# test LIMIT support
193 my $lim = $ars->search( {},
194 {
195 rows => 3,
196 offset => 4,
197 order_by => 'artistid'
198 }
199 );
200 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
201 is( $lim->all, 2, 'Number of ->all objects matches count' );
202
203# test iterator
204 $lim->reset;
dd2109ee 205 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
206 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
3a8aae80 207 is( $lim->next, undef, "next past end of resultset ok" );
208
5346c8ff 209# test bug in paging
210 my $paged = $ars->search({ name => { -like => 'Artist%' } }, {
211 page => 1,
212 rows => 2,
213 order_by => 'artistid',
214 });
215
216 my $row;
217 lives_ok {
218 $row = $paged->next;
219 } 'paged query survived';
220
221 is try { $row->artistid }, 5, 'correct row from paged query';
222
223 # DBD bug - if any unfinished statements are present during
224 # DDL manipulation (test blobs below)- a segfault will occur
225 $paged->reset;
226
b9889595 227# test nested cursors
17d59d97 228 {
229 my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }});
17d59d97 230
b9889595 231 my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
232 order_by => { -desc => 'artistid' }
233 });
234
235 is $rs2->next->artistid, 1, 'nested cursors';
17d59d97 236 }
237
3a8aae80 238# test empty insert
28d28903 239 lives_and {
240 my $row = $ars->create({});
241 ok $row->artistid;
242 } 'empty insert works';
243
244# test inferring the generator from the trigger source and using it with
245# auto_nextval
3a8aae80 246 {
28d28903 247 local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
3a8aae80 248
28d28903 249 lives_and {
250 my $row = $ars->create({ name => 'introspecting generator' });
251 ok $row->artistid;
252 } 'inferring generator from trigger source works';
3a8aae80 253 }
254
a2f22854 255 # at this point there should be no active statements
256 # (finish() was called everywhere, either explicitly via
257 # reset() or on DESTROY)
258 for (keys %{$schema->storage->dbh->{CachedKids}}) {
259 fail("Unreachable cached statement still active: $_")
260 if $schema->storage->dbh->{CachedKids}{$_}->FETCH('Active');
261 }
262
3a8aae80 263# test blobs (stolen from 73oracle.t)
ba19fccc 264 eval { $dbh->do('DROP TABLE "bindtype_test"') };
e1958268 265 $dbh->do(q[
ba19fccc 266 CREATE TABLE "bindtype_test"
e1958268 267 (
268 "id" INT PRIMARY KEY,
269 "bytea" INT,
ba19fccc 270 "blob" BLOB,
f3a9ea3d 271 "clob" BLOB SUB_TYPE TEXT,
272 "a_memo" INT
e1958268 273 )
274 ]);
275
276 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
277 $binstr{'large'} = $binstr{'small'} x 1024;
278
279 my $maxloblen = length $binstr{'large'};
280 local $dbh->{'LongReadLen'} = $maxloblen;
281
ba19fccc 282 my $rs = $schema->resultset('BindType');
e1958268 283 my $id = 0;
284
ba19fccc 285 foreach my $type (qw( blob clob )) {
e1958268 286 foreach my $size (qw( small large )) {
287 $id++;
3a8aae80 288
289# turn off horrendous binary DBIC_TRACE output
e1958268 290 local $schema->storage->{debug} = 0;
3a8aae80 291
e1958268 292 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
293 "inserted $size $type without dying";
3a8aae80 294
cdf7f026 295 my $got = $rs->find($id)->$type;
296
297 my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
298
299 ok($got eq $binstr{$size}, "verified inserted $size $type" )
300 or do {
301 diag "For " . (ref $schema->storage) . "\n";
302 diag "Got blob:\n";
303 diag $hexdump->(substr($got,0,50));
304 diag "Expecting blob:\n";
305 diag $hexdump->(substr($binstr{$size},0,50));
306 };
3a8aae80 307 }
308 }
49b3a264 309}}
3a8aae80 310
311done_testing;
312
313# clean up our mess
dff4c3a3 314
315sub cleanup {
65d35121 316 my $schema = shift;
317
145b2a3d 318 my $dbh;
319 eval {
320 $schema->storage->disconnect; # to avoid object FOO is in use errors
321 $dbh = $schema->storage->dbh;
322 };
323 return unless $dbh;
324
930689e6 325 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
145b2a3d 326 diag $@ if $@;
327
07cda1c5 328 foreach my $generator (qw/
329 "gen_artist_artistid"
330 "pkid1_seq"
331 pkid2_seq
332 "nonpkid_seq"
333 /) {
334 eval { $dbh->do(qq{DROP GENERATOR $generator}) };
e1958268 335 diag $@ if $@;
336 }
145b2a3d 337
5346c8ff 338 foreach my $table (qw/artist sequence_test/) {
9633951d 339 eval { $dbh->do(qq[DROP TABLE "$table"]) };
e1958268 340 diag $@ if $@;
3a8aae80 341 }
5346c8ff 342
343 eval { $dbh->do(q{DROP TABLE "bindtype_test"}) };
344 diag $@ if $@;
3a8aae80 345}