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