6 eval "use DBIx::Class::CDBICompat;";
8 plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required');
11 eval "use DBD::SQLite";
12 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 98);
20 ok(Film->can('db_Main'), 'set_db()');
21 is(Film->__driver, "SQLite", "Driver set correctly");
24 my $nul = eval { Film->retrieve() };
25 is $nul, undef, "Can't retrieve nothing";
26 like $@, qr/./, "retrieve needs parameters"; # TODO fix this...
30 eval { my $id = Film->id };
31 like $@, qr/class method/, "Can't get id with no object";
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";
40 eval { my $duh = Film->insert; };
41 like $@, qr/create needs a hashref/, "needs a hashref";
43 ok +Film->create_test_film;
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');
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";
59 ok my $gone = Film->find_or_create(
61 Title => 'Gone With The Wind',
62 Director => 'Bob Baggadonuts',
64 NumExplodingSheep => 0
67 "Add Gone With The Wind";
69 ok $gone = Film->retrieve(Title => 'Gone With The Wind'),
70 "Fetch it back again";
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');
79 is($gone->Rating, 'PG', 'Rating() get again');
80 $gone->Rating('NC-17');
81 is($gone->Rating, 'NC-17', 'Rating() set');
85 my @films = eval { Film->retrieve_all };
86 cmp_ok(@films, '==', 2, "We have 2 films in total");
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");
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');
96 # Grab the 'Bladerunner' entry.
99 Title => 'Bladerunner',
100 Director => 'Bob Ridley Scott',
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";
112 # Make a copy of 'Bladerunner' and create an entry of the directors cut
113 my $blrunner_dc = $blrunner->copy(
115 title => "Bladerunner: Director's Cut",
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');
127 Film->add_constructor(title_asc => "title LIKE ? ORDER BY title");
128 Film->add_constructor(title_desc => "title LIKE ? ORDER BY title DESC");
129 Film->add_constructor(title_asc_nl => q{
136 my @films = Film->title_asc("Bladerunner%");
137 is @films, 2, "We have 2 Bladerunners";
138 is $films[0]->Title, $blrunner->Title, "Ordered correctly";
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";
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";
152 # Multi-column search
154 my @films = $blrunner->search_like(title => "Bladerunner%", rating => '15');
155 is @films, 1, "Only one Bladerunner is a 15";
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";
166 # Inline SQL removes WHERE
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";
176 my $ishtar = Film->insert({ Title => 'Ishtar', Director => 'Elaine May' });
178 Film->insert({ Title => 'Mikey and Nicky', Director => 'Elaine May' });
180 Film->insert({ Title => 'A New Leaf', Director => 'Elaine May' });
182 #use Data::Dumper; die Dumper(Film->search( Director => 'Elaine May' ));
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');
190 #local $SIG{__WARN__} = sub { $deprecated++ if $_[0] =~ /deprecated/ };
192 Film->delete(Director => 'Elaine May'),
193 "In fact, delete all films by Elaine May"
195 cmp_ok(Film->search(Director => 'Elaine May'), '==',
196 0, "0 Films by Elaine May");
198 skip "No deprecated warnings from compat layer", 1;
199 is $deprecated, 1, "Got a deprecated warning";
203 is $@, '', "No problems with deletes";
205 # Find all films which have a rating of NC-17.
206 my @films = Film->search('Rating', 'NC-17');
207 is(scalar @films, 1, ' search returns one film');
208 is($films[0]->id, $gone->id, ' ... the correct one');
210 # Find all films which were directed by Bob
211 @films = Film->search_like('Director', 'Bob %');
212 is(scalar @films, 3, ' search_like returns 3 films');
215 [ sort map { $_->id } @films ],
216 [ sort map { $_->id } $blrunner_dc, $gone, $blrunner ]
221 # Find Ridley Scott films which don't have vomit
223 Film->search(numExplodingSheep => undef, Director => 'Bob Ridley Scott');
224 is(scalar @films, 2, ' search where attribute is null returns 2 films');
227 [ sort map { $_->id } @films ],
228 [ sort map { $_->id } $blrunner_dc, $blrunner ]
233 # Test that a disconnect doesnt harm anything.
234 Film->db_Main->disconnect;
235 @films = Film->search({ Rating => 'NC-17' });
236 ok(@films == 1 && $films[0]->id eq $gone->id, 'auto reconnection');
238 # Test discard_changes().
239 my $orig_director = $btaste->Director;
240 $btaste->Director('Lenny Bruce');
241 is($btaste->Director, 'Lenny Bruce', 'set new Director');
242 $btaste->discard_changes;
243 is($btaste->Director, $orig_director, 'discard_changes()');
246 skip "ActiveState perl produces additional warnings", 3
247 if ($^O eq 'MSWin32');
250 my $btaste2 = Film->retrieve($btaste->id);
251 $btaste->NumExplodingSheep(18);
253 local $SIG{__WARN__} = sub { push(@warnings, @_); };
256 # unhook from live object cache, so next one is not from cache
257 $btaste2->remove_from_object_index;
258 my $btaste3 = Film->retrieve($btaste->id);
259 is $btaste3->NumExplodingSheep, 18, "Class based AutoCommit";
260 $btaste3->autoupdate(0); # obj a/c should override class a/c
261 is @warnings, 0, "No warnings so far";
262 $btaste3->NumExplodingSheep(13);
264 is @warnings, 1, "DESTROY without update warns";
268 { # update unchanged object
269 my $film = Film->retrieve($btaste->id);
270 my $retval = $film->update;
271 is $retval, -1, "Unchanged object";
274 { # update deleted object
275 my $rt = "Royal Tenenbaums";
276 my $ten = Film->insert({ title => $rt, Rating => "R" });
278 Film->set_sql(drt => "DELETE FROM __TABLE__ WHERE title = ?");
279 Film->sql_drt->execute($rt);
280 my @films = Film->search({ title => $rt });
281 is @films, 0, "RT gone";
282 my $retval = eval { $ten->update };
283 like $@, qr/row not found/, "Update deleted object throws error";
284 $ten->discard_changes;
288 $btaste->autoupdate(1);
289 $btaste->NumExplodingSheep(32);
290 my $btaste2 = Film->retrieve($btaste->id);
291 is $btaste2->NumExplodingSheep, 32, "Object based AutoCommit";
292 $btaste->autoupdate(0);
297 my $zero = Film->insert({ Title => 0, Rating => "U" });
298 ok defined $zero, "Create 0";
299 ok my $ret = Film->retrieve(0), "Retrieve 0";
300 is $ret->Title, 0, "Title OK";
301 is $ret->Rating, "U", "Rating OK";
304 # Change after_update policy
306 skip "DBIx::Class compat doesn't handle the exists stuff quite right yet", 4;
307 my $bt = Film->retrieve($btaste->id);
311 ok !$bt->_attribute_exists('rating'), "changed column needs reloaded";
312 ok $bt->_attribute_exists('title'), "but we still have the title";
316 after_update => sub {
317 my ($self, %args) = @_;
318 my $discard_columns = $args{discard_columns};
319 @$discard_columns = qw/title/;
323 ok $bt->_attribute_exists('rating'), "changed column needs reloaded";
324 ok !$bt->_attribute_exists('title'), "but no longer have the title";
327 # Make sure that we can have other accessors. (Bugfix in 0.28)
329 Film->mk_accessors(qw/temp1 temp2/);
330 my $blrunner = Film->retrieve('Bladerunner');
331 $blrunner->temp1("Foo");
332 $blrunner->NumExplodingSheep(2);
333 eval { $blrunner->update };
334 ok(!$@, "Other accessors");
339 is "$blrunner", "Bladerunner", "stringify";
341 ok(Film->columns(Stringify => 'rating'), "Can change stringify column");
342 is "$blrunner", "R", "And still stringifies correctly";
345 Film->columns(Stringify => qw/title rating/),
346 "Can have multiple stringify columns"
348 is "$blrunner", "Bladerunner/R", "And still stringifies correctly";
351 local *Film::stringify_self = sub { join ":", $_[0]->title, $_[0]->rating };
352 is "$blrunner", "Bladerunner:R", "Provide stringify_self()";
357 ok my $byebye = DeletingFilm->insert(
359 Title => 'Goodbye Norma Jean',
363 "Add a deleting Film";
365 isa_ok $byebye, 'DeletingFilm';
366 isa_ok $byebye, 'Film';
367 ok(Film->retrieve('Goodbye Norma Jean'), "Fetch it back again");
370 eval { $film = Film->retrieve('Goodbye Norma Jean') };
371 ok !$film, "It destroys itself";
375 skip "Caching has been removed", 5
376 if Film->isa("DBIx::Class::CDBICompat::NoObjectIndex");
378 # my bad taste is your bad taste
379 my $btaste = Film->retrieve('Bad Taste');
380 my $btaste2 = Film->retrieve('Bad Taste');
381 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2),
382 "Retrieving twice gives ref to same object";
384 my ($btaste5) = Film->search(title=>'Bad Taste');
385 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste5),
386 "Searching also gives ref to same object";
388 $btaste2->remove_from_object_index;
389 my $btaste3 = Film->retrieve('Bad Taste');
390 isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste3),
391 "Removing from object_index and retrieving again gives new object";
393 $btaste3->clear_object_index;
394 my $btaste4 = Film->retrieve('Bad Taste');
395 isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste4),
396 "Clearing cache and retrieving again gives new object";
398 $btaste=Film->insert({
399 Title => 'Bad Taste 2',
400 Director => 'Peter Jackson',
402 NumExplodingSheep => 2,
404 $btaste2 = Film->retrieve('Bad Taste 2');
405 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2),
406 "Creating and retrieving gives ref to same object";