1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
use strict; |
use strict; |
4 |
use Test::More tests => 47; |
use Test::More tests => 54; |
5 |
use Test::Exception; |
use Test::Exception; |
6 |
use blib; |
use blib; |
7 |
|
|
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 |
|
debug => $debug, |
22 |
|
), "new witout path"); |
23 |
|
|
24 |
|
ok( ! $v->{rules}, 'no path' ); |
25 |
|
|
26 |
|
ok($v = new WebPAC::Validate( |
27 |
path => "$abs_path/data/validate_test", |
path => "$abs_path/data/validate_test", |
28 |
debug => $debug, |
debug => $debug, |
29 |
), "new"); |
), "new with path"); |
30 |
|
|
31 |
ok($v->{rules}, "rules exist"); |
ok($v->{rules}, "rules exist"); |
32 |
|
|
37 |
'903' => [ 'a', 'b', 'c' ], |
'903' => [ 'a', 'b', 'c' ], |
38 |
'904' => [ 'a' ], |
'904' => [ 'a' ], |
39 |
'905' => [ 'a*' ], |
'905' => [ 'a*' ], |
40 |
|
}, 'simple rules parsed'); |
41 |
|
|
42 |
|
diag dump( $v ) if ( $debug ); |
43 |
|
|
44 |
|
ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" ); |
45 |
|
|
46 |
|
diag dump( $v ) if ( $debug ); |
47 |
|
|
48 |
|
ok($v->{rules}, "rules exist"); |
49 |
|
|
50 |
|
is_deeply( $v->{rules}, { |
51 |
|
'900' => [ 'a', 'b', 'c', 'd' ], |
52 |
}, 'rules parsed'); |
}, 'rules parsed'); |
53 |
|
|
54 |
|
ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" ); |
55 |
|
|
56 |
|
is_deeply( $v->{rules}, { |
57 |
|
'900' => 1, |
58 |
|
'901' => [ 'a' ], |
59 |
|
'902' => [ 'b', 'c' ], |
60 |
|
'903' => [ 'a', 'b', 'c' ], |
61 |
|
'904' => [ 'a' ], |
62 |
|
'905' => [ 'a*' ], |
63 |
|
}, 'rules'); |
64 |
|
|
65 |
|
ok($v->{rules}, "rules exist"); |
66 |
|
|
67 |
throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec"; |
throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec"; |
68 |
|
|
69 |
sub test_v { |
sub test_v { |
70 |
my $row = shift || die "no row?"; |
my $row = shift || die "no row?"; |
73 |
|
|
74 |
$row->{'000'} = [ 42 ]; |
$row->{'000'} = [ 42 ]; |
75 |
|
|
76 |
$v->reset_errors; |
$v->reset; |
77 |
my $e = $v->validate_errors( $row ); |
my $e = $v->validate_rec( $row ); |
78 |
|
|
79 |
diag "validate $d\n",dump($e) if ($debug); |
diag "validate $d\n",dump($e) if ($debug); |
80 |
|
|
163 |
|
|
164 |
my $expected_error = { |
my $expected_error = { |
165 |
900 => { not_repeatable => "probably bug in parsing input data" }, |
900 => { not_repeatable => "probably bug in parsing input data" }, |
166 |
901 => { missing_subfield => "a required" }, |
901 => { missing_subfield => "a required", "dump" => "baz" }, |
167 |
902 => { |
902 => { |
168 |
"dump" => "^a1^b1^b2", |
"dump" => "^a1^b1^b2", |
169 |
subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } }, |
subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } }, |
189 |
|
|
190 |
ok(my $r1 = $v->report, 'report'); |
ok(my $r1 = $v->report, 'report'); |
191 |
|
|
192 |
|
diag "report: $r1" if ( $debug ); |
193 |
|
|
194 |
is_deeply( |
is_deeply( |
195 |
test_v({ |
test_v({ |
196 |
'900' => 'foo', |
'900' => 'foo', |
202 |
}, undef), |
}, undef), |
203 |
$expected_error, 'validate with subfields'); |
$expected_error, 'validate with subfields'); |
204 |
|
|
|
|
|
205 |
ok(my $r2 = $v->report, 'report'); |
ok(my $r2 = $v->report, 'report'); |
206 |
|
|
207 |
cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields'); |
cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields'); |