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