5 #local $SIG{__WARN__} =
6 #sub { like $_[0], qr/clashes with built-in method/, $_[0] };
7 use lib 't/cdbi/testlib';
12 Actor->has_a(film => 'Film');
13 Film->has_a(director => 'Director');
15 sub Class::DBI::sheep { ok 0; }
18 # Install the deprecation warning intercept here for the rest of the 08 dev cycle
19 local $SIG{__WARN__} = sub {
20 warn @_ unless (DBIx::Class->VERSION < 0.09 and $_[0] =~ /Query returned more than one row/);
23 sub Film::mutator_name {
24 my ($class, $col) = @_;
25 return "set_sheep" if lc $col eq "numexplodingsheep";
29 sub Film::accessor_name {
30 my ($class, $col) = @_;
31 return "sheep" if lc $col eq "numexplodingsheep";
35 sub Actor::accessor_name_for {
36 my ($class, $col) = @_;
37 return "movie" if lc $col eq "film";
41 # This is a class with accessor_name_for() but no corresponding mutator_name_for()
42 sub Director::accessor_name_for {
43 my($class, $col) = @_;
44 return "nutty_as_a_fruitcake" if lc $col eq "isinsane";
50 Director => 'Peter Jackson',
55 my $data = { %$data };
56 $data->{NumExplodingSheep} = 1;
57 ok my $bt = Film->create($data), "Modified accessor - with column name";
59 is $bt->sheep, 1, 'sheep bursting violently';
61 is $@, '', "No errors";
64 my $data = { %$data };
66 ok my $bt = Film->create($data), "Modified accessor - with accessor";
68 is $bt->sheep, 2, 'sheep bursting violently';
70 is $@, '', "No errors";
73 my $data = { %$data };
74 $data->{NumExplodingSheep} = 1;
75 ok my $bt = Film->find_or_create($data),
76 "find_or_create Modified accessor - find with column name";
78 is $bt->sheep, 1, 'sheep bursting violently';
80 is $@, '', "No errors";
83 my $data = { %$data };
85 ok my $bt = Film->find_or_create($data),
86 "find_or_create Modified accessor - find with accessor";
88 is $bt->sheep, 1, 'sheep bursting violently';
90 is $@, '', "No errors";
92 TODO: { local $TODO = 'TODOifying failing tests, waiting for Schwern'; ok (1, 'remove me');
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";
99 is $bt->sheep, 3, 'sheep bursting violently';
101 is $@, '', "No errors";
104 my $data = { %$data };
106 ok my $bt = Film->find_or_create($data),
107 "find_or_create Modified accessor - create with accessor";
109 is $bt->sheep, 4, 'sheep bursting violently';
111 is $@, '', "No errors";
114 my @film = Film->search({ sheep => 1 });
115 is @film, 2, "Can search with modified accessor";
117 is $@, '', "No errors";
124 local $data->{set_sheep} = 1;
125 ok my $bt = Film->create($data), "Modified mutator - with mutator";
128 is $@, '', "No errors";
131 local $data->{NumExplodingSheep} = 1;
132 ok my $bt = Film->create($data), "Modified mutator - with column name";
135 is $@, '', "No errors";
138 local $data->{sheep} = 1;
139 ok my $bt = Film->create($data), "Modified mutator - with accessor";
142 is $@, '', "No errors";
148 name => 'Peter Jackson',
151 my $bt = Film->create($data);
152 my $ac = Actor->create($p_data);
154 ok !eval { my $f = $ac->film; 1 };
155 like $@, qr/film/, "no hasa film";
158 ok my $f = $ac->movie, "hasa movie";
160 is $f->id, $bt->id, " - Bad Taste";
162 is $@, '', "No errors";
165 local $data->{Title} = "Another film";
166 my $film = Film->create($data);
168 eval { $ac->film($film) };
171 eval { $ac->movie($film) };
175 ok $ac->set_film($film), "Set movie through hasa";
177 ok my $f = $ac->movie, "hasa movie";
179 is $f->id, $film->id, " - Another Film";
181 is $@, '', "No problem";
187 # Make sure a class with an accessor_name() method has a similar mutator.
189 my $aki = Director->create({
190 name => "Aki Kaurismaki",
193 $aki->nutty_as_a_fruitcake(1);
194 is $aki->nutty_as_a_fruitcake, 1,
195 "a custom accessor without a custom mutator is setable";
200 Film->columns(TEMP => qw/nonpersistent/);
201 ok(Film->find_column('nonpersistent'), "nonpersistent is a column");
202 ok(!Film->has_real_column('nonpersistent'), " - but it's not real");
205 my $film = Film->create({ Title => "Veronique", nonpersistent => 42 });
206 is $film->title, "Veronique", "Title set OK";
207 is $film->nonpersistent, 42, "As is non persistent value";
208 $film->remove_from_object_index;
209 ok $film = Film->retrieve('Veronique'), "Re-retrieve film";
210 is $film->title, "Veronique", "Title still OK";
211 is $film->nonpersistent, undef, "Non persistent value gone";
212 ok $film->nonpersistent(40), "Can set it";
213 is $film->nonpersistent, 40, "And it's there again";
214 ok $film->update, "Commit the film";
215 is $film->nonpersistent, 40, "And it's still there";
221 [Actor->columns('Essential')],
222 [Actor->columns('Primary')],
223 "Actor has no specific essential columns"
225 ok(Actor->find_column('nonpersistent'), "nonpersistent is a column");
226 ok(!Actor->has_real_column('nonpersistent'), " - but it's not real");
227 my $pj = eval { Actor->search(name => "Peter Jackson")->first };
228 is $@, '', "no problems retrieving actors";
229 isa_ok $pj => "Actor";
234 my $naked = Film->create({ title => 'Naked' });
235 my $sandl = Film->create({ title => 'Secrets and Lies' });
238 my $update_failure = sub {
240 eval { $obj->rating($rating++) };
241 return $@ =~ /read only/;
244 ok !$update_failure->($naked), "Can update Naked";
245 ok $naked->make_read_only, "Make Naked read only";
246 ok $update_failure->($naked), "Can't update Naked any more";
247 ok !$update_failure->($sandl), "But can still update Secrets and Lies";
248 my $july4 = eval { Film->create({ title => "4 Days in July" }) };
249 isa_ok $july4 => "Film", "And can still create new films";
251 ok(Film->make_read_only, "Make all Films read only");
252 ok $update_failure->($naked), "Still can't update Naked";
253 ok $update_failure->($sandl), "And can't update S&L any more";
254 eval { $july4->delete };
255 like $@, qr/read only/, "And can't delete 4 Days in July";
256 my $abigail = eval { Film->create({ title => "Abigail's Party" }) };
257 like $@, qr/read only/, "Or create new films";
259 $_->discard_changes for ($naked, $sandl);