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