5 use DBIx::Class::_Util 'sigwarn_silencer';
7 @YA::Film::ISA = 'Film';
9 #local $SIG{__WARN__} = sub { };
12 use lib 't/cdbi/testlib';
17 Film->create_test_film;
18 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
19 ok(my $pj = $btaste->Director, "Bad taste has_a() director");
20 ok(!ref($pj), ' ... which is not an object');
22 ok(Film->has_a('Director' => 'Director'), "Link Director table");
26 Name => 'Peter Jackson',
27 Birthday => -300000000,
34 $btaste = Film->retrieve('Bad Taste');
36 ok($pj = $btaste->Director, "Bad taste now has_a() director");
37 isa_ok($pj => 'Director');
38 is($pj->id, 'Peter Jackson', ' ... and is the correct director');
40 # Oh no! Its Peter Jacksons even twin, Skippy! Born one minute after him.
41 my $sj = Director->create(
43 Name => 'Skippy Jackson',
44 Birthday => (-300000000 + 60),
49 is($sj->id, 'Skippy Jackson', 'We have a new director');
51 Film->has_a(CoDirector => 'Director');
53 $btaste->CoDirector($sj);
55 is($btaste->CoDirector->Name, 'Skippy Jackson', 'He co-directed');
57 $btaste->Director->Name,
59 "Didnt interfere with each other"
62 { # Ensure search can take an object
63 my @films = Film->search(Director => $pj);
64 is @films, 1, "1 Film directed by $pj";
65 is $films[0]->id, "Bad Taste", "Bad Taste";
72 # Skippy directs a film and Peter helps!
73 $sj = Director->retrieve('Skippy Jackson');
74 $pj = Director->retrieve('Peter Jackson');
76 fail_with_bad_object($sj, $btaste);
81 my $btaste = YA::Film->retrieve('Bad Taste');
82 is(ref($btaste->Director), 'Director', 'inheriting has_a()');
83 is(ref($btaste->CoDirector), 'Director', 'inheriting has_a()');
84 is($btaste->CoDirector->Name, 'Skippy Jackson', ' ... correctly');
88 my ($dir, $codir) = @_;
89 my $tastes_bad = YA::Film->create(
91 Title => 'Tastes Bad',
95 NumExplodingSheep => 23
98 is($tastes_bad->_Director_accessor, 'Skippy Jackson', 'Director_accessor');
99 is($tastes_bad->Director->Name, 'Skippy Jackson', 'Director');
100 is($tastes_bad->CoDirector->Name, 'Peter Jackson', 'CoDirector');
102 $tastes_bad->_CoDirector_accessor,
104 'CoDirector_accessor'
108 sub fail_with_bad_object {
109 my ($dir, $codir) = @_;
111 local $SIG{__WARN__} = sigwarn_silencer( qr/\Qusually should inherit from the related ResultClass ('Director')/ );
114 Title => 'Tastes Bad',
116 CoDirector => $codir,
118 NumExplodingSheep => 23
121 } qr/isn't a Director/;
126 __PACKAGE__->table('foo');
127 __PACKAGE__->columns('All' => qw/ id fav /);
129 __PACKAGE__->db_Main->do( qq{
139 __PACKAGE__->table('bar');
140 __PACKAGE__->columns('All' => qw/ id fav /);
142 __PACKAGE__->db_Main->do( qq{
150 Foo->has_a("fav" => "Film");
151 Bar->has_a("fav" => "Foo");
152 my $foo = Foo->create({ id => 6, fav => 'Bad Taste' });
153 my $bar = Bar->create({ id => 2, fav => 6 });
154 isa_ok($bar->fav, "Foo");
155 isa_ok($foo->fav, "Film");
159 Foo->add_trigger(after_create => sub { $foo = shift->fav });
160 my $gwh = Foo->create({ id => 93, fav => 'Good Will Hunting' });
161 isa_ok $foo, "Film", "Object in after_create trigger";