Debian Codegraph @

t/cdbi-t-ocache/09-has_many.t

raw
1 use strict;
2 use Test::More;
3 use Class::DBI::Sweet;
4 Class::DBI::Sweet->default_search_attributes({ use_resultset_cache => 0 });
5 Class::DBI::Sweet->cache(Cache::MemoryCache->new(
6 { namespace => "SweetTest", default_expires_in => 60 } ) );
7
8 BEGIN {
9 eval "use Cache::MemoryCache";
10 plan skip_all => "needs Cache::Cache for testing" if $@;
11 eval "use DBD::SQLite";
12 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 42);
13 }
14
15 use lib 't/cdbi-t/testlib';
16 use Film;
17 use Actor;
18 use Director;
19
20 Film->has_many(actors => Actor => 'Film', { order_by => 'name' });
21 Director->has_many(films => Film => 'Director', { order_by => 'title' });
22 Director->has_many(
23 r_rated_films =>
24 Film => 'Director',
25 { order_by => 'title', constraint => { Rating => 'R' } }
26 );
27
28 Actor->has_a(Film => 'Film');
29 is(Actor->primary_column, 'id', "Actor primary OK");
30
31 ok(Actor->can('Salary'), "Actor table set-up OK");
32 ok(Film->can('actors'), " and have a suitable method in Film");
33
34 Film->create_test_film;
35 ok(my $btaste = Film->retrieve('Bad Taste'), "We have Bad Taste");
36
37 ok(
38 my $pvj = Actor->insert(
39 {
40 Name => 'Peter Vere-Jones',
41 Film => undef,
42 Salary => '30_000', # For a voice!
43 }
44 ),
45 'insert Actor'
46 );
47 is $pvj->Name, "Peter Vere-Jones", "PVJ name ok";
48 is $pvj->Film, undef, "No film";
49 ok $pvj->set_Film($btaste), "Set film";
50 $pvj->update;
51 is $pvj->Film->id, $btaste->id, "Now film";
52 {
53 my @actors = $btaste->actors;
54 is(@actors, 1, "Bad taste has one actor");
55 is($actors[0]->Name, $pvj->Name, " - the correct one");
56 }
57
58 my %pj_data = (
59 Name => 'Peter Jackson',
60 Salary => '0', # it's a labour of love
61 );
62
63 eval { my $pj = Film->add_to_actors(\%pj_data) };
64 like $@, qr/class/, "add_to_actors must be object method";
65
66 eval { my $pj = $btaste->add_to_actors(%pj_data) };
67 like $@, qr/needs/, "add_to_actors takes hash";
68
69 ok(
70 my $pj = $btaste->add_to_actors(
71 {
72 Name => 'Peter Jackson',
73 Salary => '0', # it's a labour of love
74 }
75 ),
76 'add_to_actors'
77 );
78 is $pj->Name, "Peter Jackson", "PJ ok";
79 is $pvj->Name, "Peter Vere-Jones", "PVJ still ok";
80
81 {
82 my @actors = $btaste->actors;
83 is @actors, 2, " - so now we have 2";
84 is $actors[0]->Name, $pj->Name, "PJ first";
85 is $actors[1]->Name, $pvj->Name, "PVJ first";
86 }
87
88 eval {
89 my @actors = $btaste->actors({Name => $pj->Name});
90 is @actors, 1, "One actor from restricted (sorted) has_many";
91 is $actors[0]->Name, $pj->Name, "It's PJ";
92 };
93 is $@, '', "No errors";
94
95 my $as = Actor->insert(
96 {
97 Name => 'Arnold Schwarzenegger',
98 Film => 'Terminator 2',
99 Salary => '15_000_000'
100 }
101 );
102
103 eval { $btaste->actors($pj, $pvj, $as) };
104 ok $@, $@;
105 is($btaste->actors, 2, " - so we still only have 2 actors");
106
107 my @bta_before = Actor->search(Film => 'Bad Taste');
108 is(@bta_before, 2, "We have 2 actors in bad taste");
109 ok($btaste->delete, "Delete bad taste");
110 my @bta_after = Actor->search(Film => 'Bad Taste');
111 is(@bta_after, 0, " - after deleting there are no actors");
112
113 # While we're here, make sure Actors have unreadable mutators and
114 # unwritable accessors
115
116 eval { $as->Name("Paul Reubens") };
117 ok $@, $@;
118 eval { my $name = $as->set_Name };
119 ok $@, $@;
120
121 is($as->Name, 'Arnold Schwarzenegger', "Arnie's still Arnie");
122
123 ok(my $director = Director->insert({ Name => 'Director 1', }),
124 'insert Director');
125
126 ok(
127 $director->add_to_films(
128 {
129 Title => 'Film 1',
130 Director => 'Director 1',
131 Rating => 'PG',
132 }
133 ),
134 'add_to_films'
135 );
136
137 ok(
138 $director->add_to_r_rated_films(
139 {
140 Title => 'Film 2',
141 Director => 'Director 1',
142 }
143 ),
144 'add_to_r_rated_films'
145 );
146
147 eval {
148 $director->add_to_r_rated_films(
149 {
150 Title => 'Film 3',
151 Director => 'Director 1',
152 Rating => 'G',
153 }
154 );
155 };
156 ok $@, $@;
157
158 {
159 my @films = $director->films;
160 is(@films, 2, "Director 1 has three films");
161 is $films[0]->Title, "Film 1", "Film 1";
162 is $films[0]->Rating, "PG", "is PG";
163 is $films[1]->Title, "Film 2", "Film 2";
164 is $films[1]->Rating, "R", "is R";
165 }
166
167 {
168 my @films = $director->r_rated_films;
169 is @films, 1, "... but only 1 R-Rated";
170 is $films[0]->Title, "Film 2", "- Film 2";
171 }
172
173 # Subclass can override has_many
174
175 package Film::Subclass;
176
177 use base 'Film';
178
179 eval {
180 Film::Subclass->has_many(actors => Actor => 'Film', { order_by => 'id' });
181 };
182
183 package main;
184
185 ok ! $@, "We can set up a has_many subclass";
186
187
188