6 eval "use DBD::SQLite";
7 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 93);
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";
34 eval { my $duh = Film->create; };
35 like $@, qr/create needs a hashref/, "needs a hashref";
37 ok +Film->create_test_film;
39 my $btaste = Film->retrieve('Bad Taste');
40 isa_ok $btaste, 'Film';
41 is($btaste->Title, 'Bad Taste', 'Title() get');
42 is($btaste->Director, 'Peter Jackson', 'Director() get');
43 is($btaste->Rating, 'R', 'Rating() get');
44 is($btaste->NumExplodingSheep, 1, 'NumExplodingSheep() get');
47 my $bt2 = Film->find_or_create(Title => 'Bad Taste');
48 is $bt2->Director, $btaste->Director, "find_or_create";
49 my @bt = Film->search(Title => 'Bad Taste');
50 is @bt, 1, " doesn't create a new one";
53 ok my $gone = Film->find_or_create(
55 Title => 'Gone With The Wind',
56 Director => 'Bob Baggadonuts',
58 NumExplodingSheep => 0
61 "Add Gone With The Wind";
63 ok $gone = Film->retrieve(Title => 'Gone With The Wind'),
64 "Fetch it back again";
67 # Shocking new footage found reveals bizarre Scarlet/sheep scene!
68 is($gone->NumExplodingSheep, 0, 'NumExplodingSheep() get again');
69 $gone->NumExplodingSheep(5);
70 is($gone->NumExplodingSheep, 5, 'NumExplodingSheep() set');
71 is($gone->numexplodingsheep, 5, 'numexplodingsheep() set');
73 is($gone->Rating, 'PG', 'Rating() get again');
74 $gone->Rating('NC-17');
75 is($gone->Rating, 'NC-17', 'Rating() set');
79 my @films = eval { Film->retrieve_all };
80 is(@films, 2, "We have 2 films in total");
83 my $gone_copy = Film->retrieve('Gone With The Wind');
84 ok($gone->NumExplodingSheep == 5, 'update()');
85 ok($gone->Rating eq 'NC-17', 'update() again');
87 # Grab the 'Bladerunner' entry.
90 Title => 'Bladerunner',
91 Director => 'Bob Ridley Scott',
96 my $blrunner = Film->retrieve('Bladerunner');
97 is(ref $blrunner, 'Film', 'retrieve() again');
98 is $blrunner->Title, 'Bladerunner', "Correct title";
99 is $blrunner->Director, 'Bob Ridley Scott', " and Director";
100 is $blrunner->Rating, 'R', " and Rating";
101 is $blrunner->NumExplodingSheep, undef, " and sheep";
103 # Make a copy of 'Bladerunner' and create an entry of the directors cut
104 my $blrunner_dc = $blrunner->copy(
106 title => "Bladerunner: Director's Cut",
110 is(ref $blrunner_dc, 'Film', "copy() produces a film");
111 is($blrunner_dc->Title, "Bladerunner: Director's Cut", 'Title correct');
112 is($blrunner_dc->Director, 'Bob Ridley Scott', 'Director correct');
113 is($blrunner_dc->Rating, '15', 'Rating correct');
114 is($blrunner_dc->NumExplodingSheep, undef, 'Sheep correct');
118 Film->add_constructor(title_asc => "title LIKE ? ORDER BY title");
119 Film->add_constructor(title_desc => "title LIKE ? ORDER BY title DESC");
122 my @films = Film->title_asc("Bladerunner%");
123 is @films, 2, "We have 2 Bladerunners";
124 is $films[0]->Title, $blrunner->Title, "Ordered correctly";
127 my @films = Film->title_desc("Bladerunner%");
128 is @films, 2, "We have 2 Bladerunners";
129 is $films[0]->Title, $blrunner_dc->Title, "Ordered correctly";
133 # Multi-column search
135 my @films = $blrunner->search_like(title => "Bladerunner%", rating => '15');
136 is @films, 1, "Only one Bladerunner is a 15";
141 my @films = Film->retrieve_from_sql("numexplodingsheep > 0 ORDER BY title");
142 is @films, 2, "Inline SQL";
143 is $films[0]->id, $btaste->id, "Correct film";
144 is $films[1]->id, $gone->id, "Correct film";
147 # Inline SQL removes WHERE
150 Film->retrieve_from_sql(" WHErE 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";
157 my $ishtar = Film->create({ Title => 'Ishtar', Director => 'Elaine May' });
159 Film->create({ Title => 'Mikey and Nicky', Director => 'Elaine May' });
161 Film->create({ Title => 'A New Leaf', Director => 'Elaine May' });
162 is(Film->search(Director => 'Elaine May')->count,
163 3, "3 Films by Elaine May");
164 ok(Film->retrieve('Ishtar')->delete,
165 "Ishtar doesn't deserve an entry any more");
166 ok(!Film->retrieve('Ishtar'), 'Ishtar no longer there');
169 local $SIG{__WARN__} = sub { $deprecated++ if $_[0] =~ /deprecated/ };
171 Film->delete(Director => 'Elaine May'),
172 "In fact, delete all films by Elaine May"
174 is(Film->search(Director => 'Elaine May')->count,
175 0, "0 Films by Elaine May");
176 is $deprecated, 1, "Got a deprecated warning";
179 is $@, '', "No problems with deletes";
181 # Find all films which have a rating of NC-17.
182 my @films = Film->search('Rating', 'NC-17');
183 is(scalar @films, 1, ' search returns one film');
184 is($films[0]->id, $gone->id, ' ... the correct one');
186 # Find all films which were directed by Bob
187 @films = Film->search_like('Director', 'Bob %');
188 is(scalar @films, 3, ' search_like returns 3 films');
191 [ sort map { $_->id } @films ],
192 [ sort map { $_->id } $blrunner_dc, $gone, $blrunner ]
197 # Find Ridley Scott films which don't have vomit
199 Film->search(numExplodingSheep => undef, Director => 'Bob Ridley Scott');
200 is(scalar @films, 2, ' search where attribute is null returns 2 films');
203 [ sort map { $_->id } @films ],
204 [ sort map { $_->id } $blrunner_dc, $blrunner ]
209 # Test that a disconnect doesnt harm anything.
210 Film->db_Main->disconnect;
211 @films = Film->search({ Rating => 'NC-17' });
212 ok(@films == 1 && $films[0]->id eq $gone->id, 'auto reconnection');
214 # Test discard_changes().
215 my $orig_director = $btaste->Director;
216 $btaste->Director('Lenny Bruce');
217 is($btaste->Director, 'Lenny Bruce', 'set new Director');
218 $btaste->discard_changes;
219 is($btaste->Director, $orig_director, 'discard_changes()');
223 my $btaste2 = Film->retrieve($btaste->id);
224 $btaste->NumExplodingSheep(18);
226 local $SIG{__WARN__} = sub { push @warnings, @_; };
229 # unhook from live object cache, so next one is not from cache
230 $btaste2->remove_from_object_index;
231 my $btaste3 = Film->retrieve($btaste->id);
232 is $btaste3->NumExplodingSheep, 18, "Class based AutoCommit";
233 $btaste3->autoupdate(0); # obj a/c should override class a/c
234 is @warnings, 0, "No warnings so far";
235 $btaste3->NumExplodingSheep(13);
237 is @warnings, 1, "DESTROY without update warns";
241 { # update unchanged object
242 my $film = Film->retrieve($btaste->id);
243 my $retval = $film->update;
244 is $retval, -1, "Unchanged object";
247 { # update deleted object
248 my $rt = "Royal Tenenbaums";
249 my $ten = Film->create({ title => $rt, Rating => "R" });
251 Film->set_sql(drt => "DELETE FROM __TABLE__ WHERE title = ?");
252 Film->sql_drt->execute($rt);
253 my @films = Film->search({ title => $rt });
254 is @films, 0, "RT gone";
255 my $retval = eval { $ten->update };
256 like $@, qr/row not found/, "Update deleted object throws error";
257 $ten->discard_changes;
261 $btaste->autoupdate(1);
262 $btaste->NumExplodingSheep(32);
263 my $btaste2 = Film->retrieve($btaste->id);
264 is $btaste2->NumExplodingSheep, 32, "Object based AutoCommit";
265 $btaste->autoupdate(0);
270 my $zero = Film->create({ Title => 0, Rating => "U" });
271 ok defined $zero, "Create 0";
272 ok my $ret = Film->retrieve(0), "Retrieve 0";
273 is $ret->Title, 0, "Title OK";
274 is $ret->Rating, "U", "Rating OK";
277 # Change after_update policy
279 my $bt = Film->retrieve($btaste->id);
283 ok !$bt->_attribute_exists('rating'), "changed column needs reloaded";
284 ok $bt->_attribute_exists('title'), "but we still have the title";
288 after_update => sub {
289 my ($self, %args) = @_;
290 my $discard_columns = $args{discard_columns};
291 @$discard_columns = qw/title/;
295 ok $bt->_attribute_exists('rating'), "changed column needs reloaded";
296 ok !$bt->_attribute_exists('title'), "but no longer have the title";
299 # Make sure that we can have other accessors. (Bugfix in 0.28)
301 Film->mk_accessors(qw/temp1 temp2/);
302 my $blrunner = Film->retrieve('Bladerunner');
303 $blrunner->temp1("Foo");
304 $blrunner->NumExplodingSheep(2);
305 eval { $blrunner->update };
306 ok(!$@, "Other accessors");
311 is "$blrunner", "Bladerunner", "stringify";
313 ok(Film->columns(Stringify => 'rating'), "Can change stringify column");
314 is "$blrunner", "R", "And still stringifies correctly";
317 Film->columns(Stringify => qw/title rating/),
318 "Can have multiple stringify columns"
320 is "$blrunner", "Bladerunner/R", "And still stringifies correctly";
323 local *Film::stringify_self = sub { join ":", $_[0]->title, $_[0]->rating };
324 is "$blrunner", "Bladerunner:R", "Provide stringify_self()";
329 ok my $byebye = DeletingFilm->create(
331 Title => 'Goodbye Norma Jean',
335 "Add a deleting Film";
337 isa_ok $byebye, 'DeletingFilm';
338 isa_ok $byebye, 'Film';
339 ok(Film->retrieve('Goodbye Norma Jean'), "Fetch it back again");
342 eval { $film = Film->retrieve('Goodbye Norma Jean') };
343 ok !$film, "It destroys itself";
347 skip "Scalar::Util::weaken not available", 3;
348 #if !$Class::DBI::Weaken_Is_Available;
350 # my bad taste is your bad taste
351 my $btaste = Film->retrieve('Bad Taste');
352 my $btaste2 = Film->retrieve('Bad Taste');
353 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2),
354 "Retrieving twice gives ref to same object";
356 $btaste2->remove_from_object_index;
357 my $btaste3 = Film->retrieve('Bad Taste');
358 isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste3),
359 "Removing from object_index and retrieving again gives new object";
361 $btaste3->clear_object_index;
362 my $btaste4 = Film->retrieve('Bad Taste');
363 isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste4),
364 "Clearing cache and retrieving again gives new object";