| 1 |
use strict; |
| 2 |
use Test::More; |
| 3 |
use Class::DBI::Sweet; |
| 4 |
Class::DBI::Sweet->default_search_attributes({ use_resultset_cache => 0 }); |
| 5 |
Class::DBI::Sweet->cache(Cache::MemoryCache->new( |
| 6 |
{ namespace => "SweetTest", default_expires_in => 60 } ) ); |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
eval "use Cache::MemoryCache"; |
| 10 |
plan skip_all => "needs Cache::Cache for testing" if $@; |
| 11 |
eval "use DBD::SQLite"; |
| 12 |
plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 41); |
| 13 |
} |
| 14 |
|
| 15 |
use lib 't/cdbi-t/testlib'; |
| 16 |
use Film; |
| 17 |
use Director; |
| 18 |
@YA::Film::ISA = 'Film'; |
| 19 |
|
| 20 |
Film->create_test_film; |
| 21 |
|
| 22 |
ok my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste"; |
| 23 |
ok my $pj = $btaste->Director, "Bad taste has a director"; |
| 24 |
ok !ref($pj), ' ... which is not an object'; |
| 25 |
|
| 26 |
ok(Film->has_a('director' => 'Director'), "Link Director table"); |
| 27 |
ok( |
| 28 |
Director->insert( |
| 29 |
{ |
| 30 |
Name => 'Peter Jackson', |
| 31 |
Birthday => -300000000, |
| 32 |
IsInsane => 1 |
| 33 |
} |
| 34 |
), |
| 35 |
'insert Director' |
| 36 |
); |
| 37 |
|
| 38 |
{ |
| 39 |
ok $btaste = Film->retrieve('Bad Taste'), "Reretrieve Bad Taste"; |
| 40 |
ok $pj = $btaste->Director, "Bad taste now hasa() director"; |
| 41 |
isa_ok $pj => 'Director'; |
| 42 |
{ |
| 43 |
no warnings 'redefine'; |
| 44 |
local *Ima::DBI::st::execute = |
| 45 |
sub { ::fail("Shouldn't need to query db"); }; |
| 46 |
is $pj->id, 'Peter Jackson', 'ID already stored'; |
| 47 |
} |
| 48 |
ok $pj->IsInsane, "But we know he's insane"; |
| 49 |
} |
| 50 |
|
| 51 |
# Oh no! Its Peter Jacksons even twin, Skippy! Born one minute after him. |
| 52 |
my $sj = Director->insert( |
| 53 |
{ |
| 54 |
Name => 'Skippy Jackson', |
| 55 |
Birthday => (-300000000 + 60), |
| 56 |
IsInsane => 1, |
| 57 |
} |
| 58 |
); |
| 59 |
|
| 60 |
{ |
| 61 |
eval { $btaste->Director($btaste) }; |
| 62 |
like $@, qr/is not a Director/, "Can't set film as director"; |
| 63 |
is $btaste->Director->id, $pj->id, "PJ still the director"; |
| 64 |
|
| 65 |
# drop from cache so that next retrieve() is from db |
| 66 |
$btaste->remove_from_object_index; |
| 67 |
} |
| 68 |
|
| 69 |
{ # Still inflated after update |
| 70 |
my $btaste = Film->retrieve('Bad Taste'); |
| 71 |
isa_ok $btaste->Director, "Director"; |
| 72 |
$btaste->numexplodingsheep(17); |
| 73 |
$btaste->update; |
| 74 |
isa_ok $btaste->Director, "Director"; |
| 75 |
|
| 76 |
$btaste->Director('Someone Else'); |
| 77 |
$btaste->update; |
| 78 |
isa_ok $btaste->Director, "Director"; |
| 79 |
is $btaste->Director->id, "Someone Else", "Can change director"; |
| 80 |
} |
| 81 |
|
| 82 |
is $sj->id, 'Skippy Jackson', 'Create new director - Skippy'; |
| 83 |
Film->has_a('codirector' => 'Director'); |
| 84 |
{ |
| 85 |
eval { $btaste->CoDirector("Skippy Jackson") }; |
| 86 |
is $@, "", "Auto inflates"; |
| 87 |
isa_ok $btaste->CoDirector, "Director"; |
| 88 |
is $btaste->CoDirector->id, $sj->id, "To skippy"; |
| 89 |
} |
| 90 |
|
| 91 |
$btaste->CoDirector($sj); |
| 92 |
$btaste->update; |
| 93 |
is($btaste->CoDirector->Name, 'Skippy Jackson', 'He co-directed'); |
| 94 |
is( |
| 95 |
$btaste->Director->Name, |
| 96 |
'Peter Jackson', |
| 97 |
"Didnt interfere with each other" |
| 98 |
); |
| 99 |
|
| 100 |
{ # Inheriting hasa |
| 101 |
my $btaste = YA::Film->retrieve('Bad Taste'); |
| 102 |
is(ref($btaste->Director), 'Director', 'inheriting hasa()'); |
| 103 |
is(ref($btaste->CoDirector), 'Director', 'inheriting hasa()'); |
| 104 |
is($btaste->CoDirector->Name, 'Skippy Jackson', ' ... correctly'); |
| 105 |
} |
| 106 |
|
| 107 |
{ |
| 108 |
$sj = Director->retrieve('Skippy Jackson'); |
| 109 |
$pj = Director->retrieve('Peter Jackson'); |
| 110 |
|
| 111 |
my $fail; |
| 112 |
eval { |
| 113 |
$fail = YA::Film->insert( |
| 114 |
{ |
| 115 |
Title => 'Tastes Bad', |
| 116 |
Director => $sj, |
| 117 |
codirector => $btaste, |
| 118 |
Rating => 'R', |
| 119 |
NumExplodingSheep => 23 |
| 120 |
} |
| 121 |
); |
| 122 |
}; |
| 123 |
ok $@, "Can't have film as codirector: $@"; |
| 124 |
is $fail, undef, "We didn't get anything"; |
| 125 |
|
| 126 |
my $tastes_bad = YA::Film->insert( |
| 127 |
{ |
| 128 |
Title => 'Tastes Bad', |
| 129 |
Director => $sj, |
| 130 |
codirector => $pj, |
| 131 |
Rating => 'R', |
| 132 |
NumExplodingSheep => 23 |
| 133 |
} |
| 134 |
); |
| 135 |
is($tastes_bad->Director->Name, 'Skippy Jackson', 'Director'); |
| 136 |
is( |
| 137 |
$tastes_bad->_director_accessor->Name, |
| 138 |
'Skippy Jackson', |
| 139 |
'director_accessor' |
| 140 |
); |
| 141 |
is($tastes_bad->codirector->Name, 'Peter Jackson', 'codirector'); |
| 142 |
is( |
| 143 |
$tastes_bad->_codirector_accessor->Name, |
| 144 |
'Peter Jackson', |
| 145 |
'codirector_accessor' |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
{ |
| 150 |
{ |
| 151 |
|
| 152 |
YA::Film->add_relationship_type(has_a => "YA::HasA"); |
| 153 |
|
| 154 |
package YA::HasA; |
| 155 |
use base 'Class::DBI::Relationship::HasA'; |
| 156 |
|
| 157 |
sub _inflator { |
| 158 |
my $self = shift; |
| 159 |
my $col = $self->accessor; |
| 160 |
my $super = $self->SUPER::_inflator($col); |
| 161 |
|
| 162 |
return $super |
| 163 |
unless $col eq $self->class->find_column('Director'); |
| 164 |
|
| 165 |
return sub { |
| 166 |
my $self = shift; |
| 167 |
$self->_attribute_store($col, 'Ghostly Peter') |
| 168 |
if $self->_attribute_exists($col) |
| 169 |
and not defined $self->_attrs($col); |
| 170 |
return &$super($self); |
| 171 |
}; |
| 172 |
} |
| 173 |
} |
| 174 |
{ |
| 175 |
|
| 176 |
package Rating; |
| 177 |
|
| 178 |
sub new { |
| 179 |
my ($class, $mpaa, @details) = @_; |
| 180 |
bless { |
| 181 |
MPAA => $mpaa, |
| 182 |
WHY => "@details" |
| 183 |
}, $class; |
| 184 |
} |
| 185 |
sub mpaa { shift->{MPAA}; } |
| 186 |
sub why { shift->{WHY}; } |
| 187 |
} |
| 188 |
local *Director::mapme = sub { |
| 189 |
my ($class, $val) = @_; |
| 190 |
$val =~ s/Skippy/Peter/; |
| 191 |
$val; |
| 192 |
}; |
| 193 |
no warnings 'once'; |
| 194 |
local *Director::sanity_check = sub { $_[0]->IsInsane ? undef: $_[0] }; |
| 195 |
YA::Film->has_a( |
| 196 |
director => 'Director', |
| 197 |
inflate => 'mapme', |
| 198 |
deflate => 'sanity_check' |
| 199 |
); |
| 200 |
YA::Film->has_a( |
| 201 |
rating => 'Rating', |
| 202 |
inflate => sub { |
| 203 |
my ($val, $parent) = @_; |
| 204 |
my $sheep = $parent->find_column('NumexplodingSheep'); |
| 205 |
if ($parent->_attrs($sheep) || 0 > 20) { |
| 206 |
return new Rating 'NC17', 'Graphic ovine violence'; |
| 207 |
} else { |
| 208 |
return new Rating $val, 'Just because'; |
| 209 |
} |
| 210 |
}, |
| 211 |
deflate => sub { |
| 212 |
shift->mpaa; |
| 213 |
} |
| 214 |
); |
| 215 |
|
| 216 |
my $tbad = YA::Film->retrieve('Tastes Bad'); |
| 217 |
|
| 218 |
isa_ok $tbad->Director, 'Director'; |
| 219 |
is $tbad->Director->Name, 'Peter Jackson', 'Director shuffle'; |
| 220 |
$tbad->Director('Skippy Jackson'); |
| 221 |
$tbad->update; |
| 222 |
is $tbad->Director, 'Ghostly Peter', 'Sanity checked'; |
| 223 |
|
| 224 |
isa_ok $tbad->Rating, 'Rating'; |
| 225 |
is $tbad->Rating->mpaa, 'NC17', 'Rating bumped'; |
| 226 |
$tbad->Rating(new Rating 'NS17', 'Shaken sheep'); |
| 227 |
no warnings 'redefine'; |
| 228 |
local *Director::mapme = sub { |
| 229 |
my ($class, $obj) = @_; |
| 230 |
$obj->isa('Film') ? $obj->Director : $obj; |
| 231 |
}; |
| 232 |
|
| 233 |
$pj->IsInsane(0); |
| 234 |
$pj->update; # Hush warnings |
| 235 |
|
| 236 |
ok $tbad->Director($btaste), 'Cross-class mapping'; |
| 237 |
is $tbad->Director, 'Peter Jackson', 'Yields PJ'; |
| 238 |
$tbad->update; |
| 239 |
|
| 240 |
$tbad = Film->retrieve('Tastes Bad'); |
| 241 |
ok !ref($tbad->Rating), 'Unmagical rating'; |
| 242 |
is $tbad->Rating, 'NS17', 'but prior change stuck'; |
| 243 |
} |
| 244 |
|
| 245 |
{ # Broken has_a declaration |
| 246 |
eval { Film->has_a(driector => "Director") }; |
| 247 |
like $@, qr/driector/, |
| 248 |
"Sensible error from has_a with incorrect column: $@"; |
| 249 |
} |