Fix failing tests with DBICTEST_SYBASE_DSN set
[dbsrgits/DBIx-Class.git] / t / 746sybase.t
CommitLineData
cb551b07 1use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_ase';
2
a964a928 3use strict;
68de9438 4use warnings;
d867eeda 5no warnings 'uninitialized';
a964a928 6
7use Test::More;
e0b2344f 8use Test::Exception;
a964a928 9use lib qw(t/lib);
10use DBICTest;
2baff5da 11
d867eeda 12my @storage_types = (
95787afe 13 'DBI::Sybase::ASE',
14 'DBI::Sybase::ASE::NoBindVars',
d867eeda 15);
cb551b07 16eval "require DBIx::Class::Storage::$_;" or die $@
17 for @storage_types;
95787afe 18
d867eeda 19my $schema;
20my $storage_idx = -1;
21
cb551b07 22my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
23
d867eeda 24sub get_schema {
25 DBICTest::Schema->connect($dsn, $user, $pass, {
26 on_connect_call => [
27 [ blob_setup => log_on_update => 1 ], # this is a safer option
28 ],
29 });
30}
31
32my $ping_count = 0;
33{
95787afe 34 my $ping = DBIx::Class::Storage::DBI::Sybase::ASE->can('_ping');
35 *DBIx::Class::Storage::DBI::Sybase::ASE::_ping = sub {
d867eeda 36 $ping_count++;
37 goto $ping;
38 };
39}
40
41for my $storage_type (@storage_types) {
42 $storage_idx++;
43
95787afe 44 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
d867eeda 45 DBICTest::Schema->storage_type("::$storage_type");
46 }
61cfaef7 47
d867eeda 48 $schema = get_schema();
a964a928 49
d867eeda 50 $schema->storage->ensure_connected;
a964a928 51
d867eeda 52 if ($storage_idx == 0 &&
95787afe 53 $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::ASE::NoBindVars')) {
6d5679b2 54 # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
55 skip "Skipping entire test for $storage_type - no placeholder support", 1;
d867eeda 56 next;
57 }
5703eb14 58
d867eeda 59 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
6b1f5ef7 60
d867eeda 61 $schema->storage->_dbh->disconnect;
62 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
64f4e691 63
d867eeda 64 $schema->storage->dbh_do (sub {
65 my ($storage, $dbh) = @_;
66 eval { $dbh->do("DROP TABLE artist") };
67 $dbh->do(<<'SQL');
a964a928 68CREATE TABLE artist (
d867eeda 69 artistid INT IDENTITY PRIMARY KEY,
a964a928 70 name VARCHAR(100),
71 rank INT DEFAULT 13 NOT NULL,
d867eeda 72 charfield CHAR(10) NULL
a964a928 73)
26283ee3 74SQL
d867eeda 75 });
a964a928 76
d867eeda 77 my %seen_id;
a964a928 78
d867eeda 79# so we start unconnected
80 $schema->storage->disconnect;
fcc2ec11 81
26283ee3 82# test primary key handling
d867eeda 83 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
044f5b3e 84 like $new->artistid, qr/^\d+\z/, 'Auto-PK returned a number';
d867eeda 85 ok($new->artistid > 0, "Auto-PK worked");
fcc2ec11 86
d867eeda 87 $seen_id{$new->artistid}++;
fcc2ec11 88
d867eeda 89# check redispatch to storage-specific insert when auto-detected storage
95787afe 90 if ($storage_type eq 'DBI::Sybase::ASE') {
d867eeda 91 DBICTest::Schema->storage_type('::DBI');
92 $schema = get_schema();
93 }
94
95 $new = $schema->resultset('Artist')->create({ name => 'Artist 1' });
96 is ( $seen_id{$new->artistid}, undef, 'id for Artist 1 is unique' );
97 $seen_id{$new->artistid}++;
98
99# inserts happen in a txn, so we make sure it still works inside a txn too
100 $schema->txn_begin;
101
102 for (2..6) {
a964a928 103 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
104 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
105 $seen_id{$new->artistid}++;
d867eeda 106 }
a964a928 107
d867eeda 108 $schema->txn_commit;
a964a928 109
d867eeda 110# test simple count
111 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
112
113# test LIMIT support
114 my $it = $schema->resultset('Artist')->search({
115 artistid => { '>' => 0 }
116 }, {
a0348159 117 rows => 3,
a0348159 118 order_by => 'artistid',
d867eeda 119 });
af9e4a5e 120
d867eeda 121 is( $it->count, 3, "LIMIT count ok" );
e19677ad 122
d867eeda 123 is( $it->next->name, "foo", "iterator->next ok" );
124 $it->next;
125 is( $it->next->name, "Artist 2", "iterator->next ok" );
126 is( $it->next, undef, "next past end of resultset ok" );
127
128# now try with offset
129 $it = $schema->resultset('Artist')->search({}, {
130 rows => 3,
131 offset => 3,
132 order_by => 'artistid',
133 });
134
135 is( $it->count, 3, "LIMIT with offset count ok" );
136
137 is( $it->next->name, "Artist 3", "iterator->next ok" );
138 $it->next;
139 is( $it->next->name, "Artist 5", "iterator->next ok" );
140 is( $it->next, undef, "next past end of resultset ok" );
141
142# now try a grouped count
143 $schema->resultset('Artist')->create({ name => 'Artist 6' })
144 for (1..6);
145
146 $it = $schema->resultset('Artist')->search({}, {
147 group_by => 'name'
148 });
149
150 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
151
2baff5da 152# do an IDENTITY_INSERT
d867eeda 153 {
154 no warnings 'redefine';
155
156 my @debug_out;
157 local $schema->storage->{debug} = 1;
158 local $schema->storage->debugobj->{callback} = sub {
159 push @debug_out, $_[1];
160 };
161
162 my $txn_used = 0;
163 my $txn_commit = \&DBIx::Class::Storage::DBI::txn_commit;
164 local *DBIx::Class::Storage::DBI::txn_commit = sub {
165 $txn_used = 1;
166 goto &$txn_commit;
167 };
168
169 $schema->resultset('Artist')
170 ->create({ artistid => 999, name => 'mtfnpy' });
171
2baff5da 172 ok((grep /IDENTITY_INSERT/i, @debug_out), 'IDENTITY_INSERT used');
d867eeda 173
174 SKIP: {
175 skip 'not testing lack of txn on IDENTITY_INSERT with NoBindVars', 1
176 if $storage_type =~ /NoBindVars/i;
177
178 is $txn_used, 0, 'no txn on insert with IDENTITY_INSERT';
179 }
180 }
181
2baff5da 182# do an IDENTITY_UPDATE
183 {
184 my @debug_out;
185 local $schema->storage->{debug} = 1;
186 local $schema->storage->debugobj->{callback} = sub {
187 push @debug_out, $_[1];
188 };
189
190 lives_and {
191 $schema->resultset('Artist')
192 ->find(999)->update({ artistid => 555 });
193 ok((grep /IDENTITY_UPDATE/i, @debug_out));
194 } 'IDENTITY_UPDATE used';
195 $ping_count-- if $@;
196 }
d867eeda 197
198 my $bulk_rs = $schema->resultset('Artist')->search({
199 name => { -like => 'bulk artist %' }
200 });
201
2a6dda4b 202# test _insert_bulk using populate.
2baff5da 203 SKIP: {
2a6dda4b 204 skip '_insert_bulk not supported', 4
d390bd3c 205 unless $storage_type !~ /NoBindVars/i;
e06ad5d5 206
2baff5da 207 lives_ok {
208 $schema->resultset('Artist')->populate([
209 {
210 name => 'bulk artist 1',
211 charfield => 'foo',
212 },
213 {
214 name => 'bulk artist 2',
215 charfield => 'foo',
216 },
217 {
218 name => 'bulk artist 3',
219 charfield => 'foo',
220 },
221 ]);
2a6dda4b 222 } '_insert_bulk via populate';
2baff5da 223
2a6dda4b 224 is $bulk_rs->count, 3, 'correct number inserted via _insert_bulk';
2baff5da 225
226 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
2a6dda4b 227 'column set correctly via _insert_bulk');
2baff5da 228
229 my %bulk_ids;
230 @bulk_ids{map $_->artistid, $bulk_rs->all} = ();
231
232 is ((scalar keys %bulk_ids), 3,
2a6dda4b 233 'identities generated correctly in _insert_bulk');
2baff5da 234
235 $bulk_rs->delete;
236 }
e06ad5d5 237
2a6dda4b 238# make sure _insert_bulk works a second time on the same connection
0a9a9955 239 SKIP: {
2a6dda4b 240 skip '_insert_bulk not supported', 3
d390bd3c 241 unless $storage_type !~ /NoBindVars/i;
0a9a9955 242
243 lives_ok {
244 $schema->resultset('Artist')->populate([
245 {
246 name => 'bulk artist 1',
247 charfield => 'bar',
248 },
249 {
250 name => 'bulk artist 2',
251 charfield => 'bar',
252 },
253 {
254 name => 'bulk artist 3',
255 charfield => 'bar',
256 },
257 ]);
2a6dda4b 258 } '_insert_bulk via populate called a second time';
0a9a9955 259
260 is $bulk_rs->count, 3,
2a6dda4b 261 'correct number inserted via _insert_bulk';
0a9a9955 262
263 is ((grep $_->charfield eq 'bar', $bulk_rs->all), 3,
2a6dda4b 264 'column set correctly via _insert_bulk');
0a9a9955 265
266 $bulk_rs->delete;
267 }
268
2a6dda4b 269# test invalid _insert_bulk (missing required column)
0a9a9955 270#
0a9a9955 271 throws_ok {
272 $schema->resultset('Artist')->populate([
273 {
274 charfield => 'foo',
275 }
276 ]);
619f81c4 277 }
0a9a9955 278# The second pattern is the error from fallback to regular array insert on
279# incompatible charset.
280# The third is for ::NoBindVars with no syb_has_blk.
619f81c4 281 qr/
282 \Qno value or default\E
283 |
284 \Qdoes not allow null\E
285 |
286 \QUnable to invoke fast-path insert without storage placeholder support\E
287 /xi,
2a6dda4b 288 '_insert_bulk with missing required column throws error';
0a9a9955 289
2a6dda4b 290# now test _insert_bulk with IDENTITY_INSERT
2baff5da 291 SKIP: {
2a6dda4b 292 skip '_insert_bulk not supported', 3
d390bd3c 293 unless $storage_type !~ /NoBindVars/i;
e19677ad 294
2baff5da 295 lives_ok {
296 $schema->resultset('Artist')->populate([
297 {
298 artistid => 2001,
299 name => 'bulk artist 1',
300 charfield => 'foo',
301 },
302 {
303 artistid => 2002,
304 name => 'bulk artist 2',
305 charfield => 'foo',
306 },
307 {
308 artistid => 2003,
309 name => 'bulk artist 3',
310 charfield => 'foo',
311 },
312 ]);
2a6dda4b 313 } '_insert_bulk with IDENTITY_INSERT via populate';
2baff5da 314
315 is $bulk_rs->count, 3,
2a6dda4b 316 'correct number inserted via _insert_bulk with IDENTITY_INSERT';
2baff5da 317
318 is ((grep $_->charfield eq 'foo', $bulk_rs->all), 3,
2a6dda4b 319 'column set correctly via _insert_bulk with IDENTITY_INSERT');
2baff5da 320
321 $bulk_rs->delete;
322 }
d867eeda 323
324# test correlated subquery
325 my $subq = $schema->resultset('Artist')->search({ artistid => { '>' => 3 } })
326 ->get_column('artistid')
327 ->as_query;
328 my $subq_rs = $schema->resultset('Artist')->search({
329 artistid => { -in => $subq }
330 });
331 is $subq_rs->count, 11, 'correlated subquery';
332
333# mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
334 SKIP: {
587daa97 335 skip 'TEXT/IMAGE support does not work with FreeTDS', 22
aca3b4c3 336 if $schema->storage->_using_freetds;
d867eeda 337
338 my $dbh = $schema->storage->_dbh;
339 {
340 local $SIG{__WARN__} = sub {};
341 eval { $dbh->do('DROP TABLE bindtype_test') };
342
343 $dbh->do(qq[
8273e845 344 CREATE TABLE bindtype_test
d867eeda 345 (
f3a9ea3d 346 id INT IDENTITY PRIMARY KEY,
347 bytea IMAGE NULL,
348 blob IMAGE NULL,
349 clob TEXT NULL,
350 a_memo IMAGE NULL
d867eeda 351 )
352 ],{ RaiseError => 1, PrintError => 0 });
353 }
354
355 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
356 $binstr{'large'} = $binstr{'small'} x 1024;
357
358 my $maxloblen = length $binstr{'large'};
8273e845 359
aca3b4c3 360 if (not $schema->storage->_using_freetds) {
d867eeda 361 $dbh->{'LongReadLen'} = $maxloblen * 2;
362 } else {
363 $dbh->do("set textsize ".($maxloblen * 2));
364 }
365
366 my $rs = $schema->resultset('BindType');
367 my $last_id;
368
369 foreach my $type (qw(blob clob)) {
370 foreach my $size (qw(small large)) {
371 no warnings 'uninitialized';
372
0a9a9955 373 my $created;
374 lives_ok {
375 $created = $rs->create( { $type => $binstr{$size} } )
376 } "inserted $size $type without dying";
d867eeda 377
378 $last_id = $created->id if $created;
379
0a9a9955 380 lives_and {
381 ok($rs->find($last_id)->$type eq $binstr{$size})
382 } "verified inserted $size $type";
d867eeda 383 }
384 }
385
689819e1 386 $rs->delete;
387
0a9a9955 388 # blob insert with explicit PK
389 # also a good opportunity to test IDENTITY_INSERT
390 lives_ok {
391 $rs->create( { id => 1, blob => $binstr{large} } )
392 } 'inserted large blob without dying with manual PK';
d867eeda 393
0a9a9955 394 lives_and {
395 ok($rs->find(1)->blob eq $binstr{large})
396 } 'verified inserted large blob with manual PK';
d867eeda 397
398 # try a blob update
399 my $new_str = $binstr{large} . 'mtfnpy';
400
401 # check redispatch to storage-specific update when auto-detected storage
95787afe 402 if ($storage_type eq 'DBI::Sybase::ASE') {
d867eeda 403 DBICTest::Schema->storage_type('::DBI');
404 $schema = get_schema();
405 }
406
0a9a9955 407 lives_ok {
408 $rs->search({ id => 1 })->update({ blob => $new_str })
409 } 'updated blob successfully';
410
411 lives_and {
412 ok($rs->find(1)->blob eq $new_str)
413 } 'verified updated blob';
d867eeda 414
2baff5da 415 # try a blob update with IDENTITY_UPDATE
416 lives_and {
417 $new_str = $binstr{large} . 'hlagh';
418 $rs->find(1)->update({ id => 999, blob => $new_str });
419 ok($rs->find(999)->blob eq $new_str);
420 } 'verified updated blob with IDENTITY_UPDATE';
7ef97d26 421
d867eeda 422 ## try multi-row blob update
423 # first insert some blobs
d867eeda 424 $new_str = $binstr{large} . 'foo';
0a9a9955 425 lives_and {
426 $rs->delete;
427 $rs->create({ blob => $binstr{large} }) for (1..2);
428 $rs->update({ blob => $new_str });
429 is((grep $_->blob eq $new_str, $rs->all), 2);
430 } 'multi-row blob update';
431
432 $rs->delete;
433
2a6dda4b 434 # now try _insert_bulk with blobs and only blobs
0a9a9955 435 $new_str = $binstr{large} . 'bar';
436 lives_ok {
437 $rs->populate([
438 {
0a9a9955 439 blob => $binstr{large},
440 clob => $new_str,
441 },
442 {
0a9a9955 443 blob => $binstr{large},
444 clob => $new_str,
445 },
446 ]);
2a6dda4b 447 } '_insert_bulk with blobs does not die';
0a9a9955 448
449 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
2a6dda4b 450 'IMAGE column set correctly via _insert_bulk');
0a9a9955 451
452 is((grep $_->clob eq $new_str, $rs->all), 2,
2a6dda4b 453 'TEXT column set correctly via _insert_bulk');
2baff5da 454
2a6dda4b 455 # now try _insert_bulk with blobs and a non-blob which also happens to be an
587daa97 456 # identity column
457 SKIP: {
2a6dda4b 458 skip 'no _insert_bulk without placeholders', 4
587daa97 459 if $storage_type =~ /NoBindVars/i;
460
461 $rs->delete;
462 $new_str = $binstr{large} . 'bar';
463 lives_ok {
464 $rs->populate([
465 {
466 id => 1,
467 bytea => 1,
468 blob => $binstr{large},
469 clob => $new_str,
f3a9ea3d 470 a_memo => 2,
587daa97 471 },
472 {
473 id => 2,
474 bytea => 1,
475 blob => $binstr{large},
476 clob => $new_str,
f3a9ea3d 477 a_memo => 2,
587daa97 478 },
479 ]);
2a6dda4b 480 } '_insert_bulk with blobs and explicit identity does NOT die';
587daa97 481
482 is((grep $_->blob eq $binstr{large}, $rs->all), 2,
2a6dda4b 483 'IMAGE column set correctly via _insert_bulk with identity');
587daa97 484
485 is((grep $_->clob eq $new_str, $rs->all), 2,
2a6dda4b 486 'TEXT column set correctly via _insert_bulk with identity');
587daa97 487
488 is_deeply [ map $_->id, $rs->all ], [ 1,2 ],
2a6dda4b 489 'explicit identities set correctly via _insert_bulk with blobs';
587daa97 490 }
491
d390bd3c 492 lives_and {
493 $rs->delete;
494 $rs->create({ blob => $binstr{large} }) for (1..2);
495 $rs->update({ blob => undef });
496 is((grep !defined($_->blob), $rs->all), 2);
497 } 'blob update to NULL';
d867eeda 498 }
499
cd048330 500# test MONEY column support (and some other misc. stuff)
d867eeda 501 $schema->storage->dbh_do (sub {
502 my ($storage, $dbh) = @_;
503 eval { $dbh->do("DROP TABLE money_test") };
504 $dbh->do(<<'SQL');
505CREATE TABLE money_test (
506 id INT IDENTITY PRIMARY KEY,
cd048330 507 amount MONEY DEFAULT $999.99 NULL
d867eeda 508)
509SQL
510 });
511
cd048330 512 my $rs = $schema->resultset('Money');
513
514# test insert with defaults
515 lives_and {
516 $rs->create({});
517 is((grep $_->amount == 999.99, $rs->all), 1);
518 } 'insert with all defaults works';
519 $rs->delete;
520
d867eeda 521# test insert transaction when there's an active cursor
2baff5da 522 {
d867eeda 523 my $artist_rs = $schema->resultset('Artist');
524 $artist_rs->first;
525 lives_ok {
526 my $row = $schema->resultset('Money')->create({ amount => 100 });
527 $row->delete;
528 } 'inserted a row with an active cursor';
529 $ping_count-- if $@; # dbh_do calls ->connected
530 }
531
532# test insert in an outer transaction when there's an active cursor
4ca1fd6f 533 {
d867eeda 534 local $TODO = 'this should work once we have eager cursors';
535
536# clear state, or we get a deadlock on $row->delete
537# XXX figure out why this happens
538 $schema->storage->disconnect;
539
540 lives_ok {
541 $schema->txn_do(sub {
542 my $artist_rs = $schema->resultset('Artist');
543 $artist_rs->first;
544 my $row = $schema->resultset('Money')->create({ amount => 100 });
545 $row->delete;
546 });
547 } 'inserted a row with an active cursor in outer txn';
548 $ping_count-- if $@; # dbh_do calls ->connected
549 }
550
551# Now test money values.
d867eeda 552 my $row;
553 lives_ok {
554 $row = $rs->create({ amount => 100 });
555 } 'inserted a money value';
556
cdf7f026 557 cmp_ok eval { $rs->find($row->id)->amount }, '==', 100,
558 'money value round-trip';
d867eeda 559
560 lives_ok {
561 $row->update({ amount => 200 });
562 } 'updated a money value';
563
cdf7f026 564 cmp_ok eval { $rs->find($row->id)->amount }, '==', 200,
565 'updated money value round-trip';
d867eeda 566
567 lives_ok {
568 $row->update({ amount => undef });
569 } 'updated a money value to NULL';
570
46891041 571 lives_and {
ca507a2f 572 my $null_amount = $rs->find($row->id)->amount;
46891041 573 is $null_amount, undef;
574 } 'updated money value to NULL round-trip';
6469dabf 575
576# Test computed columns and timestamps
577 $schema->storage->dbh_do (sub {
578 my ($storage, $dbh) = @_;
579 eval { $dbh->do("DROP TABLE computed_column_test") };
580 $dbh->do(<<'SQL');
581CREATE TABLE computed_column_test (
582 id INT IDENTITY PRIMARY KEY,
583 a_computed_column AS getdate(),
584 a_timestamp timestamp,
8273e845 585 charfield VARCHAR(20) DEFAULT 'foo'
6469dabf 586)
587SQL
588 });
589
590 require DBICTest::Schema::ComputedColumn;
591 $schema->register_class(
592 ComputedColumn => 'DBICTest::Schema::ComputedColumn'
593 );
594
595 ok (($rs = $schema->resultset('ComputedColumn')),
596 'got rs for ComputedColumn');
597
598 lives_ok { $row = $rs->create({}) }
599 'empty insert for a table with computed columns survived';
600
601 lives_ok {
602 $row->update({ charfield => 'bar' })
603 } 'update of a table with computed columns survived';
d867eeda 604}
605
606is $ping_count, 0, 'no pings';
f1358489 607
72933b15 608# if tests passed and did so under a non-C lang - let's rerun the test
609if (Test::Builder->new->is_passing and $ENV{LANG} and $ENV{LANG} ne 'C') {
610 my $oldlang = $ENV{LANG};
611 local $ENV{LANG} = 'C';
612
613 pass ("Your lang is set to $oldlang - retesting with C");
614
f3ec358e 615 local $ENV{PATH};
616 my @cmd = map { $_ =~ /(.+)/ } ($^X, __FILE__);
72933b15 617
618 # this is cheating, and may even hang here and there (testing on windows passed fine)
619 # will be replaced with Test::SubExec::Noninteractive in due course
620 require IPC::Open2;
621 IPC::Open2::open2(my $out, undef, @cmd);
622 while (my $ln = <$out>) {
623 print " $ln";
624 }
625
626 wait;
627 ok (! $?, "Wstat $? from: @cmd");
628}
629
6d5679b2 630done_testing;
631
a964a928 632# clean up our mess
633END {
d867eeda 634 if (my $dbh = eval { $schema->storage->_dbh }) {
635 eval { $dbh->do("DROP TABLE $_") }
6469dabf 636 for qw/artist bindtype_test money_test computed_column_test/;
d867eeda 637 }
65d35121 638
639 undef $schema;
a964a928 640}