/[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 665 by dpavlin, Mon Sep 11 11:57:30 2006 UTC revision 949 by dpavlin, Thu Nov 1 00:16:48 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 => 51;  
 use Test::Exception;  
4  use blib;  use blib;
5    
6  use Data::Dump qw/dump/;  use Test::More tests => 54;
 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 32  is_deeply( $v->{rules}, { Line 28  is_deeply( $v->{rules}, {
28          '903' => [ 'a', 'b', 'c' ],          '903' => [ 'a', 'b', 'c' ],
29          '904' => [ 'a' ],          '904' => [ 'a' ],
30          '905' => [ 'a*' ],          '905' => [ 'a*' ],
31    }, 'simple rules parsed');
32    
33    diag dump( $v ) if ( $debug );
34    
35    ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" );
36    
37    diag dump( $v ) if ( $debug );
38    
39    ok($v->{rules}, "rules exist");
40    
41    is_deeply( $v->{rules}, {
42            '900' => [ 'a', 'b', 'c', 'd' ],
43  }, 'rules parsed');  }, 'rules parsed');
44    
45    ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" );
46    
47    is_deeply( $v->{rules}, {
48            '900' => 1,
49            '901' => [ 'a' ],
50            '902' => [ 'b', 'c' ],
51            '903' => [ 'a', 'b', 'c' ],
52            '904' => [ 'a' ],
53            '905' => [ 'a*' ],
54    }, 'rules');
55    
56    ok($v->{rules}, "rules exist");
57    
58  throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec";  throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec";
59    
60  sub test_v {  sub test_v {
61          my $row = shift || die "no row?";          my $row = shift || die "no row?";
# Line 45  sub test_v { Line 64  sub test_v {
64    
65          $row->{'000'} = [ 42 ];          $row->{'000'} = [ 42 ];
66    
67          $v->reset_errors;          $v->reset;
68          my $e = $v->validate_errors( $row );          my $e = $v->validate_rec( $row );
69    
70          diag "validate $d\n",dump($e) if ($debug);          diag "validate $d\n",dump($e) if ($debug);
71    
# Line 54  sub test_v { Line 73  sub test_v {
73                  my $tmp = $e;                  my $tmp = $e;
74                  while (@_) {                  while (@_) {
75                          my $k = shift @_;                          my $k = shift @_;
76                          ok($tmp = $tmp->{$k}, "found $k");                          ok($tmp = $tmp->{$k}, "found $k") if (defined($k));
77                  }                  }
78                  diag "tmp: ",dump($tmp) if ($debug);                  diag "tmp: ",dump($tmp) if ($debug);
79                  if ($tmp) {                  if ($tmp) {
80                          if (ref($tmp) eq 'HASH') {                          if (ref($tmp) eq 'HASH') {
81                                  return $tmp;                                  return $tmp;
82                          } else {                          } else {
83                                  diag "explanation: $tmp";                                  diag "explanation: $tmp" if $debug;
84                          }                          }
85                  }                  }
86          } else {          } else {
# Line 73  sub test_v { Line 92  sub test_v {
92    
93  test_v({  test_v({
94          '900' => 'foo'          '900' => 'foo'
95  }, qw/field 900 not_repeatable/);  }, qw/900 not_repeatable/);
96    
97  test_v({  test_v({
98          '900' => [ qw/foo bar baz/ ]          '900' => [ qw/foo bar baz/ ]
# Line 81  test_v({ Line 100  test_v({
100    
101  test_v({  test_v({
102          '901' => [ qw/foo bar baz/ ]          '901' => [ qw/foo bar baz/ ]
103  }, qw/field 901 missing_subfield/);  }, qw/901 missing_subfield/);
104    
105  test_v({  test_v({
106          '901' => [ { 'a' => 42 } ]          '901' => [ { 'a' => 42 } ]
# Line 89  test_v({ Line 108  test_v({
108    
109  test_v({  test_v({
110          '901' => [ { 'b' => 42 } ]          '901' => [ { 'b' => 42 } ]
111  }, qw/field 901 subfield extra b/);  }, qw/901 subfield extra b/);
112    
113  test_v({  test_v({
114          '902' => [ { 'b' => 1 }, { 'c' => 2 } ]          '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
# Line 97  test_v({ Line 116  test_v({
116    
117  test_v({  test_v({
118          '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]          '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
119  }, qw/field 902 subfield extra a/);  }, qw/902 subfield extra a/);
120    
121  test_v({  test_v({
122          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
# Line 105  test_v({ Line 124  test_v({
124    
125  test_v({  test_v({
126          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
127  }, qw/field 903 subfield extra d/);  }, qw/903 subfield extra d/);
128    
129  is_deeply(  is_deeply(
130            test_v({
131  test_v({                  '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]
132          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]          }, qw/903 subfield extra/),
 }, qw/field 903 subfield extra/),  
133  { 'd' => 1, 'e' => 1 }, 'additional fields d, e');  { 'd' => 1, 'e' => 1 }, 'additional fields d, e');
134    
135  test_v({  test_v({
# Line 120  test_v({ Line 138  test_v({
138    
139  test_v({  test_v({
140          '904' => [ { 'b' => 1 } ]          '904' => [ { 'b' => 1 } ]
141  }, qw/field 904 subfield extra b/);  }, qw/904 subfield extra b/);
142    
143  test_v({  test_v({
144          '904' => [ { 'a' => [ 1,2 ] } ]          '904' => [ { 'a' => [ 1,2 ] } ]
145  }, qw/field 904 subfield extra_repeatable a/);  }, qw/904 subfield extra_repeatable a/);
146    
147  test_v({  test_v({
148          '905' => [ { 'a' => [ 1,2 ] } ]          '905' => [ { 'a' => [ 1,2 ] } ]
# Line 134  test_v({ Line 152  test_v({
152          '905' => [ ]          '905' => [ ]
153  });  });
154    
155  test_v({  my $expected_error = {
156          '900' => 'foo',     900 => { not_repeatable => "probably bug in parsing input data" },
157          '901' => [ qw/foo bar baz/ ],     901 => { missing_subfield => "a required", "dump" => "baz" },
158          '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],     902 => {
159          '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],              "dump"   => "^a1^b1^b2",
160          '904' => [ { 'b' => 1 } ],              subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
161          '905' => [ { 'a' => 1 } ],            },
162  });     903 => {
163                "dump"   => "^a1^a2^c1",
164                subfield => { extra_repeatable => { a => 1 } },
165              },
166       904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } },
167    };
168    
169    
170    is_deeply(
171            test_v({
172                    '900' => 'foo',
173                    '901' => [ qw/foo bar baz/ ],
174                    '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],
175                    '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],
176                    '904' => [ { 'b' => 1 } ],
177                    '905' => [ { 'a' => 1 } ],
178            }, undef),
179    $expected_error, 'validate without subfields');
180    
181    ok(my $r1 = $v->report, 'report');
182    
183    diag "report: $r1" if ( $debug );
184    
185    is_deeply(
186            test_v({
187                    '900' => 'foo',
188                    '901' => [ qw/foo bar baz/ ],
189                    '902' => [ { 'a' => 1, 'b' => [ 1,2 ], subfields => [ qw/a 0 b 0 b 1/ ] } ],
190                    '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, subfields => [ qw/a 0 a 1 c 0/ ] } ],
191                    '904' => [ { 'b' => 1, subfields => [ qw/b 0/ ] } ],
192                    '905' => [ { 'a' => 1, subfields => [ qw/a 0/ ] } ],
193            }, undef),
194    $expected_error, 'validate with subfields');
195    
196  diag "errors: ",dump( $v->all_errors );  ok(my $r2 = $v->report, 'report');
197    
198  print $v->report;  cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');

Legend:
Removed from v.665  
changed lines
  Added in v.949

  ViewVC Help
Powered by ViewVC 1.1.26