1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
8 use Scalar::Util 'refaddr';
12 use lib 't/cdbi/testlib';
15 ok(Film->can('db_Main'), 'set_db()');
16 is(Film->__driver, "SQLite", "Driver set correctly");
19 my $nul = eval { Film->retrieve() };
20 is $nul, undef, "Can't retrieve nothing";
21 like $@, qr/./, "retrieve needs parameters"; # TODO fix this...
25 eval { my $id = Film->id };
26 like $@, qr/class method/, "Can't get id with no object";
30 eval { my $id = Film->title };
31 #like $@, qr/class method/, "Can't get title with no object";
32 ok $@, "Can't get title with no object";
35 eval { my $duh = Film->insert; };
36 like $@, qr/Result object instantiation requires a single hashref argument/, "needs a hashref";
38 ok +Film->create_test_film;
40 my $btaste = Film->retrieve('Bad Taste');
41 isa_ok $btaste, 'Film';
42 is($btaste->Title, 'Bad Taste', 'Title() get');
43 is($btaste->Director, 'Peter Jackson', 'Director() get');
44 is($btaste->Rating, 'R', 'Rating() get');
45 is($btaste->NumExplodingSheep, 1, 'NumExplodingSheep() get');
48 my $bt2 = Film->find_or_create(Title => 'Bad Taste');
49 is $bt2->Director, $btaste->Director, "find_or_create";
50 my @bt = Film->search(Title => 'Bad Taste');
51 is @bt, 1, " doesn't create a new one";
54 ok my $gone = Film->find_or_create(
56 Title => 'Gone With The Wind',
57 Director => 'Bob Baggadonuts',
59 NumExplodingSheep => 0
62 "Add Gone With The Wind";
64 ok $gone = Film->retrieve(Title => 'Gone With The Wind'),
65 "Fetch it back again";
68 # Shocking new footage found reveals bizarre Scarlet/sheep scene!
69 is($gone->NumExplodingSheep, 0, 'NumExplodingSheep() get again');
70 $gone->NumExplodingSheep(5);
71 is($gone->NumExplodingSheep, 5, 'NumExplodingSheep() set');
72 is($gone->numexplodingsheep, 5, 'numexplodingsheep() set');
74 is($gone->Rating, 'PG', 'Rating() get again');
75 $gone->Rating('NC-17');
76 is($gone->Rating, 'NC-17', 'Rating() set');
80 my @films = eval { Film->retrieve_all };
81 cmp_ok(@films, '==', 2, "We have 2 films in total");
84 # EXTRA TEST: added by mst to check a bug found by Numa
85 cmp_ok(Film->count_all, '==', 2, "count_all confirms 2 films");
87 my $gone_copy = Film->retrieve('Gone With The Wind');
88 ok($gone->NumExplodingSheep == 5, 'update()');
89 ok($gone->Rating eq 'NC-17', 'update() again');
91 # Grab the 'Bladerunner' entry.
94 Title => 'Bladerunner',
95 Director => 'Bob Ridley Scott',
100 my $blrunner = Film->retrieve('Bladerunner');
101 is(ref $blrunner, 'Film', 'retrieve() again');
102 is $blrunner->Title, 'Bladerunner', "Correct title";
103 is $blrunner->Director, 'Bob Ridley Scott', " and Director";
104 is $blrunner->Rating, 'R', " and Rating";
105 is $blrunner->NumExplodingSheep, undef, " and sheep";
107 # Make a copy of 'Bladerunner' and create an entry of the directors cut
108 my $blrunner_dc = $blrunner->copy(
110 title => "Bladerunner: Director's Cut",
114 is(ref $blrunner_dc, 'Film', "copy() produces a film");
115 is($blrunner_dc->Title, "Bladerunner: Director's Cut", 'Title correct');
116 is($blrunner_dc->Director, 'Bob Ridley Scott', 'Director correct');
117 is($blrunner_dc->Rating, '15', 'Rating correct');
118 is($blrunner_dc->NumExplodingSheep, undef, 'Sheep correct');
122 Film->add_constructor(title_asc => "title LIKE ? ORDER BY title");
123 Film->add_constructor(title_desc => "title LIKE ? ORDER BY title DESC");
124 Film->add_constructor(title_asc_nl => q{
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";
141 my @films = Film->title_asc_nl("Bladerunner%");
142 is @films, 1, "We have 2 Bladerunners";
143 is $films[0]->Title, $blrunner->Title, "Ordered correctly";
147 # Multi-column search
149 my @films = $blrunner->search (title => { -like => "Bladerunner%"}, rating => '15');
150 is @films, 1, "Only one Bladerunner is a 15";
155 my @films = Film->retrieve_from_sql("numexplodingsheep > 0 ORDER BY title");
156 is @films, 2, "Inline SQL";
157 is $films[0]->id, $btaste->id, "Correct film";
158 is $films[1]->id, $gone->id, "Correct film";
161 # Inline SQL removes WHERE
164 Film->retrieve_from_sql(" WHErE numexplodingsheep > 0 ORDER BY title");
165 is @films, 2, "Inline SQL";
166 is $films[0]->id, $btaste->id, "Correct film";
167 is $films[1]->id, $gone->id, "Correct film";
171 my $ishtar = Film->insert({ Title => 'Ishtar', Director => 'Elaine May' });
173 Film->insert({ Title => 'Mikey and Nicky', Director => 'Elaine May' });
175 Film->insert({ Title => 'A New Leaf', Director => 'Elaine May' });
177 #use Data::Dumper; die Dumper(Film->search( Director => 'Elaine May' ));
178 cmp_ok(Film->search(Director => 'Elaine May'), '==', 3,
179 "3 Films by Elaine May");
180 ok(Film->retrieve('Ishtar')->delete,
181 "Ishtar doesn't deserve an entry any more");
182 ok(!Film->retrieve('Ishtar'), 'Ishtar no longer there');
185 local $SIG{__WARN__} = sub { $deprecated++ if $_[0] =~ /deprecated/ };
187 Film->delete(Director => 'Elaine May'),
188 "In fact, delete all films by Elaine May"
190 cmp_ok(Film->search(Director => 'Elaine May'), '==',
191 0, "0 Films by Elaine May");
192 is $deprecated, 0, "No deprecated warnings from compat layer";
195 is $@, '', "No problems with deletes";
197 # Find all films which have a rating of NC-17.
198 my @films = Film->search('Rating', 'NC-17');
199 is(scalar @films, 1, ' search returns one film');
200 is($films[0]->id, $gone->id, ' ... the correct one');
202 # Find all films which were directed by Bob
203 @films = Film->search ( { 'Director' => { -like => 'Bob %' } });
204 is(scalar @films, 3, ' search_like returns 3 films');
207 [ sort map { $_->id } @films ],
208 [ sort map { $_->id } $blrunner_dc, $gone, $blrunner ]
213 # Find Ridley Scott films which don't have vomit
215 Film->search(numExplodingSheep => undef, Director => 'Bob Ridley Scott');
216 is(scalar @films, 2, ' search where attribute is null returns 2 films');
219 [ sort map { $_->id } @films ],
220 [ sort map { $_->id } $blrunner_dc, $blrunner ]
225 # Test that a disconnect doesnt harm anything.
227 # SQLite is loud on disconnect/reconnect.
228 # This is solved in DBIC but not in ContextualFetch
229 local $SIG{__WARN__} = sub {
230 warn @_ unless $_[0] =~
231 /active statement handles|inactive database handle/;
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()');
247 skip "ActiveState perl produces additional warnings", 3
248 if ($^O eq 'MSWin32');
251 my $btaste2 = Film->retrieve($btaste->id);
252 $btaste->NumExplodingSheep(18);
254 local $SIG{__WARN__} = sub { push(@warnings, @_); };
257 # unhook from live object cache, so next one is not from cache
258 $btaste2->remove_from_object_index;
259 my $btaste3 = Film->retrieve($btaste->id);
260 is $btaste3->NumExplodingSheep, 18, "Class based AutoCommit";
261 $btaste3->autoupdate(0); # obj a/c should override class a/c
262 is @warnings, 0, "No warnings so far";
263 $btaste3->NumExplodingSheep(13);
265 is @warnings, 1, "DESTROY without update warns";
269 { # update unchanged object
270 my $film = Film->retrieve($btaste->id);
271 my $retval = $film->update;
272 is $retval, -1, "Unchanged object";
275 { # update deleted object
276 my $rt = "Royal Tenenbaums";
277 my $ten = Film->insert({ title => $rt, Rating => "R" });
279 Film->set_sql(drt => "DELETE FROM __TABLE__ WHERE title = ?");
280 Film->sql_drt->execute($rt);
281 my @films = Film->search({ title => $rt });
282 is @films, 0, "RT gone";
283 my $retval = eval { $ten->update };
284 like $@, qr/row not found/, "Update deleted object throws error";
285 $ten->discard_changes;
289 $btaste->autoupdate(1);
290 $btaste->NumExplodingSheep(32);
291 my $btaste2 = Film->retrieve($btaste->id);
292 is $btaste2->NumExplodingSheep, 32, "Object based AutoCommit";
293 $btaste->autoupdate(0);
298 my $zero = Film->insert({ Title => 0, Rating => "U" });
299 ok defined $zero, "Create 0";
300 ok my $ret = Film->retrieve(0), "Retrieve 0";
301 is $ret->Title, 0, "Title OK";
302 is $ret->Rating, "U", "Rating OK";
305 # Change after_update policy
307 skip "DBIx::Class compat doesn't handle the exists stuff quite right yet", 4;
308 my $bt = Film->retrieve($btaste->id);
312 ok !$bt->_attribute_exists('rating'), "changed column needs reloaded";
313 ok $bt->_attribute_exists('title'), "but we still have the title";
317 after_update => sub {
318 my ($self, %args) = @_;
319 my $discard_columns = $args{discard_columns};
320 @$discard_columns = qw/title/;
324 ok $bt->_attribute_exists('rating'), "changed column needs reloaded";
325 ok !$bt->_attribute_exists('title'), "but no longer have the title";
328 # Make sure that we can have other accessors. (Bugfix in 0.28)
330 Film->mk_accessors(qw/temp1 temp2/);
331 my $blrunner = Film->retrieve('Bladerunner');
332 $blrunner->temp1("Foo");
333 $blrunner->NumExplodingSheep(2);
334 eval { $blrunner->update };
335 ok(!$@, "Other accessors");
340 is "$blrunner", "Bladerunner", "stringify";
342 ok(Film->columns(Stringify => 'rating'), "Can change stringify column");
343 is "$blrunner", "R", "And still stringifies correctly";
346 Film->columns(Stringify => qw/title rating/),
347 "Can have multiple stringify columns"
349 is "$blrunner", "Bladerunner/R", "And still stringifies correctly";
352 local *Film::stringify_self = sub { join ":", $_[0]->title, $_[0]->rating };
353 is "$blrunner", "Bladerunner:R", "Provide stringify_self()";
358 ok my $byebye = DeletingFilm->insert(
360 Title => 'Goodbye Norma Jean',
364 "Add a deleting Film";
366 isa_ok $byebye, 'DeletingFilm';
367 isa_ok $byebye, 'Film';
368 ok(Film->retrieve('Goodbye Norma Jean'), "Fetch it back again");
371 eval { $film = Film->retrieve('Goodbye Norma Jean') };
372 ok !$film, "It destroys itself";
377 # my bad taste is your bad taste
378 my $btaste = Film->retrieve('Bad Taste');
379 my $btaste2 = Film->retrieve('Bad Taste');
380 is refaddr $btaste, refaddr $btaste2,
381 "Retrieving twice gives ref to same object";
383 my ($btaste5) = Film->search(title=>'Bad Taste');
384 is refaddr $btaste, refaddr $btaste5,
385 "Searching also gives ref to same object";
387 $btaste2->remove_from_object_index;
388 my $btaste3 = Film->retrieve('Bad Taste');
389 isnt refaddr $btaste2, refaddr $btaste3,
390 "Removing from object_index and retrieving again gives new object";
392 $btaste3->clear_object_index;
393 my $btaste4 = Film->retrieve('Bad Taste');
394 isnt refaddr $btaste2, refaddr $btaste4,
395 "Clearing cache and retrieving again gives new object";
397 $btaste=Film->insert({
398 Title => 'Bad Taste 2',
399 Director => 'Peter Jackson',
401 NumExplodingSheep => 2,
403 $btaste2 = Film->retrieve('Bad Taste 2');
404 is refaddr $btaste, refaddr $btaste2,
405 "Creating and retrieving gives ref to same object";