5 use DBIx::Class::_Util 'sigwarn_silencer';
7 @YA::Film::ISA = 'Film';
9 use lib 't/cdbi/testlib';
13 Film->create_test_film;
14 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
15 ok(my $pj = $btaste->Director, "Bad taste has_a() director");
16 ok(!ref($pj), ' ... which is not an object');
18 ok(Film->has_a('Director' => 'Director'), "Link Director table");
22 Name => 'Peter Jackson',
23 Birthday => -300000000,
30 $btaste = Film->retrieve('Bad Taste');
32 ok($pj = $btaste->Director, "Bad taste now has_a() director");
33 isa_ok($pj => 'Director');
34 is($pj->id, 'Peter Jackson', ' ... and is the correct director');
36 # Oh no! Its Peter Jacksons even twin, Skippy! Born one minute after him.
37 my $sj = Director->create(
39 Name => 'Skippy Jackson',
40 Birthday => (-300000000 + 60),
45 is($sj->id, 'Skippy Jackson', 'We have a new director');
47 Film->has_a(CoDirector => 'Director');
49 $btaste->CoDirector($sj);
51 is($btaste->CoDirector->Name, 'Skippy Jackson', 'He co-directed');
53 $btaste->Director->Name,
55 "Didnt interfere with each other"
58 { # Ensure search can take an object
59 my @films = Film->search(Director => $pj);
60 is @films, 1, "1 Film directed by $pj";
61 is $films[0]->id, "Bad Taste", "Bad Taste";
68 # Skippy directs a film and Peter helps!
69 $sj = Director->retrieve('Skippy Jackson');
70 $pj = Director->retrieve('Peter Jackson');
72 fail_with_bad_object($sj, $btaste);
77 my $btaste = YA::Film->retrieve('Bad Taste');
78 is(ref($btaste->Director), 'Director', 'inheriting has_a()');
79 is(ref($btaste->CoDirector), 'Director', 'inheriting has_a()');
80 is($btaste->CoDirector->Name, 'Skippy Jackson', ' ... correctly');
84 my ($dir, $codir) = @_;
85 my $tastes_bad = YA::Film->create(
87 Title => 'Tastes Bad',
91 NumExplodingSheep => 23
94 is($tastes_bad->_Director_accessor, 'Skippy Jackson', 'Director_accessor');
95 is($tastes_bad->Director->Name, 'Skippy Jackson', 'Director');
96 is($tastes_bad->CoDirector->Name, 'Peter Jackson', 'CoDirector');
98 $tastes_bad->_CoDirector_accessor,
100 'CoDirector_accessor'
104 sub fail_with_bad_object {
105 my ($dir, $codir) = @_;
107 local $SIG{__WARN__} = sigwarn_silencer( qr/\Qusually should inherit from the related ResultClass ('Director')/ );
110 Title => 'Tastes Bad',
112 CoDirector => $codir,
114 NumExplodingSheep => 23
117 } qr/isn't a Director/;
122 __PACKAGE__->table('foo');
123 __PACKAGE__->columns('All' => qw/ id fav /);
125 __PACKAGE__->db_Main->do( qq{
135 __PACKAGE__->table('bar');
136 __PACKAGE__->columns('All' => qw/ id fav /);
138 __PACKAGE__->db_Main->do( qq{
146 Foo->has_a("fav" => "Film");
147 Bar->has_a("fav" => "Foo");
148 my $foo = Foo->create({ id => 6, fav => 'Bad Taste' });
149 my $bar = Bar->create({ id => 2, fav => 6 });
150 isa_ok($bar->fav, "Foo");
151 isa_ok($foo->fav, "Film");
155 Foo->add_trigger(after_create => sub { $foo = shift->fav });
156 my $gwh = Foo->create({ id => 93, fav => 'Good Will Hunting' });
157 isa_ok $foo, "Film", "Object in after_create trigger";