/[webpac2]/trunk/t/1-validate.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/t/1-validate.t

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 515 by dpavlin, Tue May 16 15:23:05 2006 UTC revision 906 by dpavlin, Mon Oct 29 22:49:46 2007 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use strict;  use strict;
4  use Test::More tests => 16;  use Test::More tests => 54;
5  use Test::Exception;  use Test::Exception;
6  use blib;  use blib;
7    
8  use Data::Dumper;  use Data::Dump qw/dump/;
9  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
10    
11  BEGIN {  BEGIN {
# Line 17  my $debug = shift @ARGV; Line 17  my $debug = shift @ARGV;
17  ok(my $abs_path = abs_path($0), "abs_path");  ok(my $abs_path = abs_path($0), "abs_path");
18  $abs_path =~ s#/[^/]*$#/#;  $abs_path =~ s#/[^/]*$#/#;
19    
 throws_ok { new WebPAC::Validate( no_log => 1 ) } qr/need path/, "new without path";  
   
20  ok(my $v = new WebPAC::Validate(  ok(my $v = new WebPAC::Validate(
21            debug => $debug,
22    ), "new witout path");
23    
24    ok( ! $v->{rules}, 'no path' );
25    
26    ok($v = new WebPAC::Validate(
27          path => "$abs_path/data/validate_test",          path => "$abs_path/data/validate_test",
28  ), "new");          debug => $debug,
29    ), "new with path");
30    
31  ok($v->{rules}, "rules exist");  ok($v->{rules}, "rules exist");
32    
# Line 30  is_deeply( $v->{rules}, { Line 35  is_deeply( $v->{rules}, {
35          '901' => [ 'a' ],          '901' => [ 'a' ],
36          '902' => [ 'b', 'c' ],          '902' => [ 'b', 'c' ],
37          '903' => [ 'a', 'b', 'c' ],          '903' => [ 'a', 'b', 'c' ],
38            '904' => [ 'a' ],
39            '905' => [ 'a*' ],
40    }, 'simple rules parsed');
41    
42    diag dump( $v ) if ( $debug );
43    
44    ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" );
45    
46    diag dump( $v ) if ( $debug );
47    
48    ok($v->{rules}, "rules exist");
49    
50    is_deeply( $v->{rules}, {
51            '900' => [ 'a', 'b', 'c', 'd' ],
52  }, 'rules parsed');  }, 'rules parsed');
53    
54  throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec";  ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" );
55    
56    is_deeply( $v->{rules}, {
57            '900' => 1,
58            '901' => [ 'a' ],
59            '902' => [ 'b', 'c' ],
60            '903' => [ 'a', 'b', 'c' ],
61            '904' => [ 'a' ],
62            '905' => [ 'a*' ],
63    }, 'rules');
64    
65    ok($v->{rules}, "rules exist");
66    
67    throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec";
68    
69    sub test_v {
70            my $row = shift || die "no row?";
71    
72            my $d = dump( $row );
73    
74  ok($v->validate_errors({ '900' => 'foo' }), '900 garbage input');          $row->{'000'} = [ 42 ];
75    
76  ok(! $v->validate_errors({          $v->reset;
77            my $e = $v->validate_rec( $row );
78    
79            diag "validate $d\n",dump($e) if ($debug);
80    
81            if (@_) {
82                    my $tmp = $e;
83                    while (@_) {
84                            my $k = shift @_;
85                            ok($tmp = $tmp->{$k}, "found $k") if (defined($k));
86                    }
87                    diag "tmp: ",dump($tmp) if ($debug);
88                    if ($tmp) {
89                            if (ref($tmp) eq 'HASH') {
90                                    return $tmp;
91                            } else {
92                                    diag "explanation: $tmp";
93                            }
94                    }
95            } else {
96                    ok(! $e, "validated $d");
97                    diag "expected error: ", dump($e) if($e);
98            }
99    
100    }
101    
102    test_v({
103            '900' => 'foo'
104    }, qw/900 not_repeatable/);
105    
106    test_v({
107          '900' => [ qw/foo bar baz/ ]          '900' => [ qw/foo bar baz/ ]
108  }), '900 no sf');  });
109    
110  ok($v->validate_errors({  test_v({
111          '901' => [ qw/foo bar baz/ ]          '901' => [ qw/foo bar baz/ ]
112  }), '901 no sf');  }, qw/901 missing_subfield/);
113    
114  ok(! $v->validate_errors({  test_v({
115          '901' => [ { 'a' => 42 } ]          '901' => [ { 'a' => 42 } ]
116  }), '901^a');  });
117    
118  ok($v->validate_errors({  test_v({
119          '901' => [ { 'b' => 42 } ]          '901' => [ { 'b' => 42 } ]
120  }), '901^b');  }, qw/901 subfield extra b/);
121    
122  ok(! $v->validate_errors({  test_v({
123          '902' => [ { 'b' => 1 }, { 'c' => 2 } ]          '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
124  }), '902^b 902^c');  });
125    
126  ok($v->validate_errors({  test_v({
127          '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]          '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
128  }), '902^a 902^b 902^c');  }, qw/902 subfield extra a/);
129    
130  ok(! $v->validate_errors({  test_v({
131          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
132  }), '903^a 903^b 903^c');  });
133    
134  ok($v->validate_errors({  test_v({
135          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
136  }), '903^a 903^b 903^c 903^d');  }, qw/903 subfield extra d/);
137    
138    is_deeply(
139            test_v({
140                    '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]
141            }, qw/903 subfield extra/),
142    { 'd' => 1, 'e' => 1 }, 'additional fields d, e');
143    
144    test_v({
145            '904' => [ { 'a' => 1, } ]
146    });
147    
148    test_v({
149            '904' => [ { 'b' => 1 } ]
150    }, qw/904 subfield extra b/);
151    
152    test_v({
153            '904' => [ { 'a' => [ 1,2 ] } ]
154    }, qw/904 subfield extra_repeatable a/);
155    
156    test_v({
157            '905' => [ { 'a' => [ 1,2 ] } ]
158    });
159    
160    test_v({
161            '905' => [ ]
162    });
163    
164    my $expected_error = {
165       900 => { not_repeatable => "probably bug in parsing input data" },
166       901 => { missing_subfield => "a required", "dump" => "baz" },
167       902 => {
168                "dump"   => "^a1^b1^b2",
169                subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
170              },
171       903 => {
172                "dump"   => "^a1^a2^c1",
173                subfield => { extra_repeatable => { a => 1 } },
174              },
175       904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } },
176    };
177    
178    
179    is_deeply(
180            test_v({
181                    '900' => 'foo',
182                    '901' => [ qw/foo bar baz/ ],
183                    '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],
184                    '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],
185                    '904' => [ { 'b' => 1 } ],
186                    '905' => [ { 'a' => 1 } ],
187            }, undef),
188    $expected_error, 'validate without subfields');
189    
190    ok(my $r1 = $v->report, 'report');
191    
192    diag "report: $r1" if ( $debug );
193    
194    is_deeply(
195            test_v({
196                    '900' => 'foo',
197                    '901' => [ qw/foo bar baz/ ],
198                    '902' => [ { 'a' => 1, 'b' => [ 1,2 ], subfields => [ qw/a 0 b 0 b 1/ ] } ],
199                    '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, subfields => [ qw/a 0 a 1 c 0/ ] } ],
200                    '904' => [ { 'b' => 1, subfields => [ qw/b 0/ ] } ],
201                    '905' => [ { 'a' => 1, subfields => [ qw/a 0/ ] } ],
202            }, undef),
203    $expected_error, 'validate with subfields');
204    
205    ok(my $r2 = $v->report, 'report');
206    
207    cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');

Legend:
Removed from v.515  
changed lines
  Added in v.906

  ViewVC Help
Powered by ViewVC 1.1.26