4 use lib 't/cdbi/testlib';
7 Actor->has_a(Film => 'Film');
8 Film->has_many(actors => 'Actor', { order_by => 'name' });
9 is(Actor->primary_column, 'id', "Actor primary OK");
11 ok(Actor->can('Salary'), "Actor table set-up OK");
12 ok(Film->can('actors'), " and have a suitable method in Film");
14 Film->create_test_film;
16 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
19 my $pvj = Actor->create(
21 Name => 'Peter Vere-Jones',
23 Salary => '30_000', # For a voice!
28 is $pvj->Name, "Peter Vere-Jones", "PVJ name ok";
29 is $pvj->Film, undef, "No film";
30 ok $pvj->set_Film($btaste), "Set film";
32 is $pvj->Film->id, $btaste->id, "Now film";
34 my @actors = $btaste->actors;
35 is(@actors, 1, "Bad taste has one actor");
36 is($actors[0]->Name, $pvj->Name, " - the correct one");
40 Name => 'Peter Jackson',
41 Salary => '0', # it's a labour of love
44 eval { my $pj = Film->add_to_actors(\%pj_data) };
45 like $@, qr/class/, "add_to_actors must be object method";
47 eval { my $pj = $btaste->add_to_actors(%pj_data) };
48 like $@, qr/needs/, "add_to_actors takes hash";
51 my $pj = $btaste->add_to_actors(
53 Name => 'Peter Jackson',
54 Salary => '0', # it's a labour of love
59 is $pj->Name, "Peter Jackson", "PJ ok";
60 is $pvj->Name, "Peter Vere-Jones", "PVJ still ok";
63 my @actors = $btaste->actors;
64 is @actors, 2, " - so now we have 2";
65 is $actors[0]->Name, $pj->Name, "PJ first";
66 is $actors[1]->Name, $pvj->Name, "PVJ first";
70 my @actors = $btaste->actors(Name => $pj->Name);
71 is @actors, 1, "One actor from restricted (sorted) has_many";
72 is $actors[0]->Name, $pj->Name, "It's PJ";
74 is $@, '', "No errors";
76 my $as = Actor->create(
78 Name => 'Arnold Schwarzenegger',
79 Film => 'Terminator 2',
80 Salary => '15_000_000'
84 eval { $btaste->actors($pj, $pvj, $as) };
86 is($btaste->actors, 2, " - so we still only have 2 actors");
88 my @bta_before = Actor->search(Film => 'Bad Taste');
89 is(@bta_before, 2, "We have 2 actors in bad taste");
90 ok($btaste->delete, "Delete bad taste");
91 my @bta_after = Actor->search(Film => 'Bad Taste');
92 is(@bta_after, 0, " - after deleting there are no actors");
94 # While we're here, make sure Actors have unreadable mutators and
95 # unwritable accessors
97 eval { $as->Name("Paul Reubens") };
99 eval { my $name = $as->set_Name };
102 is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie");
105 # Test infering of the foreign key of a has_many from an existing has_a
110 Thing->has_a(that_thing => "OtherThing");
111 OtherThing->has_many(things => "Thing");
113 my $other_thing = OtherThing->create({ id => 1 });
114 Thing->create({ id => 1, that_thing => $other_thing });
115 Thing->create({ id => 2, that_thing => $other_thing });
117 is_deeply [sort map { $_->id } $other_thing->things], [1,2];