Final round of detabify
[dbsrgits/DBIx-Class.git] / t / cdbi / 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   plan tests => 98;
12 }
13
14 INIT {
15   use lib 't/cdbi/testlib';
16   use Film;
17 }
18
19 ok(Film->can('db_Main'), 'set_db()');
20 is(Film->__driver, "SQLite", "Driver set correctly");
21
22 {
23   my $nul = eval { Film->retrieve() };
24   is $nul, undef, "Can't retrieve nothing";
25   like $@, qr/./, "retrieve needs parameters";    # TODO fix this...
26 }
27
28 {
29   eval { my $id = Film->id };
30   like $@, qr/class method/, "Can't get id with no object";
31 }
32
33 {
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";
37
38
39 eval { my $duh = Film->insert; };
40 like $@, qr/create needs a hashref/, "needs a hashref";
41
42 ok +Film->create_test_film;
43
44 my $btaste = Film->retrieve('Bad Taste');
45 isa_ok $btaste, 'Film';
46 is($btaste->Title,             'Bad Taste',     'Title() get');
47 is($btaste->Director,          'Peter Jackson', 'Director() get');
48 is($btaste->Rating,            'R',             'Rating() get');
49 is($btaste->NumExplodingSheep, 1,               'NumExplodingSheep() get');
50
51 {
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";
56 }
57
58 ok my $gone = Film->find_or_create(
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";
67 isa_ok $gone, 'Film';
68 ok $gone = Film->retrieve(Title => 'Gone With The Wind'),
69   "Fetch it back again";
70 isa_ok $gone, 'Film';
71
72 # Shocking new footage found reveals bizarre Scarlet/sheep scene!
73 is($gone->NumExplodingSheep, 0, 'NumExplodingSheep() get again');
74 $gone->NumExplodingSheep(5);
75 is($gone->NumExplodingSheep, 5, 'NumExplodingSheep() set');
76 is($gone->numexplodingsheep, 5, 'numexplodingsheep() set');
77
78 is($gone->Rating, 'PG', 'Rating() get again');
79 $gone->Rating('NC-17');
80 is($gone->Rating, 'NC-17', 'Rating() set');
81 $gone->update;
82
83 {
84   my @films = eval { Film->retrieve_all };
85   cmp_ok(@films, '==', 2, "We have 2 films in total");
86 }
87
88 # EXTRA TEST: added by mst to check a bug found by Numa
89 cmp_ok(Film->count_all, '==', 2, "count_all confirms 2 films");
90
91 my $gone_copy = Film->retrieve('Gone With The Wind');
92 ok($gone->NumExplodingSheep == 5, 'update()');
93 ok($gone->Rating eq 'NC-17', 'update() again');
94
95 # Grab the 'Bladerunner' entry.
96 Film->create(
97   {
98     Title    => 'Bladerunner',
99     Director => 'Bob Ridley Scott',
100     Rating   => 'R'
101   }
102 );
103
104 my $blrunner = Film->retrieve('Bladerunner');
105 is(ref $blrunner, 'Film', 'retrieve() again');
106 is $blrunner->Title,    'Bladerunner',      "Correct title";
107 is $blrunner->Director, 'Bob Ridley Scott', " and Director";
108 is $blrunner->Rating,   'R',                " and Rating";
109 is $blrunner->NumExplodingSheep, undef, " and sheep";
110
111 # Make a copy of 'Bladerunner' and create an entry of the directors cut
112 my $blrunner_dc = $blrunner->copy(
113   {
114     title  => "Bladerunner: Director's Cut",
115     rating => "15",
116   }
117 );
118 is(ref $blrunner_dc, 'Film', "copy() produces a film");
119 is($blrunner_dc->Title,    "Bladerunner: Director's Cut", 'Title correct');
120 is($blrunner_dc->Director, 'Bob Ridley Scott',            'Director correct');
121 is($blrunner_dc->Rating,   '15',                          'Rating correct');
122 is($blrunner_dc->NumExplodingSheep, undef, 'Sheep correct');
123
124 # Set up own SQL:
125 {
126   Film->add_constructor(title_asc  => "title LIKE ? ORDER BY title");
127   Film->add_constructor(title_desc => "title LIKE ? ORDER BY title DESC");
128     Film->add_constructor(title_asc_nl => q{
129         title LIKE ?
130         ORDER BY title
131         LIMIT 1
132     });
133
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   }
149 }
150
151 # Multi-column search
152 {
153   my @films = $blrunner->search (title => { -like => "Bladerunner%"}, rating => '15');
154   is @films, 1, "Only one Bladerunner is a 15";
155 }
156
157 # Inline SQL
158 {
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";
163 }
164
165 # Inline SQL removes WHERE
166 {
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";
172 }
173
174 eval {
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' });
180
181 #use Data::Dumper; die Dumper(Film->search( Director => 'Elaine May' ));
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   }
198 };
199 is $@, '', "No problems with deletes";
200
201 # Find all films which have a rating of NC-17.
202 my @films = Film->search('Rating', 'NC-17');
203 is(scalar @films, 1, ' search returns one film');
204 is($films[0]->id, $gone->id, ' ... the correct one');
205
206 # Find all films which were directed by Bob
207 @films = Film->search ( { 'Director' => { -like => 'Bob %' } });
208 is(scalar @films, 3, ' search_like returns 3 films');
209 ok(
210   eq_array(
211     [ sort map { $_->id } @films ],
212     [ sort map { $_->id } $blrunner_dc, $gone, $blrunner ]
213   ),
214   'the correct ones'
215 );
216
217 # Find Ridley Scott films which don't have vomit
218 @films =
219   Film->search(numExplodingSheep => undef, Director => 'Bob Ridley Scott');
220 is(scalar @films, 2, ' search where attribute is null returns 2 films');
221 ok(
222   eq_array(
223     [ sort map { $_->id } @films ],
224     [ sort map { $_->id } $blrunner_dc, $blrunner ]
225   ),
226   'the correct ones'
227 );
228
229 # Test that a disconnect doesnt harm anything.
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 }
249
250 SKIP: {
251   skip "ActiveState perl produces additional warnings", 3
252           if ($^O eq 'MSWin32');
253
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);
271 }
272
273 {                               # update unchanged object
274   my $film   = Film->retrieve($btaste->id);
275   my $retval = $film->update;
276   is $retval, -1, "Unchanged object";
277 }
278
279 {                               # update deleted object
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;
290 }
291
292 {
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);
298 }
299
300 # Primary key of 0
301 {
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";
307 }
308
309 # Change after_update policy
310 SKIP: {
311         skip "DBIx::Class compat doesn't handle the exists stuff quite right yet", 4;
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";
330 }
331
332 # Make sure that we can have other accessors. (Bugfix in 0.28)
333 if (0) {
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");
340 }
341
342 # overloading
343 {
344   is "$blrunner", "Bladerunner", "stringify";
345
346   ok(Film->columns(Stringify => 'rating'), "Can change stringify column");
347   is "$blrunner", "R", "And still stringifies correctly";
348
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";
354
355   no warnings 'once';
356   local *Film::stringify_self = sub { join ":", $_[0]->title, $_[0]->rating };
357   is "$blrunner", "Bladerunner:R", "Provide stringify_self()";
358 }
359
360 {
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";
377 }
378
379 SKIP: {
380     skip "Caching has been removed", 5
381         if Film->isa("DBIx::Class::CDBICompat::NoObjectIndex");
382
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";
402  
403   $btaste=Film->insert({
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";
412  
413 }