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