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 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',
60 my $data = { %$data };
61 $data->{NumExplodingSheep} = 1;
62 ok my $bt = Film->create($data), "Modified accessor - with column name";
64 is $bt->sheep, 1, 'sheep bursting violently';
66 is $@, '', "No errors";
69 my $data = { %$data };
71 ok my $bt = Film->create($data), "Modified accessor - with accessor";
73 is $bt->sheep, 2, 'sheep bursting violently';
75 is $@, '', "No errors";
78 my $data = { %$data };
79 $data->{NumExplodingSheep} = 1;
80 ok my $bt = Film->find_or_create($data),
81 "find_or_create Modified accessor - find with column name";
83 is $bt->sheep, 1, 'sheep bursting violently';
85 is $@, '', "No errors";
88 my $data = { %$data };
90 ok my $bt = Film->find_or_create($data),
91 "find_or_create Modified accessor - find with accessor";
93 is $bt->sheep, 1, 'sheep bursting violently';
95 is $@, '', "No errors";
97 TODO: { local $TODO = 'TODOifying failing tests, waiting for Schwern'; ok (1, 'remove me');
99 my $data = { %$data };
100 $data->{NumExplodingSheep} = 3;
101 ok my $bt = Film->find_or_create($data),
102 "find_or_create Modified accessor - create with column name";
104 is $bt->sheep, 3, 'sheep bursting violently';
106 is $@, '', "No errors";
109 my $data = { %$data };
111 ok my $bt = Film->find_or_create($data),
112 "find_or_create Modified accessor - create with accessor";
114 is $bt->sheep, 4, 'sheep bursting violently';
116 is $@, '', "No errors";
119 my @film = Film->search({ sheep => 1 });
120 is @film, 2, "Can search with modified accessor";
122 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 local $SIG{__WARN__} = sub {
164 warn @_ unless $_[0] =~ /Query returned more than one row/;
166 ok my $f = $ac->movie, "hasa movie";
168 is $f->id, $bt->id, " - Bad Taste";
170 is $@, '', "No errors";
173 local $data->{Title} = "Another film";
174 my $film = Film->create($data);
176 eval { $ac->film($film) };
179 eval { $ac->movie($film) };
183 ok $ac->set_film($film), "Set movie through hasa";
185 ok my $f = $ac->movie, "hasa movie";
187 is $f->id, $film->id, " - Another Film";
189 is $@, '', "No problem";
195 # Make sure a class with an accessor_name() method has a similar mutator.
197 my $aki = Director->create({
198 name => "Aki Kaurismaki",
201 $aki->nutty_as_a_fruitcake(1);
202 is $aki->nutty_as_a_fruitcake, 1,
203 "a custom accessor without a custom mutator is setable";
208 Film->columns(TEMP => qw/nonpersistent/);
209 ok(Film->find_column('nonpersistent'), "nonpersistent is a column");
210 ok(!Film->has_real_column('nonpersistent'), " - but it's not real");
213 my $film = Film->create({ Title => "Veronique", nonpersistent => 42 });
214 is $film->title, "Veronique", "Title set OK";
215 is $film->nonpersistent, 42, "As is non persistent value";
216 $film->remove_from_object_index;
217 ok $film = Film->retrieve('Veronique'), "Re-retrieve film";
218 is $film->title, "Veronique", "Title still OK";
219 is $film->nonpersistent, undef, "Non persistent value gone";
220 ok $film->nonpersistent(40), "Can set it";
221 is $film->nonpersistent, 40, "And it's there again";
222 ok $film->update, "Commit the film";
223 is $film->nonpersistent, 40, "And it's still there";
229 [Actor->columns('Essential')],
230 [Actor->columns('Primary')],
231 "Actor has no specific essential columns"
233 ok(Actor->find_column('nonpersistent'), "nonpersistent is a column");
234 ok(!Actor->has_real_column('nonpersistent'), " - but it's not real");
235 my $pj = eval { Actor->search(name => "Peter Jackson")->first };
236 is $@, '', "no problems retrieving actors";
237 isa_ok $pj => "Actor";
242 my $naked = Film->create({ title => 'Naked' });
243 my $sandl = Film->create({ title => 'Secrets and Lies' });
246 my $update_failure = sub {
248 eval { $obj->rating($rating++) };
249 return $@ =~ /read only/;
252 ok !$update_failure->($naked), "Can update Naked";
253 ok $naked->make_read_only, "Make Naked read only";
254 ok $update_failure->($naked), "Can't update Naked any more";
255 ok !$update_failure->($sandl), "But can still update Secrets and Lies";
256 my $july4 = eval { Film->create({ title => "4 Days in July" }) };
257 isa_ok $july4 => "Film", "And can still create new films";
259 ok(Film->make_read_only, "Make all Films read only");
260 ok $update_failure->($naked), "Still can't update Naked";
261 ok $update_failure->($sandl), "And can't update S&L any more";
262 eval { $july4->delete };
263 like $@, qr/read only/, "And can't delete 4 Days in July";
264 my $abigail = eval { Film->create({ title => "Abigail's Party" }) };
265 like $@, qr/read only/, "Or create new films";
267 $sandl->discard_changes;