1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
|
#!/usr/bin/perl -w
use strict;
#use Test::LongString;
use Test::More tests => (91 * 4) + 3;
BEGIN
{
require 't/test-lib.pl';
use_ok('Rose::DB::Object');
use_ok('Rose::DB::Object::Manager');
use_ok('Rose::DB::Object::Helpers');
}
our(%Have, $Have_YAML, $Have_JSON);
#
# Tests
#
use Rose::DB::Object::Util qw(:state get_column_value_modified);
my $i = 0;
foreach my $db_type (qw(mysql pg informix sqlite))
{
SKIP:
{
skip("$db_type tests", 91) unless($Have{$db_type});
}
next unless($Have{$db_type});
MyMixIn->clear_export_tags;
MyMixIn->export_tag('all' => [ 'a', 'b' ]);
my $class = 'My' . ucfirst($db_type) . 'Object';
my $other_class = 'My' . ucfirst($db_type) . 'OtherObject';
my $o = $class->new(id => 1, name => 'John', age => 30);
my @tags = MyMixIn->export_tags;
is_deeply(\@tags, [ 'all' ], "export_tags() 1 - $db_type");
my $tags = MyMixIn->export_tags;
is_deeply($tags, [ 'all' ], "export_tags() 1 - $db_type");
eval { MyMixIn->export_tag('foo') };
ok($@, "export_tag() 1 - $db_type");
MyMixIn->export_tag('foo' => [ 'bar', 'baz' ]);
my @methods = sort(MyMixIn->export_tag('foo'));
is_deeply(\@methods, [ 'bar', 'baz' ], "export_tag() 1 - $db_type");
my $methods = MyMixIn->export_tag('foo');
$methods = [ sort @$methods ];
is_deeply($methods, [ 'bar', 'baz' ], "export_tag() 2 - $db_type");
eval { MyMixIn->export_tag('foo', 'bar') };
ok($@, "export_tag() 3 - $db_type");
eval { MyMixIn->export_tag('foo', [ 'bar' ], 'baz') };
ok($@, "export_tag() 4 - $db_type");
MyMixIn->clear_export_tags;
@tags = MyMixIn->export_tags;
is_deeply(\@tags, [ ], "clear_export_tags() 1 - $db_type");
MyMixIn->add_export_tags('foo' => [], 'all' => []);
MyMixIn->delete_export_tags('foo', 'all');
@tags = MyMixIn->export_tags;
is_deeply(\@tags, [ ], "delete_export_tags() 1 - $db_type");
MyMixIn->export_tag('xx', [ 'a' ]);
@tags = MyMixIn->export_tags;
is_deeply(\@tags, [ 'xx' ], "export_tag() 5 - $db_type");
MyMixIn->delete_export_tag('xx');
@tags = MyMixIn->export_tags;
is_deeply(\@tags, [ ], "delete_export_tag() 1 - $db_type");
ok(!$o->load_speculative, "load_speculative() 1 - $db_type");
ok($o->load_or_insert(), "load_or_insert() 1 - $db_type");
$o = $class->new(id => 1);
ok($o->load_speculative, "load_speculative() 2 - $db_type");
$o = $class->new(id => 2, name => 'Alex');
ok($o->find_or_create(), "find_or_create() 1 - $db_type");
$o = $class->new(id => 2);
ok($o->find_or_create(), "find_or_create() 2 - $db_type");
$o = $class->new(id => 2);
ok($o->load_speculative, "load_speculative() 3 - $db_type");
my $other = $other_class->new(name => 'foo');
my $o3 =
$class->new(id => 5,
name => 'load_or_save',
rose_db_object_test_other => $other)->load_or_save;
$o3 = $class->new(id => $o3->id)->load;
is($o3->name, 'load_or_save', "load_or_save() 1 - $db_type");
$other = $other_class->new(id => $other->id)->load;
is($other->name, 'foo', "load_or_save() 2 - $db_type");
my $o2 = $o->clone;
is($o2->id, $o->id, "clone() 1 - $db_type");
is($o2->name, $o->name, "clone() 2 - $db_type");
is($o2->age, $o->age, "clone() 3 - $db_type");
ok(!defined $o2->{'db'}, "clone() 4 - $db_type");
$o2 = $o->clone_and_reset;
ok(!defined $o2->id, "clone_and_reset() 1 - $db_type");
# Crazy MySQL prvides an empty string as a default value
# (or no default for 5.0.51, apparently)
if($db_type eq 'mysql')
{
no warnings 'uninitialized';
ok(!length $o2->name, "clone_and_reset() 2 - $db_type");
}
else
{
ok(!defined $o2->name, "clone_and_reset() 2 - $db_type");
}
is($o2->age, $o->age, "clone_and_reset() 3 - $db_type");
is($o2->db, $o->db, "clone_and_reset() 4 - $db_type");
my $clone = $class->new(id => 2)->load->clone;
$clone->laz('Z0');
foreach my $i (1, 2)
{
$clone->update; # reset to initial state
$o->meta->allow_inline_column_values($i == 2);
#local $Rose::DB::Object::Debug = 1;
# Insert or update
$o = $class->new(id => 2, name => 'Alex', age => 2);
$o->insert_or_update;
$o2 = $class->new(id => 2)->load;
is($o2->name, 'Alex', "insert_or_update() 1.$i - $db_type");
is($o2->laz, 'Z0', "insert_or_update() 2.$i - $db_type");
# Insert or update - update regular and lazy columns
$o->name('Alex2');
$o->laz('Z1');
$o->insert_or_update;
$o2 = $class->new(id => 2)->load;
is($o2->name, 'Alex2', "insert_or_update() 3.$i - $db_type");
is($o2->laz, 'Z1', "insert_or_update() 4.$i - $db_type");
# Insert or update on duplicate key
$o = $class->new(id => 2, name => 'Alex3', age => 3);
$o->insert_or_update_on_duplicate_key;
$o2 = $class->new(id => 2)->load;
is($o2->name, 'Alex3', "insert_or_update_on_duplicate_key() 1.$i - $db_type");
is($o2->age, 3, "insert_or_update_on_duplicate_key() 2.$i - $db_type");
is($o2->laz, 'Z1', "insert_or_update_on_duplicate_key() 3.$i - $db_type");
is($o2->id, 2, "insert_or_update_on_duplicate_key() 4.$i - $db_type");
# Insert or update on duplicate key - with unique key only
$o = $class->new(name => 'Alex3', age => 5);
$o->insert_or_update_on_duplicate_key;
$o = $class->new(name => 'Alex3')->load;
is($o->name, 'Alex3', "insert_or_update_on_duplicate_key() 5.$i - $db_type");
is($o->age, 5, "insert_or_update_on_duplicate_key() 6.$i - $db_type");
is($o->laz, 'Z1', "insert_or_update_on_duplicate_key() 7.$i - $db_type");
is($o->id, 2, "insert_or_update_on_duplicate_key() 8.$i - $db_type");
$o = $class->new(name => 'Alex3', laz => 'Z2', age => 5);
$o->insert_or_update_on_duplicate_key;
$o = $class->new(name => 'Alex3')->load;
is($o->name, 'Alex3', "insert_or_update_on_duplicate_key() 9.$i - $db_type");
is($o->age, 5, "insert_or_update_on_duplicate_key() 10.$i - $db_type");
is($o->laz, 'Z2', "insert_or_update_on_duplicate_key() 11.$i - $db_type");
is($o->id, 2, "insert_or_update_on_duplicate_key() 12.$i - $db_type");
$o = $class->new(name => 'Alex3')->load;
$o->age(6);
#local $Rose::DB::Object::Debug = 1;
$o->insert_or_update_on_duplicate_key(changes_only => 1);
$o = $class->new(name => 'Alex3')->load;
is($o->name, 'Alex3', "insert_or_update_on_duplicate_key() 13.$i - $db_type");
is($o->age, 6, "insert_or_update_on_duplicate_key() 14.$i - $db_type");
is($o->laz, 'Z2', "insert_or_update_on_duplicate_key() 15.$i - $db_type");
is($o->id, 2, "insert_or_update_on_duplicate_key() 16.$i - $db_type");
}
$o->meta->allow_inline_column_values(0);
is_deeply(scalar $o->column_value_pairs(),
{ age => 6, id => 2, laz => 'Z2', name => 'Alex3' },
"column_value_pairs() - $db_type");
is_deeply(scalar $o->column_accessor_value_pairs(),
{ age => 6, id => 2, get_laz => 'Z2', name => 'Alex3' },
"column_accessor_value_pairs() - $db_type");
is_deeply(scalar $o->column_mutator_value_pairs(),
{ age => 6, id => 2, set_laz => 'Z2', name => 'Alex3' },
"column_mutator_value_pairs() - $db_type");
my $c = $class->new(age => 456);
$c->init_with_column_value_pairs({ age => 6, laz => 'Z3', name => 'Alex4' });
is($c->get_laz, 'Z3', "init_with_column_value_pairs() 1 - $db_type");
is($c->name, 'Alex4', "init_with_column_value_pairs() 2 - $db_type");
$c = $class->new(age => 456);
$c->init_with_column_value_pairs(age => 6, laz => 'Z4', name => 'Alex5');
is($c->get_laz, 'Z4', "init_with_column_value_pairs() 3 - $db_type");
is($c->name, 'Alex5', "init_with_column_value_pairs() 4 - $db_type");
if($Have_YAML)
{
local $YAML::Syck::SortKeys = 1;
$YAML::Syck::SortKeys = 1; # quiet stupid perl 5.6.x warning
my $yaml = $o->column_values_as_yaml;
$yaml =~ s/'//g; # number/string issues are annoying...
is($yaml, "--- \nage: 6\nid: 2\nlaz: Z2\nname: Alex3\n",
"column_values_as_yaml() - $db_type");
my $c = $class->new(age => 456);
$c->init_with_yaml($yaml);
is($c->column_values_as_yaml, "--- \nage: 6\nid: 2\nlaz: Z2\nname: Alex3\n",
"init_with_yaml() - $db_type")
}
else
{
ok(1, "skip column_values_as_yaml() - $db_type");
ok(1, "skip init_with_yaml() - $db_type");
}
if($Have_JSON)
{
# I don't know if I can rely on this key order...
# {"laz":"Z2","name":"Alex3","id":2,"age":6}
my $json = $o->column_values_as_json;
ok($json =~ /^\{/ && $json =~ /"laz": "Z2"/ &&
$json =~ /"name": "Alex3"/ && $json =~ /"id": (?:2|"2")/ &&
$json =~ /"age": (?:6|"6")/ && $json =~ /\}\z/, "column_values_as_json() - $db_type");
my $c = $class->new(age => 456);
$c->init_with_json($json);
ok($json =~ /^\{/ && $json =~ /"laz": "Z2"/ &&
$json =~ /"name": "Alex3"/ && $json =~ /"id": (?:2|"2")/ &&
$json =~ /"age": (?:6|"6")/ && $json =~ /\}\z/, "init_with_json() - $db_type");
}
else
{
ok(1, "skip column_values_as_json() - $db_type");
ok(1, "skip init_with_json() - $db_type");
}
$c = $class->new(age => 456);
set_state_loading($c);
ok(is_loading($c), "state utils - $db_type");
unset_state_loading($c);
# has_loaded_related() tested in db-object-relationship.t
# forget_related() tested in db-object-relationship.t
eval { require Storable };
unless($@)
{
$o = $class->new(id => 1)->load_or_save;
# Confirm stripping of "on-save" code references
$o->rose_db_object_test_other({ name => 'test' });
eval { $o->strip };
like($@, qr/Refusing to strip "on-save" actions from \w+ object without strip_on_save_ok parameter/, "strip 1 - $db_type");
my $frozen = Storable::freeze($o->strip(strip_on_save_ok => 1));
my $thawed = Storable::thaw($frozen);
is_deeply($thawed, $o, "strip 2 - $db_type");
}
else { SKIP: { skip("tests that require Storable - $db_type", 2) } }
$o = $class->new(id => 1, name => 'John', age => 30)->load_or_save;
is(scalar $o->dirty_columns, 0, "dirty_columns 1 - $db_type");
$o->dirty_columns(qw(name age));
is(scalar $o->dirty_columns, 2, "dirty_columns 2 - $db_type");
is(join(',', sort $o->dirty_columns), 'age,name', "dirty_columns 3 - $db_type");
ok(get_column_value_modified($o, 'age'), "dirty_columns 4 - $db_type");
ok(get_column_value_modified($o, 'name'), "dirty_columns 5 - $db_type");
# insert_or_update with no key
Rose::DB::Object::Manager->delete_objects(all => 1, object_class => $other_class);
Rose::DB::Object::Manager->delete_objects(all => 1, object_class => $class);
my $unknown = $class->new(age => 99);
eval { $unknown->insert_or_update };
ok(!$@ && $unknown->id =~ /^\d+$/, "insert_or_update() with no key - $db_type");
# Must remove all data to prevent a UK conflict where name is NULL
Rose::DB::Object::Manager->delete_objects(all => 1, object_class => $other_class);
Rose::DB::Object::Manager->delete_objects(all => 1, object_class => $class);
# load_or_insert with no key
$unknown = $class->new(age => 100);
eval { $unknown->load_or_insert };
ok(!$@ && $unknown->id =~ /^\d+$/, "load_or_insert() with no key - $db_type");
# Must remove all data to prevent a UK conflict where name is NULL
Rose::DB::Object::Manager->delete_objects(all => 1, object_class => $other_class);
Rose::DB::Object::Manager->delete_objects(all => 1, object_class => $class);
# load_or_save with no key
$unknown = $class->new(age => 101);
eval { $unknown->load_or_save };
ok(!$@ && $unknown->id =~ /^\d+$/, "load_or_save() with no key - $db_type");
}
BEGIN
{
our(%Have, $Have_YAML, $Have_JSON, $All);
# All or nothing
eval { require YAML::Syck; require JSON; };
$All = $@ ? ':all_noprereq' : ':all';
unless($@)
{
$Have_YAML = $Have_JSON = 1;
}
if($Have_JSON)
{
no warnings 'uninitialized';
unless($JSON::VERSION >= 2.12)
{
$Have_YAML = $Have_JSON = 0;
}
eval { require JSON::XS };
if(defined $JSON::XS::VERSION && $JSON::XS::VERSION < 2.2222)
{
$Have_YAML = $Have_JSON = 0;
}
}
$All = ':all_noprereq' unless($Have_YAML && $Have_YAML);
}
BEGIN
{
our(%Have, $Have_YAML, $Have_JSON, $All);
#
# PostgreSQL
#
my $dbh;
eval
{
$dbh = Rose::DB->new('pg_admin')->retain_dbh()
or die Rose::DB->error;
};
if(!$@ && $dbh)
{
$Have{'pg'} = 1;
$Have{'pg_with_schema'} = 1;
# Drop existing tables and create schema, ignoring errors
{
local $dbh->{'RaiseError'} = 0;
local $dbh->{'PrintError'} = 0;
$dbh->do('DROP TABLE rose_db_object_test_other');
$dbh->do('DROP TABLE rose_db_object_test');
}
$dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test
(
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255),
age INT,
laz VARCHAR(255),
UNIQUE(name)
)
EOF
$dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test_other
(
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255),
rose_db_object_test_id INT REFERENCES rose_db_object_test (id)
)
EOF
$dbh->disconnect;
package MyPgObject;
our @ISA = qw(Rose::DB::Object);
use Rose::DB::Object::Helpers ($All);
eval { Rose::DB::Object::Helpers->import($All) };
Test::More::ok($@, 'import conflict - pg');
eval { Rose::DB::Object::Helpers->import('-force', $All) };
Test::More::ok(!$@, 'import override - pg');
Rose::DB::Object::Helpers->import({ load_or_insert => 'find_or_create' });
sub init_db { Rose::DB->new('pg') }
__PACKAGE__->meta->table('rose_db_object_test');
__PACKAGE__->meta->auto_initialize;
__PACKAGE__->meta->column('laz')->lazy(1);
__PACKAGE__->meta->column('laz')->add_auto_method_types(qw(get set));
__PACKAGE__->meta->initialize(replace_existing => 1);
package MyPgOtherObject;
our @ISA = qw(Rose::DB::Object);
sub init_db { Rose::DB->new('pg') }
__PACKAGE__->meta->table('rose_db_object_test_other');
__PACKAGE__->meta->auto_initialize;
}
#
# MySQL
#
eval
{
my $db = Rose::DB->new('mysql_admin');
$dbh = $db->retain_dbh or die Rose::DB->error;
die "MySQL version too old" unless($db->database_version >= 4_000_000);
# Drop existing tables, ignoring errors
{
local $dbh->{'RaiseError'} = 0;
local $dbh->{'PrintError'} = 0;
$dbh->do('DROP TABLE rose_db_object_test_other');
$dbh->do('DROP TABLE rose_db_object_test');
}
};
if(!$@ && $dbh)
{
$Have{'mysql'} = 1;
$dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test
(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
age INT,
laz VARCHAR(255),
UNIQUE(name)
)
EOF
$dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test_other
(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
rose_db_object_test_id INT REFERENCES rose_db_object_test (id)
)
EOF
$dbh->disconnect;
package MyMysqlObject;
our @ISA = qw(Rose::DB::Object);
use Rose::DB::Object::Helpers ($All);
eval { Rose::DB::Object::Helpers->import(qw(load_or_insert load_speculative insert_or_update)) };
Test::More::ok($@, 'import conflict - mysql');
eval { Rose::DB::Object::Helpers->import(qw(--force load_or_insert load_speculative)) };
Test::More::ok(!$@, 'import override - mysql');
Rose::DB::Object::Helpers->import({ load_or_insert => 'find_or_create' });
sub init_db { Rose::DB->new('mysql') }
__PACKAGE__->meta->table('rose_db_object_test');
__PACKAGE__->meta->auto_initialize;
__PACKAGE__->meta->column('laz')->lazy(1);
__PACKAGE__->meta->column('laz')->add_auto_method_types(qw(get set));
__PACKAGE__->meta->add_relationship
(
rose_db_object_test_other =>
{
class => 'MyMysqlOtherObject',
column_map => { id => 'rose_db_object_test_id' },
type => 'one to many',
}
);
__PACKAGE__->meta->initialize(replace_existing => 1);
package MyMysqlOtherObject;
our @ISA = qw(Rose::DB::Object);
sub init_db { Rose::DB->new('mysql') }
__PACKAGE__->meta->table('rose_db_object_test_other');
__PACKAGE__->meta->auto_initialize;
}
#
# Informix
#
eval
{
$dbh = Rose::DB->new('informix_admin')->retain_dbh()
or die Rose::DB->error;
};
if(!$@ && $dbh)
{
$Have{'informix'} = 1;
# Drop existing tables, ignoring errors
{
local $dbh->{'RaiseError'} = 0;
local $dbh->{'PrintError'} = 0;
$dbh->do('DROP TABLE rose_db_object_test_other');
$dbh->do('DROP TABLE rose_db_object_test');
}
$dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test
(
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255),
age INT,
laz VARCHAR(255),
UNIQUE(name)
)
EOF
$dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test_other
(
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255),
rose_db_object_test_id INT REFERENCES rose_db_object_test (id)
)
EOF
$dbh->disconnect;
package MyInformixObject;
our @ISA = qw(Rose::DB::Object);
use Rose::DB::Object::Helpers ($All);
eval { Rose::DB::Object::Helpers->import($All) };
Test::More::ok($@, 'import conflict - informix');
eval { Rose::DB::Object::Helpers->import('-force', $All) };
Test::More::ok(!$@, 'import override - informix');
Rose::DB::Object::Helpers->import({ load_or_insert => 'find_or_create' });
sub init_db { Rose::DB->new('informix') }
__PACKAGE__->meta->table('rose_db_object_test');
__PACKAGE__->meta->auto_initialize;
__PACKAGE__->meta->column('laz')->lazy(1);
__PACKAGE__->meta->column('laz')->add_auto_method_types(qw(get set));
__PACKAGE__->meta->initialize(replace_existing => 1);
package MyInformixOtherObject;
our @ISA = qw(Rose::DB::Object);
sub init_db { Rose::DB->new('informix') }
__PACKAGE__->meta->table('rose_db_object_test_other');
__PACKAGE__->meta->auto_initialize;
}
#
# SQLite
#
eval
{
$dbh = Rose::DB->new('sqlite_admin')->retain_dbh()
or die Rose::DB->error;
};
if(!$@ && $dbh)
{
$Have{'sqlite'} = 1;
# Drop existing tables, ignoring errors
{
local $dbh->{'RaiseError'} = 0;
local $dbh->{'PrintError'} = 0;
$dbh->do('DROP TABLE rose_db_object_test_other');
$dbh->do('DROP TABLE rose_db_object_test');
}
$dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(255),
age INT,
laz VARCHAR(255),
UNIQUE(name)
)
EOF
$dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test_other
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(255),
rose_db_object_test_id INT REFERENCES rose_db_object_test (id)
)
EOF
$dbh->disconnect;
package MySqliteObject;
our @ISA = qw(Rose::DB::Object);
use Rose::DB::Object::Helpers ($All);
eval { Rose::DB::Object::Helpers->import($All) };
Test::More::ok($@, 'import conflict - sqlite');
eval { Rose::DB::Object::Helpers->import('--force', $All) };
Test::More::ok(!$@, 'import override - sqlite');
Rose::DB::Object::Helpers->import({ load_or_insert => 'find_or_create' });
sub init_db { Rose::DB->new('sqlite') }
__PACKAGE__->meta->table('rose_db_object_test');
__PACKAGE__->meta->auto_initialize;
__PACKAGE__->meta->column('laz')->lazy(1);
__PACKAGE__->meta->column('laz')->add_auto_method_types(qw(get set));
__PACKAGE__->meta->initialize(replace_existing => 1);
package MySqliteOtherObject;
our @ISA = qw(Rose::DB::Object);
sub init_db { Rose::DB->new('sqlite') }
__PACKAGE__->meta->table('rose_db_object_test_other');
__PACKAGE__->meta->auto_initialize;
}
package MyMixIn;
use Rose::DB::Object::MixIn;
our @ISA = qw(Rose::DB::Object::MixIn);
}
END
{
# Delete test tables
if($Have{'pg'})
{
# PostgreSQL
my $dbh = Rose::DB->new('pg_admin')->retain_dbh()
or die Rose::DB->error;
$dbh->do('DROP TABLE rose_db_object_test_other');
$dbh->do('DROP TABLE rose_db_object_test');
$dbh->disconnect;
}
if($Have{'mysql'})
{
# MySQL
my $dbh = Rose::DB->new('mysql_admin')->retain_dbh()
or die Rose::DB->error;
$dbh->do('DROP TABLE rose_db_object_test_other');
$dbh->do('DROP TABLE rose_db_object_test');
$dbh->disconnect;
}
if($Have{'informix'})
{
# Informix
my $dbh = Rose::DB->new('informix_admin')->retain_dbh()
or die Rose::DB->error;
$dbh->do('DROP TABLE rose_db_object_test_other');
$dbh->do('DROP TABLE rose_db_object_test');
$dbh->disconnect;
}
if($Have{'sqlite'})
{
# Informix
my $dbh = Rose::DB->new('sqlite_admin')->retain_dbh()
or die Rose::DB->error;
$dbh->do('DROP TABLE rose_db_object_test_other');
$dbh->do('DROP TABLE rose_db_object_test');
$dbh->disconnect;
}
}
|