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