5 eval "use DBIx::Class::CDBICompat;";
7 plan (skip_all => "Class::Trigger and DBIx::ContextualFetch required: $@");
12 @YA::Film::ISA = 'Film';
14 #local $SIG{__WARN__} = sub { };
17 use lib 't/cdbi/testlib';
22 Film->create_test_film;
23 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
24 ok(my $pj = $btaste->Director, "Bad taste has_a() director");
25 ok(!ref($pj), ' ... which is not an object');
27 ok(Film->has_a('Director' => 'Director'), "Link Director table");
31 Name => 'Peter Jackson',
32 Birthday => -300000000,
39 $btaste = Film->retrieve('Bad Taste');
41 ok($pj = $btaste->Director, "Bad taste now has_a() director");
42 isa_ok($pj => 'Director');
43 is($pj->id, 'Peter Jackson', ' ... and is the correct director');
45 # Oh no! Its Peter Jacksons even twin, Skippy! Born one minute after him.
46 my $sj = Director->create(
48 Name => 'Skippy Jackson',
49 Birthday => (-300000000 + 60),
54 is($sj->id, 'Skippy Jackson', 'We have a new director');
56 Film->has_a(CoDirector => 'Director');
58 $btaste->CoDirector($sj);
60 is($btaste->CoDirector->Name, 'Skippy Jackson', 'He co-directed');
62 $btaste->Director->Name,
64 "Didnt interfere with each other"
67 { # Ensure search can take an object
68 my @films = Film->search(Director => $pj);
69 is @films, 1, "1 Film directed by $pj";
70 is $films[0]->id, "Bad Taste", "Bad Taste";
77 # Skippy directs a film and Peter helps!
78 $sj = Director->retrieve('Skippy Jackson');
79 $pj = Director->retrieve('Peter Jackson');
81 fail_with_bad_object($sj, $btaste);
86 my $btaste = YA::Film->retrieve('Bad Taste');
87 is(ref($btaste->Director), 'Director', 'inheriting has_a()');
88 is(ref($btaste->CoDirector), 'Director', 'inheriting has_a()');
89 is($btaste->CoDirector->Name, 'Skippy Jackson', ' ... correctly');
93 my ($dir, $codir) = @_;
94 my $tastes_bad = YA::Film->create(
96 Title => 'Tastes Bad',
100 NumExplodingSheep => 23
103 is($tastes_bad->_Director_accessor, 'Skippy Jackson', 'Director_accessor');
104 is($tastes_bad->Director->Name, 'Skippy Jackson', 'Director');
105 is($tastes_bad->CoDirector->Name, 'Peter Jackson', 'CoDirector');
107 $tastes_bad->_CoDirector_accessor,
109 'CoDirector_accessor'
113 sub fail_with_bad_object {
114 my ($dir, $codir) = @_;
118 Title => 'Tastes Bad',
120 CoDirector => $codir,
122 NumExplodingSheep => 23
131 __PACKAGE__->table('foo');
132 __PACKAGE__->columns('All' => qw/ id fav /);
134 __PACKAGE__->db_Main->do( qq{
144 __PACKAGE__->table('bar');
145 __PACKAGE__->columns('All' => qw/ id fav /);
147 __PACKAGE__->db_Main->do( qq{
155 Foo->has_a("fav" => "Film");
156 Bar->has_a("fav" => "Foo");
157 my $foo = Foo->create({ id => 6, fav => 'Bad Taste' });
158 my $bar = Bar->create({ id => 2, fav => 6 });
159 isa_ok($bar->fav, "Foo");
160 isa_ok($foo->fav, "Film");
164 Foo->add_trigger(after_create => sub { $foo = shift->fav });
165 my $gwh = Foo->create({ id => 93, fav => 'Good Will Hunting' });
166 isa_ok $foo, "Film", "Object in after_create trigger";