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