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 => 55);
15 #local $SIG{__WARN__} =
16 #sub { like $_[0], qr/clashes with built-in method/, $_[0] };
22 Actor->has_a(film => 'Film');
23 Film->has_a(director => 'Director');
25 sub Class::DBI::sheep { ok 0; }
28 sub Film::mutator_name {
29 my ($class, $col) = @_;
30 return "set_sheep" if lc $col eq "numexplodingsheep";
34 sub Film::accessor_name {
35 my ($class, $col) = @_;
36 return "sheep" if lc $col eq "numexplodingsheep";
40 sub Actor::accessor_name_for {
41 my ($class, $col) = @_;
42 return "movie" if lc $col eq "film";
46 # This is a class with accessor_name_for() but no corresponding mutator_name_for()
47 sub Director::accessor_name_for {
48 my($class, $col) = @_;
49 return "nutty_as_a_fruitcake" if lc $col eq "isinsane";
55 Director => 'Peter Jackson',
61 $data->{NumExplodingSheep} = 1;
62 ok my $bt = Film->create($data), "Modified accessor - with column name";
65 is $@, '', "No errors";
70 ok my $bt = Film->create($data), "Modified accessor - with accessor";
73 is $@, '', "No errors";
76 my @film = Film->search({ sheep => 1 });
77 is @film, 2, "Can search with modified accessor";
83 local $data->{set_sheep} = 1;
84 ok my $bt = Film->create($data), "Modified mutator - with mutator";
87 is $@, '', "No errors";
90 local $data->{NumExplodingSheep} = 1;
91 ok my $bt = Film->create($data), "Modified mutator - with column name";
94 is $@, '', "No errors";
97 local $data->{sheep} = 1;
98 ok my $bt = Film->create($data), "Modified mutator - with accessor";
101 is $@, '', "No errors";
107 name => 'Peter Jackson',
110 my $bt = Film->create($data);
111 my $ac = Actor->create($p_data);
113 ok !eval { my $f = $ac->film; 1 };
114 like $@, qr/film/, "no hasa film";
117 ok my $f = $ac->movie, "hasa movie";
119 is $f->id, $bt->id, " - Bad Taste";
121 is $@, '', "No errors";
124 local $data->{Title} = "Another film";
125 my $film = Film->create($data);
127 eval { $ac->film($film) };
130 eval { $ac->movie($film) };
134 ok $ac->set_film($film), "Set movie through hasa";
136 ok my $f = $ac->movie, "hasa movie";
138 is $f->id, $film->id, " - Another Film";
140 is $@, '', "No problem";
146 # Make sure a class with an accessor_name() method has a similar mutator.
148 my $aki = Director->create({
149 name => "Aki Kaurismaki",
152 $aki->nutty_as_a_fruitcake(1);
153 is $aki->nutty_as_a_fruitcake, 1,
154 "a custom accessor without a custom mutator is setable";
159 Film->columns(TEMP => qw/nonpersistent/);
160 ok(Film->find_column('nonpersistent'), "nonpersistent is a column");
161 ok(!Film->has_real_column('nonpersistent'), " - but it's not real");
164 my $film = Film->create({ Title => "Veronique", nonpersistent => 42 });
165 is $film->title, "Veronique", "Title set OK";
166 is $film->nonpersistent, 42, "As is non persistent value";
167 $film->remove_from_object_index;
168 ok $film = Film->retrieve('Veronique'), "Re-retrieve film";
169 is $film->title, "Veronique", "Title still OK";
170 is $film->nonpersistent, undef, "Non persistent value gone";
171 ok $film->nonpersistent(40), "Can set it";
172 is $film->nonpersistent, 40, "And it's there again";
173 ok $film->update, "Commit the film";
174 is $film->nonpersistent, 40, "And it's still there";
180 [Actor->columns('Essential')],
181 [Actor->columns('Primary')],
182 "Actor has no specific essential columns"
184 ok(Actor->find_column('nonpersistent'), "nonpersistent is a column");
185 ok(!Actor->has_real_column('nonpersistent'), " - but it's not real");
186 my $pj = eval { Actor->search(name => "Peter Jackson")->first };
187 is $@, '', "no problems retrieving actors";
188 isa_ok $pj => "Actor";
193 my $naked = Film->create({ title => 'Naked' });
194 my $sandl = Film->create({ title => 'Secrets and Lies' });
197 my $update_failure = sub {
199 eval { $obj->rating($rating++) };
200 return $@ =~ /read only/;
203 ok !$update_failure->($naked), "Can update Naked";
204 ok $naked->make_read_only, "Make Naked read only";
205 ok $update_failure->($naked), "Can't update Naked any more";
206 ok !$update_failure->($sandl), "But can still update Secrets and Lies";
207 my $july4 = eval { Film->create({ title => "4 Days in July" }) };
208 isa_ok $july4 => "Film", "And can still create new films";
210 ok(Film->make_read_only, "Make all Films read only");
211 ok $update_failure->($naked), "Still can't update Naked";
212 ok $update_failure->($sandl), "And can't update S&L any more";
213 eval { $july4->delete };
214 like $@, qr/read only/, "And can't delete 4 Days in July";
215 my $abigail = eval { Film->create({ title => "Abigail's Party" }) };
216 like $@, qr/read only/, "Or create new films";
218 $sandl->discard_changes;