5 use lib 't/cdbi/testlib';
11 Actor->has_a(film => 'Film');
12 Film->has_a(director => 'Director');
14 sub Class::DBI::sheep { ok 0; }
17 # Install the deprecation warning intercept here for the rest of the 08 dev cycle
18 local $SIG{__WARN__} = sub {
19 warn @_ unless (DBIx::Class->VERSION < 0.09 and $_[0] =~ /Query returned more than one row/);
22 sub Film::mutator_name {
23 my ($class, $col) = @_;
24 return "set_sheep" if lc $col eq "numexplodingsheep";
28 sub Film::accessor_name {
29 my ($class, $col) = @_;
30 return "sheep" if lc $col eq "numexplodingsheep";
34 sub Actor::accessor_name_for {
35 my ($class, $col) = @_;
36 return "movie" if lc $col eq "film";
40 # This is a class with accessor_name_for() but no corresponding mutator_name_for()
41 sub Director::accessor_name_for {
42 my($class, $col) = @_;
43 return "nutty_as_a_fruitcake" if lc $col eq "isinsane";
49 Director => 'Peter Jackson',
54 my $data = { %$data };
55 $data->{NumExplodingSheep} = 1;
56 ok my $bt = Film->create($data), "Modified accessor - with column name";
58 is $bt->sheep, 1, 'sheep bursting violently';
60 is $@, '', "No errors";
63 my $data = { %$data };
65 ok my $bt = Film->create($data), "Modified accessor - with accessor";
67 is $bt->sheep, 2, 'sheep bursting violently';
69 is $@, '', "No errors";
72 my $data = { %$data };
73 $data->{NumExplodingSheep} = 1;
74 ok my $bt = Film->find_or_create($data),
75 "find_or_create Modified accessor - find with column name";
77 is $bt->sheep, 1, 'sheep bursting violently';
79 is $@, '', "No errors";
82 my $data = { %$data };
84 ok my $bt = Film->find_or_create($data),
85 "find_or_create Modified accessor - find with accessor";
87 is $bt->sheep, 1, 'sheep bursting violently';
89 is $@, '', "No errors";
92 my $data = { %$data };
93 $data->{NumExplodingSheep} = 3;
94 ok my $bt = Film->find_or_create($data),
95 "find_or_create Modified accessor - create with column name";
98 local $TODO = 'TODOifying failing tests, waiting for Schwern';
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";
110 local $TODO = 'TODOifying failing tests, waiting for Schwern';
111 is $bt->sheep, 4, 'sheep bursting violently';
113 is $@, '', "No errors";
116 my @film = Film->search({ sheep => 1 });
117 is @film, 2, "Can search with modified accessor";
120 local $TODO = 'TODOifying failing tests, waiting for Schwern';
121 is $@, '', "No errors";
127 local $data->{set_sheep} = 1;
128 ok my $bt = Film->create($data), "Modified mutator - with mutator";
131 is $@, '', "No errors";
134 local $data->{NumExplodingSheep} = 1;
135 ok my $bt = Film->create($data), "Modified mutator - with column name";
138 is $@, '', "No errors";
141 local $data->{sheep} = 1;
142 ok my $bt = Film->create($data), "Modified mutator - with accessor";
145 is $@, '', "No errors";
151 name => 'Peter Jackson',
154 my $bt = Film->create($data);
155 my $ac = Actor->create($p_data);
157 ok !eval { my $f = $ac->film; 1 };
158 like $@, qr/film/, "no hasa film";
161 ok my $f = $ac->movie, "hasa movie";
163 is $f->id, $bt->id, " - Bad Taste";
165 is $@, '', "No errors";
168 local $data->{Title} = "Another film";
169 my $film = Film->create($data);
171 eval { $ac->film($film) };
174 eval { $ac->movie($film) };
178 ok $ac->set_film($film), "Set movie through hasa";
180 ok my $f = $ac->movie, "hasa movie";
182 is $f->id, $film->id, " - Another Film";
184 is $@, '', "No problem";
190 # Make sure a class with an accessor_name() method has a similar mutator.
192 my $aki = Director->create({
193 name => "Aki Kaurismaki",
196 $aki->nutty_as_a_fruitcake(1);
197 is $aki->nutty_as_a_fruitcake, 1,
198 "a custom accessor without a custom mutator is setable";
203 Film->columns(TEMP => qw/nonpersistent/);
204 ok(Film->find_column('nonpersistent'), "nonpersistent is a column");
205 ok(!Film->has_real_column('nonpersistent'), " - but it's not real");
208 my $film = Film->create({ Title => "Veronique", nonpersistent => 42 });
209 is $film->title, "Veronique", "Title set OK";
210 is $film->nonpersistent, 42, "As is non persistent value";
211 $film->remove_from_object_index;
212 ok $film = Film->retrieve('Veronique'), "Re-retrieve film";
213 is $film->title, "Veronique", "Title still OK";
214 is $film->nonpersistent, undef, "Non persistent value gone";
215 ok $film->nonpersistent(40), "Can set it";
216 is $film->nonpersistent, 40, "And it's there again";
217 ok $film->update, "Commit the film";
218 is $film->nonpersistent, 40, "And it's still there";
224 [Actor->columns('Essential')],
225 [Actor->columns('Primary')],
226 "Actor has no specific essential columns"
228 ok(Actor->find_column('nonpersistent'), "nonpersistent is a column");
229 ok(!Actor->has_real_column('nonpersistent'), " - but it's not real");
230 my $pj = eval { Actor->search(name => "Peter Jackson")->first };
231 is $@, '', "no problems retrieving actors";
232 isa_ok $pj => "Actor";
237 my $naked = Film->create({ title => 'Naked' });
238 my $sandl = Film->create({ title => 'Secrets and Lies' });
241 my $update_failure = sub {
243 eval { $obj->rating($rating++) };
244 return $@ =~ /read only/;
247 ok !$update_failure->($naked), "Can update Naked";
248 ok $naked->make_read_only, "Make Naked read only";
249 ok $update_failure->($naked), "Can't update Naked any more";
250 ok !$update_failure->($sandl), "But can still update Secrets and Lies";
251 my $july4 = eval { Film->create({ title => "4 Days in July" }) };
252 isa_ok $july4 => "Film", "And can still create new films";
254 ok(Film->make_read_only, "Make all Films read only");
255 ok $update_failure->($naked), "Still can't update Naked";
256 ok $update_failure->($sandl), "And can't update S&L any more";
257 eval { $july4->delete };
258 like $@, qr/read only/, "And can't delete 4 Days in July";
259 my $abigail = eval { Film->create({ title => "Abigail's Party" }) };
260 like $@, qr/read only/, "Or create new films";
262 $_->discard_changes for ($naked, $sandl);