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 => 23);
14 use lib 't/cdbi/testlib';
19 my $ok = grep $value eq $_, qw/U Uc PG 12 15 18/;
23 Film->add_constraint('valid rating', Rating => \&valid_rating);
26 Title => 'La Double Vie De Veronique',
27 Director => 'Kryzstof Kieslowski',
32 local $info{Title} = "nonsense";
33 local $info{Rating} = 19;
34 eval { Film->create({%info}) };
36 ok !Film->retrieve($info{Title}), "No film created";
37 is(Film->retrieve_all, 0, "So no films");
40 ok(my $ver = Film->create({%info}), "Can create with valid rating");
41 is $ver->Rating, 18, "Rating 18";
43 ok $ver->Rating(12), "Change to 12";
44 ok $ver->update, "And update";
45 is $ver->Rating, 12, "Rating now 12";
52 is $ver->Rating, 12, "Rating still 12";
53 ok $ver->delete, "Delete";
55 # this threw an infinite loop in old versions
56 Film->add_constraint('valid director', Director => sub { 1 });
57 my $fred = Film->create({ Rating => '12' });
59 # this test is a bit problematical because we don't supply a primary key
60 # to the create() and the table doesn't use auto_increment or a sequence.
64 ok +Film->constrain_column(rating => [qw/U PG 12 15 19/]),
66 my $narrower = eval { Film->create({ Rating => 'Uc' }) };
67 like $@, qr/fails.*constraint/, "Fails listref constraint";
68 my $ok = eval { Film->create({ Rating => 'U' }) };
69 is $@, '', "Can create with rating U";
71 skip "No column objects", 2;
72 ok +Film->find_column('rating')->is_constrained, "Rating is constrained";
73 ok +Film->find_column('director')->is_constrained, "Director is not";
78 ok +Film->constrain_column(title => qr/The/), "constraint_column";
79 my $inferno = eval { Film->create({ Title => 'Towering Infero' }) };
80 like $@, qr/fails.*constraint/, "Can't create towering inferno";
81 my $the_inferno = eval { Film->create({ Title => 'The Towering Infero' }) };
82 is $@, '', "But can create THE towering inferno";
87 sub Film::_constrain_by_untaint {
88 my ($class, $col, $string, $type) = @_;
89 $class->add_constraint(
90 untaint => $col => sub {
91 my ($value, $self, $column_name, $changing) = @_;
92 $value eq "today" ? $changing->{$column_name} = "2001-03-03" : 0;
96 eval { Film->constrain_column(codirector => Untaint => 'date') };
97 is $@, '', 'Can constrain with untaint';
100 eval { Film->create({ title => "The Freaa", codirector => 'today' }) };
101 is $@, '', "Can create codirector";
102 is $freeaa && $freeaa->codirector, '2001-03-03', "Set the codirector";
109 sub _constrain_by_untaint {
110 my ($class, $col, $string, $type) = @_;
111 $class->add_constraint(untaint => $col => sub {
112 my ($value, $self, $column_name, $changing) = @_;
113 my $h = CGI::Untaint->new({ %$changing });
114 return unless my $val = $h->extract("-as_$type" => $column_name);
115 $changing->{$column_name} = $val;