Restore ability to handle underdefined root (t/prefetch/incomplete.t)
[dbsrgits/DBIx-Class.git] / t / 750firebird.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use DBIx::Class::Optional::Dependencies ();
7 use Scope::Guard ();
8 use Try::Tiny;
9 use lib qw(t/lib);
10 use DBICTest;
11
12 my $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
18 plan 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;
27
28 # tests stolen from 749sybase_asa.t
29
30 # Example DSNs:
31 # dbi:Firebird:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
32 # dbi:InterBase:db=/var/lib/firebird/2.5/data/hlaghdb.fdb
33
34 # Example ODBC DSN:
35 # dbi:ODBC:Driver=Firebird;Dbname=/var/lib/firebird/2.5/data/hlaghdb.fdb
36
37 my $schema;
38
39 for my $prefix (keys %$env2optdep) { SKIP: {
40
41   my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
42
43   next unless $dsn;
44
45   note "Testing with ${prefix}_DSN";
46
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
50   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
51     auto_savepoint  => 1,
52     quote_names     => 1,
53     ($dsn !~ /ODBC/ ? (on_connect_call => 'use_softcommit') : ()),
54   });
55   my $dbh = $schema->storage->dbh;
56
57   my $sg = Scope::Guard->new(sub { cleanup($schema) });
58
59   eval { $dbh->do(q[DROP TABLE "artist"]) };
60   $dbh->do(<<EOF);
61   CREATE TABLE "artist" (
62     "artistid" INT PRIMARY KEY,
63     "name" VARCHAR(255),
64     "charfield" CHAR(10),
65     "rank" INT DEFAULT 13
66   )
67 EOF
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"') };
71   $dbh->do(<<EOF);
72   CREATE TRIGGER "artist_bi" FOR "artist"
73   ACTIVE BEFORE INSERT POSITION 0
74   AS
75   BEGIN
76    IF (NEW."artistid" IS NULL) THEN
77     NEW."artistid" = GEN_ID("gen_artist_artistid",1);
78   END
79 EOF
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   )
88 EOF
89   $dbh->do('ALTER TABLE "sequence_test" ADD CONSTRAINT "sequence_test_constraint" PRIMARY KEY ("pkid1", "pkid2")');
90   eval { $dbh->do('DROP GENERATOR "pkid1_seq"') };
91   eval { $dbh->do('DROP GENERATOR pkid2_seq') };
92   eval { $dbh->do('DROP GENERATOR "nonpkid_seq"') };
93   $dbh->do('CREATE GENERATOR "pkid1_seq"');
94   $dbh->do('CREATE GENERATOR pkid2_seq');
95   $dbh->do('SET GENERATOR pkid2_seq TO 9');
96   $dbh->do('CREATE GENERATOR "nonpkid_seq"');
97   $dbh->do('SET GENERATOR "nonpkid_seq" TO 19');
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
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
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
127 # test savepoints
128   throws_ok {
129     $schema->txn_do(sub {
130       my ($schema, $ars) = @_;
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";
141     }, $schema, $ars);
142   } qr/rolling back outer txn/,
143     'correct exception for rollback';
144
145   is $schema->storage->_dbh->{AutoCommit}, 1,
146     '$dbh->{AutoCommit} is correct after transaction rollback';
147
148   ok ((not $ars->search({ name => 'in_outer_txn' })->first),
149     'outer txn rolled back');
150
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
157 # row update
158   lives_ok {
159     $new->update({ name => 'baz' })
160   } 'update survived';
161   $new->discard_changes;
162   is $new->name, 'baz', 'row updated';
163
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     }
179     $ars->populate (\@pop);
180   });
181
182 # count what we did so far
183   is ($ars->count, 6, 'Simple count works');
184
185 # test ResultSet UPDATE
186   lives_and {
187     $ars->search({ name => 'foo' })->update({ rank => 4 });
188
189     is eval { $ars->search({ name => 'foo' })->first->rank }, 4;
190   } 'Can update a column';
191
192   my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
193   is eval { $updated->rank }, 4, 'and the update made it to the database';
194
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;
208   is( eval { $lim->next->artistid }, 101, "iterator->next ok" );
209   is( eval { $lim->next->artistid }, 102, "iterator->next ok" );
210   is( $lim->next, undef, "next past end of resultset ok" );
211
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
230 # test nested cursors
231   {
232     my $rs1 = $ars->search({}, { order_by => { -asc  => 'artistid' }});
233
234     my $rs2 = $ars->search({ artistid => $rs1->next->artistid }, {
235       order_by => { -desc => 'artistid' }
236     });
237
238     is $rs2->next->artistid, 1, 'nested cursors';
239   }
240
241 # test empty insert
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
249   {
250     local $ars->result_source->column_info('artistid')->{auto_nextval} = 1;
251
252     lives_and {
253       my $row = $ars->create({ name => 'introspecting generator' });
254       ok $row->artistid;
255     } 'inferring generator from trigger source works';
256   }
257
258 # test blobs (stolen from 73oracle.t)
259   eval { $dbh->do('DROP TABLE "bindtype_test"') };
260   $dbh->do(q[
261   CREATE TABLE "bindtype_test"
262   (
263     "id"     INT PRIMARY KEY,
264     "bytea"  INT,
265     "blob"   BLOB,
266     "clob"   BLOB SUB_TYPE TEXT,
267     "a_memo" INT
268   )
269   ]);
270
271   my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
272   $binstr{'large'} = $binstr{'small'} x 1024;
273
274   my $maxloblen = length $binstr{'large'};
275   local $dbh->{'LongReadLen'} = $maxloblen;
276
277   my $rs = $schema->resultset('BindType');
278   my $id = 0;
279
280   foreach my $type (qw( blob clob )) {
281     foreach my $size (qw( small large )) {
282       $id++;
283
284 # turn off horrendous binary DBIC_TRACE output
285       local $schema->storage->{debug} = 0;
286
287       lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
288       "inserted $size $type without dying";
289
290       my $got = $rs->find($id)->$type;
291
292       my $hexdump = sub { join '', map sprintf('%02X', ord), split //, shift };
293
294       ok($got eq $binstr{$size}, "verified inserted $size $type" )
295         or do {
296             diag "For " . (ref $schema->storage) . "\n";
297             diag "Got blob:\n";
298             diag $hexdump->(substr($got,0,50));
299             diag "Expecting blob:\n";
300             diag $hexdump->(substr($binstr{$size},0,50));
301         };
302     }
303   }
304 }}
305
306 done_testing;
307
308 # clean up our mess
309
310 sub cleanup {
311   my $schema = shift;
312
313   my $dbh;
314   eval {
315     $schema->storage->disconnect; # to avoid object FOO is in use errors
316     $dbh = $schema->storage->dbh;
317   };
318   return unless $dbh;
319
320   eval { $dbh->do('DROP TRIGGER "artist_bi"') };
321   diag $@ if $@;
322
323   foreach my $generator (qw/
324     "gen_artist_artistid"
325     "pkid1_seq"
326     pkid2_seq
327     "nonpkid_seq"
328   /) {
329     eval { $dbh->do(qq{DROP GENERATOR $generator}) };
330     diag $@ if $@;
331   }
332
333   foreach my $table (qw/artist sequence_test/) {
334     eval { $dbh->do(qq[DROP TABLE "$table"]) };
335     diag $@ if $@;
336   }
337
338   eval { $dbh->do(q{DROP TABLE "bindtype_test"}) };
339   diag $@ if $@;
340 }