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