/[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 666 by dpavlin, Mon Sep 11 12:32:51 2006 UTC revision 1256 by dpavlin, Tue Jul 28 12:40:46 2009 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 => 51;  use lib 'lib';
 use Test::Exception;  
 use blib;  
5    
6  use Data::Dump qw/dump/;  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          debug => $debug,          %LOG,
20  ), "new");  ), "new with path");
21    
22  ok($v->{rules}, "rules exist");  ok($v->{rules}, "rules exist");
23    
# Line 33  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            '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    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_errors() } qr/rec/, "validate_rec need rec";  throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec";
61    
62  sub test_v {  sub test_v {
63          my $row = shift || die "no row?";          my $row = shift || die "no row?";
# Line 46  sub test_v { Line 66  sub test_v {
66    
67          $row->{'000'} = [ 42 ];          $row->{'000'} = [ 42 ];
68    
69          $v->reset_errors;          $v->reset;
70          my $e = $v->validate_errors( $row );          my $e = $v->validate_rec( $row );
71    
72          diag "validate $d\n",dump($e) if ($debug);          diag "validate $d\n",dump($e) if ($debug);
73    
# Line 55  sub test_v { Line 75  sub test_v {
75                  my $tmp = $e;                  my $tmp = $e;
76                  while (@_) {                  while (@_) {
77                          my $k = shift @_;                          my $k = shift @_;
78                          ok($tmp = $tmp->{$k}, "found $k");                          ok($tmp = $tmp->{$k}, "found $k") if (defined($k));
79                  }                  }
80                  diag "tmp: ",dump($tmp) if ($debug);                  diag "tmp: ",dump($tmp) if ($debug);
81                  if ($tmp) {                  if ($tmp) {
82                          if (ref($tmp) eq 'HASH') {                          if (ref($tmp) eq 'HASH') {
83                                  return $tmp;                                  return $tmp;
84                          } else {                          } else {
85                                  diag "explanation: $tmp";                                  diag "explanation: $tmp" if $debug;
86                          }                          }
87                  }                  }
88          } else {          } else {
# Line 109  test_v({ Line 129  test_v({
129  }, qw/903 subfield extra d/);  }, qw/903 subfield extra d/);
130    
131  is_deeply(  is_deeply(
132            test_v({
133  test_v({                  '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]
134          '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]          }, qw/903 subfield extra/),
 }, qw/903 subfield extra/),  
135  { 'd' => 1, 'e' => 1 }, 'additional fields d, e');  { 'd' => 1, 'e' => 1 }, 'additional fields d, e');
136    
137  test_v({  test_v({
# Line 136  test_v({ Line 155  test_v({
155  });  });
156    
157  test_v({  test_v({
158          '900' => 'foo',          '906' => [ ]
         '901' => [ qw/foo bar baz/ ],  
         '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],  
         '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],  
         '904' => [ { 'b' => 1 } ],  
         '905' => [ { 'a' => 1 } ],  
159  });  });
160    
161  diag "errors: ",dump( $v->all_errors );  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  diag "report: ", $v->report;  cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');

Legend:
Removed from v.666  
changed lines
  Added in v.1256

  ViewVC Help
Powered by ViewVC 1.1.26