4 use Test::More tests => 54;
8 use Data::Dump qw/dump/;
12 use_ok( 'WebPAC::Validate' );
15 my $debug = shift @ARGV;
17 ok(my $abs_path = abs_path($0), "abs_path");
18 $abs_path =~ s#/[^/]*$#/#;
20 ok(my $v = new WebPAC::Validate(
22 ), "new witout path");
24 ok( ! $v->{rules}, 'no path' );
26 ok($v = new WebPAC::Validate(
27 path => "$abs_path/data/validate_test",
31 ok($v->{rules}, "rules exist");
33 is_deeply( $v->{rules}, {
36 '902' => [ 'b', 'c' ],
37 '903' => [ 'a', 'b', 'c' ],
40 }, 'simple rules parsed');
42 diag dump( $v ) if ( $debug );
44 ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" );
46 diag dump( $v ) if ( $debug );
48 ok($v->{rules}, "rules exist");
50 is_deeply( $v->{rules}, {
51 '900' => [ 'a', 'b', 'c', 'd' ],
54 ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" );
56 is_deeply( $v->{rules}, {
59 '902' => [ 'b', 'c' ],
60 '903' => [ 'a', 'b', 'c' ],
65 ok($v->{rules}, "rules exist");
67 throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec";
70 my $row = shift || die "no row?";
74 $row->{'000'} = [ 42 ];
77 my $e = $v->validate_rec( $row );
79 diag "validate $d\n",dump($e) if ($debug);
85 ok($tmp = $tmp->{$k}, "found $k") if (defined($k));
87 diag "tmp: ",dump($tmp) if ($debug);
89 if (ref($tmp) eq 'HASH') {
92 diag "explanation: $tmp";
96 ok(! $e, "validated $d");
97 diag "expected error: ", dump($e) if($e);
104 }, qw/900 not_repeatable/);
107 '900' => [ qw/foo bar baz/ ]
111 '901' => [ qw/foo bar baz/ ]
112 }, qw/901 missing_subfield/);
115 '901' => [ { 'a' => 42 } ]
119 '901' => [ { 'b' => 42 } ]
120 }, qw/901 subfield extra b/);
123 '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
127 '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
128 }, qw/902 subfield extra a/);
131 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
135 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
136 }, qw/903 subfield extra d/);
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');
145 '904' => [ { 'a' => 1, } ]
149 '904' => [ { 'b' => 1 } ]
150 }, qw/904 subfield extra b/);
153 '904' => [ { 'a' => [ 1,2 ] } ]
154 }, qw/904 subfield extra_repeatable a/);
157 '905' => [ { 'a' => [ 1,2 ] } ]
164 my $expected_error = {
165 900 => { not_repeatable => "probably bug in parsing input data" },
166 901 => { missing_subfield => "a required", "dump" => "baz" },
168 "dump" => "^a1^b1^b2",
169 subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
172 "dump" => "^a1^a2^c1",
173 subfield => { extra_repeatable => { a => 1 } },
175 904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } },
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 } ],
188 $expected_error, 'validate without subfields');
190 ok(my $r1 = $v->report, 'report');
192 diag "report: $r1" if ( $debug );
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/ ] } ],
203 $expected_error, 'validate with subfields');
205 ok(my $r2 = $v->report, 'report');
207 cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');