6 eval "use DBIx::Class::CDBICompat;";
7 plan skip_all => 'Class::Trigger and DBIx::ContextualFetch required' if $@;
12 use lib 't/cdbi/testlib';
15 Actor->has_a(Film => 'Film');
16 Film->has_many(actors => 'Actor', { order_by => 'name' });
17 is(Actor->primary_column, 'id', "Actor primary OK");
19 ok(Actor->can('Salary'), "Actor table set-up OK");
20 ok(Film->can('actors'), " and have a suitable method in Film");
22 Film->create_test_film;
24 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
27 my $pvj = Actor->create(
29 Name => 'Peter Vere-Jones',
31 Salary => '30_000', # For a voice!
36 is $pvj->Name, "Peter Vere-Jones", "PVJ name ok";
37 is $pvj->Film, undef, "No film";
38 ok $pvj->set_Film($btaste), "Set film";
40 is $pvj->Film->id, $btaste->id, "Now film";
42 my @actors = $btaste->actors;
43 is(@actors, 1, "Bad taste has one actor");
44 is($actors[0]->Name, $pvj->Name, " - the correct one");
48 Name => 'Peter Jackson',
49 Salary => '0', # it's a labour of love
52 eval { my $pj = Film->add_to_actors(\%pj_data) };
53 like $@, qr/class/, "add_to_actors must be object method";
55 eval { my $pj = $btaste->add_to_actors(%pj_data) };
56 like $@, qr/needs/, "add_to_actors takes hash";
59 my $pj = $btaste->add_to_actors(
61 Name => 'Peter Jackson',
62 Salary => '0', # it's a labour of love
67 is $pj->Name, "Peter Jackson", "PJ ok";
68 is $pvj->Name, "Peter Vere-Jones", "PVJ still ok";
71 my @actors = $btaste->actors;
72 is @actors, 2, " - so now we have 2";
73 is $actors[0]->Name, $pj->Name, "PJ first";
74 is $actors[1]->Name, $pvj->Name, "PVJ first";
78 my @actors = $btaste->actors(Name => $pj->Name);
79 is @actors, 1, "One actor from restricted (sorted) has_many";
80 is $actors[0]->Name, $pj->Name, "It's PJ";
82 is $@, '', "No errors";
84 my $as = Actor->create(
86 Name => 'Arnold Schwarzenegger',
87 Film => 'Terminator 2',
88 Salary => '15_000_000'
92 eval { $btaste->actors($pj, $pvj, $as) };
94 is($btaste->actors, 2, " - so we still only have 2 actors");
96 my @bta_before = Actor->search(Film => 'Bad Taste');
97 is(@bta_before, 2, "We have 2 actors in bad taste");
98 ok($btaste->delete, "Delete bad taste");
99 my @bta_after = Actor->search(Film => 'Bad Taste');
100 is(@bta_after, 0, " - after deleting there are no actors");
102 # While we're here, make sure Actors have unreadable mutators and
103 # unwritable accessors
105 eval { $as->Name("Paul Reubens") };
107 eval { my $name = $as->set_Name };
110 is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie");
113 # Test infering of the foreign key of a has_many from an existing has_a
118 Thing->has_a(that_thing => "OtherThing");
119 OtherThing->has_many(things => "Thing");
121 my $other_thing = OtherThing->create({ id => 1 });
122 Thing->create({ id => 1, that_thing => $other_thing });
123 Thing->create({ id => 2, that_thing => $other_thing });
125 is_deeply [sort map { $_->id } $other_thing->things], [1,2];