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