6 eval "use DBIx::Class::CDBICompat;";
8 plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required');
15 use lib 't/cdbi/testlib';
19 ok(Film->can('db_Main'), 'set_db()');
20 is(Film->__driver, "SQLite", "Driver set correctly");
23 my $nul = eval { Film->retrieve() };
24 is $nul, undef, "Can't retrieve nothing";
25 like $@, qr/./, "retrieve needs parameters"; # TODO fix this...
29 eval { my $id = Film->id };
30 like $@, qr/class method/, "Can't get id with no object";
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";
39 eval { my $duh = Film->insert; };
40 like $@, qr/create needs a hashref/, "needs a hashref";
42 ok +Film->create_test_film;
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');
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";
58 ok my $gone = Film->find_or_create(
60 Title => 'Gone With The Wind',
61 Director => 'Bob Baggadonuts',
63 NumExplodingSheep => 0
66 "Add Gone With The Wind";
68 ok $gone = Film->retrieve(Title => 'Gone With The Wind'),
69 "Fetch it back again";
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');
78 is($gone->Rating, 'PG', 'Rating() get again');
79 $gone->Rating('NC-17');
80 is($gone->Rating, 'NC-17', 'Rating() set');
84 my @films = eval { Film->retrieve_all };
85 cmp_ok(@films, '==', 2, "We have 2 films in total");
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");
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');
95 # Grab the 'Bladerunner' entry.
98 Title => 'Bladerunner',
99 Director => 'Bob Ridley Scott',
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";
111 # Make a copy of 'Bladerunner' and create an entry of the directors cut
112 my $blrunner_dc = $blrunner->copy(
114 title => "Bladerunner: Director's Cut",
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');
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{
135 my @films = Film->title_asc("Bladerunner%");
136 is @films, 2, "We have 2 Bladerunners";
137 is $films[0]->Title, $blrunner->Title, "Ordered correctly";
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";
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";
151 # Multi-column search
153 my @films = $blrunner->search (title => { -like => "Bladerunner%"}, rating => '15');
154 is @films, 1, "Only one Bladerunner is a 15";
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";
165 # Inline SQL removes WHERE
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";
175 my $ishtar = Film->insert({ Title => 'Ishtar', Director => 'Elaine May' });
177 Film->insert({ Title => 'Mikey and Nicky', Director => 'Elaine May' });
179 Film->insert({ Title => 'A New Leaf', Director => 'Elaine May' });
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');
189 local $SIG{__WARN__} = sub { $deprecated++ if $_[0] =~ /deprecated/ };
191 Film->delete(Director => 'Elaine May'),
192 "In fact, delete all films by Elaine May"
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";
199 is $@, '', "No problems with deletes";
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');
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');
211 [ sort map { $_->id } @films ],
212 [ sort map { $_->id } $blrunner_dc, $gone, $blrunner ]
217 # Find Ridley Scott films which don't have vomit
219 Film->search(numExplodingSheep => undef, Director => 'Bob Ridley Scott');
220 is(scalar @films, 2, ' search where attribute is null returns 2 films');
223 [ sort map { $_->id } @films ],
224 [ sort map { $_->id } $blrunner_dc, $blrunner ]
229 # Test that a disconnect doesnt harm anything.
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/;
238 Film->db_Main->disconnect;
239 @films = Film->search({ Rating => 'NC-17' });
240 ok(@films == 1 && $films[0]->id eq $gone->id, 'auto reconnection');
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()');
251 skip "ActiveState perl produces additional warnings", 3
252 if ($^O eq 'MSWin32');
255 my $btaste2 = Film->retrieve($btaste->id);
256 $btaste->NumExplodingSheep(18);
258 local $SIG{__WARN__} = sub { push(@warnings, @_); };
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);
269 is @warnings, 1, "DESTROY without update warns";
273 { # update unchanged object
274 my $film = Film->retrieve($btaste->id);
275 my $retval = $film->update;
276 is $retval, -1, "Unchanged object";
279 { # update deleted object
280 my $rt = "Royal Tenenbaums";
281 my $ten = Film->insert({ title => $rt, Rating => "R" });
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;
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);
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";
309 # Change after_update policy
311 skip "DBIx::Class compat doesn't handle the exists stuff quite right yet", 4;
312 my $bt = Film->retrieve($btaste->id);
316 ok !$bt->_attribute_exists('rating'), "changed column needs reloaded";
317 ok $bt->_attribute_exists('title'), "but we still have the title";
321 after_update => sub {
322 my ($self, %args) = @_;
323 my $discard_columns = $args{discard_columns};
324 @$discard_columns = qw/title/;
328 ok $bt->_attribute_exists('rating'), "changed column needs reloaded";
329 ok !$bt->_attribute_exists('title'), "but no longer have the title";
332 # Make sure that we can have other accessors. (Bugfix in 0.28)
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");
344 is "$blrunner", "Bladerunner", "stringify";
346 ok(Film->columns(Stringify => 'rating'), "Can change stringify column");
347 is "$blrunner", "R", "And still stringifies correctly";
350 Film->columns(Stringify => qw/title rating/),
351 "Can have multiple stringify columns"
353 is "$blrunner", "Bladerunner/R", "And still stringifies correctly";
356 local *Film::stringify_self = sub { join ":", $_[0]->title, $_[0]->rating };
357 is "$blrunner", "Bladerunner:R", "Provide stringify_self()";
362 ok my $byebye = DeletingFilm->insert(
364 Title => 'Goodbye Norma Jean',
368 "Add a deleting Film";
370 isa_ok $byebye, 'DeletingFilm';
371 isa_ok $byebye, 'Film';
372 ok(Film->retrieve('Goodbye Norma Jean'), "Fetch it back again");
375 eval { $film = Film->retrieve('Goodbye Norma Jean') };
376 ok !$film, "It destroys itself";
380 skip "Caching has been removed", 5
381 if Film->isa("DBIx::Class::CDBICompat::NoObjectIndex");
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";
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";
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";
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";
403 $btaste=Film->insert({
404 Title => 'Bad Taste 2',
405 Director => 'Peter Jackson',
407 NumExplodingSheep => 2,
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";