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
|
#!/usr/bin/perl -w
use strict;
use Test::More tests => 1 + (5 * 1);
BEGIN
{
require 't/test-lib.pl';
use_ok('Rose::DB::Object::Loader');
}
our %Have;
#
# Tests
#
#$Rose::DB::Object::Manager::Debug = 1;
foreach my $db_type (qw(sqlite))
{
SKIP:
{
skip("$db_type tests", 5) unless($Have{$db_type});
}
next unless($Have{$db_type});
Rose::DB::Object::Metadata->unregister_all_classes;
Rose::DB->default_type($db_type);
my $class_prefix = ucfirst($db_type);
my $loader =
Rose::DB::Object::Loader->new(
db => Rose::DB->new,
class_prefix => $class_prefix);
my @classes = $loader->make_classes(include_tables => '^(foos|bars)$');
#foreach my $class (@classes)
#{
# print $class->meta->perl_class_definition if($class->can('meta'));
#}
my $foo_class = $class_prefix . '::Foo';
my $bar_class = $class_prefix . '::Bar';
is($foo_class->meta->relationship('bar')->type, 'one to one', "check rel type - $db_type");
my $bar = $bar_class->new;
my $foo = $foo_class->new(foo => 'xyz');
#$Rose::DB::Object::Debug = 1;
$foo->bar($bar);
$foo->bar->bar('some text');
$foo->save;
my $check_foo = $foo_class->new(id => $foo->id)->load;
my $check_bar = $bar_class->new(foo_id => $bar->foo_id)->load;
is($check_foo->foo, 'xyz', "check foo - $db_type");
is($check_bar->bar, 'some text', "check bar - $db_type");
is($bar_class->meta->relationship('foo')->type, 'one to one', "check foo one to one - $db_type");
is($bar_class->meta->relationship('foo')->foreign_key,
$bar_class->meta->foreign_key('foo'), "check foo fk rel - $db_type");
#foreach my $rel ($bar_class->meta->relationships)
#{
# print $rel->name, ' ', $rel->type, "\n";
#}
}
BEGIN
{
our %Have;
my $dbh;
#
# SQLite
#
eval
{
$dbh = Rose::DB->new('sqlite_admin')->retain_dbh()
or die Rose::DB->error;
};
if(!$@ && $dbh)
{
$Have{'sqlite'} = 1;
# Drop existing tables and create schema, ignoring errors
{
local $dbh->{'RaiseError'} = 0;
local $dbh->{'PrintError'} = 0;
$dbh->do('DROP TABLE bars');
$dbh->do('DROP TABLE foos');
}
$dbh->do(<<"EOF");
CREATE TABLE foos
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
foo VARCHAR(255)
)
EOF
$dbh->do(<<"EOF");
CREATE TABLE "bars"
(
"foo_id" INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES foos (id),
bar VARCHAR(255)
)
EOF
$dbh->disconnect;
}
}
END
{
# Delete test tables
if($Have{'sqlite'})
{
my $dbh = Rose::DB->new('sqlite_admin')->retain_dbh()
or die Rose::DB->error;
$dbh->do('DROP TABLE bars');
$dbh->do('DROP TABLE foos');
$dbh->disconnect;
}
}
|