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
|
use strict;
use Test::More;
BEGIN {
eval "use DBD::SQLite";
plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 41);
}
use lib 't/testlib';
use Film;
use Director;
@YA::Film::ISA = 'Film';
Film->create_test_film;
ok my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste";
ok my $pj = $btaste->Director, "Bad taste has a director";
ok !ref($pj), ' ... which is not an object';
ok(Film->has_a('director' => 'Director'), "Link Director table");
ok(
Director->insert(
{
Name => 'Peter Jackson',
Birthday => -300000000,
IsInsane => 1
}
),
'insert Director'
);
{
ok $btaste = Film->retrieve('Bad Taste'), "Reretrieve Bad Taste";
ok $pj = $btaste->Director, "Bad taste now hasa() director";
isa_ok $pj => 'Director';
{
no warnings 'redefine';
local *Ima::DBI::st::execute =
sub { ::fail("Shouldn't need to query db"); };
is $pj->id, 'Peter Jackson', 'ID already stored';
}
ok $pj->IsInsane, "But we know he's insane";
}
# Oh no! Its Peter Jacksons even twin, Skippy! Born one minute after him.
my $sj = Director->insert(
{
Name => 'Skippy Jackson',
Birthday => (-300000000 + 60),
IsInsane => 1,
}
);
{
eval { $btaste->Director($btaste) };
like $@, qr/is not a Director/, "Can't set film as director";
is $btaste->Director->id, $pj->id, "PJ still the director";
# drop from cache so that next retrieve() is from db
$btaste->remove_from_object_index;
}
{ # Still inflated after update
my $btaste = Film->retrieve('Bad Taste');
isa_ok $btaste->Director, "Director";
$btaste->numexplodingsheep(17);
$btaste->update;
isa_ok $btaste->Director, "Director";
$btaste->Director('Someone Else');
$btaste->update;
isa_ok $btaste->Director, "Director";
is $btaste->Director->id, "Someone Else", "Can change director";
}
is $sj->id, 'Skippy Jackson', 'Create new director - Skippy';
Film->has_a('codirector' => 'Director');
{
eval { $btaste->CoDirector("Skippy Jackson") };
is $@, "", "Auto inflates";
isa_ok $btaste->CoDirector, "Director";
is $btaste->CoDirector->id, $sj->id, "To skippy";
}
$btaste->CoDirector($sj);
$btaste->update;
is($btaste->CoDirector->Name, 'Skippy Jackson', 'He co-directed');
is(
$btaste->Director->Name,
'Peter Jackson',
"Didnt interfere with each other"
);
{ # Inheriting hasa
my $btaste = YA::Film->retrieve('Bad Taste');
is(ref($btaste->Director), 'Director', 'inheriting hasa()');
is(ref($btaste->CoDirector), 'Director', 'inheriting hasa()');
is($btaste->CoDirector->Name, 'Skippy Jackson', ' ... correctly');
}
{
$sj = Director->retrieve('Skippy Jackson');
$pj = Director->retrieve('Peter Jackson');
my $fail;
eval {
$fail = YA::Film->insert(
{
Title => 'Tastes Bad',
Director => $sj,
codirector => $btaste,
Rating => 'R',
NumExplodingSheep => 23
}
);
};
ok $@, "Can't have film as codirector: $@";
is $fail, undef, "We didn't get anything";
my $tastes_bad = YA::Film->insert(
{
Title => 'Tastes Bad',
Director => $sj,
codirector => $pj,
Rating => 'R',
NumExplodingSheep => 23
}
);
is($tastes_bad->Director->Name, 'Skippy Jackson', 'Director');
is(
$tastes_bad->_director_accessor->Name,
'Skippy Jackson',
'director_accessor'
);
is($tastes_bad->codirector->Name, 'Peter Jackson', 'codirector');
is(
$tastes_bad->_codirector_accessor->Name,
'Peter Jackson',
'codirector_accessor'
);
}
{
{
YA::Film->add_relationship_type(has_a => "YA::HasA");
package YA::HasA;
use base 'Class::DBI::Relationship::HasA';
sub _inflator {
my $self = shift;
my $col = $self->accessor;
my $super = $self->SUPER::_inflator($col);
return $super
unless $col eq $self->class->find_column('Director');
return sub {
my $self = shift;
$self->_attribute_store($col, 'Ghostly Peter')
if $self->_attribute_exists($col)
and not defined $self->_attrs($col);
return &$super($self);
};
}
}
{
package Rating;
sub new {
my ($class, $mpaa, @details) = @_;
bless {
MPAA => $mpaa,
WHY => "@details"
}, $class;
}
sub mpaa { shift->{MPAA}; }
sub why { shift->{WHY}; }
}
local *Director::mapme = sub {
my ($class, $val) = @_;
$val =~ s/Skippy/Peter/;
$val;
};
no warnings 'once';
local *Director::sanity_check = sub { $_[0]->IsInsane ? undef: $_[0] };
YA::Film->has_a(
director => 'Director',
inflate => 'mapme',
deflate => 'sanity_check'
);
YA::Film->has_a(
rating => 'Rating',
inflate => sub {
my ($val, $parent) = @_;
my $sheep = $parent->find_column('NumexplodingSheep');
if ($parent->_attrs($sheep) || 0 > 20) {
return new Rating 'NC17', 'Graphic ovine violence';
} else {
return new Rating $val, 'Just because';
}
},
deflate => sub {
shift->mpaa;
}
);
my $tbad = YA::Film->retrieve('Tastes Bad');
isa_ok $tbad->Director, 'Director';
is $tbad->Director->Name, 'Peter Jackson', 'Director shuffle';
$tbad->Director('Skippy Jackson');
$tbad->update;
is $tbad->Director, 'Ghostly Peter', 'Sanity checked';
isa_ok $tbad->Rating, 'Rating';
is $tbad->Rating->mpaa, 'NC17', 'Rating bumped';
$tbad->Rating(new Rating 'NS17', 'Shaken sheep');
no warnings 'redefine';
local *Director::mapme = sub {
my ($class, $obj) = @_;
$obj->isa('Film') ? $obj->Director : $obj;
};
$pj->IsInsane(0);
$pj->update; # Hush warnings
ok $tbad->Director($btaste), 'Cross-class mapping';
is $tbad->Director, 'Peter Jackson', 'Yields PJ';
$tbad->update;
$tbad = Film->retrieve('Tastes Bad');
ok !ref($tbad->Rating), 'Unmagical rating';
is $tbad->Rating, 'NS17', 'but prior change stuck';
}
{ # Broken has_a declaration
eval { Film->has_a(driector => "Director") };
like $@, qr/driector/,
"Sensible error from has_a with incorrect column: $@";
}
|