Add strict/warnings test, adjust all offenders (wow, that was a lot)
[dbsrgits/DBIx-Class.git] / t / cdbi / 15-accessor.t
CommitLineData
9bc6db13 1use strict;
4a233f30 2use warnings;
9bc6db13 3use Test::More;
4
9bc6db13 5INIT {
d4519f23 6 #local $SIG{__WARN__} =
7 #sub { like $_[0], qr/clashes with built-in method/, $_[0] };
50891152 8 use lib 't/cdbi/testlib';
d4519f23 9 require Film;
10 require Actor;
48cb8be4 11 require Director;
12
d4519f23 13 Actor->has_a(film => 'Film');
48cb8be4 14 Film->has_a(director => 'Director');
15
d4519f23 16 sub Class::DBI::sheep { ok 0; }
9bc6db13 17}
18
9381840d 19# Install the deprecation warning intercept here for the rest of the 08 dev cycle
20local $SIG{__WARN__} = sub {
21 warn @_ unless (DBIx::Class->VERSION < 0.09 and $_[0] =~ /Query returned more than one row/);
22};
23
9bc6db13 24sub Film::mutator_name {
d4519f23 25 my ($class, $col) = @_;
26 return "set_sheep" if lc $col eq "numexplodingsheep";
27 return $col;
9bc6db13 28}
29
30sub Film::accessor_name {
d4519f23 31 my ($class, $col) = @_;
32 return "sheep" if lc $col eq "numexplodingsheep";
33 return $col;
9bc6db13 34}
35
e60dc79f 36sub Actor::accessor_name_for {
d4519f23 37 my ($class, $col) = @_;
38 return "movie" if lc $col eq "film";
39 return $col;
9bc6db13 40}
41
5e85c671 42# This is a class with accessor_name_for() but no corresponding mutator_name_for()
e60dc79f 43sub Director::accessor_name_for {
44 my($class, $col) = @_;
45 return "nutty_as_a_fruitcake" if lc $col eq "isinsane";
46 return $col;
47}
48
9bc6db13 49my $data = {
d4519f23 50 Title => 'Bad Taste',
51 Director => 'Peter Jackson',
52 Rating => 'R',
9bc6db13 53};
54
55eval {
c73f755d 56 my $data = { %$data };
d4519f23 57 $data->{NumExplodingSheep} = 1;
58 ok my $bt = Film->create($data), "Modified accessor - with column name";
59 isa_ok $bt, "Film";
c73f755d 60 is $bt->sheep, 1, 'sheep bursting violently';
9bc6db13 61};
62is $@, '', "No errors";
63
64eval {
c73f755d 65 my $data = { %$data };
66 $data->{sheep} = 2;
d4519f23 67 ok my $bt = Film->create($data), "Modified accessor - with accessor";
68 isa_ok $bt, "Film";
c73f755d 69 is $bt->sheep, 2, 'sheep bursting violently';
70};
71is $@, '', "No errors";
72
73eval {
74 my $data = { %$data };
75 $data->{NumExplodingSheep} = 1;
76 ok my $bt = Film->find_or_create($data),
6a3bf251 77 "find_or_create Modified accessor - find with column name";
c73f755d 78 isa_ok $bt, "Film";
79 is $bt->sheep, 1, 'sheep bursting violently';
80};
81is $@, '', "No errors";
82
83eval {
84 my $data = { %$data };
85 $data->{sheep} = 1;
86 ok my $bt = Film->find_or_create($data),
6a3bf251 87 "find_or_create Modified accessor - find with accessor";
c73f755d 88 isa_ok $bt, "Film";
89 is $bt->sheep, 1, 'sheep bursting violently';
90};
91is $@, '', "No errors";
92
93eval {
94 my $data = { %$data };
95 $data->{NumExplodingSheep} = 3;
96 ok my $bt = Film->find_or_create($data),
6a3bf251 97 "find_or_create Modified accessor - create with column name";
c73f755d 98 isa_ok $bt, "Film";
9fd5c112 99
100 local $TODO = 'TODOifying failing tests, waiting for Schwern';
c73f755d 101 is $bt->sheep, 3, 'sheep bursting violently';
102};
103is $@, '', "No errors";
104
105eval {
106 my $data = { %$data };
107 $data->{sheep} = 4;
108 ok my $bt = Film->find_or_create($data),
6a3bf251 109 "find_or_create Modified accessor - create with accessor";
c73f755d 110 isa_ok $bt, "Film";
9fd5c112 111
112 local $TODO = 'TODOifying failing tests, waiting for Schwern';
c73f755d 113 is $bt->sheep, 4, 'sheep bursting violently';
9bc6db13 114};
115is $@, '', "No errors";
116
117eval {
d4519f23 118 my @film = Film->search({ sheep => 1 });
119 is @film, 2, "Can search with modified accessor";
9bc6db13 120};
9fd5c112 121{
122 local $TODO = 'TODOifying failing tests, waiting for Schwern';
123 is $@, '', "No errors";
1fecf835 124}
125
9bc6db13 126{
127
d4519f23 128 eval {
129 local $data->{set_sheep} = 1;
130 ok my $bt = Film->create($data), "Modified mutator - with mutator";
131 isa_ok $bt, "Film";
132 };
133 is $@, '', "No errors";
134
135 eval {
136 local $data->{NumExplodingSheep} = 1;
137 ok my $bt = Film->create($data), "Modified mutator - with column name";
138 isa_ok $bt, "Film";
139 };
140 is $@, '', "No errors";
141
142 eval {
143 local $data->{sheep} = 1;
144 ok my $bt = Film->create($data), "Modified mutator - with accessor";
145 isa_ok $bt, "Film";
146 };
147 is $@, '', "No errors";
9bc6db13 148
149}
150
151{
d4519f23 152 my $p_data = {
153 name => 'Peter Jackson',
154 film => 'Bad Taste',
155 };
156 my $bt = Film->create($data);
157 my $ac = Actor->create($p_data);
158
48cb8be4 159 ok !eval { my $f = $ac->film; 1 };
d4519f23 160 like $@, qr/film/, "no hasa film";
161
162 eval {
163 ok my $f = $ac->movie, "hasa movie";
164 isa_ok $f, "Film";
165 is $f->id, $bt->id, " - Bad Taste";
166 };
167 is $@, '', "No errors";
168
169 {
170 local $data->{Title} = "Another film";
171 my $film = Film->create($data);
172
173 eval { $ac->film($film) };
174 ok $@, $@;
175
176 eval { $ac->movie($film) };
177 ok $@, $@;
178
179 eval {
180 ok $ac->set_film($film), "Set movie through hasa";
181 $ac->update;
182 ok my $f = $ac->movie, "hasa movie";
183 isa_ok $f, "Film";
184 is $f->id, $film->id, " - Another Film";
185 };
186 is $@, '', "No problem";
187 }
9bc6db13 188
189}
190
e60dc79f 191
192# Make sure a class with an accessor_name() method has a similar mutator.
193{
194 my $aki = Director->create({
195 name => "Aki Kaurismaki",
196 });
197
198 $aki->nutty_as_a_fruitcake(1);
199 is $aki->nutty_as_a_fruitcake, 1,
200 "a custom accessor without a custom mutator is setable";
201 $aki->update;
202}
203
204{
d4519f23 205 Film->columns(TEMP => qw/nonpersistent/);
206 ok(Film->find_column('nonpersistent'), "nonpersistent is a column");
207 ok(!Film->has_real_column('nonpersistent'), " - but it's not real");
208
209 {
210 my $film = Film->create({ Title => "Veronique", nonpersistent => 42 });
211 is $film->title, "Veronique", "Title set OK";
212 is $film->nonpersistent, 42, "As is non persistent value";
213 $film->remove_from_object_index;
214 ok $film = Film->retrieve('Veronique'), "Re-retrieve film";
215 is $film->title, "Veronique", "Title still OK";
216 is $film->nonpersistent, undef, "Non persistent value gone";
217 ok $film->nonpersistent(40), "Can set it";
218 is $film->nonpersistent, 40, "And it's there again";
219 ok $film->update, "Commit the film";
220 is $film->nonpersistent, 40, "And it's still there";
221 }
9bc6db13 222}
223
e60dc79f 224{
d4519f23 225 is_deeply(
226 [Actor->columns('Essential')],
227 [Actor->columns('Primary')],
228 "Actor has no specific essential columns"
229 );
230 ok(Actor->find_column('nonpersistent'), "nonpersistent is a column");
231 ok(!Actor->has_real_column('nonpersistent'), " - but it's not real");
232 my $pj = eval { Actor->search(name => "Peter Jackson")->first };
233 is $@, '', "no problems retrieving actors";
234 isa_ok $pj => "Actor";
9bc6db13 235}
236
e60dc79f 237{
d4519f23 238 Film->autoupdate(1);
239 my $naked = Film->create({ title => 'Naked' });
240 my $sandl = Film->create({ title => 'Secrets and Lies' });
241
242 my $rating = 1;
243 my $update_failure = sub {
244 my $obj = shift;
245 eval { $obj->rating($rating++) };
246 return $@ =~ /read only/;
247 };
248
249 ok !$update_failure->($naked), "Can update Naked";
250 ok $naked->make_read_only, "Make Naked read only";
251 ok $update_failure->($naked), "Can't update Naked any more";
252 ok !$update_failure->($sandl), "But can still update Secrets and Lies";
253 my $july4 = eval { Film->create({ title => "4 Days in July" }) };
254 isa_ok $july4 => "Film", "And can still create new films";
255
256 ok(Film->make_read_only, "Make all Films read only");
257 ok $update_failure->($naked), "Still can't update Naked";
258 ok $update_failure->($sandl), "And can't update S&L any more";
259 eval { $july4->delete };
260 like $@, qr/read only/, "And can't delete 4 Days in July";
261 my $abigail = eval { Film->create({ title => "Abigail's Party" }) };
262 like $@, qr/read only/, "Or create new films";
48cb8be4 263
9381840d 264 $_->discard_changes for ($naked, $sandl);
9bc6db13 265}
d9bd5195 266
267done_testing;