5 eval "use DBD::SQLite";
6 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 23);
14 my $ok = grep $value eq $_, qw/U Uc PG 12 15 18/;
18 Film->add_constraint('valid rating', Rating => \&valid_rating);
21 Title => 'La Double Vie De Veronique',
22 Director => 'Kryzstof Kieslowski',
27 local $info{Title} = "nonsense";
28 local $info{Rating} = 19;
29 eval { Film->create({%info}) };
31 ok !Film->retrieve($info{Title}), "No film created";
32 is(Film->retrieve_all, 0, "So no films");
35 ok(my $ver = Film->create({%info}), "Can create with valid rating");
36 is $ver->Rating, 18, "Rating 18";
38 ok $ver->Rating(12), "Change to 12";
39 ok $ver->update, "And update";
40 is $ver->Rating, 12, "Rating now 12";
47 is $ver->Rating, 12, "Rating still 12";
48 ok $ver->delete, "Delete";
50 # this threw an infinite loop in old versions
51 Film->add_constraint('valid director', Director => sub { 1 });
52 my $fred = Film->create({ Rating => '12' });
54 # this test is a bit problematical because we don't supply a primary key
55 # to the create() and the table doesn't use auto_increment or a sequence.
59 ok +Film->constrain_column(rating => [qw/U PG 12 15 19/]),
61 my $narrower = eval { Film->create({ Rating => 'Uc' }) };
62 like $@, qr/fails.*constraint/, "Fails listref constraint";
63 my $ok = eval { Film->create({ Rating => 'U' }) };
64 is $@, '', "Can create with rating U";
66 skip "No column objects", 2;
67 ok +Film->find_column('rating')->is_constrained, "Rating is constrained";
68 ok +Film->find_column('director')->is_constrained, "Director is not";
73 ok +Film->constrain_column(title => qr/The/), "constraint_column";
74 my $inferno = eval { Film->create({ Title => 'Towering Infero' }) };
75 like $@, qr/fails.*constraint/, "Can't create towering inferno";
76 my $the_inferno = eval { Film->create({ Title => 'The Towering Infero' }) };
77 is $@, '', "But can create THE towering inferno";
82 sub Film::_constrain_by_untaint {
83 my ($class, $col, $string, $type) = @_;
84 $class->add_constraint(
85 untaint => $col => sub {
86 my ($value, $self, $column_name, $changing) = @_;
87 $value eq "today" ? $changing->{$column_name} = "2001-03-03" : 0;
91 eval { Film->constrain_column(codirector => Untaint => 'date') };
92 is $@, '', 'Can constrain with untaint';
94 eval { Film->create({ title => "The Freaa", codirector => 'today' }) };
95 is $@, '', "Can create codirector";
96 is $freeaa->codirector, '2001-03-03', "Set the codirector";
103 sub _constrain_by_untaint {
104 my ($class, $col, $string, $type) = @_;
105 $class->add_constraint(untaint => $col => sub {
106 my ($value, $self, $column_name, $changing) = @_;
107 my $h = CGI::Untaint->new({ %$changing });
108 return unless my $val = $h->extract("-as_$type" => $column_name);
109 $changing->{$column_name} = $val;