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