r1399@llin: dpavlin | 2007-10-31 11:19:39 +0100
[webpac2] / t / 1-validate.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More tests => 54;
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 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",
28         debug => $debug,
29 ), "new with path");
30
31 ok($v->{rules}, "rules exist");
32
33 is_deeply( $v->{rules}, {
34         '900' => 1,
35         '901' => [ 'a' ],
36         '902' => [ 'b', 'c' ],
37         '903' => [ 'a', 'b', 'c' ],
38         '904' => [ 'a' ],
39         '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');
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_rec() } qr/rec/, "validate_rec need rec";
68
69 sub test_v {
70         my $row = shift || die "no row?";
71
72         my $d = dump( $row );
73
74         $row->{'000'} = [ 42 ];
75
76         $v->reset;
77         my $e = $v->validate_rec( $row );
78
79         diag "validate $d\n",dump($e) if ($debug);
80
81         if (@_) {
82                 my $tmp = $e;
83                 while (@_) {
84                         my $k = shift @_;
85                         ok($tmp = $tmp->{$k}, "found $k") if (defined($k));
86                 }
87                 diag "tmp: ",dump($tmp) if ($debug);
88                 if ($tmp) {
89                         if (ref($tmp) eq 'HASH') {
90                                 return $tmp;
91                         } else {
92                                 diag "explanation: $tmp";
93                         }
94                 }
95         } else {
96                 ok(! $e, "validated $d");
97                 diag "expected error: ", dump($e) if($e);
98         }
99
100 }
101
102 test_v({
103         '900' => 'foo'
104 }, qw/900 not_repeatable/);
105
106 test_v({
107         '900' => [ qw/foo bar baz/ ]
108 });
109
110 test_v({
111         '901' => [ qw/foo bar baz/ ]
112 }, qw/901 missing_subfield/);
113
114 test_v({
115         '901' => [ { 'a' => 42 } ]
116 });
117
118 test_v({
119         '901' => [ { 'b' => 42 } ]
120 }, qw/901 subfield extra b/);
121
122 test_v({
123         '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
124 });
125
126 test_v({
127         '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
128 }, qw/902 subfield extra a/);
129
130 test_v({
131         '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
132 });
133
134 test_v({
135         '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
136 }, qw/903 subfield extra d/);
137
138 is_deeply(
139         test_v({
140                 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]
141         }, qw/903 subfield extra/),
142 { 'd' => 1, 'e' => 1 }, 'additional fields d, e');
143
144 test_v({
145         '904' => [ { 'a' => 1, } ]
146 });
147
148 test_v({
149         '904' => [ { 'b' => 1 } ]
150 }, qw/904 subfield extra b/);
151
152 test_v({
153         '904' => [ { 'a' => [ 1,2 ] } ]
154 }, qw/904 subfield extra_repeatable a/);
155
156 test_v({
157         '905' => [ { 'a' => [ 1,2 ] } ]
158 });
159
160 test_v({
161         '905' => [ ]
162 });
163
164 my $expected_error = {
165    900 => { not_repeatable => "probably bug in parsing input data" },
166    901 => { missing_subfield => "a required", "dump" => "baz" },
167    902 => {
168             "dump"   => "^a1^b1^b2",
169             subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
170           },
171    903 => {
172             "dump"   => "^a1^a2^c1",
173             subfield => { extra_repeatable => { a => 1 } },
174           },
175    904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } },
176 };
177
178
179 is_deeply(
180         test_v({
181                 '900' => 'foo',
182                 '901' => [ qw/foo bar baz/ ],
183                 '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],
184                 '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],
185                 '904' => [ { 'b' => 1 } ],
186                 '905' => [ { 'a' => 1 } ],
187         }, undef),
188 $expected_error, 'validate without subfields');
189
190 ok(my $r1 = $v->report, 'report');
191
192 diag "report: $r1" if ( $debug );
193
194 is_deeply(
195         test_v({
196                 '900' => 'foo',
197                 '901' => [ qw/foo bar baz/ ],
198                 '902' => [ { 'a' => 1, 'b' => [ 1,2 ], subfields => [ qw/a 0 b 0 b 1/ ] } ],
199                 '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, subfields => [ qw/a 0 a 1 c 0/ ] } ],
200                 '904' => [ { 'b' => 1, subfields => [ qw/b 0/ ] } ],
201                 '905' => [ { 'a' => 1, subfields => [ qw/a 0/ ] } ],
202         }, undef),
203 $expected_error, 'validate with subfields');
204
205 ok(my $r2 = $v->report, 'report');
206
207 cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');