Add strict/warnings test, adjust all offenders (wow, that was a lot)
[dbsrgits/DBIx-Class.git] / t / cdbi / 13-constraint.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use lib 't/cdbi/testlib';
6 use Film;
7
8 sub valid_rating {
9     my $value = shift;
10     my $ok = grep $value eq $_, qw/U Uc PG 12 15 18/;
11     return $ok;
12 }
13
14 Film->add_constraint('valid rating', Rating => \&valid_rating);
15
16 my %info = (
17     Title    => 'La Double Vie De Veronique',
18     Director => 'Kryzstof Kieslowski',
19     Rating   => '18',
20 );
21
22 {
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");
29 }
30
31 ok(my $ver = Film->create({%info}), "Can create with valid rating");
32 is $ver->Rating, 18, "Rating 18";
33
34 ok $ver->Rating(12), "Change to 12";
35 ok $ver->update, "And update";
36 is $ver->Rating, 12, "Rating now 12";
37
38 eval {
39     $ver->Rating(13);
40     $ver->update;
41 };
42 ok $@, $@;
43 is $ver->Rating, 12, "Rating still 12";
44 ok $ver->delete, "Delete";
45
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' });
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.
52 ok $fred, "Got fred";
53
54 {
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";
61     SKIP: {
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";
65     }
66 }
67
68 {
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";
74 }
75
76 {
77
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';
89
90     my $freeaa =
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";
94 }
95
96 done_testing;
97
98 __DATA__
99
100 use CGI::Untaint;
101
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;
109         return 1;
110     });
111 }