1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use Test::More tests => 43; |
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 |
throws_ok { new WebPAC::Validate( no_log => 1 ) } qr/need path/, "new without path"; |
21 |
|
22 |
ok(my $v = new WebPAC::Validate( |
23 |
path => "$abs_path/data/validate_test", |
24 |
), "new"); |
25 |
|
26 |
ok($v->{rules}, "rules exist"); |
27 |
|
28 |
is_deeply( $v->{rules}, { |
29 |
'900' => 1, |
30 |
'901' => [ 'a' ], |
31 |
'902' => [ 'b', 'c' ], |
32 |
'903' => [ 'a', 'b', 'c' ], |
33 |
'904' => [ 'a' ], |
34 |
}, 'rules parsed'); |
35 |
|
36 |
|
37 |
|
38 |
throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec"; |
39 |
|
40 |
sub test_v { |
41 |
my $row = shift || die "no row?"; |
42 |
|
43 |
my $d = dump( $row ); |
44 |
|
45 |
$row->{'000'} = [ 42 ]; |
46 |
|
47 |
$v->reset_errors; |
48 |
my $e = $v->validate_errors( $row ); |
49 |
|
50 |
diag "validate $d\n",dump($e) if ($debug); |
51 |
|
52 |
if (@_) { |
53 |
my $tmp = $e; |
54 |
while (@_) { |
55 |
my $k = shift @_; |
56 |
ok($tmp = $tmp->{$k}, "found $k"); |
57 |
} |
58 |
diag "tmp: ",dump($tmp) if ($debug); |
59 |
if ($tmp) { |
60 |
if (ref($tmp) eq 'HASH') { |
61 |
return $tmp; |
62 |
} else { |
63 |
diag "explanation: $tmp"; |
64 |
} |
65 |
} |
66 |
} else { |
67 |
ok(! $e, "validated $d"); |
68 |
diag "expected error: ", dump($e) if($e); |
69 |
} |
70 |
|
71 |
} |
72 |
|
73 |
test_v({ |
74 |
'900' => 'foo' |
75 |
}, qw/field 900 not_repeatable/); |
76 |
|
77 |
test_v({ |
78 |
'900' => [ qw/foo bar baz/ ] |
79 |
}); |
80 |
|
81 |
test_v({ |
82 |
'901' => [ qw/foo bar baz/ ] |
83 |
}, qw/field 901 missing_subfield/); |
84 |
|
85 |
test_v({ |
86 |
'901' => [ { 'a' => 42 } ] |
87 |
}); |
88 |
|
89 |
test_v({ |
90 |
'901' => [ { 'b' => 42 } ] |
91 |
}, qw/field 901 subfield extra b/); |
92 |
|
93 |
test_v({ |
94 |
'902' => [ { 'b' => 1 }, { 'c' => 2 } ] |
95 |
}); |
96 |
|
97 |
test_v({ |
98 |
'902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] |
99 |
}, qw/field 902 subfield extra a/); |
100 |
|
101 |
test_v({ |
102 |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] |
103 |
}); |
104 |
|
105 |
test_v({ |
106 |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ] |
107 |
}, qw/field 903 subfield extra d/); |
108 |
|
109 |
is_deeply( |
110 |
|
111 |
test_v({ |
112 |
'903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ] |
113 |
}, qw/field 903 subfield extra/), |
114 |
|
115 |
{ 'd' => 1, 'e' => 1 }, 'additional fields d, e'); |
116 |
|
117 |
test_v({ |
118 |
'904' => [ { 'a' => 1, } ] |
119 |
}); |
120 |
|
121 |
test_v({ |
122 |
'904' => [ { 'b' => 1 } ] |
123 |
}, qw/field 904 subfield extra b/); |
124 |
|