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