Commit | Line | Data |
ea2e61bf |
1 | use strict; |
2 | use Test::More; |
3 | $| = 1; |
4 | |
5 | BEGIN { |
289ba852 |
6 | eval "use DBIx::Class::CDBICompat;"; |
7 | if ($@) { |
8 | plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required'); |
9 | next; |
10 | } |
11 | eval "use DBD::SQLite"; |
12 | plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 96); |
ea2e61bf |
13 | } |
14 | |
15 | INIT { |
16 | use lib 't/testlib'; |
17 | use Film; |
18 | } |
19 | |
20 | ok(Film->can('db_Main'), 'set_db()'); |
21 | is(Film->__driver, "SQLite", "Driver set correctly"); |
22 | |
ea2e61bf |
23 | { |
24 | my $nul = eval { Film->retrieve() }; |
25 | is $nul, undef, "Can't retrieve nothing"; |
26 | like $@, qr/./, "retrieve needs parameters"; # TODO fix this... |
27 | } |
28 | |
29 | { |
30 | eval { my $id = Film->id }; |
31 | like $@, qr/class method/, "Can't get id with no object"; |
32 | } |
33 | |
34 | { |
35 | eval { my $id = Film->title }; |
36 | like $@, qr/class method/, "Can't get title with no object"; |
37 | } |
38 | |
39 | eval { my $duh = Film->create; }; |
8fe001e1 |
40 | like $@, qr/create needs a hashref/, "needs a hashref"; |
ea2e61bf |
41 | |
42 | ok +Film->create_test_film; |
43 | |
44 | my $btaste = Film->retrieve('Bad Taste'); |
45 | isa_ok $btaste, 'Film'; |
46 | is($btaste->Title, 'Bad Taste', 'Title() get'); |
47 | is($btaste->Director, 'Peter Jackson', 'Director() get'); |
48 | is($btaste->Rating, 'R', 'Rating() get'); |
49 | is($btaste->NumExplodingSheep, 1, 'NumExplodingSheep() get'); |
50 | |
51 | { |
52 | my $bt2 = Film->find_or_create(Title => 'Bad Taste'); |
53 | is $bt2->Director, $btaste->Director, "find_or_create"; |
54 | my @bt = Film->search(Title => 'Bad Taste'); |
55 | is @bt, 1, " doesn't create a new one"; |
56 | } |
57 | |
58 | ok my $gone = Film->find_or_create( |
59 | { |
60 | Title => 'Gone With The Wind', |
61 | Director => 'Bob Baggadonuts', |
62 | Rating => 'PG', |
63 | NumExplodingSheep => 0 |
64 | } |
65 | ), |
66 | "Add Gone With The Wind"; |
67 | isa_ok $gone, 'Film'; |
68 | ok $gone = Film->retrieve(Title => 'Gone With The Wind'), |
69 | "Fetch it back again"; |
70 | isa_ok $gone, 'Film'; |
71 | |
72 | # Shocking new footage found reveals bizarre Scarlet/sheep scene! |
73 | is($gone->NumExplodingSheep, 0, 'NumExplodingSheep() get again'); |
74 | $gone->NumExplodingSheep(5); |
75 | is($gone->NumExplodingSheep, 5, 'NumExplodingSheep() set'); |
76 | is($gone->numexplodingsheep, 5, 'numexplodingsheep() set'); |
77 | |
78 | is($gone->Rating, 'PG', 'Rating() get again'); |
79 | $gone->Rating('NC-17'); |
80 | is($gone->Rating, 'NC-17', 'Rating() set'); |
81 | $gone->update; |
82 | |
83 | { |
84 | my @films = eval { Film->retrieve_all }; |
33ec7be0 |
85 | cmp_ok(@films, '==', 2, "We have 2 films in total"); |
ea2e61bf |
86 | } |
87 | |
3125eb1f |
88 | # EXTRA TEST: added by mst to check a bug found by Numa |
89 | cmp_ok(Film->count_all, '==', 2, "count_all confirms 2 films"); |
90 | |
ea2e61bf |
91 | my $gone_copy = Film->retrieve('Gone With The Wind'); |
92 | ok($gone->NumExplodingSheep == 5, 'update()'); |
93 | ok($gone->Rating eq 'NC-17', 'update() again'); |
94 | |
95 | # Grab the 'Bladerunner' entry. |
96 | Film->create( |
97 | { |
98 | Title => 'Bladerunner', |
99 | Director => 'Bob Ridley Scott', |
100 | Rating => 'R' |
101 | } |
102 | ); |
103 | |
104 | my $blrunner = Film->retrieve('Bladerunner'); |
105 | is(ref $blrunner, 'Film', 'retrieve() again'); |
106 | is $blrunner->Title, 'Bladerunner', "Correct title"; |
107 | is $blrunner->Director, 'Bob Ridley Scott', " and Director"; |
108 | is $blrunner->Rating, 'R', " and Rating"; |
109 | is $blrunner->NumExplodingSheep, undef, " and sheep"; |
110 | |
111 | # Make a copy of 'Bladerunner' and create an entry of the directors cut |
112 | my $blrunner_dc = $blrunner->copy( |
113 | { |
114 | title => "Bladerunner: Director's Cut", |
115 | rating => "15", |
116 | } |
117 | ); |
118 | is(ref $blrunner_dc, 'Film', "copy() produces a film"); |
119 | is($blrunner_dc->Title, "Bladerunner: Director's Cut", 'Title correct'); |
120 | is($blrunner_dc->Director, 'Bob Ridley Scott', 'Director correct'); |
121 | is($blrunner_dc->Rating, '15', 'Rating correct'); |
122 | is($blrunner_dc->NumExplodingSheep, undef, 'Sheep correct'); |
123 | |
124 | # Set up own SQL: |
125 | { |
126 | Film->add_constructor(title_asc => "title LIKE ? ORDER BY title"); |
127 | Film->add_constructor(title_desc => "title LIKE ? ORDER BY title DESC"); |
128 | |
129 | { |
130 | my @films = Film->title_asc("Bladerunner%"); |
131 | is @films, 2, "We have 2 Bladerunners"; |
132 | is $films[0]->Title, $blrunner->Title, "Ordered correctly"; |
133 | } |
134 | { |
135 | my @films = Film->title_desc("Bladerunner%"); |
136 | is @films, 2, "We have 2 Bladerunners"; |
137 | is $films[0]->Title, $blrunner_dc->Title, "Ordered correctly"; |
138 | } |
139 | } |
140 | |
141 | # Multi-column search |
142 | { |
143 | my @films = $blrunner->search_like(title => "Bladerunner%", rating => '15'); |
144 | is @films, 1, "Only one Bladerunner is a 15"; |
145 | } |
146 | |
147 | # Inline SQL |
148 | { |
149 | my @films = Film->retrieve_from_sql("numexplodingsheep > 0 ORDER BY title"); |
150 | is @films, 2, "Inline SQL"; |
151 | is $films[0]->id, $btaste->id, "Correct film"; |
152 | is $films[1]->id, $gone->id, "Correct film"; |
153 | } |
154 | |
155 | # Inline SQL removes WHERE |
156 | { |
157 | my @films = |
158 | Film->retrieve_from_sql(" WHErE numexplodingsheep > 0 ORDER BY title"); |
159 | is @films, 2, "Inline SQL"; |
160 | is $films[0]->id, $btaste->id, "Correct film"; |
161 | is $films[1]->id, $gone->id, "Correct film"; |
162 | } |
163 | |
164 | eval { |
165 | my $ishtar = Film->create({ Title => 'Ishtar', Director => 'Elaine May' }); |
166 | my $mandn = |
167 | Film->create({ Title => 'Mikey and Nicky', Director => 'Elaine May' }); |
168 | my $new_leaf = |
169 | Film->create({ Title => 'A New Leaf', Director => 'Elaine May' }); |
8b445e33 |
170 | |
171 | #use Data::Dumper; die Dumper(Film->search( Director => 'Elaine May' )); |
a3018bd3 |
172 | cmp_ok(Film->search(Director => 'Elaine May'), '==', 3, |
173 | "3 Films by Elaine May"); |
ea2e61bf |
174 | ok(Film->retrieve('Ishtar')->delete, |
175 | "Ishtar doesn't deserve an entry any more"); |
176 | ok(!Film->retrieve('Ishtar'), 'Ishtar no longer there'); |
177 | { |
178 | my $deprecated = 0; |
a3018bd3 |
179 | #local $SIG{__WARN__} = sub { $deprecated++ if $_[0] =~ /deprecated/ }; |
ea2e61bf |
180 | ok( |
181 | Film->delete(Director => 'Elaine May'), |
182 | "In fact, delete all films by Elaine May" |
183 | ); |
a3018bd3 |
184 | cmp_ok(Film->search(Director => 'Elaine May'), '==', |
ea2e61bf |
185 | 0, "0 Films by Elaine May"); |
a3018bd3 |
186 | SKIP: { |
9bc6db13 |
187 | skip "No deprecated warnings from compat layer", 1; |
a3018bd3 |
188 | is $deprecated, 1, "Got a deprecated warning"; |
189 | } |
ea2e61bf |
190 | } |
191 | }; |
192 | is $@, '', "No problems with deletes"; |
193 | |
194 | # Find all films which have a rating of NC-17. |
195 | my @films = Film->search('Rating', 'NC-17'); |
196 | is(scalar @films, 1, ' search returns one film'); |
197 | is($films[0]->id, $gone->id, ' ... the correct one'); |
198 | |
199 | # Find all films which were directed by Bob |
200 | @films = Film->search_like('Director', 'Bob %'); |
201 | is(scalar @films, 3, ' search_like returns 3 films'); |
202 | ok( |
203 | eq_array( |
204 | [ sort map { $_->id } @films ], |
205 | [ sort map { $_->id } $blrunner_dc, $gone, $blrunner ] |
206 | ), |
207 | 'the correct ones' |
208 | ); |
209 | |
210 | # Find Ridley Scott films which don't have vomit |
211 | @films = |
212 | Film->search(numExplodingSheep => undef, Director => 'Bob Ridley Scott'); |
213 | is(scalar @films, 2, ' search where attribute is null returns 2 films'); |
214 | ok( |
215 | eq_array( |
216 | [ sort map { $_->id } @films ], |
217 | [ sort map { $_->id } $blrunner_dc, $blrunner ] |
218 | ), |
219 | 'the correct ones' |
220 | ); |
221 | |
222 | # Test that a disconnect doesnt harm anything. |
223 | Film->db_Main->disconnect; |
224 | @films = Film->search({ Rating => 'NC-17' }); |
225 | ok(@films == 1 && $films[0]->id eq $gone->id, 'auto reconnection'); |
226 | |
227 | # Test discard_changes(). |
228 | my $orig_director = $btaste->Director; |
229 | $btaste->Director('Lenny Bruce'); |
230 | is($btaste->Director, 'Lenny Bruce', 'set new Director'); |
231 | $btaste->discard_changes; |
232 | is($btaste->Director, $orig_director, 'discard_changes()'); |
233 | |
234 | { |
235 | Film->autoupdate(1); |
236 | my $btaste2 = Film->retrieve($btaste->id); |
237 | $btaste->NumExplodingSheep(18); |
238 | my @warnings; |
aea8af71 |
239 | local $SIG{__WARN__} = sub { push(@warnings, @_); }; |
ea2e61bf |
240 | { |
241 | |
242 | # unhook from live object cache, so next one is not from cache |
243 | $btaste2->remove_from_object_index; |
244 | my $btaste3 = Film->retrieve($btaste->id); |
245 | is $btaste3->NumExplodingSheep, 18, "Class based AutoCommit"; |
246 | $btaste3->autoupdate(0); # obj a/c should override class a/c |
247 | is @warnings, 0, "No warnings so far"; |
248 | $btaste3->NumExplodingSheep(13); |
249 | } |
250 | is @warnings, 1, "DESTROY without update warns"; |
510ca912 |
251 | print join("\n", @warnings); |
ea2e61bf |
252 | Film->autoupdate(0); |
253 | } |
254 | |
255 | { # update unchanged object |
256 | my $film = Film->retrieve($btaste->id); |
257 | my $retval = $film->update; |
258 | is $retval, -1, "Unchanged object"; |
259 | } |
260 | |
261 | { # update deleted object |
262 | my $rt = "Royal Tenenbaums"; |
263 | my $ten = Film->create({ title => $rt, Rating => "R" }); |
264 | $ten->rating(18); |
265 | Film->set_sql(drt => "DELETE FROM __TABLE__ WHERE title = ?"); |
266 | Film->sql_drt->execute($rt); |
267 | my @films = Film->search({ title => $rt }); |
268 | is @films, 0, "RT gone"; |
269 | my $retval = eval { $ten->update }; |
270 | like $@, qr/row not found/, "Update deleted object throws error"; |
271 | $ten->discard_changes; |
272 | } |
273 | |
274 | { |
275 | $btaste->autoupdate(1); |
276 | $btaste->NumExplodingSheep(32); |
277 | my $btaste2 = Film->retrieve($btaste->id); |
278 | is $btaste2->NumExplodingSheep, 32, "Object based AutoCommit"; |
279 | $btaste->autoupdate(0); |
280 | } |
281 | |
282 | # Primary key of 0 |
283 | { |
284 | my $zero = Film->create({ Title => 0, Rating => "U" }); |
285 | ok defined $zero, "Create 0"; |
286 | ok my $ret = Film->retrieve(0), "Retrieve 0"; |
287 | is $ret->Title, 0, "Title OK"; |
288 | is $ret->Rating, "U", "Rating OK"; |
289 | } |
290 | |
291 | # Change after_update policy |
a3018bd3 |
292 | SKIP: { |
12bbb339 |
293 | skip "DBIx::Class compat doesn't handle the exists stuff quite right yet", 4; |
ea2e61bf |
294 | my $bt = Film->retrieve($btaste->id); |
295 | $bt->autoupdate(1); |
296 | |
297 | $bt->rating("17"); |
298 | ok !$bt->_attribute_exists('rating'), "changed column needs reloaded"; |
299 | ok $bt->_attribute_exists('title'), "but we still have the title"; |
300 | |
301 | # Don't re-load |
302 | $bt->add_trigger( |
303 | after_update => sub { |
304 | my ($self, %args) = @_; |
305 | my $discard_columns = $args{discard_columns}; |
306 | @$discard_columns = qw/title/; |
307 | } |
308 | ); |
309 | $bt->rating("19"); |
310 | ok $bt->_attribute_exists('rating'), "changed column needs reloaded"; |
311 | ok !$bt->_attribute_exists('title'), "but no longer have the title"; |
312 | } |
313 | |
314 | # Make sure that we can have other accessors. (Bugfix in 0.28) |
315 | if (0) { |
316 | Film->mk_accessors(qw/temp1 temp2/); |
317 | my $blrunner = Film->retrieve('Bladerunner'); |
318 | $blrunner->temp1("Foo"); |
319 | $blrunner->NumExplodingSheep(2); |
320 | eval { $blrunner->update }; |
321 | ok(!$@, "Other accessors"); |
322 | } |
323 | |
324 | # overloading |
325 | { |
326 | is "$blrunner", "Bladerunner", "stringify"; |
327 | |
328 | ok(Film->columns(Stringify => 'rating'), "Can change stringify column"); |
329 | is "$blrunner", "R", "And still stringifies correctly"; |
330 | |
331 | ok( |
332 | Film->columns(Stringify => qw/title rating/), |
333 | "Can have multiple stringify columns" |
334 | ); |
335 | is "$blrunner", "Bladerunner/R", "And still stringifies correctly"; |
336 | |
337 | no warnings 'once'; |
338 | local *Film::stringify_self = sub { join ":", $_[0]->title, $_[0]->rating }; |
339 | is "$blrunner", "Bladerunner:R", "Provide stringify_self()"; |
340 | } |
341 | |
342 | { |
343 | { |
344 | ok my $byebye = DeletingFilm->create( |
345 | { |
346 | Title => 'Goodbye Norma Jean', |
347 | Rating => 'PG', |
348 | } |
349 | ), |
350 | "Add a deleting Film"; |
351 | |
352 | isa_ok $byebye, 'DeletingFilm'; |
353 | isa_ok $byebye, 'Film'; |
354 | ok(Film->retrieve('Goodbye Norma Jean'), "Fetch it back again"); |
355 | } |
356 | my $film; |
357 | eval { $film = Film->retrieve('Goodbye Norma Jean') }; |
358 | ok !$film, "It destroys itself"; |
359 | } |
360 | |
361 | SKIP: { |
c1d23573 |
362 | #skip "DBIx::Class doesn't yet have a live objects index", 3; |
a3018bd3 |
363 | #skip "Scalar::Util::weaken not available", 3 |
ea2e61bf |
364 | #if !$Class::DBI::Weaken_Is_Available; |
365 | |
366 | # my bad taste is your bad taste |
367 | my $btaste = Film->retrieve('Bad Taste'); |
368 | my $btaste2 = Film->retrieve('Bad Taste'); |
369 | is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2), |
370 | "Retrieving twice gives ref to same object"; |
371 | |
451517e7 |
372 | my ($btaste5) = Film->search(title=>'Bad Taste'); |
373 | is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste5), |
374 | "Searching also gives ref to same object"; |
375 | |
ea2e61bf |
376 | $btaste2->remove_from_object_index; |
377 | my $btaste3 = Film->retrieve('Bad Taste'); |
378 | isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste3), |
379 | "Removing from object_index and retrieving again gives new object"; |
380 | |
381 | $btaste3->clear_object_index; |
382 | my $btaste4 = Film->retrieve('Bad Taste'); |
383 | isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste4), |
384 | "Clearing cache and retrieving again gives new object"; |
451517e7 |
385 | |
386 | $btaste=Film->create({ |
387 | Title => 'Bad Taste 2', |
388 | Director => 'Peter Jackson', |
389 | Rating => 'R', |
390 | NumExplodingSheep => 2, |
391 | }); |
392 | $btaste2 = Film->retrieve('Bad Taste 2'); |
393 | is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2), |
394 | "Creating and retrieving gives ref to same object"; |
395 | |
ea2e61bf |
396 | } |