5 use lib 't/cdbi/testlib';
8 Actor->has_a(Film => 'Film');
9 Film->has_many(actors => 'Actor', { order_by => 'name' });
10 is(Actor->primary_column, 'id', "Actor primary OK");
12 ok(Actor->can('Salary'), "Actor table set-up OK");
13 ok(Film->can('actors'), " and have a suitable method in Film");
15 Film->create_test_film;
17 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
20 my $pvj = Actor->create(
22 Name => 'Peter Vere-Jones',
24 Salary => '30_000', # For a voice!
29 is $pvj->Name, "Peter Vere-Jones", "PVJ name ok";
30 is $pvj->Film, undef, "No film";
31 ok $pvj->set_Film($btaste), "Set film";
33 is $pvj->Film->id, $btaste->id, "Now film";
35 my @actors = $btaste->actors;
36 is(@actors, 1, "Bad taste has one actor");
37 is($actors[0]->Name, $pvj->Name, " - the correct one");
41 Name => 'Peter Jackson',
42 Salary => '0', # it's a labour of love
45 eval { my $pj = Film->add_to_actors(\%pj_data) };
46 like $@, qr/class/, "add_to_actors must be object method";
48 eval { my $pj = $btaste->add_to_actors(%pj_data) };
49 like $@, qr/Result object instantiation requires a hashref as argument/, "add_to_actors takes hash";
52 my $pj = $btaste->add_to_actors(
54 Name => 'Peter Jackson',
55 Salary => '0', # it's a labour of love
60 is $pj->Name, "Peter Jackson", "PJ ok";
61 is $pvj->Name, "Peter Vere-Jones", "PVJ still ok";
64 my @actors = $btaste->actors;
65 is @actors, 2, " - so now we have 2";
66 is $actors[0]->Name, $pj->Name, "PJ first";
67 is $actors[1]->Name, $pvj->Name, "PVJ first";
71 my @actors = $btaste->actors(Name => $pj->Name);
72 is @actors, 1, "One actor from restricted (sorted) has_many";
73 is $actors[0]->Name, $pj->Name, "It's PJ";
75 is $@, '', "No errors";
77 my $as = Actor->create(
79 Name => 'Arnold Schwarzenegger',
80 Film => 'Terminator 2',
81 Salary => '15_000_000'
85 eval { $btaste->actors($pj, $pvj, $as) };
87 is($btaste->actors, 2, " - so we still only have 2 actors");
89 my @bta_before = Actor->search(Film => 'Bad Taste');
90 is(@bta_before, 2, "We have 2 actors in bad taste");
91 ok($btaste->delete, "Delete bad taste");
92 my @bta_after = Actor->search(Film => 'Bad Taste');
93 is(@bta_after, 0, " - after deleting there are no actors");
95 # While we're here, make sure Actors have unreadable mutators and
96 # unwritable accessors
98 eval { $as->Name("Paul Reubens") };
100 eval { my $name = $as->set_Name };
103 is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie");
106 # Test infering of the foreign key of a has_many from an existing has_a
111 Thing->has_a(that_thing => "OtherThing");
112 OtherThing->has_many(things => "Thing");
114 my $other_thing = OtherThing->create({ id => 1 });
115 Thing->create({ id => 1, that_thing => $other_thing });
116 Thing->create({ id => 2, that_thing => $other_thing });
118 is_deeply [sort map { $_->id } $other_thing->things], [1,2];