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