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