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
|
use strict;
use Test::More;
BEGIN {
eval "use DBD::SQLite";
plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 24);
}
@YA::Film::ISA = 'Film';
local $SIG{__WARN__} = sub { };
INIT {
use lib 't/testlib';
use Film;
use Director;
Film->CONSTRUCT;
Director->CONSTRUCT;
}
ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
ok(my $pj = $btaste->Director, "Bad taste hasa() director");
ok(!ref($pj), ' ... which is not an object');
ok(Film->hasa('Director' => 'Director'), "Link Director table");
ok(
Director->create(
{
Name => 'Peter Jackson',
Birthday => -300000000,
IsInsane => 1
}
),
'create Director'
);
$btaste = Film->retrieve('Bad Taste');
ok($pj = $btaste->Director, "Bad taste now hasa() director");
isa_ok($pj => 'Director');
is($pj->id, 'Peter Jackson', ' ... and is the correct director');
# Oh no! Its Peter Jacksons even twin, Skippy! Born one minute after him.
my $sj = Director->create(
{
Name => 'Skippy Jackson',
Birthday => (-300000000 + 60),
IsInsane => 1,
}
);
is($sj->id, 'Skippy Jackson', 'We have a new director');
Film->hasa('Director' => 'CoDirector');
$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"
);
{ # Ensure search can take an object
my @films = Film->search(Director => $pj);
is @films, 1, "1 Film directed by $pj";
is $films[0]->id, "Bad Taste", "Bad Taste";
}
inheriting_hasa();
{
# Skippy directs a film and Peter helps!
$sj = Director->retrieve('Skippy Jackson');
$pj = Director->retrieve('Peter Jackson');
fail_with_bad_object($sj, $btaste);
taste_bad($sj, $pj);
}
sub 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');
}
sub taste_bad {
my ($dir, $codir) = @_;
my $tastes_bad = YA::Film->create(
{
Title => 'Tastes Bad',
Director => $dir,
CoDirector => $codir,
Rating => 'R',
NumExplodingSheep => 23
}
);
is($tastes_bad->_Director_accessor, 'Skippy Jackson', 'Director_accessor');
is($tastes_bad->Director->Name, 'Skippy Jackson', 'Director');
is($tastes_bad->CoDirector->Name, 'Peter Jackson', 'CoDirector');
is(
$tastes_bad->_CoDirector_accessor,
'Peter Jackson',
'CoDirector_accessor'
);
}
sub fail_with_bad_object {
my ($dir, $codir) = @_;
eval {
YA::Film->create(
{
Title => 'Tastes Bad',
Director => $dir,
CoDirector => $codir,
Rating => 'R',
NumExplodingSheep => 23
}
);
};
ok $@, $@;
}
package Foo;
use base 'CDBase';
__PACKAGE__->table('foo');
__PACKAGE__->columns('All' => qw/ id fav /);
# fav is a film
__PACKAGE__->db_Main->do( qq{
CREATE TABLE foo (
id INTEGER,
fav VARCHAR(255)
)
});
package Bar;
use base 'CDBase';
__PACKAGE__->table('bar');
__PACKAGE__->columns('All' => qw/ id fav /);
# fav is a foo
__PACKAGE__->db_Main->do( qq{
CREATE TABLE bar (
id INTEGER,
fav INTEGER
)
});
package main;
Foo->has_a("fav" => "Film");
Bar->has_a("fav" => "Foo");
my $foo = Foo->create({ id => 6, fav => 'Bad Taste' });
my $bar = Bar->create({ id => 2, fav => 6 });
isa_ok($bar->fav, "Foo");
isa_ok($foo->fav, "Film");
{
my $foo;
Foo->add_trigger(after_create => sub { $foo = shift->fav });
my $gwh = Foo->create({ id => 93, fav => 'Good Will Hunting' });
isa_ok $foo, "Film", "Object in after_create trigger";
}
|