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