/[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 653 by dpavlin, Thu Sep 7 15:57:48 2006 UTC revision 862 by dpavlin, Sun May 27 19:28:18 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 => 29;  use Test::More tests => 54;
5  use Test::Exception;  use Test::Exception;
6  use blib;  use blib;
7    
# 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    
20  throws_ok { new WebPAC::Validate( no_log => 1 ) } qr/need path/, "new without path";  ok(my $v = new WebPAC::Validate(
21            debug => $debug,
22    ), "new witout path");
23    
24    ok( ! $v->{rules}, 'no path' );
25    
26  ok(my $v = new WebPAC::Validate(  ok(my $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 {  sub test_v {
70          my $row = shift || die "no row?";          my $row = shift || die "no row?";
# Line 41  sub test_v { Line 73  sub test_v {
73    
74          $row->{'000'} = [ 42 ];          $row->{'000'} = [ 42 ];
75    
76          $v->reset_errors;          $v->reset;
77          my $e = $v->validate_errors( $row );          my $e = $v->validate_rec( $row );
78    
79          diag "validate $d\n",dump($e) if ($debug);          diag "validate $d\n",dump($e) if ($debug);
80    
# Line 50  sub test_v { Line 82  sub test_v {
82                  my $tmp = $e;                  my $tmp = $e;
83                  while (@_) {                  while (@_) {
84                          my $k = shift @_;                          my $k = shift @_;
85                          ok($tmp = $tmp->{$k}, "found $k");                          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                  }                  }
                 diag "additional message: $tmp" if ($tmp);  
95          } else {          } else {
96                  ok(! $e, "validated $d");                  ok(! $e, "validated $d");
97                  diag "expected error: ", dump($e) if($e);                  diag "expected error: ", dump($e) if($e);
# Line 62  sub test_v { Line 101  sub test_v {
101    
102  test_v({  test_v({
103          '900' => 'foo'          '900' => 'foo'
104  }, qw/field 900 not_repeatable/);  }, qw/900 not_repeatable/);
105    
106  test_v({  test_v({
107          '900' => [ qw/foo bar baz/ ]          '900' => [ qw/foo bar baz/ ]
# Line 70  test_v({ Line 109  test_v({
109    
110  test_v({  test_v({
111          '901' => [ qw/foo bar baz/ ]          '901' => [ qw/foo bar baz/ ]
112  }, qw/field 901 missing_subfield/);  }, qw/901 missing_subfield/);
113    
114  test_v({  test_v({
115          '901' => [ { 'a' => 42 } ]          '901' => [ { 'a' => 42 } ]
# Line 78  test_v({ Line 117  test_v({
117    
118  test_v({  test_v({
119          '901' => [ { 'b' => 42 } ]          '901' => [ { 'b' => 42 } ]
120  }, qw/field 901 subfield b/);  }, qw/901 subfield extra b/);
121    
122  test_v({  test_v({
123          '902' => [ { 'b' => 1 }, { 'c' => 2 } ]          '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
# Line 86  test_v({ Line 125  test_v({
125    
126  test_v({  test_v({
127          '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]          '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
128  }, qw/field 902 subfield a/);  }, qw/902 subfield extra a/);
129    
130  test_v({  test_v({
131          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
# Line 94  test_v({ Line 133  test_v({
133    
134  test_v({  test_v({
135          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
136  }, qw/field 903 subfield 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" },
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.653  
changed lines
  Added in v.862

  ViewVC Help
Powered by ViewVC 1.1.26