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