X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=t%2F1-validate.t;h=734d274d26a1f3a62415a8d1ef04c4a9bd56f587;hb=ae1abb940214aa28ff93aab2f6120f2a78030260;hp=05728cb14c26d0c5d2ab70e65b1fb703b92442f3;hpb=88a80e355757eba474e92a2fe576a2dfeb940f3e;p=webpac2 diff --git a/t/1-validate.t b/t/1-validate.t index 05728cb..734d274 100755 --- a/t/1-validate.t +++ b/t/1-validate.t @@ -1,27 +1,23 @@ #!/usr/bin/perl -w use strict; -use Test::More tests => 51; -use Test::Exception; use blib; -use Data::Dump qw/dump/; -use Cwd qw/abs_path/; +use Test::More tests => 56; BEGIN { +use_ok( 'WebPAC::Test' ); use_ok( 'WebPAC::Validate' ); } -my $debug = shift @ARGV; +ok(my $v = new WebPAC::Validate(%LOG), "new witout path"); -ok(my $abs_path = abs_path($0), "abs_path"); -$abs_path =~ s#/[^/]*$#/#; +ok( ! $v->{rules}, 'no path' ); -throws_ok { new WebPAC::Validate( no_log => 1 ) } qr/need path/, "new without path"; - -ok(my $v = new WebPAC::Validate( +ok($v = new WebPAC::Validate( path => "$abs_path/data/validate_test", -), "new"); + %LOG, +), "new with path"); ok($v->{rules}, "rules exist"); @@ -32,11 +28,36 @@ is_deeply( $v->{rules}, { '903' => [ 'a', 'b', 'c' ], '904' => [ 'a' ], '905' => [ 'a*' ], + '906' => [ '0' ], +}, 'simple rules parsed'); + +diag dump( $v ) if ( $debug ); + +ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" ); + +diag dump( $v ) if ( $debug ); + +ok($v->{rules}, "rules exist"); + +is_deeply( $v->{rules}, { + '900' => [ 'a', 'b', 'c', 'd' ], }, 'rules parsed'); +ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" ); + +is_deeply( $v->{rules}, { + '900' => 1, + '901' => [ 'a' ], + '902' => [ 'b', 'c' ], + '903' => [ 'a', 'b', 'c' ], + '904' => [ 'a' ], + '905' => [ 'a*' ], + '906' => [ '0' ], +}, 'rules'); +ok($v->{rules}, "rules exist"); -throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec"; +throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec"; sub test_v { my $row = shift || die "no row?"; @@ -45,8 +66,8 @@ sub test_v { $row->{'000'} = [ 42 ]; - $v->reset_errors; - my $e = $v->validate_errors( $row ); + $v->reset; + my $e = $v->validate_rec( $row ); diag "validate $d\n",dump($e) if ($debug); @@ -54,14 +75,14 @@ sub test_v { my $tmp = $e; while (@_) { my $k = shift @_; - ok($tmp = $tmp->{$k}, "found $k"); + ok($tmp = $tmp->{$k}, "found $k") if (defined($k)); } diag "tmp: ",dump($tmp) if ($debug); if ($tmp) { if (ref($tmp) eq 'HASH') { return $tmp; } else { - diag "explanation: $tmp"; + diag "explanation: $tmp" if $debug; } } } else { @@ -73,7 +94,7 @@ sub test_v { test_v({ '900' => 'foo' -}, qw/field 900 not_repeatable/); +}, qw/900 not_repeatable/); test_v({ '900' => [ qw/foo bar baz/ ] @@ -81,7 +102,7 @@ test_v({ test_v({ '901' => [ qw/foo bar baz/ ] -}, qw/field 901 missing_subfield/); +}, qw/901 missing_subfield/); test_v({ '901' => [ { 'a' => 42 } ] @@ -89,7 +110,7 @@ test_v({ test_v({ '901' => [ { 'b' => 42 } ] -}, qw/field 901 subfield extra b/); +}, qw/901 subfield extra b/); test_v({ '902' => [ { 'b' => 1 }, { 'c' => 2 } ] @@ -97,7 +118,7 @@ test_v({ test_v({ '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] -}, qw/field 902 subfield extra a/); +}, qw/902 subfield extra a/); test_v({ '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ] @@ -105,13 +126,12 @@ test_v({ test_v({ '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ] -}, qw/field 903 subfield extra d/); +}, qw/903 subfield extra d/); is_deeply( - -test_v({ - '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ] -}, qw/field 903 subfield extra/), + test_v({ + '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ] + }, qw/903 subfield extra/), { 'd' => 1, 'e' => 1 }, 'additional fields d, e'); test_v({ @@ -120,11 +140,11 @@ test_v({ test_v({ '904' => [ { 'b' => 1 } ] -}, qw/field 904 subfield extra b/); +}, qw/904 subfield extra b/); test_v({ '904' => [ { 'a' => [ 1,2 ] } ] -}, qw/field 904 subfield extra_repeatable a/); +}, qw/904 subfield extra_repeatable a/); test_v({ '905' => [ { 'a' => [ 1,2 ] } ] @@ -135,14 +155,54 @@ test_v({ }); test_v({ - '900' => 'foo', - '901' => [ qw/foo bar baz/ ], - '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ], - '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ], - '904' => [ { 'b' => 1 } ], - '905' => [ { 'a' => 1 } ], + '906' => [ ] +}); + +test_v({ + '906' => [ { '0' => 'foo' } ] }); -diag "errors: ",dump( $v->all_errors ); +my $expected_error = { + 900 => { not_repeatable => "probably bug in parsing input data" }, + 901 => { missing_subfield => "a required", "dump" => "baz" }, + 902 => { + "dump" => "^a1^b1^b2", + subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } }, + }, + 903 => { + "dump" => "^a1^a2^c1", + subfield => { extra_repeatable => { a => 1 } }, + }, + 904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } }, +}; + + +is_deeply( + test_v({ + '900' => 'foo', + '901' => [ qw/foo bar baz/ ], + '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ], + '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ], + '904' => [ { 'b' => 1 } ], + '905' => [ { 'a' => 1 } ], + }, undef), +$expected_error, 'validate without subfields'); -print $v->report; +ok(my $r1 = $v->report, 'report'); + +diag "report: $r1" if ( $debug ); + +is_deeply( + test_v({ + '900' => 'foo', + '901' => [ qw/foo bar baz/ ], + '902' => [ { 'a' => 1, 'b' => [ 1,2 ], subfields => [ qw/a 0 b 0 b 1/ ] } ], + '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, subfields => [ qw/a 0 a 1 c 0/ ] } ], + '904' => [ { 'b' => 1, subfields => [ qw/b 0/ ] } ], + '905' => [ { 'a' => 1, subfields => [ qw/a 0/ ] } ], + }, undef), +$expected_error, 'validate with subfields'); + +ok(my $r2 = $v->report, 'report'); + +cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');