Switch CDBICompat and its tests to OptDeps
[dbsrgits/DBIx-Class-Historic.git] / t / cdbi / 13-constraint.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 use lib 't/cdbi/testlib';
10 use Film;
11
12 sub valid_rating {
13     my $value = shift;
14     my $ok = grep $value eq $_, qw/U Uc PG 12 15 18/;
15     return $ok;
16 }
17
18 Film->add_constraint('valid rating', Rating => \&valid_rating);
19
20 my %info = (
21     Title    => 'La Double Vie De Veronique',
22     Director => 'Kryzstof Kieslowski',
23     Rating   => '18',
24 );
25
26 {
27     local $info{Title}  = "nonsense";
28     local $info{Rating} = 19;
29     eval { Film->create({%info}) };
30     ok $@, $@;
31     ok !Film->retrieve($info{Title}), "No film created";
32     is(Film->retrieve_all, 0, "So no films");
33 }
34
35 ok(my $ver = Film->create({%info}), "Can create with valid rating");
36 is $ver->Rating, 18, "Rating 18";
37
38 ok $ver->Rating(12), "Change to 12";
39 ok $ver->update, "And update";
40 is $ver->Rating, 12, "Rating now 12";
41
42 eval {
43     $ver->Rating(13);
44     $ver->update;
45 };
46 ok $@, $@;
47 is $ver->Rating, 12, "Rating still 12";
48 ok $ver->delete, "Delete";
49
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' });
53
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.
56 ok $fred, "Got fred";
57
58 {
59     ok +Film->constrain_column(rating => [qw/U PG 12 15 19/]),
60         "constraint_column";
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";
65     {
66       local $TODO = "No column objects";
67       lives_ok { Film->find_column('rating')->is_constrained || die } "Rating is constrained";
68       lives_ok { Film->find_column('director')->is_constrained || die } "Director is not";
69     }
70 }
71
72 {
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";
78 }
79
80 {
81
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;
88             }
89         );
90     }
91     eval { Film->constrain_column(codirector => Untaint => 'date') };
92     is $@, '', 'Can constrain with untaint';
93
94     my $freeaa =
95         eval { Film->create({ title => "The Freaa", codirector => 'today' }) };
96     is $@, '', "Can create codirector";
97     is $freeaa && $freeaa->codirector, '2001-03-03', "Set the codirector";
98 }
99
100 done_testing;
101
102 __DATA__
103
104 use CGI::Untaint;
105
106 sub _constrain_by_untaint {
107     my ($class, $col, $string, $type) = @_;
108     $class->add_constraint(untaint => $col => sub {
109         my ($value, $self, $column_name, $changing) = @_;
110         my $h = CGI::Untaint->new({ %$changing });
111         return unless my $val = $h->extract("-as_$type" => $column_name);
112         $changing->{$column_name} = $val;
113         return 1;
114     });
115 }