r966@llin: dpavlin | 2006-09-24 19:22:45 +0200
[webpac2] / t / 1-validate.t
index f01881d..c6abeec 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl -w
 
 use strict;
-use Test::More tests => 29;
+use Test::More tests => 47;
 use Test::Exception;
 use blib;
 
@@ -21,6 +21,7 @@ throws_ok { new WebPAC::Validate( no_log => 1 ) } qr/need path/, "new without pa
 
 ok(my $v = new WebPAC::Validate(
        path => "$abs_path/data/validate_test",
+       debug => $debug,
 ), "new");
 
 ok($v->{rules}, "rules exist");
@@ -30,8 +31,12 @@ is_deeply( $v->{rules}, {
        '901' => [ 'a' ],
        '902' => [ 'b', 'c' ],
        '903' => [ 'a', 'b', 'c' ],
+       '904' => [ 'a' ],
+       '905' => [ 'a*' ],
 }, 'rules parsed');
 
+
+
 throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec";
 
 sub test_v {
@@ -50,9 +55,16 @@ 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 "additional message: $tmp" if ($tmp);
        } else {
                ok(! $e, "validated $d");
                diag "expected error: ", dump($e) if($e);
@@ -62,7 +74,7 @@ sub test_v {
 
 test_v({
        '900' => 'foo'
-}, qw/field 900 not_repeatable/);
+}, qw/900 not_repeatable/);
 
 test_v({
        '900' => [ qw/foo bar baz/ ]
@@ -70,7 +82,7 @@ test_v({
 
 test_v({
        '901' => [ qw/foo bar baz/ ]
-}, qw/field 901 missing_subfield/);
+}, qw/901 missing_subfield/);
 
 test_v({
        '901' => [ { 'a' => 42 } ]
@@ -78,7 +90,7 @@ test_v({
 
 test_v({
        '901' => [ { 'b' => 42 } ]
-}, qw/field 901 subfield b/);
+}, qw/901 subfield extra b/);
 
 test_v({
        '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
@@ -86,7 +98,7 @@ test_v({
 
 test_v({
        '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
-}, qw/field 902 subfield a/);
+}, qw/902 subfield extra a/);
 
 test_v({
        '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
@@ -94,5 +106,74 @@ test_v({
 
 test_v({
        '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
-}, qw/field 903 subfield d/);
+}, qw/903 subfield extra d/);
+
+is_deeply(
+       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({
+       '904' => [ { 'a' => 1, } ]
+});
+
+test_v({
+       '904' => [ { 'b' => 1 } ]
+}, qw/904 subfield extra b/);
+
+test_v({
+       '904' => [ { 'a' => [ 1,2 ] } ]
+}, qw/904 subfield extra_repeatable a/);
+
+test_v({
+       '905' => [ { 'a' => [ 1,2 ] } ]
+});
+
+test_v({
+       '905' => [ ]
+});
 
+my $expected_error = {
+   900 => { not_repeatable => "probably bug in parsing input data" },
+   901 => { missing_subfield => "a required" },
+   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');
+
+ok(my $r1 = $v->report, 'report');
+
+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');