Simplify guarded pass-through added to CDBI in ee333775
[dbsrgits/DBIx-Class.git] / t / cdbi / 09-has_many.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
83eef562 2use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
3
b8e1e21f 4use strict;
4a233f30 5use warnings;
83eef562 6
b8e1e21f 7use Test::More;
8
50891152 9use lib 't/cdbi/testlib';
b8e1e21f 10use Film;
11use Actor;
b8e1e21f 12Actor->has_a(Film => 'Film');
3bec1f52 13Film->has_many(actors => 'Actor', { order_by => 'name' });
b8e1e21f 14is(Actor->primary_column, 'id', "Actor primary OK");
15
16ok(Actor->can('Salary'), "Actor table set-up OK");
17ok(Film->can('actors'), " and have a suitable method in Film");
18
19Film->create_test_film;
20
21ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
22
23ok(
6a3bf251 24 my $pvj = Actor->create(
25 {
26 Name => 'Peter Vere-Jones',
27 Film => undef,
28 Salary => '30_000', # For a voice!
29 }
30 ),
31 'create Actor'
b8e1e21f 32);
33is $pvj->Name, "Peter Vere-Jones", "PVJ name ok";
34is $pvj->Film, undef, "No film";
35ok $pvj->set_Film($btaste), "Set film";
36$pvj->update;
37is $pvj->Film->id, $btaste->id, "Now film";
38{
6a3bf251 39 my @actors = $btaste->actors;
40 is(@actors, 1, "Bad taste has one actor");
41 is($actors[0]->Name, $pvj->Name, " - the correct one");
b8e1e21f 42}
43
44my %pj_data = (
6a3bf251 45 Name => 'Peter Jackson',
46 Salary => '0', # it's a labour of love
b8e1e21f 47);
48
49eval { my $pj = Film->add_to_actors(\%pj_data) };
50like $@, qr/class/, "add_to_actors must be object method";
51
52eval { my $pj = $btaste->add_to_actors(%pj_data) };
eab44f52 53like $@, qr/Result object instantiation requires a hashref as argument/, "add_to_actors takes hash";
b8e1e21f 54
55ok(
6a3bf251 56 my $pj = $btaste->add_to_actors(
57 {
58 Name => 'Peter Jackson',
59 Salary => '0', # it's a labour of love
60 }
61 ),
62 'add_to_actors'
b8e1e21f 63);
64is $pj->Name, "Peter Jackson", "PJ ok";
65is $pvj->Name, "Peter Vere-Jones", "PVJ still ok";
66
67{
6a3bf251 68 my @actors = $btaste->actors;
69 is @actors, 2, " - so now we have 2";
70 is $actors[0]->Name, $pj->Name, "PJ first";
71 is $actors[1]->Name, $pvj->Name, "PVJ first";
b8e1e21f 72}
73
74eval {
6a3bf251 75 my @actors = $btaste->actors(Name => $pj->Name);
76 is @actors, 1, "One actor from restricted (sorted) has_many";
77 is $actors[0]->Name, $pj->Name, "It's PJ";
b8e1e21f 78};
79is $@, '', "No errors";
80
81my $as = Actor->create(
6a3bf251 82 {
83 Name => 'Arnold Schwarzenegger',
84 Film => 'Terminator 2',
85 Salary => '15_000_000'
86 }
b8e1e21f 87);
88
89eval { $btaste->actors($pj, $pvj, $as) };
90ok $@, $@;
91is($btaste->actors, 2, " - so we still only have 2 actors");
92
93my @bta_before = Actor->search(Film => 'Bad Taste');
94is(@bta_before, 2, "We have 2 actors in bad taste");
95ok($btaste->delete, "Delete bad taste");
96my @bta_after = Actor->search(Film => 'Bad Taste');
97is(@bta_after, 0, " - after deleting there are no actors");
98
99# While we're here, make sure Actors have unreadable mutators and
100# unwritable accessors
101
102eval { $as->Name("Paul Reubens") };
103ok $@, $@;
104eval { my $name = $as->set_Name };
105ok $@, $@;
106
107is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie");
108
3bec1f52 109
110# Test infering of the foreign key of a has_many from an existing has_a
111{
112 use Thing;
113 use OtherThing;
114
115 Thing->has_a(that_thing => "OtherThing");
116 OtherThing->has_many(things => "Thing");
117
118 my $other_thing = OtherThing->create({ id => 1 });
119 Thing->create({ id => 1, that_thing => $other_thing });
120 Thing->create({ id => 2, that_thing => $other_thing });
121
122 is_deeply [sort map { $_->id } $other_thing->things], [1,2];
123}
d9bd5195 124
125done_testing;