Slice support for iterators
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 13-constraint.t
CommitLineData
525035fb 1use strict;
2use Test::More;
3
4BEGIN {
5 eval "use DBD::SQLite";
6 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 23);
7}
8
9use lib 't/testlib';
10use Film;
11
12sub valid_rating {
13 my $value = shift;
14 my $ok = grep $value eq $_, qw/U Uc PG 12 15 18/;
15 return $ok;
16}
17
18Film->add_constraint('valid rating', Rating => \&valid_rating);
19
20my %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
35ok(my $ver = Film->create({%info}), "Can create with valid rating");
36is $ver->Rating, 18, "Rating 18";
37
38ok $ver->Rating(12), "Change to 12";
39ok $ver->update, "And update";
40is $ver->Rating, 12, "Rating now 12";
41
42eval {
43 $ver->Rating(13);
44 $ver->update;
45};
46ok $@, $@;
47is $ver->Rating, 12, "Rating still 12";
48ok $ver->delete, "Delete";
49
50# this threw an infinite loop in old versions
51Film->add_constraint('valid director', Director => sub { 1 });
52my $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.
56ok $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
101use CGI::Untaint;
102
103sub _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