6 use Test::More tests => 56;
9 use_ok( 'WebPAC::Test' );
10 use_ok( 'WebPAC::Validate' );
13 ok(my $v = new WebPAC::Validate(%LOG), "new witout path");
15 ok( ! $v->{rules}, 'no path' );
17 ok($v = new WebPAC::Validate(
18 path => "$abs_path/data/validate_test",
22 ok($v->{rules}, "rules exist");
24 is_deeply( $v->{rules}, {
27 '902' => [ 'b', 'c' ],
28 '903' => [ 'a', 'b', 'c' ],
32 }, 'simple rules parsed');
34 diag dump( $v ) if ( $debug );
36 ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" );
38 diag dump( $v ) if ( $debug );
40 ok($v->{rules}, "rules exist");
42 is_deeply( $v->{rules}, {
43 '900' => [ 'a', 'b', 'c', 'd' ],
46 ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" );
48 is_deeply( $v->{rules}, {
51 '902' => [ 'b', 'c' ],
52 '903' => [ 'a', 'b', 'c' ],
58 ok($v->{rules}, "rules exist");
60 throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec";
63 my $row = shift || die "no row?";
67 $row->{'000'} = [ 42 ];
70 my $e = $v->validate_rec( $row );
72 diag "validate $d\n",dump($e) if ($debug);
78 ok($tmp = $tmp->{$k}, "found $k") if (defined($k));
80 diag "tmp: ",dump($tmp) if ($debug);
82 if (ref($tmp) eq 'HASH') {
85 diag "explanation: $tmp" if $debug;
89 ok(! $e, "validated $d");
90 diag "expected error: ", dump($e) if($e);
97 }, qw/900 not_repeatable/);
100 '900' => [ qw/foo bar baz/ ]
104 '901' => [ qw/foo bar baz/ ]
105 }, qw/901 missing_subfield/);
108 '901' => [ { 'a' => 42 } ]
112 '901' => [ { 'b' => 42 } ]
113 }, qw/901 subfield extra b/);
116 '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
120 '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
121 }, qw/902 subfield extra a/);
124 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
128 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
129 }, qw/903 subfield extra d/);
133 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]
134 }, qw/903 subfield extra/),
135 { 'd' => 1, 'e' => 1 }, 'additional fields d, e');
138 '904' => [ { 'a' => 1, } ]
142 '904' => [ { 'b' => 1 } ]
143 }, qw/904 subfield extra b/);
146 '904' => [ { 'a' => [ 1,2 ] } ]
147 }, qw/904 subfield extra_repeatable a/);
150 '905' => [ { 'a' => [ 1,2 ] } ]
162 '906' => [ { '0' => 'foo' } ]
165 my $expected_error = {
166 900 => { not_repeatable => "probably bug in parsing input data" },
167 901 => { missing_subfield => "a required", "dump" => "baz" },
169 "dump" => "^a1^b1^b2",
170 subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
173 "dump" => "^a1^a2^c1",
174 subfield => { extra_repeatable => { a => 1 } },
176 904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } },
183 '901' => [ qw/foo bar baz/ ],
184 '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],
185 '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],
186 '904' => [ { 'b' => 1 } ],
187 '905' => [ { 'a' => 1 } ],
189 $expected_error, 'validate without subfields');
191 ok(my $r1 = $v->report, 'report');
193 diag "report: $r1" if ( $debug );
198 '901' => [ qw/foo bar baz/ ],
199 '902' => [ { 'a' => 1, 'b' => [ 1,2 ], subfields => [ qw/a 0 b 0 b 1/ ] } ],
200 '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, subfields => [ qw/a 0 a 1 c 0/ ] } ],
201 '904' => [ { 'b' => 1, subfields => [ qw/b 0/ ] } ],
202 '905' => [ { 'a' => 1, subfields => [ qw/a 0/ ] } ],
204 $expected_error, 'validate with subfields');
206 ok(my $r2 = $v->report, 'report');
208 cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');