/[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 670 by dpavlin, Mon Sep 11 14:29:08 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 => 47;  
 use Test::Exception;  
4  use blib;  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 62  sub test_v { Line 82  sub test_v {
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 134  test_v({ Line 154  test_v({
154          '905' => [ ]          '905' => [ ]
155  });  });
156    
157    test_v({
158            '906' => [ ]
159    });
160    
161    test_v({
162            '906' => [ { '0' => 'foo' } ]
163    });
164    
165  my $expected_error = {  my $expected_error = {
166     900 => { not_repeatable => "probably bug in parsing input data" },     900 => { not_repeatable => "probably bug in parsing input data" },
167     901 => { missing_subfield => "a required" },     901 => { missing_subfield => "a required", "dump" => "baz" },
168     902 => {     902 => {
169              "dump"   => "^a1^b1^b2",              "dump"   => "^a1^b1^b2",
170              subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },              subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
# Line 162  $expected_error, 'validate without subfi Line 190  $expected_error, 'validate without subfi
190    
191  ok(my $r1 = $v->report, 'report');  ok(my $r1 = $v->report, 'report');
192    
193    diag "report: $r1" if ( $debug );
194    
195  is_deeply(  is_deeply(
196          test_v({          test_v({
197                  '900' => 'foo',                  '900' => 'foo',
# Line 173  is_deeply( Line 203  is_deeply(
203          }, undef),          }, undef),
204  $expected_error, 'validate with subfields');  $expected_error, 'validate with subfields');
205    
   
206  ok(my $r2 = $v->report, 'report');  ok(my $r2 = $v->report, 'report');
207    
208  cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');  cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');

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

  ViewVC Help
Powered by ViewVC 1.1.26