Debian Codegraph @

t/cdbi-t-ocache/13-constraint.t

raw
1 use strict;
2 use Test::More;
3 use Class::DBI::Sweet;
4 Class::DBI::Sweet->default_search_attributes({ use_resultset_cache => 0 });
5 Class::DBI::Sweet->cache(Cache::MemoryCache->new(
6 { namespace => "SweetTest", default_expires_in => 60 } ) );
7
8 BEGIN {
9 eval "use Cache::MemoryCache";
10 plan skip_all => "needs Cache::Cache for testing" if $@;
11 eval "use DBD::SQLite";
12 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 28);
13 }
14
15 use lib 't/cdbi-t/testlib';
16 use Film;
17
18 sub valid_rating {
19 my $value = shift;
20 my $ok = grep $value eq $_, qw/U Uc PG 12 15 18/;
21 return $ok;
22 }
23
24 Film->add_constraint('valid rating', Rating => \&valid_rating);
25
26 my %info = (
27 Title => 'La Double Vie De Veronique',
28 Director => 'Kryzstof Kieslowski',
29 Rating => '18',
30 );
31
32 {
33 local $info{Title} = "nonsense";
34 local $info{Rating} = 19;
35 eval { Film->insert({%info}) };
36 ok $@, $@;
37 ok !Film->retrieve($info{Title}), "No film created";
38 is(Film->retrieve_all, 0, "So no films");
39 }
40
41 ok(my $ver = Film->insert({%info}), "Can insert with valid rating");
42 is $ver->Rating, 18, "Rating 18";
43
44 ok $ver->Rating(12), "Change to 12";
45 ok $ver->update, "And update";
46 is $ver->Rating, 12, "Rating now 12";
47
48 {
49 local *Film::_croak = sub {
50 my ($self, $msg, %info) = @_;
51 die %info ? bless \%info => "My::Error" : $msg;
52 };
53 eval {
54 $ver->Rating(13);
55 $ver->update;
56 };
57 isa_ok $@ => 'My::Error';
58 my $fail = $@->{data}{rating}{data};
59 is $fail->{column}->name_lc, "rating", "Rating fails";
60 is $fail->{value}, 13, "Can't set to 13";
61 is $ver->Rating, 12, "Rating still 12";
62 ok $ver->delete, "Delete";
63 }
64
65 # this threw an infinite loop in old versions
66 Film->add_constraint('valid director', Director => sub { 1 });
67 my $fred = Film->insert({ Rating => '12' });
68
69 # this test is a bit problematic because we don't supply a primary key
70 # to the create() and the table doesn't use auto_increment or a sequence.
71 ok $fred, "Got fred";
72
73 {
74 ok +Film->constrain_column(rating => [qw/U PG 12 15 19/]),
75 "constrain_column";
76 my $narrower = eval { Film->insert({ Rating => 'Uc' }) };
77 like $@, qr/fails.*constraint/, "Fails listref constraint";
78 my $ok = eval { Film->insert({ Rating => 'U' }) };
79 is $@, '', "Can insert with rating U";
80 ok +Film->find_column('rating')->is_constrained, "Rating is constrained";
81 ok +Film->find_column('director')->is_constrained, "Director is not";
82 }
83
84 {
85 ok +Film->constrain_column(title => qr/The/), "constraint_column";
86 my $inferno = eval { Film->insert({ Title => 'Towering Infero' }) };
87 like $@, qr/fails.*constraint/, "Can't insert towering inferno";
88 my $the_inferno = eval { Film->insert({ Title => 'The Towering Infero' }) };
89 is $@, '', "But can insert THE towering inferno";
90 }
91
92 {
93
94 sub Film::_constrain_by_untaint {
95 my ($class, $col, $string, $type) = @_;
96 $class->add_constraint(
97 untaint => $col => sub {
98 my ($value, $self, $column_name, $changing) = @_;
99 $value eq "today" ? $changing->{$column_name} = "2001-03-03" : 0;
100 }
101 );
102 }
103 eval { Film->constrain_column(codirector => Untaint => 'date') };
104 is $@, '', 'Can constrain with untaint';
105 my $freeaa =
106 eval { Film->insert({ title => "The Freaa", codirector => 'today' }) };
107 is $@, '', "Can insert codirector";
108 is $freeaa->codirector, '2001-03-03', "Set the codirector";
109 }
110
111 {
112 ok +Film->constrain_column(title => sub { length() <= 10 }), "and again";
113 my $toolong = eval { Film->insert({ Title => 'The Wonderful' }) };
114 like $@, qr/fails.*constraint/, "Can't insert too long title";
115 my $then = eval { Film->insert({ Title => 'The Blob' }) };
116 is $@, '', "But can insert The XXX";
117 }
118
119 __DATA__
120
121 use CGI::Untaint;
122
123 sub _constrain_by_untaint {
124 my ($class, $col, $string, $type) = @_;
125 $class->add_constraint(untaint => $col => sub {
126 my ($value, $self, $column_name, $changing) = @_;
127 my $h = CGI::Untaint->new({ %$changing });
128 return unless my $val = $h->extract("-as_$type" => $column_name);
129 $changing->{$column_name} = $val;
130 return 1;
131 });
132 }
133