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