Fix the DESTROY/discard_changes() infinite recursion at the DBIC level.
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 02-Film.t
CommitLineData
ea2e61bf 1use strict;
2use Test::More;
3$| = 1;
4
5BEGIN {
289ba852 6 eval "use DBIx::Class::CDBICompat;";
7 if ($@) {
8 plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required');
9 next;
10 }
11 eval "use DBD::SQLite";
12 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 96);
ea2e61bf 13}
14
15INIT {
16 use lib 't/testlib';
17 use Film;
18}
19
20ok(Film->can('db_Main'), 'set_db()');
21is(Film->__driver, "SQLite", "Driver set correctly");
22
ea2e61bf 23{
24 my $nul = eval { Film->retrieve() };
25 is $nul, undef, "Can't retrieve nothing";
26 like $@, qr/./, "retrieve needs parameters"; # TODO fix this...
27}
28
29{
30 eval { my $id = Film->id };
31 like $@, qr/class method/, "Can't get id with no object";
32}
33
34{
35 eval { my $id = Film->title };
ba026511 36 #like $@, qr/class method/, "Can't get title with no object";
37 ok $@, "Can't get title with no object";
ea2e61bf 38}
39
e60dc79f 40eval { my $duh = Film->insert; };
8fe001e1 41like $@, qr/create needs a hashref/, "needs a hashref";
ea2e61bf 42
43ok +Film->create_test_film;
44
45my $btaste = Film->retrieve('Bad Taste');
46isa_ok $btaste, 'Film';
47is($btaste->Title, 'Bad Taste', 'Title() get');
48is($btaste->Director, 'Peter Jackson', 'Director() get');
49is($btaste->Rating, 'R', 'Rating() get');
50is($btaste->NumExplodingSheep, 1, 'NumExplodingSheep() get');
51
52{
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";
57}
58
59ok my $gone = Film->find_or_create(
60 {
61 Title => 'Gone With The Wind',
62 Director => 'Bob Baggadonuts',
63 Rating => 'PG',
64 NumExplodingSheep => 0
65 }
66 ),
67 "Add Gone With The Wind";
68isa_ok $gone, 'Film';
69ok $gone = Film->retrieve(Title => 'Gone With The Wind'),
70 "Fetch it back again";
71isa_ok $gone, 'Film';
72
73# Shocking new footage found reveals bizarre Scarlet/sheep scene!
74is($gone->NumExplodingSheep, 0, 'NumExplodingSheep() get again');
75$gone->NumExplodingSheep(5);
76is($gone->NumExplodingSheep, 5, 'NumExplodingSheep() set');
77is($gone->numexplodingsheep, 5, 'numexplodingsheep() set');
78
79is($gone->Rating, 'PG', 'Rating() get again');
80$gone->Rating('NC-17');
81is($gone->Rating, 'NC-17', 'Rating() set');
82$gone->update;
83
84{
85 my @films = eval { Film->retrieve_all };
33ec7be0 86 cmp_ok(@films, '==', 2, "We have 2 films in total");
ea2e61bf 87}
88
3125eb1f 89# EXTRA TEST: added by mst to check a bug found by Numa
90cmp_ok(Film->count_all, '==', 2, "count_all confirms 2 films");
91
ea2e61bf 92my $gone_copy = Film->retrieve('Gone With The Wind');
93ok($gone->NumExplodingSheep == 5, 'update()');
94ok($gone->Rating eq 'NC-17', 'update() again');
95
96# Grab the 'Bladerunner' entry.
97Film->create(
98 {
99 Title => 'Bladerunner',
100 Director => 'Bob Ridley Scott',
101 Rating => 'R'
102 }
103);
104
105my $blrunner = Film->retrieve('Bladerunner');
106is(ref $blrunner, 'Film', 'retrieve() again');
107is $blrunner->Title, 'Bladerunner', "Correct title";
108is $blrunner->Director, 'Bob Ridley Scott', " and Director";
109is $blrunner->Rating, 'R', " and Rating";
110is $blrunner->NumExplodingSheep, undef, " and sheep";
111
112# Make a copy of 'Bladerunner' and create an entry of the directors cut
113my $blrunner_dc = $blrunner->copy(
114 {
115 title => "Bladerunner: Director's Cut",
116 rating => "15",
117 }
118);
119is(ref $blrunner_dc, 'Film', "copy() produces a film");
120is($blrunner_dc->Title, "Bladerunner: Director's Cut", 'Title correct');
121is($blrunner_dc->Director, 'Bob Ridley Scott', 'Director correct');
122is($blrunner_dc->Rating, '15', 'Rating correct');
123is($blrunner_dc->NumExplodingSheep, undef, 'Sheep correct');
124
125# Set up own SQL:
126{
127 Film->add_constructor(title_asc => "title LIKE ? ORDER BY title");
128 Film->add_constructor(title_desc => "title LIKE ? ORDER BY title DESC");
129
130 {
131 my @films = Film->title_asc("Bladerunner%");
132 is @films, 2, "We have 2 Bladerunners";
133 is $films[0]->Title, $blrunner->Title, "Ordered correctly";
134 }
135 {
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";
139 }
140}
141
142# Multi-column search
143{
144 my @films = $blrunner->search_like(title => "Bladerunner%", rating => '15');
145 is @films, 1, "Only one Bladerunner is a 15";
146}
147
148# Inline SQL
149{
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";
154}
155
156# Inline SQL removes WHERE
157{
158 my @films =
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";
163}
164
165eval {
e60dc79f 166 my $ishtar = Film->insert({ Title => 'Ishtar', Director => 'Elaine May' });
ea2e61bf 167 my $mandn =
e60dc79f 168 Film->insert({ Title => 'Mikey and Nicky', Director => 'Elaine May' });
ea2e61bf 169 my $new_leaf =
e60dc79f 170 Film->insert({ Title => 'A New Leaf', Director => 'Elaine May' });
8b445e33 171
172#use Data::Dumper; die Dumper(Film->search( Director => 'Elaine May' ));
a3018bd3 173 cmp_ok(Film->search(Director => 'Elaine May'), '==', 3,
174 "3 Films by Elaine May");
ea2e61bf 175 ok(Film->retrieve('Ishtar')->delete,
176 "Ishtar doesn't deserve an entry any more");
177 ok(!Film->retrieve('Ishtar'), 'Ishtar no longer there');
178 {
179 my $deprecated = 0;
a3018bd3 180 #local $SIG{__WARN__} = sub { $deprecated++ if $_[0] =~ /deprecated/ };
ea2e61bf 181 ok(
182 Film->delete(Director => 'Elaine May'),
183 "In fact, delete all films by Elaine May"
184 );
a3018bd3 185 cmp_ok(Film->search(Director => 'Elaine May'), '==',
ea2e61bf 186 0, "0 Films by Elaine May");
a3018bd3 187 SKIP: {
9bc6db13 188 skip "No deprecated warnings from compat layer", 1;
a3018bd3 189 is $deprecated, 1, "Got a deprecated warning";
190 }
ea2e61bf 191 }
192};
193is $@, '', "No problems with deletes";
194
195# Find all films which have a rating of NC-17.
196my @films = Film->search('Rating', 'NC-17');
197is(scalar @films, 1, ' search returns one film');
198is($films[0]->id, $gone->id, ' ... the correct one');
199
200# Find all films which were directed by Bob
201@films = Film->search_like('Director', 'Bob %');
202is(scalar @films, 3, ' search_like returns 3 films');
203ok(
204 eq_array(
205 [ sort map { $_->id } @films ],
206 [ sort map { $_->id } $blrunner_dc, $gone, $blrunner ]
207 ),
208 'the correct ones'
209);
210
211# Find Ridley Scott films which don't have vomit
212@films =
213 Film->search(numExplodingSheep => undef, Director => 'Bob Ridley Scott');
214is(scalar @films, 2, ' search where attribute is null returns 2 films');
215ok(
216 eq_array(
217 [ sort map { $_->id } @films ],
218 [ sort map { $_->id } $blrunner_dc, $blrunner ]
219 ),
220 'the correct ones'
221);
222
223# Test that a disconnect doesnt harm anything.
224Film->db_Main->disconnect;
225@films = Film->search({ Rating => 'NC-17' });
226ok(@films == 1 && $films[0]->id eq $gone->id, 'auto reconnection');
227
228# Test discard_changes().
229my $orig_director = $btaste->Director;
230$btaste->Director('Lenny Bruce');
231is($btaste->Director, 'Lenny Bruce', 'set new Director');
232$btaste->discard_changes;
233is($btaste->Director, $orig_director, 'discard_changes()');
234
d651c864 235SKIP: {
236 skip "ActiveState perl produces additional warnings", 3
1e316b1b 237 if ($^O eq 'MSWin32');
d651c864 238
ea2e61bf 239 Film->autoupdate(1);
240 my $btaste2 = Film->retrieve($btaste->id);
241 $btaste->NumExplodingSheep(18);
242 my @warnings;
aea8af71 243 local $SIG{__WARN__} = sub { push(@warnings, @_); };
ea2e61bf 244 {
245
246 # unhook from live object cache, so next one is not from cache
247 $btaste2->remove_from_object_index;
248 my $btaste3 = Film->retrieve($btaste->id);
249 is $btaste3->NumExplodingSheep, 18, "Class based AutoCommit";
250 $btaste3->autoupdate(0); # obj a/c should override class a/c
251 is @warnings, 0, "No warnings so far";
252 $btaste3->NumExplodingSheep(13);
253 }
254 is @warnings, 1, "DESTROY without update warns";
255 Film->autoupdate(0);
256}
257
258{ # update unchanged object
259 my $film = Film->retrieve($btaste->id);
260 my $retval = $film->update;
261 is $retval, -1, "Unchanged object";
262}
263
264{ # update deleted object
265 my $rt = "Royal Tenenbaums";
e60dc79f 266 my $ten = Film->insert({ title => $rt, Rating => "R" });
ea2e61bf 267 $ten->rating(18);
268 Film->set_sql(drt => "DELETE FROM __TABLE__ WHERE title = ?");
269 Film->sql_drt->execute($rt);
270 my @films = Film->search({ title => $rt });
271 is @films, 0, "RT gone";
272 my $retval = eval { $ten->update };
273 like $@, qr/row not found/, "Update deleted object throws error";
274 $ten->discard_changes;
275}
276
277{
278 $btaste->autoupdate(1);
279 $btaste->NumExplodingSheep(32);
280 my $btaste2 = Film->retrieve($btaste->id);
281 is $btaste2->NumExplodingSheep, 32, "Object based AutoCommit";
282 $btaste->autoupdate(0);
283}
284
285# Primary key of 0
286{
e60dc79f 287 my $zero = Film->insert({ Title => 0, Rating => "U" });
ea2e61bf 288 ok defined $zero, "Create 0";
289 ok my $ret = Film->retrieve(0), "Retrieve 0";
290 is $ret->Title, 0, "Title OK";
291 is $ret->Rating, "U", "Rating OK";
292}
293
294# Change after_update policy
a3018bd3 295SKIP: {
12bbb339 296 skip "DBIx::Class compat doesn't handle the exists stuff quite right yet", 4;
ea2e61bf 297 my $bt = Film->retrieve($btaste->id);
298 $bt->autoupdate(1);
299
300 $bt->rating("17");
301 ok !$bt->_attribute_exists('rating'), "changed column needs reloaded";
302 ok $bt->_attribute_exists('title'), "but we still have the title";
303
304 # Don't re-load
305 $bt->add_trigger(
306 after_update => sub {
307 my ($self, %args) = @_;
308 my $discard_columns = $args{discard_columns};
309 @$discard_columns = qw/title/;
310 }
311 );
312 $bt->rating("19");
313 ok $bt->_attribute_exists('rating'), "changed column needs reloaded";
314 ok !$bt->_attribute_exists('title'), "but no longer have the title";
315}
316
317# Make sure that we can have other accessors. (Bugfix in 0.28)
318if (0) {
319 Film->mk_accessors(qw/temp1 temp2/);
320 my $blrunner = Film->retrieve('Bladerunner');
321 $blrunner->temp1("Foo");
322 $blrunner->NumExplodingSheep(2);
323 eval { $blrunner->update };
324 ok(!$@, "Other accessors");
325}
326
327# overloading
328{
329 is "$blrunner", "Bladerunner", "stringify";
330
331 ok(Film->columns(Stringify => 'rating'), "Can change stringify column");
332 is "$blrunner", "R", "And still stringifies correctly";
333
334 ok(
335 Film->columns(Stringify => qw/title rating/),
336 "Can have multiple stringify columns"
337 );
338 is "$blrunner", "Bladerunner/R", "And still stringifies correctly";
339
340 no warnings 'once';
341 local *Film::stringify_self = sub { join ":", $_[0]->title, $_[0]->rating };
342 is "$blrunner", "Bladerunner:R", "Provide stringify_self()";
343}
344
345{
346 {
e60dc79f 347 ok my $byebye = DeletingFilm->insert(
ea2e61bf 348 {
349 Title => 'Goodbye Norma Jean',
350 Rating => 'PG',
351 }
352 ),
353 "Add a deleting Film";
354
355 isa_ok $byebye, 'DeletingFilm';
356 isa_ok $byebye, 'Film';
357 ok(Film->retrieve('Goodbye Norma Jean'), "Fetch it back again");
358 }
359 my $film;
360 eval { $film = Film->retrieve('Goodbye Norma Jean') };
361 ok !$film, "It destroys itself";
362}
363
364SKIP: {
c1d23573 365 #skip "DBIx::Class doesn't yet have a live objects index", 3;
a3018bd3 366 #skip "Scalar::Util::weaken not available", 3
ea2e61bf 367 #if !$Class::DBI::Weaken_Is_Available;
368
369 # my bad taste is your bad taste
370 my $btaste = Film->retrieve('Bad Taste');
371 my $btaste2 = Film->retrieve('Bad Taste');
372 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2),
373 "Retrieving twice gives ref to same object";
374
451517e7 375 my ($btaste5) = Film->search(title=>'Bad Taste');
376 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste5),
377 "Searching also gives ref to same object";
378
ea2e61bf 379 $btaste2->remove_from_object_index;
380 my $btaste3 = Film->retrieve('Bad Taste');
381 isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste3),
382 "Removing from object_index and retrieving again gives new object";
383
384 $btaste3->clear_object_index;
385 my $btaste4 = Film->retrieve('Bad Taste');
386 isnt Scalar::Util::refaddr($btaste2), Scalar::Util::refaddr($btaste4),
387 "Clearing cache and retrieving again gives new object";
451517e7 388
e60dc79f 389 $btaste=Film->insert({
451517e7 390 Title => 'Bad Taste 2',
391 Director => 'Peter Jackson',
392 Rating => 'R',
393 NumExplodingSheep => 2,
394 });
395 $btaste2 = Film->retrieve('Bad Taste 2');
396 is Scalar::Util::refaddr($btaste), Scalar::Util::refaddr($btaste2),
397 "Creating and retrieving gives ref to same object";
398
ea2e61bf 399}