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