/[webpac2]/trunk/t/1-validate.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/t/1-validate.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 665 - (show annotations)
Mon Sep 11 11:57:30 2006 UTC (17 years, 8 months ago) by dpavlin
File MIME type: application/x-troff
File size: 2649 byte(s)
 r929@llin:  dpavlin | 2006-09-11 13:56:02 +0200
 another cut at simplification of report

1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More tests => 51;
5 use Test::Exception;
6 use blib;
7
8 use Data::Dump qw/dump/;
9 use Cwd qw/abs_path/;
10
11 BEGIN {
12 use_ok( 'WebPAC::Validate' );
13 }
14
15 my $debug = shift @ARGV;
16
17 ok(my $abs_path = abs_path($0), "abs_path");
18 $abs_path =~ s#/[^/]*$#/#;
19
20 throws_ok { new WebPAC::Validate( no_log => 1 ) } qr/need path/, "new without path";
21
22 ok(my $v = new WebPAC::Validate(
23 path => "$abs_path/data/validate_test",
24 ), "new");
25
26 ok($v->{rules}, "rules exist");
27
28 is_deeply( $v->{rules}, {
29 '900' => 1,
30 '901' => [ 'a' ],
31 '902' => [ 'b', 'c' ],
32 '903' => [ 'a', 'b', 'c' ],
33 '904' => [ 'a' ],
34 '905' => [ 'a*' ],
35 }, 'rules parsed');
36
37
38
39 throws_ok { $v->validate_errors() } qr/rec/, "validate_rec need rec";
40
41 sub test_v {
42 my $row = shift || die "no row?";
43
44 my $d = dump( $row );
45
46 $row->{'000'} = [ 42 ];
47
48 $v->reset_errors;
49 my $e = $v->validate_errors( $row );
50
51 diag "validate $d\n",dump($e) if ($debug);
52
53 if (@_) {
54 my $tmp = $e;
55 while (@_) {
56 my $k = shift @_;
57 ok($tmp = $tmp->{$k}, "found $k");
58 }
59 diag "tmp: ",dump($tmp) if ($debug);
60 if ($tmp) {
61 if (ref($tmp) eq 'HASH') {
62 return $tmp;
63 } else {
64 diag "explanation: $tmp";
65 }
66 }
67 } else {
68 ok(! $e, "validated $d");
69 diag "expected error: ", dump($e) if($e);
70 }
71
72 }
73
74 test_v({
75 '900' => 'foo'
76 }, qw/field 900 not_repeatable/);
77
78 test_v({
79 '900' => [ qw/foo bar baz/ ]
80 });
81
82 test_v({
83 '901' => [ qw/foo bar baz/ ]
84 }, qw/field 901 missing_subfield/);
85
86 test_v({
87 '901' => [ { 'a' => 42 } ]
88 });
89
90 test_v({
91 '901' => [ { 'b' => 42 } ]
92 }, qw/field 901 subfield extra b/);
93
94 test_v({
95 '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
96 });
97
98 test_v({
99 '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
100 }, qw/field 902 subfield extra a/);
101
102 test_v({
103 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
104 });
105
106 test_v({
107 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
108 }, qw/field 903 subfield extra d/);
109
110 is_deeply(
111
112 test_v({
113 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 }, { 'e' => 4 } ]
114 }, qw/field 903 subfield extra/),
115 { 'd' => 1, 'e' => 1 }, 'additional fields d, e');
116
117 test_v({
118 '904' => [ { 'a' => 1, } ]
119 });
120
121 test_v({
122 '904' => [ { 'b' => 1 } ]
123 }, qw/field 904 subfield extra b/);
124
125 test_v({
126 '904' => [ { 'a' => [ 1,2 ] } ]
127 }, qw/field 904 subfield extra_repeatable a/);
128
129 test_v({
130 '905' => [ { 'a' => [ 1,2 ] } ]
131 });
132
133 test_v({
134 '905' => [ ]
135 });
136
137 test_v({
138 '900' => 'foo',
139 '901' => [ qw/foo bar baz/ ],
140 '902' => [ { 'a' => 1, 'b' => [ 1,2 ] } ],
141 '903' => [ { 'a' => [ 1, 2 ], 'c' => 1, } ],
142 '904' => [ { 'b' => 1 } ],
143 '905' => [ { 'a' => 1 } ],
144 });
145
146 diag "errors: ",dump( $v->all_errors );
147
148 print $v->report;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26