5 eval "use DBIx::Class::CDBICompat;";
7 plan (skip_all => "Class::Trigger and DBIx::ContextualFetch required: $@");
10 eval "use DBD::SQLite";
11 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 24);
14 @YA::Film::ISA = 'Film';
16 #local $SIG{__WARN__} = sub { };
19 use lib 't/cdbi/testlib';
24 Film->create_test_film;
25 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
26 ok(my $pj = $btaste->Director, "Bad taste has_a() director");
27 ok(!ref($pj), ' ... which is not an object');
29 ok(Film->has_a('Director' => 'Director'), "Link Director table");
33 Name => 'Peter Jackson',
34 Birthday => -300000000,
41 $btaste = Film->retrieve('Bad Taste');
43 ok($pj = $btaste->Director, "Bad taste now has_a() director");
44 isa_ok($pj => 'Director');
45 is($pj->id, 'Peter Jackson', ' ... and is the correct director');
47 # Oh no! Its Peter Jacksons even twin, Skippy! Born one minute after him.
48 my $sj = Director->create(
50 Name => 'Skippy Jackson',
51 Birthday => (-300000000 + 60),
56 is($sj->id, 'Skippy Jackson', 'We have a new director');
58 Film->has_a(CoDirector => 'Director');
60 $btaste->CoDirector($sj);
62 is($btaste->CoDirector->Name, 'Skippy Jackson', 'He co-directed');
64 $btaste->Director->Name,
66 "Didnt interfere with each other"
69 { # Ensure search can take an object
70 my @films = Film->search(Director => $pj);
71 is @films, 1, "1 Film directed by $pj";
72 is $films[0]->id, "Bad Taste", "Bad Taste";
79 # Skippy directs a film and Peter helps!
80 $sj = Director->retrieve('Skippy Jackson');
81 $pj = Director->retrieve('Peter Jackson');
83 fail_with_bad_object($sj, $btaste);
88 my $btaste = YA::Film->retrieve('Bad Taste');
89 is(ref($btaste->Director), 'Director', 'inheriting has_a()');
90 is(ref($btaste->CoDirector), 'Director', 'inheriting has_a()');
91 is($btaste->CoDirector->Name, 'Skippy Jackson', ' ... correctly');
95 my ($dir, $codir) = @_;
96 my $tastes_bad = YA::Film->create(
98 Title => 'Tastes Bad',
100 CoDirector => $codir,
102 NumExplodingSheep => 23
105 is($tastes_bad->_Director_accessor, 'Skippy Jackson', 'Director_accessor');
106 is($tastes_bad->Director->Name, 'Skippy Jackson', 'Director');
107 is($tastes_bad->CoDirector->Name, 'Peter Jackson', 'CoDirector');
109 $tastes_bad->_CoDirector_accessor,
111 'CoDirector_accessor'
115 sub fail_with_bad_object {
116 my ($dir, $codir) = @_;
120 Title => 'Tastes Bad',
122 CoDirector => $codir,
124 NumExplodingSheep => 23
133 __PACKAGE__->table('foo');
134 __PACKAGE__->columns('All' => qw/ id fav /);
136 __PACKAGE__->db_Main->do( qq{
146 __PACKAGE__->table('bar');
147 __PACKAGE__->columns('All' => qw/ id fav /);
149 __PACKAGE__->db_Main->do( qq{
157 Foo->has_a("fav" => "Film");
158 Bar->has_a("fav" => "Foo");
159 my $foo = Foo->create({ id => 6, fav => 'Bad Taste' });
160 my $bar = Bar->create({ id => 2, fav => 6 });
161 isa_ok($bar->fav, "Foo");
162 isa_ok($foo->fav, "Film");
166 Foo->add_trigger(after_create => sub { $foo = shift->fav });
167 my $gwh = Foo->create({ id => 93, fav => 'Good Will Hunting' });
168 isa_ok $foo, "Film", "Object in after_create trigger";