1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use Test::More tests => 51; |
5 |
use Test::Exception; |
6 |
use blib; |
7 |
|
8 |
use Data::Dump qw/dump/; |
9 |
use Cwd qw/abs_path/; |
10 |
|
11 |
BEGIN { |
12 |
use_ok( 'WebPAC::Validate' ); |
13 |
} |
14 |
|
15 |
my $debug = shift @ARGV; |
16 |
|
17 |
ok(my $abs_path = abs_path($0), "abs_path"); |
18 |
$abs_path =~ s#/[^/]*$#/#; |
19 |
|
20 |
my $validate_path = "$abs_path/data/validate_test"; |
21 |
$validate_path =~ s#//+#/#g; |
22 |
|
23 |
ok(my $v = new WebPAC::Validate( |
24 |
path => $validate_path, |
25 |
debug => $debug, |
26 |
), "new with path"); |
27 |
|
28 |
ok($v->{rules}, "rules exist"); |
29 |
|
30 |
is_deeply( $v->{rules}, { |
31 |
'900' => 1, |
32 |
'901' => [ 'a' ], |
33 |
'902' => [ 'b', 'c' ], |
34 |
'903' => [ 'a', 'b', 'c' ], |
35 |
'904' => [ 'a' ], |
36 |
'905' => [ 'a*' ], |
37 |
}, 'rules parsed'); |
38 |
|
39 |
ok($v = new WebPAC::Validate( |
40 |
debug => $debug, |
41 |
), "new witout path"); |
42 |
|
43 |
ok( ! $v->{rules}, 'no path' ); |
44 |
|
45 |
ok( $v->read_validate_file( $validate_path ), "read_validate_file( $validate_path )" ); |
46 |
|
47 |
ok($v->{rules}, "rules exist"); |
48 |
|
49 |
is_deeply( $v->{rules}, { |
50 |
'900' => 1, |
51 |
'901' => [ 'a' ], |
52 |
'902' => [ 'b', 'c' ], |
53 |
'903' => [ 'a', 'b', 'c' ], |
54 |
'904' => [ 'a' ], |
55 |
'905' => [ 'a*' ], |
56 |
}, 'rules parsed'); |
57 |
|
58 |
|
59 |
throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec"; |
60 |
|
61 |
sub test_v { |
62 |
my $row = shift || die "no row?"; |
63 |
|
64 |
my $d = dump( $row ); |
65 |
|
66 |
$row->{'000'} = [ 42 ]; |
67 |
|
68 |
$v->reset; |
69 |
my $e = $v->validate_rec( $row ); |
70 |
|
71 |
diag "validate $d\n",dump($e) if ($debug); |
72 |
|
73 |
if (@_) { |
74 |
my $tmp = $e; |
75 |
while (@_) { |
76 |
my $k = shift @_; |
77 |
ok($tmp = $tmp->{$k}, "found $k") if (defined($k)); |
78 |
} |
79 |
diag "tmp: ",dump($tmp) if ($debug); |
80 |
if ($tmp) { |
81 |
if (ref($tmp) eq 'HASH') { |
82 |
return $tmp; |
83 |
} else { |
84 |
diag "explanation: $tmp"; |
85 |
} |
86 |
} |
87 |
} else { |
88 |
ok(! $e, "validated $d"); |
89 |
diag "expected error: ", dump($e) if($e); |
90 |
} |
91 |
|
92 |
} |
93 |
|
94 |
test_v({ |
95 |
'900' => 'foo' |
96 |
}, qw/900 not_repeatable/); |
97 |
|
98 |
test_v({ |
99 |
'900' => [ qw/foo bar baz/ ] |
100 |
}); |
101 |
|
102 |
test_v({ |
103 |
'901' => [ qw/foo bar baz/ ] |
104 |
}, qw/901 missing_subfield/); |
105 |
|
106 |
test_v({ |
107 |
'901' => [ { 'a' => 42 } ] |
108 |
}); |
109 |
|
110 |
test_v({ |
111 |
'901' => [ { 'b' => 42 } ] |
112 |
}, qw/901 subfield extra b/); |
113 |
|
114 |
test_v({ |
115 |
'902' => [ { 'b' => 1 }, { 'c' => 2 } ] |
116 |
}); |
117 |
|
118 |
test_v({ |
119 |
'902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] |
120 |
}, qw/902 subfield extra a/); |
121 |
|
122 |
test_v({ |
123 |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] |
124 |
}); |
125 |
|
126 |
test_v({ |
127 |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ] |
128 |
}, qw/903 subfield extra d/); |
129 |
|
130 |
is_deeply( |
131 |
test_v({ |
132 |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ] |
133 |
}, qw/903 subfield extra/), |
134 |
{ 'd' => 1, 'e' => 1 }, 'additional fields d, e'); |
135 |
|
136 |
test_v({ |
137 |
'904' => [ { 'a' => 1, } ] |
138 |
}); |
139 |
|
140 |
test_v({ |
141 |
'904' => [ { 'b' => 1 } ] |
142 |
}, qw/904 subfield extra b/); |
143 |
|
144 |
test_v({ |
145 |
'904' => [ { 'a' => [ 1,2 ] } ] |
146 |
}, qw/904 subfield extra_repeatable a/); |
147 |
|
148 |
test_v({ |
149 |
'905' => [ { 'a' => [ 1,2 ] } ] |
150 |
}); |
151 |
|
152 |
test_v({ |
153 |
'905' => [ ] |
154 |
}); |
155 |
|
156 |
my $expected_error = { |
157 |
900 => { not_repeatable => "probably bug in parsing input data" }, |
158 |
901 => { missing_subfield => "a required" }, |
159 |
902 => { |
160 |
"dump" => "^a1^b1^b2", |
161 |
subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } }, |
162 |
}, |
163 |
903 => { |
164 |
"dump" => "^a1^a2^c1", |
165 |
subfield => { extra_repeatable => { a => 1 } }, |
166 |
}, |
167 |
904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } }, |
168 |
}; |
169 |
|
170 |
|
171 |
is_deeply( |
172 |
test_v({ |
173 |
'900' => 'foo', |
174 |
'901' => [ qw/foo bar baz/ ], |
175 |
'902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ], |
176 |
'903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ], |
177 |
'904' => [ { 'b' => 1 } ], |
178 |
'905' => [ { 'a' => 1 } ], |
179 |
}, undef), |
180 |
$expected_error, 'validate without subfields'); |
181 |
|
182 |
ok(my $r1 = $v->report, 'report'); |
183 |
|
184 |
is_deeply( |
185 |
test_v({ |
186 |
'900' => 'foo', |
187 |
'901' => [ qw/foo bar baz/ ], |
188 |
'902' => [ { 'a' => 1, 'b' => [ 1,2 ], subfields => [ qw/a 0 b 0 b 1/ ] } ], |
189 |
'903' => [ { 'a' => [ 1, 2 ], 'c' => 1, subfields => [ qw/a 0 a 1 c 0/ ] } ], |
190 |
'904' => [ { 'b' => 1, subfields => [ qw/b 0/ ] } ], |
191 |
'905' => [ { 'a' => 1, subfields => [ qw/a 0/ ] } ], |
192 |
}, undef), |
193 |
$expected_error, 'validate with subfields'); |
194 |
|
195 |
|
196 |
ok(my $r2 = $v->report, 'report'); |
197 |
|
198 |
cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields'); |