Add strict/warnings test, adjust all offenders (wow, that was a lot)
[dbsrgits/DBIx-Class.git] / t / cdbi / 13-constraint.t
CommitLineData
525035fb 1use strict;
4a233f30 2use warnings;
525035fb 3use Test::More;
4
50891152 5use lib 't/cdbi/testlib';
525035fb 6use Film;
7
8sub valid_rating {
9a440a19 9 my $value = shift;
10 my $ok = grep $value eq $_, qw/U Uc PG 12 15 18/;
11 return $ok;
525035fb 12}
13
14Film->add_constraint('valid rating', Rating => \&valid_rating);
15
16my %info = (
9a440a19 17 Title => 'La Double Vie De Veronique',
18 Director => 'Kryzstof Kieslowski',
19 Rating => '18',
525035fb 20);
21
22{
9a440a19 23 local $info{Title} = "nonsense";
24 local $info{Rating} = 19;
25 eval { Film->create({%info}) };
26 ok $@, $@;
27 ok !Film->retrieve($info{Title}), "No film created";
28 is(Film->retrieve_all, 0, "So no films");
525035fb 29}
30
31ok(my $ver = Film->create({%info}), "Can create with valid rating");
32is $ver->Rating, 18, "Rating 18";
33
34ok $ver->Rating(12), "Change to 12";
35ok $ver->update, "And update";
36is $ver->Rating, 12, "Rating now 12";
37
38eval {
9a440a19 39 $ver->Rating(13);
40 $ver->update;
525035fb 41};
42ok $@, $@;
43is $ver->Rating, 12, "Rating still 12";
44ok $ver->delete, "Delete";
45
46# this threw an infinite loop in old versions
47Film->add_constraint('valid director', Director => sub { 1 });
48my $fred = Film->create({ Rating => '12' });
49
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.
52ok $fred, "Got fred";
53
54{
9a440a19 55 ok +Film->constrain_column(rating => [qw/U PG 12 15 19/]),
56 "constraint_column";
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";
525035fb 61 SKIP: {
62 skip "No column objects", 2;
9a440a19 63 ok +Film->find_column('rating')->is_constrained, "Rating is constrained";
64 ok +Film->find_column('director')->is_constrained, "Director is not";
525035fb 65 }
66}
67
68{
9a440a19 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";
525035fb 74}
75
76{
77
9a440a19 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;
84 }
85 );
86 }
87 eval { Film->constrain_column(codirector => Untaint => 'date') };
88 is $@, '', 'Can constrain with untaint';
157270d0 89
9a440a19 90 my $freeaa =
91 eval { Film->create({ title => "The Freaa", codirector => 'today' }) };
157270d0 92 is $@, '', "Can create codirector";
93 is $freeaa && $freeaa->codirector, '2001-03-03', "Set the codirector";
525035fb 94}
95
d9bd5195 96done_testing;
97
525035fb 98__DATA__
99
100use CGI::Untaint;
101
102sub _constrain_by_untaint {
9a440a19 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;
109 return 1;
110 });
525035fb 111}