Switch reverse_relationship_info() to the relcond resolver
[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';
c0329273 11
3a8aae80 12use DBICTest;
13
49b3a264 14my $env2optdep = {
15 DBICTEST_FIREBIRD => 'test_rdbms_firebird',
16 DBICTEST_FIREBIRD_INTERBASE => 'test_rdbms_firebird_interbase',
17 DBICTEST_FIREBIRD_ODBC => 'test_rdbms_firebird_odbc',
18};
19
20plan skip_all => join (' ',
21 'Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_INTERBASE_DSN}',
22 'and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN},',
23 '_USER and _PASS to run these tests.',
24
25 'WARNING: this test creates and drops the tables "artist", "bindtype_test" and',
26 '"sequence_test"; the generators "gen_artist_artistid", "pkid1_seq", "pkid2_seq"',
27 'and "nonpkid_seq" and the trigger "artist_bi".',
28) unless grep { $ENV{"${_}_DSN"} } keys %$env2optdep;
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
2c649faf 39for my $prefix (shuffle keys %$env2optdep) { SKIP: {
49b3a264 40
3cff955a 41 DBIx::Class::Optional::Dependencies->skip_without( $env2optdep->{$prefix} );
49b3a264 42
63af9ced 43 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
63af9ced 44 note "Testing with ${prefix}_DSN";
45
c7e85630 46 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
dd2109ee 47 auto_savepoint => 1,
5346c8ff 48 quote_names => 1,
e46df41a 49 ($dsn !~ /ODBC/ ? (on_connect_call => 'use_softcommit') : ()),
32323fc2 50 });
3a8aae80 51 my $dbh = $schema->storage->dbh;
52
bbf6a9a5 53 my $sg = scope_guard { cleanup($schema) };
3a8aae80 54
930689e6 55 eval { $dbh->do(q[DROP TABLE "artist"]) };
3a8aae80 56 $dbh->do(<<EOF);
930689e6 57 CREATE TABLE "artist" (
58 "artistid" INT PRIMARY KEY,
59 "name" VARCHAR(255),
60 "charfield" CHAR(10),
61 "rank" INT DEFAULT 13
3a8aae80 62 )
63EOF
930689e6 64 eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
65 $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
66 eval { $dbh->do('DROP TRIGGER "artist_bi"') };
3a8aae80 67 $dbh->do(<<EOF);
930689e6 68 CREATE TRIGGER "artist_bi" FOR "artist"
3a8aae80 69 ACTIVE BEFORE INSERT POSITION 0
70 AS
71 BEGIN
930689e6 72 IF (NEW."artistid" IS NULL) THEN
73 NEW."artistid" = GEN_ID("gen_artist_artistid",1);
3a8aae80 74 END
75EOF
e1958268 76 eval { $dbh->do('DROP TABLE "sequence_test"') };
77 $dbh->do(<<EOF);
78 CREATE TABLE "sequence_test" (
79 "pkid1" INT NOT NULL,
80 "pkid2" INT NOT NULL,
81 "nonpkid" INT,
82 "name" VARCHAR(255)
83 )
84EOF
85 $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
86 eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
07cda1c5 87 eval { $dbh->do('DROP GENERATOR pkid2_seq') };
e1958268 88 eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
89 $dbh->do('CREATE GENERATOR "pkid1_seq"');
07cda1c5 90 $dbh->do('CREATE GENERATOR pkid2_seq');
91 $dbh->do('SET GENERATOR pkid2_seq TO 9');
e1958268 92 $dbh->do('CREATE GENERATOR "nonpkid_seq"');
93 $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
3a8aae80 94
95 my $ars = $schema->resultset('Artist');
96 is ( $ars->count, 0, 'No rows at first' );
97
98# test primary key handling
99 my $new = $ars->create({ name => 'foo' });
100 ok($new->artistid, "Auto-PK worked");
101
e1958268 102# test auto increment using generators WITHOUT triggers
103 for (1..5) {
104 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
105 is($st->pkid1, $_, "Firebird Auto-PK without trigger: First primary key");
106 is($st->pkid2, $_ + 9, "Firebird Auto-PK without trigger: Second primary key");
107 is($st->nonpkid, $_ + 19, "Firebird Auto-PK without trigger: Non-primary key");
108 }
109 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
110 is($st->pkid1, 55, "Firebird Auto-PK without trigger: First primary key set manually");
111
e02b39b4 112# test transaction commit
113 $schema->txn_do(sub {
114 $ars->create({ name => 'in_transaction' });
115 });
116 ok (($ars->search({ name => 'in_transaction' })->first),
117 'transaction committed');
118 is $schema->storage->_dbh->{AutoCommit}, 1,
119 '$dbh->{AutoCommit} is correct after transaction commit';
120
121 $ars->search({ name => 'in_transaction' })->delete;
122
32323fc2 123# test savepoints
b9889595 124 throws_ok {
a499b173 125 $schema->txn_do(sub {
e78660c7 126 my ($schema, $ars) = @_;
a499b173 127 eval {
128 $schema->txn_do(sub {
129 $ars->create({ name => 'in_savepoint' });
130 die "rolling back savepoint";
131 });
132 };
133 ok ((not $ars->search({ name => 'in_savepoint' })->first),
134 'savepoint rolled back');
135 $ars->create({ name => 'in_outer_txn' });
136 die "rolling back outer txn";
e78660c7 137 }, $schema, $ars);
b9889595 138 } qr/rolling back outer txn/,
5c6ed0b5 139 'correct exception for rollback';
140
e02b39b4 141 is $schema->storage->_dbh->{AutoCommit}, 1,
142 '$dbh->{AutoCommit} is correct after transaction rollback';
143
a499b173 144 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
145 'outer txn rolled back');
32323fc2 146
3a8aae80 147# test explicit key spec
148 $new = $ars->create ({ name => 'bar', artistid => 66 });
149 is($new->artistid, 66, 'Explicit PK worked');
150 $new->discard_changes;
151 is($new->artistid, 66, 'Explicit PK assigned');
152
e1958268 153# row update
babd3d8e 154 lives_ok {
155 $new->update({ name => 'baz' })
156 } 'update survived';
157 $new->discard_changes;
158 is $new->name, 'baz', 'row updated';
159
3a8aae80 160# test populate
161 lives_ok (sub {
162 my @pop;
163 for (1..2) {
164 push @pop, { name => "Artist_$_" };
165 }
166 $ars->populate (\@pop);
167 });
168
169# test populate with explicit key
170 lives_ok (sub {
171 my @pop;
172 for (1..2) {
173 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
174 }
17d59d97 175 $ars->populate (\@pop);
3a8aae80 176 });
177
178# count what we did so far
179 is ($ars->count, 6, 'Simple count works');
180
e1958268 181# test ResultSet UPDATE
17d59d97 182 lives_and {
183 $ars->search({ name => 'foo' })->update({ rank => 4 });
184
dd2109ee 185 is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
72537d31 186 } 'Can update a column';
187
188 my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
dd2109ee 189 is eval { $updated->rank }, 4, 'and the update made it to the database';
72537d31 190
3a8aae80 191# test LIMIT support
192 my $lim = $ars->search( {},
193 {
194 rows => 3,
195 offset => 4,
196 order_by => 'artistid'
197 }
198 );
199 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
200 is( $lim->all, 2, 'Number of ->all objects matches count' );
201
202# test iterator
203 $lim->reset;
dd2109ee 204 is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
205 is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
3a8aae80 206 is( $lim->next, undef, "next past end of resultset ok" );
207
5346c8ff 208# test bug in paging
209 my $paged = $ars->search({ name => { -like => 'Artist%' } }, {
210 page => 1,
211 rows => 2,
212 order_by => 'artistid',
213 });
214
215 my $row;
216 lives_ok {
217 $row = $paged->next;
218 } 'paged query survived';
219
e2741c7f 220 is(
221 eval { $row->artistid },
222 5,
223 'correct row from paged query'
224 );
5346c8ff 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}