5 use lib 't/cdbi/testlib';
10 my $ok = grep $value eq $_, qw/U Uc PG 12 15 18/;
14 Film->add_constraint('valid rating', Rating => \&valid_rating);
17 Title => 'La Double Vie De Veronique',
18 Director => 'Kryzstof Kieslowski',
23 local $info{Title} = "nonsense";
24 local $info{Rating} = 19;
25 eval { Film->create({%info}) };
27 ok !Film->retrieve($info{Title}), "No film created";
28 is(Film->retrieve_all, 0, "So no films");
31 ok(my $ver = Film->create({%info}), "Can create with valid rating");
32 is $ver->Rating, 18, "Rating 18";
34 ok $ver->Rating(12), "Change to 12";
35 ok $ver->update, "And update";
36 is $ver->Rating, 12, "Rating now 12";
43 is $ver->Rating, 12, "Rating still 12";
44 ok $ver->delete, "Delete";
46 # this threw an infinite loop in old versions
47 Film->add_constraint('valid director', Director => sub { 1 });
48 my $fred = Film->create({ Rating => '12' });
50 # this test is a bit problematical because we don't supply a primary key
51 # to the create() and the table doesn't use auto_increment or a sequence.
55 ok +Film->constrain_column(rating => [qw/U PG 12 15 19/]),
57 my $narrower = eval { Film->create({ Rating => 'Uc' }) };
58 like $@, qr/fails.*constraint/, "Fails listref constraint";
59 my $ok = eval { Film->create({ Rating => 'U' }) };
60 is $@, '', "Can create with rating U";
62 skip "No column objects", 2;
63 ok +Film->find_column('rating')->is_constrained, "Rating is constrained";
64 ok +Film->find_column('director')->is_constrained, "Director is not";
69 ok +Film->constrain_column(title => qr/The/), "constraint_column";
70 my $inferno = eval { Film->create({ Title => 'Towering Infero' }) };
71 like $@, qr/fails.*constraint/, "Can't create towering inferno";
72 my $the_inferno = eval { Film->create({ Title => 'The Towering Infero' }) };
73 is $@, '', "But can create THE towering inferno";
78 sub Film::_constrain_by_untaint {
79 my ($class, $col, $string, $type) = @_;
80 $class->add_constraint(
81 untaint => $col => sub {
82 my ($value, $self, $column_name, $changing) = @_;
83 $value eq "today" ? $changing->{$column_name} = "2001-03-03" : 0;
87 eval { Film->constrain_column(codirector => Untaint => 'date') };
88 is $@, '', 'Can constrain with untaint';
91 eval { Film->create({ title => "The Freaa", codirector => 'today' }) };
92 is $@, '', "Can create codirector";
93 is $freeaa && $freeaa->codirector, '2001-03-03', "Set the codirector";
102 sub _constrain_by_untaint {
103 my ($class, $col, $string, $type) = @_;
104 $class->add_constraint(untaint => $col => sub {
105 my ($value, $self, $column_name, $changing) = @_;
106 my $h = CGI::Untaint->new({ %$changing });
107 return unless my $val = $h->extract("-as_$type" => $column_name);
108 $changing->{$column_name} = $val;