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