5 eval "use DBIx::Class::CDBICompat;";
7 plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required');
10 eval "use DBD::SQLite";
11 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 75);
15 #local $SIG{__WARN__} =
16 #sub { like $_[0], qr/clashes with built-in method/, $_[0] };
17 use lib 't/cdbi/testlib';
22 Actor->has_a(film => 'Film');
23 Film->has_a(director => 'Director');
25 sub Class::DBI::sheep { ok 0; }
28 # Install the deprecation warning intercept here for the rest of the 08 dev cycle
29 local $SIG{__WARN__} = sub {
30 warn @_ unless (DBIx::Class->VERSION < 0.09 and $_[0] =~ /Query returned more than one row/);
33 sub Film::mutator_name {
34 my ($class, $col) = @_;
35 return "set_sheep" if lc $col eq "numexplodingsheep";
39 sub Film::accessor_name {
40 my ($class, $col) = @_;
41 return "sheep" if lc $col eq "numexplodingsheep";
45 sub Actor::accessor_name_for {
46 my ($class, $col) = @_;
47 return "movie" if lc $col eq "film";
51 # This is a class with accessor_name_for() but no corresponding mutator_name_for()
52 sub Director::accessor_name_for {
53 my($class, $col) = @_;
54 return "nutty_as_a_fruitcake" if lc $col eq "isinsane";
60 Director => 'Peter Jackson',
65 my $data = { %$data };
66 $data->{NumExplodingSheep} = 1;
67 ok my $bt = Film->create($data), "Modified accessor - with column name";
69 is $bt->sheep, 1, 'sheep bursting violently';
71 is $@, '', "No errors";
74 my $data = { %$data };
76 ok my $bt = Film->create($data), "Modified accessor - with accessor";
78 is $bt->sheep, 2, 'sheep bursting violently';
80 is $@, '', "No errors";
83 my $data = { %$data };
84 $data->{NumExplodingSheep} = 1;
85 ok my $bt = Film->find_or_create($data),
86 "find_or_create Modified accessor - find with column name";
88 is $bt->sheep, 1, 'sheep bursting violently';
90 is $@, '', "No errors";
93 my $data = { %$data };
95 ok my $bt = Film->find_or_create($data),
96 "find_or_create Modified accessor - find with accessor";
98 is $bt->sheep, 1, 'sheep bursting violently';
100 is $@, '', "No errors";
102 TODO: { local $TODO = 'TODOifying failing tests, waiting for Schwern'; ok (1, 'remove me');
104 my $data = { %$data };
105 $data->{NumExplodingSheep} = 3;
106 ok my $bt = Film->find_or_create($data),
107 "find_or_create Modified accessor - create with column name";
109 is $bt->sheep, 3, 'sheep bursting violently';
111 is $@, '', "No errors";
114 my $data = { %$data };
116 ok my $bt = Film->find_or_create($data),
117 "find_or_create Modified accessor - create with accessor";
119 is $bt->sheep, 4, 'sheep bursting violently';
121 is $@, '', "No errors";
124 my @film = Film->search({ sheep => 1 });
125 is @film, 2, "Can search with modified accessor";
127 is $@, '', "No errors";
134 local $data->{set_sheep} = 1;
135 ok my $bt = Film->create($data), "Modified mutator - with mutator";
138 is $@, '', "No errors";
141 local $data->{NumExplodingSheep} = 1;
142 ok my $bt = Film->create($data), "Modified mutator - with column name";
145 is $@, '', "No errors";
148 local $data->{sheep} = 1;
149 ok my $bt = Film->create($data), "Modified mutator - with accessor";
152 is $@, '', "No errors";
158 name => 'Peter Jackson',
161 my $bt = Film->create($data);
162 my $ac = Actor->create($p_data);
164 ok !eval { my $f = $ac->film; 1 };
165 like $@, qr/film/, "no hasa film";
168 ok my $f = $ac->movie, "hasa movie";
170 is $f->id, $bt->id, " - Bad Taste";
172 is $@, '', "No errors";
175 local $data->{Title} = "Another film";
176 my $film = Film->create($data);
178 eval { $ac->film($film) };
181 eval { $ac->movie($film) };
185 ok $ac->set_film($film), "Set movie through hasa";
187 ok my $f = $ac->movie, "hasa movie";
189 is $f->id, $film->id, " - Another Film";
191 is $@, '', "No problem";
197 # Make sure a class with an accessor_name() method has a similar mutator.
199 my $aki = Director->create({
200 name => "Aki Kaurismaki",
203 $aki->nutty_as_a_fruitcake(1);
204 is $aki->nutty_as_a_fruitcake, 1,
205 "a custom accessor without a custom mutator is setable";
210 Film->columns(TEMP => qw/nonpersistent/);
211 ok(Film->find_column('nonpersistent'), "nonpersistent is a column");
212 ok(!Film->has_real_column('nonpersistent'), " - but it's not real");
215 my $film = Film->create({ Title => "Veronique", nonpersistent => 42 });
216 is $film->title, "Veronique", "Title set OK";
217 is $film->nonpersistent, 42, "As is non persistent value";
218 $film->remove_from_object_index;
219 ok $film = Film->retrieve('Veronique'), "Re-retrieve film";
220 is $film->title, "Veronique", "Title still OK";
221 is $film->nonpersistent, undef, "Non persistent value gone";
222 ok $film->nonpersistent(40), "Can set it";
223 is $film->nonpersistent, 40, "And it's there again";
224 ok $film->update, "Commit the film";
225 is $film->nonpersistent, 40, "And it's still there";
231 [Actor->columns('Essential')],
232 [Actor->columns('Primary')],
233 "Actor has no specific essential columns"
235 ok(Actor->find_column('nonpersistent'), "nonpersistent is a column");
236 ok(!Actor->has_real_column('nonpersistent'), " - but it's not real");
237 my $pj = eval { Actor->search(name => "Peter Jackson")->first };
238 is $@, '', "no problems retrieving actors";
239 isa_ok $pj => "Actor";
244 my $naked = Film->create({ title => 'Naked' });
245 my $sandl = Film->create({ title => 'Secrets and Lies' });
248 my $update_failure = sub {
250 eval { $obj->rating($rating++) };
251 return $@ =~ /read only/;
254 ok !$update_failure->($naked), "Can update Naked";
255 ok $naked->make_read_only, "Make Naked read only";
256 ok $update_failure->($naked), "Can't update Naked any more";
257 ok !$update_failure->($sandl), "But can still update Secrets and Lies";
258 my $july4 = eval { Film->create({ title => "4 Days in July" }) };
259 isa_ok $july4 => "Film", "And can still create new films";
261 ok(Film->make_read_only, "Make all Films read only");
262 ok $update_failure->($naked), "Still can't update Naked";
263 ok $update_failure->($sandl), "And can't update S&L any more";
264 eval { $july4->delete };
265 like $@, qr/read only/, "And can't delete 4 Days in July";
266 my $abigail = eval { Film->create({ title => "Abigail's Party" }) };
267 like $@, qr/read only/, "Or create new films";
269 $_->discard_changes for ($naked, $sandl);