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

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

  ViewVC Help
Powered by ViewVC 1.1.26