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 => 96);
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->create; };
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");
131 my @films = Film->title_asc("Bladerunner%");
132 is @films, 2, "We have 2 Bladerunners";
133 is $films[0]->Title, $blrunner->Title, "Ordered correctly";
136 my @films = Film->title_desc("Bladerunner%");
137 is @films, 2, "We have 2 Bladerunners";
138 is $films[0]->Title, $blrunner_dc->Title, "Ordered correctly";
142 # Multi-column search
144 my @films = $blrunner->search_like(title => "Bladerunner%", rating => '15');
145 is @films, 1, "Only one Bladerunner is a 15";
150 my @films = Film->retrieve_from_sql("numexplodingsheep > 0 ORDER BY title");
151 is @films, 2, "Inline SQL";
152 is $films[0]->id, $btaste->id, "Correct film";
153 is $films[1]->id, $gone->id, "Correct film";
156 # Inline SQL removes WHERE
159 Film->retrieve_from_sql(" WHErE 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";
166 my $ishtar = Film->create({ Title => 'Ishtar', Director => 'Elaine May' });
168 Film->create({ Title => 'Mikey and Nicky', Director => 'Elaine May' });
170 Film->create({ Title => 'A New Leaf', Director => 'Elaine May' });
172 #use Data::Dumper; die Dumper(Film->search( Director => 'Elaine May' ));
173 cmp_ok(Film->search(Director => 'Elaine May'), '==', 3,
174 "3 Films by Elaine May");
175 ok(Film->retrieve('Ishtar')->delete,
176 "Ishtar doesn't deserve an entry any more");
177 ok(!Film->retrieve('Ishtar'), 'Ishtar no longer there');
180 #local $SIG{__WARN__} = sub { $deprecated++ if $_[0] =~ /deprecated/ };
182 Film->delete(Director => 'Elaine May'),
183 "In fact, delete all films by Elaine May"
185 cmp_ok(Film->search(Director => 'Elaine May'), '==',
186 0, "0 Films by Elaine May");
188 skip "No deprecated warnings from compat layer", 1;
189 is $deprecated, 1, "Got a deprecated warning";
193 is $@, '', "No problems with deletes";
195 # Find all films which have a rating of NC-17.
196 my @films = Film->search('Rating', 'NC-17');
197 is(scalar @films, 1, ' search returns one film');
198 is($films[0]->id, $gone->id, ' ... the correct one');
200 # Find all films which were directed by Bob
201 @films = Film->search_like('Director', 'Bob %');
202 is(scalar @films, 3, ' search_like returns 3 films');
205 [ sort map { $_->id } @films ],
206 [ sort map { $_->id } $blrunner_dc, $gone, $blrunner ]
211 # Find Ridley Scott films which don't have vomit
213 Film->search(numExplodingSheep => undef, Director => 'Bob Ridley Scott');
214 is(scalar @films, 2, ' search where attribute is null returns 2 films');
217 [ sort map { $_->id } @films ],
218 [ sort map { $_->id } $blrunner_dc, $blrunner ]
223 # Test that a disconnect doesnt harm anything.
224 Film->db_Main->disconnect;
225 @films = Film->search({ Rating => 'NC-17' });
226 ok(@films == 1 && $films[0]->id eq $gone->id, 'auto reconnection');
228 # Test discard_changes().
229 my $orig_director = $btaste->Director;
230 $btaste->Director('Lenny Bruce');
231 is($btaste->Director, 'Lenny Bruce', 'set new Director');
232 $btaste->discard_changes;
233 is($btaste->Director, $orig_director, 'discard_changes()');
237 my $btaste2 = Film->retrieve($btaste->id);
238 $btaste->NumExplodingSheep(18);
240 local $SIG{__WARN__} = sub { push(@warnings, @_); };
243 # unhook from live object cache, so next one is not from cache
244 $btaste2->remove_from_object_index;
245 my $btaste3 = Film->retrieve($btaste->id);
246 is $btaste3->NumExplodingSheep, 18, "Class based AutoCommit";
247 $btaste3->autoupdate(0); # obj a/c should override class a/c
248 is @warnings, 0, "No warnings so far";
249 $btaste3->NumExplodingSheep(13);
251 is @warnings, 1, "DESTROY without update warns";
252 print join("\n", @warnings);
256 { # update unchanged object
257 my $film = Film->retrieve($btaste->id);
258 my $retval = $film->update;
259 is $retval, -1, "Unchanged object";
262 { # update deleted object
263 my $rt = "Royal Tenenbaums";
264 my $ten = Film->create({ title => $rt, Rating => "R" });
266 Film->set_sql(drt => "DELETE FROM __TABLE__ WHERE title = ?");
267 Film->sql_drt->execute($rt);
268 my @films = Film->search({ title => $rt });
269 is @films, 0, "RT gone";
270 my $retval = eval { $ten->update };
271 like $@, qr/row not found/, "Update deleted object throws error";
272 $ten->discard_changes;
276 $btaste->autoupdate(1);
277 $btaste->NumExplodingSheep(32);
278 my $btaste2 = Film->retrieve($btaste->id);
279 is $btaste2->NumExplodingSheep, 32, "Object based AutoCommit";
280 $btaste->autoupdate(0);
285 my $zero = Film->create({ Title => 0, Rating => "U" });
286 ok defined $zero, "Create 0";
287 ok my $ret = Film->retrieve(0), "Retrieve 0";
288 is $ret->Title, 0, "Title OK";
289 is $ret->Rating, "U", "Rating OK";
292 # Change after_update policy
294 skip "DBIx::Class compat doesn't handle the exists stuff quite right yet", 4;
295 my $bt = Film->retrieve($btaste->id);
299 ok !$bt->_attribute_exists('rating'), "changed column needs reloaded";
300 ok $bt->_attribute_exists('title'), "but we still have the title";
304 after_update => sub {
305 my ($self, %args) = @_;
306 my $discard_columns = $args{discard_columns};
307 @$discard_columns = qw/title/;
311 ok $bt->_attribute_exists('rating'), "changed column needs reloaded";
312 ok !$bt->_attribute_exists('title'), "but no longer have the title";
315 # Make sure that we can have other accessors. (Bugfix in 0.28)
317 Film->mk_accessors(qw/temp1 temp2/);
318 my $blrunner = Film->retrieve('Bladerunner');
319 $blrunner->temp1("Foo");
320 $blrunner->NumExplodingSheep(2);
321 eval { $blrunner->update };
322 ok(!$@, "Other accessors");
327 is "$blrunner", "Bladerunner", "stringify";
329 ok(Film->columns(Stringify => 'rating'), "Can change stringify column");
330 is "$blrunner", "R", "And still stringifies correctly";
333 Film->columns(Stringify => qw/title rating/),
334 "Can have multiple stringify columns"
336 is "$blrunner", "Bladerunner/R", "And still stringifies correctly";
339 local *Film::stringify_self = sub { join ":", $_[0]->title, $_[0]->rating };
340 is "$blrunner", "Bladerunner:R", "Provide stringify_self()";
345 ok my $byebye = DeletingFilm->create(
347 Title => 'Goodbye Norma Jean',
351 "Add a deleting Film";
353 isa_ok $byebye, 'DeletingFilm';
354 isa_ok $byebye, 'Film';
355 ok(Film->retrieve('Goodbye Norma Jean'), "Fetch it back again");
358 eval { $film = Film->retrieve('Goodbye Norma Jean') };
359 ok !$film, "It destroys itself";
363 #skip "DBIx::Class doesn't yet have a live objects index", 3;
364 #skip "Scalar::Util::weaken not available", 3
365 #if !$Class::DBI::Weaken_Is_Available;
367 # my bad taste is your bad taste
368 my $btaste = Film->retrieve('Bad Taste');
369 my $btaste2 = Film->retrieve('Bad Taste');
370 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2),
371 "Retrieving twice gives ref to same object";
373 my ($btaste5) = Film->search(title=>'Bad Taste');
374 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste5),
375 "Searching also gives ref to same object";
377 $btaste2->remove_from_object_index;
378 my $btaste3 = Film->retrieve('Bad Taste');
379 isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste3),
380 "Removing from object_index and retrieving again gives new object";
382 $btaste3->clear_object_index;
383 my $btaste4 = Film->retrieve('Bad Taste');
384 isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste4),
385 "Clearing cache and retrieving again gives new object";
387 $btaste=Film->create({
388 Title => 'Bad Taste 2',
389 Director => 'Peter Jackson',
391 NumExplodingSheep => 2,
393 $btaste2 = Film->retrieve('Bad Taste 2');
394 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2),
395 "Creating and retrieving gives ref to same object";