1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
use strict; |
use strict; |
4 |
use Test::More tests => 50; |
use Test::More tests => 47; |
5 |
use Test::Exception; |
use Test::Exception; |
6 |
use blib; |
use blib; |
7 |
|
|
21 |
|
|
22 |
ok(my $v = new WebPAC::Validate( |
ok(my $v = new WebPAC::Validate( |
23 |
path => "$abs_path/data/validate_test", |
path => "$abs_path/data/validate_test", |
24 |
|
debug => $debug, |
25 |
), "new"); |
), "new"); |
26 |
|
|
27 |
ok($v->{rules}, "rules exist"); |
ok($v->{rules}, "rules exist"); |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec"; |
throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec"; |
41 |
|
|
42 |
sub test_v { |
sub test_v { |
43 |
my $row = shift || die "no row?"; |
my $row = shift || die "no row?"; |
46 |
|
|
47 |
$row->{'000'} = [ 42 ]; |
$row->{'000'} = [ 42 ]; |
48 |
|
|
49 |
$v->reset_errors; |
$v->reset; |
50 |
my $e = $v->validate_errors( $row ); |
my $e = $v->validate_rec( $row ); |
51 |
|
|
52 |
diag "validate $d\n",dump($e) if ($debug); |
diag "validate $d\n",dump($e) if ($debug); |
53 |
|
|
55 |
my $tmp = $e; |
my $tmp = $e; |
56 |
while (@_) { |
while (@_) { |
57 |
my $k = shift @_; |
my $k = shift @_; |
58 |
ok($tmp = $tmp->{$k}, "found $k"); |
ok($tmp = $tmp->{$k}, "found $k") if (defined($k)); |
59 |
} |
} |
60 |
diag "tmp: ",dump($tmp) if ($debug); |
diag "tmp: ",dump($tmp) if ($debug); |
61 |
if ($tmp) { |
if ($tmp) { |
74 |
|
|
75 |
test_v({ |
test_v({ |
76 |
'900' => 'foo' |
'900' => 'foo' |
77 |
}, qw/field 900 not_repeatable/); |
}, qw/900 not_repeatable/); |
78 |
|
|
79 |
test_v({ |
test_v({ |
80 |
'900' => [ qw/foo bar baz/ ] |
'900' => [ qw/foo bar baz/ ] |
82 |
|
|
83 |
test_v({ |
test_v({ |
84 |
'901' => [ qw/foo bar baz/ ] |
'901' => [ qw/foo bar baz/ ] |
85 |
}, qw/field 901 missing_subfield/); |
}, qw/901 missing_subfield/); |
86 |
|
|
87 |
test_v({ |
test_v({ |
88 |
'901' => [ { 'a' => 42 } ] |
'901' => [ { 'a' => 42 } ] |
90 |
|
|
91 |
test_v({ |
test_v({ |
92 |
'901' => [ { 'b' => 42 } ] |
'901' => [ { 'b' => 42 } ] |
93 |
}, qw/field 901 subfield extra b/); |
}, qw/901 subfield extra b/); |
94 |
|
|
95 |
test_v({ |
test_v({ |
96 |
'902' => [ { 'b' => 1 }, { 'c' => 2 } ] |
'902' => [ { 'b' => 1 }, { 'c' => 2 } ] |
98 |
|
|
99 |
test_v({ |
test_v({ |
100 |
'902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] |
'902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] |
101 |
}, qw/field 902 subfield extra a/); |
}, qw/902 subfield extra a/); |
102 |
|
|
103 |
test_v({ |
test_v({ |
104 |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] |
106 |
|
|
107 |
test_v({ |
test_v({ |
108 |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ] |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ] |
109 |
}, qw/field 903 subfield extra d/); |
}, qw/903 subfield extra d/); |
110 |
|
|
111 |
is_deeply( |
is_deeply( |
112 |
|
test_v({ |
113 |
test_v({ |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ] |
114 |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ] |
}, qw/903 subfield extra/), |
|
}, qw/field 903 subfield extra/), |
|
|
|
|
115 |
{ 'd' => 1, 'e' => 1 }, 'additional fields d, e'); |
{ 'd' => 1, 'e' => 1 }, 'additional fields d, e'); |
116 |
|
|
117 |
test_v({ |
test_v({ |
120 |
|
|
121 |
test_v({ |
test_v({ |
122 |
'904' => [ { 'b' => 1 } ] |
'904' => [ { 'b' => 1 } ] |
123 |
}, qw/field 904 subfield extra b/); |
}, qw/904 subfield extra b/); |
124 |
|
|
125 |
test_v({ |
test_v({ |
126 |
'904' => [ { 'a' => [ 1,2 ] } ] |
'904' => [ { 'a' => [ 1,2 ] } ] |
127 |
}, qw/field 904 subfield extra_repeatable a/); |
}, qw/904 subfield extra_repeatable a/); |
128 |
|
|
129 |
test_v({ |
test_v({ |
130 |
'905' => [ { 'a' => [ 1,2 ] } ] |
'905' => [ { 'a' => [ 1,2 ] } ] |
133 |
test_v({ |
test_v({ |
134 |
'905' => [ ] |
'905' => [ ] |
135 |
}); |
}); |
136 |
|
|
137 |
|
my $expected_error = { |
138 |
|
900 => { not_repeatable => "probably bug in parsing input data" }, |
139 |
|
901 => { missing_subfield => "a required" }, |
140 |
|
902 => { |
141 |
|
"dump" => "^a1^b1^b2", |
142 |
|
subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } }, |
143 |
|
}, |
144 |
|
903 => { |
145 |
|
"dump" => "^a1^a2^c1", |
146 |
|
subfield => { extra_repeatable => { a => 1 } }, |
147 |
|
}, |
148 |
|
904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } }, |
149 |
|
}; |
150 |
|
|
151 |
|
|
152 |
|
is_deeply( |
153 |
|
test_v({ |
154 |
|
'900' => 'foo', |
155 |
|
'901' => [ qw/foo bar baz/ ], |
156 |
|
'902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ], |
157 |
|
'903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ], |
158 |
|
'904' => [ { 'b' => 1 } ], |
159 |
|
'905' => [ { 'a' => 1 } ], |
160 |
|
}, undef), |
161 |
|
$expected_error, 'validate without subfields'); |
162 |
|
|
163 |
|
ok(my $r1 = $v->report, 'report'); |
164 |
|
|
165 |
|
is_deeply( |
166 |
|
test_v({ |
167 |
|
'900' => 'foo', |
168 |
|
'901' => [ qw/foo bar baz/ ], |
169 |
|
'902' => [ { 'a' => 1, 'b' => [ 1,2 ], subfields => [ qw/a 0 b 0 b 1/ ] } ], |
170 |
|
'903' => [ { 'a' => [ 1, 2 ], 'c' => 1, subfields => [ qw/a 0 a 1 c 0/ ] } ], |
171 |
|
'904' => [ { 'b' => 1, subfields => [ qw/b 0/ ] } ], |
172 |
|
'905' => [ { 'a' => 1, subfields => [ qw/a 0/ ] } ], |
173 |
|
}, undef), |
174 |
|
$expected_error, 'validate with subfields'); |
175 |
|
|
176 |
|
|
177 |
|
ok(my $r2 = $v->report, 'report'); |
178 |
|
|
179 |
|
cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields'); |