6 use Test::More tests => 54;
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' ],
31 }, 'simple rules parsed');
33 diag dump( $v ) if ( $debug );
35 ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" );
37 diag dump( $v ) if ( $debug );
39 ok($v->{rules}, "rules exist");
41 is_deeply( $v->{rules}, {
42 '900' => [ 'a', 'b', 'c', 'd' ],
45 ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" );
47 is_deeply( $v->{rules}, {
50 '902' => [ 'b', 'c' ],
51 '903' => [ 'a', 'b', 'c' ],
56 ok($v->{rules}, "rules exist");
58 throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec";
61 my $row = shift || die "no row?";
65 $row->{'000'} = [ 42 ];
68 my $e = $v->validate_rec( $row );
70 diag "validate $d\n",dump($e) if ($debug);
76 ok($tmp = $tmp->{$k}, "found $k") if (defined($k));
78 diag "tmp: ",dump($tmp) if ($debug);
80 if (ref($tmp) eq 'HASH') {
83 diag "explanation: $tmp" if $debug;
87 ok(! $e, "validated $d");
88 diag "expected error: ", dump($e) if($e);
95 }, qw/900 not_repeatable/);
98 '900' => [ qw/foo bar baz/ ]
102 '901' => [ qw/foo bar baz/ ]
103 }, qw/901 missing_subfield/);
106 '901' => [ { 'a' => 42 } ]
110 '901' => [ { 'b' => 42 } ]
111 }, qw/901 subfield extra b/);
114 '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
118 '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
119 }, qw/902 subfield extra a/);
122 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
126 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
127 }, qw/903 subfield extra d/);
131 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]
132 }, qw/903 subfield extra/),
133 { 'd' => 1, 'e' => 1 }, 'additional fields d, e');
136 '904' => [ { 'a' => 1, } ]
140 '904' => [ { 'b' => 1 } ]
141 }, qw/904 subfield extra b/);
144 '904' => [ { 'a' => [ 1,2 ] } ]
145 }, qw/904 subfield extra_repeatable a/);
148 '905' => [ { 'a' => [ 1,2 ] } ]
155 my $expected_error = {
156 900 => { not_repeatable => "probably bug in parsing input data" },
157 901 => { missing_subfield => "a required", "dump" => "baz" },
159 "dump" => "^a1^b1^b2",
160 subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
163 "dump" => "^a1^a2^c1",
164 subfield => { extra_repeatable => { a => 1 } },
166 904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } },
173 '901' => [ qw/foo bar baz/ ],
174 '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],
175 '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],
176 '904' => [ { 'b' => 1 } ],
177 '905' => [ { 'a' => 1 } ],
179 $expected_error, 'validate without subfields');
181 ok(my $r1 = $v->report, 'report');
183 diag "report: $r1" if ( $debug );
188 '901' => [ qw/foo bar baz/ ],
189 '902' => [ { 'a' => 1, 'b' => [ 1,2 ], subfields => [ qw/a 0 b 0 b 1/ ] } ],
190 '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, subfields => [ qw/a 0 a 1 c 0/ ] } ],
191 '904' => [ { 'b' => 1, subfields => [ qw/b 0/ ] } ],
192 '905' => [ { 'a' => 1, subfields => [ qw/a 0/ ] } ],
194 $expected_error, 'validate with subfields');
196 ok(my $r2 = $v->report, 'report');
198 cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');