Slice support for iterators
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 13-constraint.t
1 use strict;
2 use Test::More;
3
4 BEGIN {
5         eval "use DBD::SQLite";
6         plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 23);
7 }
8
9 use lib 't/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     SKIP: {
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";
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         my $freeaa =
94                 eval { Film->create({ title => "The Freaa", codirector => 'today' }) };
95         is $@, '', "Can create codirector";
96         is $freeaa->codirector, '2001-03-03', "Set the codirector";
97 }
98
99 __DATA__
100
101 use CGI::Untaint;
102
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;
110                 return 1;
111         });
112 }
113