6 #local $SIG{__WARN__} =
7 #sub { like $_[0], qr/clashes with built-in method/, $_[0] };
8 use lib 't/cdbi/testlib';
13 Actor->has_a(film => 'Film');
14 Film->has_a(director => 'Director');
16 sub Class::DBI::sheep { ok 0; }
19 # Install the deprecation warning intercept here for the rest of the 08 dev cycle
20 local $SIG{__WARN__} = sub {
21 warn @_ unless (DBIx::Class->VERSION < 0.09 and $_[0] =~ /Query returned more than one row/);
24 sub Film::mutator_name {
25 my ($class, $col) = @_;
26 return "set_sheep" if lc $col eq "numexplodingsheep";
30 sub Film::accessor_name {
31 my ($class, $col) = @_;
32 return "sheep" if lc $col eq "numexplodingsheep";
36 sub Actor::accessor_name_for {
37 my ($class, $col) = @_;
38 return "movie" if lc $col eq "film";
42 # This is a class with accessor_name_for() but no corresponding mutator_name_for()
43 sub Director::accessor_name_for {
44 my($class, $col) = @_;
45 return "nutty_as_a_fruitcake" if lc $col eq "isinsane";
51 Director => 'Peter Jackson',
56 my $data = { %$data };
57 $data->{NumExplodingSheep} = 1;
58 ok my $bt = Film->create($data), "Modified accessor - with column name";
60 is $bt->sheep, 1, 'sheep bursting violently';
62 is $@, '', "No errors";
65 my $data = { %$data };
67 ok my $bt = Film->create($data), "Modified accessor - with accessor";
69 is $bt->sheep, 2, 'sheep bursting violently';
71 is $@, '', "No errors";
74 my $data = { %$data };
75 $data->{NumExplodingSheep} = 1;
76 ok my $bt = Film->find_or_create($data),
77 "find_or_create Modified accessor - find with column name";
79 is $bt->sheep, 1, 'sheep bursting violently';
81 is $@, '', "No errors";
84 my $data = { %$data };
86 ok my $bt = Film->find_or_create($data),
87 "find_or_create Modified accessor - find with accessor";
89 is $bt->sheep, 1, 'sheep bursting violently';
91 is $@, '', "No errors";
94 my $data = { %$data };
95 $data->{NumExplodingSheep} = 3;
96 ok my $bt = Film->find_or_create($data),
97 "find_or_create Modified accessor - create with column name";
100 local $TODO = 'TODOifying failing tests, waiting for Schwern';
101 is $bt->sheep, 3, 'sheep bursting violently';
103 is $@, '', "No errors";
106 my $data = { %$data };
108 ok my $bt = Film->find_or_create($data),
109 "find_or_create Modified accessor - create with accessor";
112 local $TODO = 'TODOifying failing tests, waiting for Schwern';
113 is $bt->sheep, 4, 'sheep bursting violently';
115 is $@, '', "No errors";
118 my @film = Film->search({ sheep => 1 });
119 is @film, 2, "Can search with modified accessor";
122 local $TODO = 'TODOifying failing tests, waiting for Schwern';
123 is $@, '', "No errors";
129 local $data->{set_sheep} = 1;
130 ok my $bt = Film->create($data), "Modified mutator - with mutator";
133 is $@, '', "No errors";
136 local $data->{NumExplodingSheep} = 1;
137 ok my $bt = Film->create($data), "Modified mutator - with column name";
140 is $@, '', "No errors";
143 local $data->{sheep} = 1;
144 ok my $bt = Film->create($data), "Modified mutator - with accessor";
147 is $@, '', "No errors";
153 name => 'Peter Jackson',
156 my $bt = Film->create($data);
157 my $ac = Actor->create($p_data);
159 ok !eval { my $f = $ac->film; 1 };
160 like $@, qr/film/, "no hasa film";
163 ok my $f = $ac->movie, "hasa movie";
165 is $f->id, $bt->id, " - Bad Taste";
167 is $@, '', "No errors";
170 local $data->{Title} = "Another film";
171 my $film = Film->create($data);
173 eval { $ac->film($film) };
176 eval { $ac->movie($film) };
180 ok $ac->set_film($film), "Set movie through hasa";
182 ok my $f = $ac->movie, "hasa movie";
184 is $f->id, $film->id, " - Another Film";
186 is $@, '', "No problem";
192 # Make sure a class with an accessor_name() method has a similar mutator.
194 my $aki = Director->create({
195 name => "Aki Kaurismaki",
198 $aki->nutty_as_a_fruitcake(1);
199 is $aki->nutty_as_a_fruitcake, 1,
200 "a custom accessor without a custom mutator is setable";
205 Film->columns(TEMP => qw/nonpersistent/);
206 ok(Film->find_column('nonpersistent'), "nonpersistent is a column");
207 ok(!Film->has_real_column('nonpersistent'), " - but it's not real");
210 my $film = Film->create({ Title => "Veronique", nonpersistent => 42 });
211 is $film->title, "Veronique", "Title set OK";
212 is $film->nonpersistent, 42, "As is non persistent value";
213 $film->remove_from_object_index;
214 ok $film = Film->retrieve('Veronique'), "Re-retrieve film";
215 is $film->title, "Veronique", "Title still OK";
216 is $film->nonpersistent, undef, "Non persistent value gone";
217 ok $film->nonpersistent(40), "Can set it";
218 is $film->nonpersistent, 40, "And it's there again";
219 ok $film->update, "Commit the film";
220 is $film->nonpersistent, 40, "And it's still there";
226 [Actor->columns('Essential')],
227 [Actor->columns('Primary')],
228 "Actor has no specific essential columns"
230 ok(Actor->find_column('nonpersistent'), "nonpersistent is a column");
231 ok(!Actor->has_real_column('nonpersistent'), " - but it's not real");
232 my $pj = eval { Actor->search(name => "Peter Jackson")->first };
233 is $@, '', "no problems retrieving actors";
234 isa_ok $pj => "Actor";
239 my $naked = Film->create({ title => 'Naked' });
240 my $sandl = Film->create({ title => 'Secrets and Lies' });
243 my $update_failure = sub {
245 eval { $obj->rating($rating++) };
246 return $@ =~ /read only/;
249 ok !$update_failure->($naked), "Can update Naked";
250 ok $naked->make_read_only, "Make Naked read only";
251 ok $update_failure->($naked), "Can't update Naked any more";
252 ok !$update_failure->($sandl), "But can still update Secrets and Lies";
253 my $july4 = eval { Film->create({ title => "4 Days in July" }) };
254 isa_ok $july4 => "Film", "And can still create new films";
256 ok(Film->make_read_only, "Make all Films read only");
257 ok $update_failure->($naked), "Still can't update Naked";
258 ok $update_failure->($sandl), "And can't update S&L any more";
259 eval { $july4->delete };
260 like $@, qr/read only/, "And can't delete 4 Days in July";
261 my $abigail = eval { Film->create({ title => "Abigail's Party" }) };
262 like $@, qr/read only/, "Or create new films";
264 $_->discard_changes for ($naked, $sandl);