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