/[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 655 by dpavlin, Thu Sep 7 16:54:47 2006 UTC revision 857 by dpavlin, Sun May 27 16:49:15 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 => 43;  use Test::More tests => 51;
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";  my $validate_path = "$abs_path/data/validate_test";
21    $validate_path =~ s#//+#/#g;
22    
23  ok(my $v = new WebPAC::Validate(  ok(my $v = new WebPAC::Validate(
24          path => "$abs_path/data/validate_test",          path => $validate_path,
25  ), "new");          debug => $debug,
26    ), "new with path");
27    
28  ok($v->{rules}, "rules exist");  ok($v->{rules}, "rules exist");
29    
# Line 31  is_deeply( $v->{rules}, { Line 33  is_deeply( $v->{rules}, {
33          '902' => [ 'b', 'c' ],          '902' => [ 'b', 'c' ],
34          '903' => [ 'a', 'b', 'c' ],          '903' => [ 'a', 'b', 'c' ],
35          '904' => [ 'a' ],          '904' => [ 'a' ],
36            '905' => [ 'a*' ],
37  }, 'rules parsed');  }, 'rules parsed');
38    
39    ok($v = new WebPAC::Validate(
40            debug => $debug,
41    ), "new witout path");
42    
43    ok( ! $v->{rules}, 'no path' );
44    
45  throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec";  ok( $v->read_validate_file( $validate_path ), "read_validate_file( $validate_path )" );
46    
47    ok($v->{rules}, "rules exist");
48    
49    is_deeply( $v->{rules}, {
50            '900' => 1,
51            '901' => [ 'a' ],
52            '902' => [ 'b', 'c' ],
53            '903' => [ 'a', 'b', 'c' ],
54            '904' => [ 'a' ],
55            '905' => [ 'a*' ],
56    }, 'rules parsed');
57    
58    
59    throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec";
60    
61  sub test_v {  sub test_v {
62          my $row = shift || die "no row?";          my $row = shift || die "no row?";
# Line 44  sub test_v { Line 65  sub test_v {
65    
66          $row->{'000'} = [ 42 ];          $row->{'000'} = [ 42 ];
67    
68          $v->reset_errors;          $v->reset;
69          my $e = $v->validate_errors( $row );          my $e = $v->validate_rec( $row );
70    
71          diag "validate $d\n",dump($e) if ($debug);          diag "validate $d\n",dump($e) if ($debug);
72    
# Line 53  sub test_v { Line 74  sub test_v {
74                  my $tmp = $e;                  my $tmp = $e;
75                  while (@_) {                  while (@_) {
76                          my $k = shift @_;                          my $k = shift @_;
77                          ok($tmp = $tmp->{$k}, "found $k");                          ok($tmp = $tmp->{$k}, "found $k") if (defined($k));
78                  }                  }
79                  diag "tmp: ",dump($tmp) if ($debug);                  diag "tmp: ",dump($tmp) if ($debug);
80                  if ($tmp) {                  if ($tmp) {
# Line 72  sub test_v { Line 93  sub test_v {
93    
94  test_v({  test_v({
95          '900' => 'foo'          '900' => 'foo'
96  }, qw/field 900 not_repeatable/);  }, qw/900 not_repeatable/);
97    
98  test_v({  test_v({
99          '900' => [ qw/foo bar baz/ ]          '900' => [ qw/foo bar baz/ ]
# Line 80  test_v({ Line 101  test_v({
101    
102  test_v({  test_v({
103          '901' => [ qw/foo bar baz/ ]          '901' => [ qw/foo bar baz/ ]
104  }, qw/field 901 missing_subfield/);  }, qw/901 missing_subfield/);
105    
106  test_v({  test_v({
107          '901' => [ { 'a' => 42 } ]          '901' => [ { 'a' => 42 } ]
# Line 88  test_v({ Line 109  test_v({
109    
110  test_v({  test_v({
111          '901' => [ { 'b' => 42 } ]          '901' => [ { 'b' => 42 } ]
112  }, qw/field 901 subfield extra b/);  }, qw/901 subfield extra b/);
113    
114  test_v({  test_v({
115          '902' => [ { 'b' => 1 }, { 'c' => 2 } ]          '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
# Line 96  test_v({ Line 117  test_v({
117    
118  test_v({  test_v({
119          '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]          '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
120  }, qw/field 902 subfield extra a/);  }, qw/902 subfield extra a/);
121    
122  test_v({  test_v({
123          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
# Line 104  test_v({ Line 125  test_v({
125    
126  test_v({  test_v({
127          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
128  }, qw/field 903 subfield extra d/);  }, qw/903 subfield extra d/);
129    
130  is_deeply(  is_deeply(
131            test_v({
132  test_v({                  '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]
133          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]          }, qw/903 subfield extra/),
 }, qw/field 903 subfield extra/),  
   
134  { 'd' => 1, 'e' => 1 }, 'additional fields d, e');  { 'd' => 1, 'e' => 1 }, 'additional fields d, e');
135    
136  test_v({  test_v({
# Line 120  test_v({ Line 139  test_v({
139    
140  test_v({  test_v({
141          '904' => [ { 'b' => 1 } ]          '904' => [ { 'b' => 1 } ]
142  }, qw/field 904 subfield extra b/);  }, qw/904 subfield extra b/);
143    
144    test_v({
145            '904' => [ { 'a' => [ 1,2 ] } ]
146    }, qw/904 subfield extra_repeatable a/);
147    
148    test_v({
149            '905' => [ { 'a' => [ 1,2 ] } ]
150    });
151    
152    test_v({
153            '905' => [ ]
154    });
155    
156    my $expected_error = {
157       900 => { not_repeatable => "probably bug in parsing input data" },
158       901 => { missing_subfield => "a required" },
159       902 => {
160                "dump"   => "^a1^b1^b2",
161                subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
162              },
163       903 => {
164                "dump"   => "^a1^a2^c1",
165                subfield => { extra_repeatable => { a => 1 } },
166              },
167       904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } },
168    };
169    
170    
171    is_deeply(
172            test_v({
173                    '900' => 'foo',
174                    '901' => [ qw/foo bar baz/ ],
175                    '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],
176                    '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],
177                    '904' => [ { 'b' => 1 } ],
178                    '905' => [ { 'a' => 1 } ],
179            }, undef),
180    $expected_error, 'validate without subfields');
181    
182    ok(my $r1 = $v->report, 'report');
183    
184    is_deeply(
185            test_v({
186                    '900' => 'foo',
187                    '901' => [ qw/foo bar baz/ ],
188                    '902' => [ { 'a' => 1, 'b' => [ 1,2 ], subfields => [ qw/a 0 b 0 b 1/ ] } ],
189                    '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, subfields => [ qw/a 0 a 1 c 0/ ] } ],
190                    '904' => [ { 'b' => 1, subfields => [ qw/b 0/ ] } ],
191                    '905' => [ { 'a' => 1, subfields => [ qw/a 0/ ] } ],
192            }, undef),
193    $expected_error, 'validate with subfields');
194    
195    
196    ok(my $r2 = $v->report, 'report');
197    
198    cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');

Legend:
Removed from v.655  
changed lines
  Added in v.857

  ViewVC Help
Powered by ViewVC 1.1.26