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
|
#!/usr/bin/perl -w
use strict;
use Test::More tests => 55;
use Scalar::Util qw(isweak refaddr);
BEGIN
{
require 't/test-lib.pl';
use_ok('Rose::DB::Object');
}
eval { require Test::Memory::Cycle };
our $HAVE_TMC = $@ ? 0 : 1;
our %HAVE;
my $db_type = $HAVE{'sqlite'} ? 'sqlite' : (sort keys %HAVE)[0];
SKIP:
{
skip("No db available", 54) unless($db_type);
package MyObject;
use base 'Rose::DB::Object';
__PACKAGE__->meta->table('objects');
__PACKAGE__->meta->columns
(
id => { type => 'int', primary_key => 1 },
start => { type => 'scalar' },
);
__PACKAGE__->meta->initialize;
sub init_db { Rose::DB->new($db_type) }
package MySubObject;
use base 'MyObject';
__PACKAGE__->meta->column('id')->default(123);
__PACKAGE__->meta->delete_column('start');
__PACKAGE__->meta->add_column(start => { type => 'datetime' });
__PACKAGE__->meta->initialize(replace_existing => 1);
package MySubObject2;
use base 'MyObject';
__PACKAGE__->meta->table('s2objs');
__PACKAGE__->meta->initialize(preserve_existing => 1);
sub id
{
my($self) = shift;
return $self->{'id'} = shift if(@_);
return defined $self->{'id'} ? $self->{'id'} : 456;
}
package MySubObject3;
use base 'MySubObject';
__PACKAGE__->meta->initialize(preserve_existing => 1);
package main;
if($HAVE_TMC)
{
Test::Memory::Cycle::memory_cycle_ok(MyObject->meta, "meta memory cycle ok MyObject - $db_type");
Test::Memory::Cycle::memory_cycle_ok(MySubObject->meta, "meta memory cycle ok MySubObject - $db_type");
Test::Memory::Cycle::memory_cycle_ok(MySubObject2->meta, "meta memory cycle ok MySubObject2 - $db_type");
}
else
{
ok(1, 'Test::Memory::Cycle not installed');
ok(1, 'Test::Memory::Cycle not installed');
ok(1, 'Test::Memory::Cycle not installed');
}
ok(MyObject->meta ne MySubObject->meta, "meta 1 - $db_type");
ok(MyObject->meta ne MySubObject2->meta, "meta 2 - $db_type");
ok(MySubObject->meta ne MySubObject2->meta, "meta 3 - $db_type");
ok(refaddr(MyObject->meta->column('id')) ne refaddr(MySubObject->meta->column('id')), "meta column 1 - $db_type");
ok(refaddr(MyObject->meta->column('id')) ne refaddr(MySubObject2->meta->column('id')), "meta column 2 - $db_type");
ok(refaddr(MySubObject->meta->column('id')) ne refaddr(MySubObject2->meta->column('id')), "meta column 3 - $db_type");
ok(isweak(MyObject->meta->column('id')->{'parent'}), "meta weakened 1 - $db_type");
ok(isweak(MySubObject->meta->column('id')->{'parent'}), "meta weakened 2 - $db_type");
ok(isweak(MySubObject2->meta->column('id')->{'parent'}), "meta weakened 3 - $db_type");
is(refaddr(MyObject->meta->column('id')->parent), refaddr(MyObject->meta), "meta parent 1 - $db_type");
is(refaddr(MySubObject->meta->column('id')->parent), refaddr(MySubObject->meta), "meta parent 2 - $db_type");
is(refaddr(MySubObject2->meta->column('id')->parent), refaddr(MySubObject2->meta), "meta parent 3 - $db_type");
my $o = MyObject->new;
is(MyObject->meta->table, 'objects', "base class 1 - $db_type");
ok(!defined $o->id, "base class 2 - $db_type");
$o->start('1/2/2003');
is($o->start, '1/2/2003', "base class 3 - $db_type");
my $s = MySubObject->new;
is(MyObject->meta->table, 'objects', "subclass 1.1 - $db_type");
is($s->id, 123, "subclass 1.2 - $db_type");
$s->start('1/2/2003');
is($s->start->strftime('%B'), 'January', "subclass 1.3 - $db_type");
my $t = MySubObject2->new;
is(MySubObject2->meta->table, 's2objs', "subclass 2.1 - $db_type");
is($t->id, 456, "subclass 2.2 - $db_type");
$t->start('1/2/2003');
is($t->start, '1/2/2003', "subclass 2.3 - $db_type");
my $f = MySubObject3->new;
is(MySubObject3->meta->table, 'objects', "subclass 3.1 - $db_type");
is($f->id, 123, "subclass 3.2 - $db_type");
$f->start('1/2/2003');
is($f->start->strftime('%B'), 'January', "subclass 3.3 - $db_type");
# Test again, but without this module
$Scalar::Util::Clone::VERSION = undef;
package My2Object;
use base 'Rose::DB::Object';
__PACKAGE__->meta->table('objects');
__PACKAGE__->meta->columns
(
id => { type => 'int', primary_key => 1 },
start => { type => 'scalar' },
);
__PACKAGE__->meta->initialize;
sub init_db { Rose::DB->new($db_type) }
package My2SubObject;
use base 'My2Object';
__PACKAGE__->meta->column('id')->default(123);
__PACKAGE__->meta->delete_column('start');
__PACKAGE__->meta->add_column(start => { type => 'datetime' });
__PACKAGE__->meta->initialize(replace_existing => 1);
package My2SubObject2;
use base 'My2Object';
__PACKAGE__->meta->table('s2objs');
__PACKAGE__->meta->initialize(preserve_existing => 1);
sub id
{
my($self) = shift;
return $self->{'id'} = shift if(@_);
return defined $self->{'id'} ? $self->{'id'} : 456;
}
package My2SubObject3;
use base 'My2SubObject';
__PACKAGE__->meta->initialize(preserve_existing => 1);
package main;
if($HAVE_TMC)
{
Test::Memory::Cycle::memory_cycle_ok(My2Object->meta, "meta memory cycle ok My2Object - $db_type");
Test::Memory::Cycle::memory_cycle_ok(My2SubObject->meta, "meta memory cycle ok My2SubObject - $db_type");
Test::Memory::Cycle::memory_cycle_ok(My2SubObject2->meta, "meta memory cycle ok My2SubObject2 - $db_type");
}
else
{
ok(1, 'Test::Memory::Cycle not installed');
ok(1, 'Test::Memory::Cycle not installed');
ok(1, 'Test::Memory::Cycle not installed');
}
ok(My2Object->meta ne My2SubObject->meta, "meta 1 - $db_type");
ok(My2Object->meta ne My2SubObject2->meta, "meta 2 - $db_type");
ok(My2SubObject->meta ne My2SubObject2->meta, "meta 3 - $db_type");
ok(refaddr(My2Object->meta->column('id')) ne refaddr(My2SubObject->meta->column('id')), "meta column 1 - $db_type");
ok(refaddr(My2Object->meta->column('id')) ne refaddr(My2SubObject2->meta->column('id')), "meta column 2 - $db_type");
ok(refaddr(My2SubObject->meta->column('id')) ne refaddr(My2SubObject2->meta->column('id')), "meta column 3 - $db_type");
ok(isweak(My2Object->meta->column('id')->{'parent'}), "meta weakened 1 - $db_type");
ok(isweak(My2SubObject->meta->column('id')->{'parent'}), "meta weakened 2 - $db_type");
ok(isweak(My2SubObject2->meta->column('id')->{'parent'}), "meta weakened 3 - $db_type");
is(refaddr(My2Object->meta->column('id')->parent), refaddr(My2Object->meta), "meta parent 1 - $db_type");
is(refaddr(My2SubObject->meta->column('id')->parent), refaddr(My2SubObject->meta), "meta parent 2 - $db_type");
is(refaddr(My2SubObject2->meta->column('id')->parent), refaddr(My2SubObject2->meta), "meta parent 3 - $db_type");
$o = My2Object->new;
is(My2Object->meta->table, 'objects', "base class 1 - $db_type");
ok(!defined $o->id, "base class 2 - $db_type");
$o->start('1/2/2003');
is($o->start, '1/2/2003', "base class 3 - $db_type");
$s = My2SubObject->new;
is(My2Object->meta->table, 'objects', "subclass 1.1 - $db_type");
is($s->id, 123, "subclass 1.2 - $db_type");
$s->start('1/2/2003');
is($s->start->strftime('%B'), 'January', "subclass 1.3 - $db_type");
$t = My2SubObject2->new;
is(My2SubObject2->meta->table, 's2objs', "subclass 2.1 - $db_type");
is($t->id, 456, "subclass 2.2 - $db_type");
$t->start('1/2/2003');
is($t->start, '1/2/2003', "subclass 2.3 - $db_type");
$f = My2SubObject3->new;
is(My2SubObject3->meta->table, 'objects', "subclass 3.1 - $db_type");
is($f->id, 123, "subclass 3.2 - $db_type");
$f->start('1/2/2003');
is($f->start->strftime('%B'), 'January', "subclass 3.3 - $db_type");
}
BEGIN
{
our %HAVE;
#
# PostgreSQL
#
my $dbh;
eval
{
$dbh = Rose::DB->new('pg_admin')->retain_dbh()
or die Rose::DB->error;
};
if(!$@ && $dbh)
{
$HAVE{'pg'} = 1;
}
#
# MySQL
#
eval
{
$dbh = Rose::DB->new('mysql_admin')->retain_dbh()
or die Rose::DB->error;
};
if(!$@ && $dbh)
{
$HAVE{'mysql'} = 1;
}
#
# SQLite
#
eval
{
$dbh = Rose::DB->new('sqlite_admin')->retain_dbh()
or die Rose::DB->error;
};
if(!$@ && $dbh)
{
$HAVE{'sqlite'} = 1;
}
}
|