1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
8 use DBIx::Class::_Util 'sigwarn_silencer';
10 @YA::Film::ISA = 'Film';
12 use lib 't/cdbi/testlib';
16 Film->create_test_film;
17 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
18 ok(my $pj = $btaste->Director, "Bad taste has_a() director");
19 ok(!ref($pj), ' ... which is not an object');
21 ok(Film->has_a('Director' => 'Director'), "Link Director table");
25 Name => 'Peter Jackson',
26 Birthday => -300000000,
33 $btaste = Film->retrieve('Bad Taste');
35 ok($pj = $btaste->Director, "Bad taste now has_a() director");
36 isa_ok($pj => 'Director');
37 is($pj->id, 'Peter Jackson', ' ... and is the correct director');
39 # Oh no! Its Peter Jacksons even twin, Skippy! Born one minute after him.
40 my $sj = Director->create(
42 Name => 'Skippy Jackson',
43 Birthday => (-300000000 + 60),
48 is($sj->id, 'Skippy Jackson', 'We have a new director');
50 Film->has_a(CoDirector => 'Director');
52 $btaste->CoDirector($sj);
54 is($btaste->CoDirector->Name, 'Skippy Jackson', 'He co-directed');
56 $btaste->Director->Name,
58 "Didnt interfere with each other"
61 { # Ensure search can take an object
62 my @films = Film->search(Director => $pj);
63 is @films, 1, "1 Film directed by $pj";
64 is $films[0]->id, "Bad Taste", "Bad Taste";
71 # Skippy directs a film and Peter helps!
72 $sj = Director->retrieve('Skippy Jackson');
73 $pj = Director->retrieve('Peter Jackson');
75 fail_with_bad_object($sj, $btaste);
80 my $btaste = YA::Film->retrieve('Bad Taste');
81 is(ref($btaste->Director), 'Director', 'inheriting has_a()');
82 is(ref($btaste->CoDirector), 'Director', 'inheriting has_a()');
83 is($btaste->CoDirector->Name, 'Skippy Jackson', ' ... correctly');
87 my ($dir, $codir) = @_;
88 my $tastes_bad = YA::Film->create(
90 Title => 'Tastes Bad',
94 NumExplodingSheep => 23
97 is($tastes_bad->_Director_accessor, 'Skippy Jackson', 'Director_accessor');
98 is($tastes_bad->Director->Name, 'Skippy Jackson', 'Director');
99 is($tastes_bad->CoDirector->Name, 'Peter Jackson', 'CoDirector');
101 $tastes_bad->_CoDirector_accessor,
103 'CoDirector_accessor'
107 sub fail_with_bad_object {
108 my ($dir, $codir) = @_;
110 local $SIG{__WARN__} = sigwarn_silencer( qr/\Qusually should inherit from the related ResultClass ('Director')/ );
113 Title => 'Tastes Bad',
115 CoDirector => $codir,
117 NumExplodingSheep => 23
120 } qr/isn't a Director/;
125 __PACKAGE__->table('foo');
126 __PACKAGE__->columns('All' => qw/ id fav /);
128 __PACKAGE__->db_Main->do( qq{
138 __PACKAGE__->table('bar');
139 __PACKAGE__->columns('All' => qw/ id fav /);
141 __PACKAGE__->db_Main->do( qq{
149 Foo->has_a("fav" => "Film");
150 Bar->has_a("fav" => "Foo");
151 my $foo = Foo->create({ id => 6, fav => 'Bad Taste' });
152 my $bar = Bar->create({ id => 2, fav => 6 });
153 isa_ok($bar->fav, "Foo");
154 isa_ok($foo->fav, "Film");
158 Foo->add_trigger(after_create => sub { $foo = shift->fav });
159 my $gwh = Foo->create({ id => 93, fav => 'Good Will Hunting' });
160 isa_ok $foo, "Film", "Object in after_create trigger";