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

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

  ViewVC Help
Powered by ViewVC 1.1.26