/[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 1256 - (show annotations)
Tue Jul 28 12:40:46 2009 UTC (14 years, 9 months ago) by dpavlin
File MIME type: application/x-troff
File size: 4255 byte(s)
use code from lib insted of blib

1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 'lib';
5
6 use Test::More tests => 56;
7
8 BEGIN {
9 use_ok( 'WebPAC::Test' );
10 use_ok( 'WebPAC::Validate' );
11 }
12
13 ok(my $v = new WebPAC::Validate(%LOG), "new witout path");
14
15 ok( ! $v->{rules}, 'no path' );
16
17 ok($v = new WebPAC::Validate(
18 path => "$abs_path/data/validate_test",
19 %LOG,
20 ), "new with path");
21
22 ok($v->{rules}, "rules exist");
23
24 is_deeply( $v->{rules}, {
25 '900' => 1,
26 '901' => [ 'a' ],
27 '902' => [ 'b', 'c' ],
28 '903' => [ 'a', 'b', 'c' ],
29 '904' => [ 'a' ],
30 '905' => [ 'a*' ],
31 '906' => [ '0' ],
32 }, 'simple rules parsed');
33
34 diag dump( $v ) if ( $debug );
35
36 ok( $v->read_validate_file( "$abs_path/data/validate_test_simple" ), "read_validate_file" );
37
38 diag dump( $v ) if ( $debug );
39
40 ok($v->{rules}, "rules exist");
41
42 is_deeply( $v->{rules}, {
43 '900' => [ 'a', 'b', 'c', 'd' ],
44 }, 'rules parsed');
45
46 ok( $v->read_validate_file( "$abs_path/data/validate_test" ), "read_validate_file" );
47
48 is_deeply( $v->{rules}, {
49 '900' => 1,
50 '901' => [ 'a' ],
51 '902' => [ 'b', 'c' ],
52 '903' => [ 'a', 'b', 'c' ],
53 '904' => [ 'a' ],
54 '905' => [ 'a*' ],
55 '906' => [ '0' ],
56 }, 'rules');
57
58 ok($v->{rules}, "rules exist");
59
60 throws_ok { $v->validate_rec() } qr/rec/, "validate_rec need rec";
61
62 sub test_v {
63 my $row = shift || die "no row?";
64
65 my $d = dump( $row );
66
67 $row->{'000'} = [ 42 ];
68
69 $v->reset;
70 my $e = $v->validate_rec( $row );
71
72 diag "validate $d\n",dump($e) if ($debug);
73
74 if (@_) {
75 my $tmp = $e;
76 while (@_) {
77 my $k = shift @_;
78 ok($tmp = $tmp->{$k}, "found $k") if (defined($k));
79 }
80 diag "tmp: ",dump($tmp) if ($debug);
81 if ($tmp) {
82 if (ref($tmp) eq 'HASH') {
83 return $tmp;
84 } else {
85 diag "explanation: $tmp" if $debug;
86 }
87 }
88 } else {
89 ok(! $e, "validated $d");
90 diag "expected error: ", dump($e) if($e);
91 }
92
93 }
94
95 test_v({
96 '900' => 'foo'
97 }, qw/900 not_repeatable/);
98
99 test_v({
100 '900' => [ qw/foo bar baz/ ]
101 });
102
103 test_v({
104 '901' => [ qw/foo bar baz/ ]
105 }, qw/901 missing_subfield/);
106
107 test_v({
108 '901' => [ { 'a' => 42 } ]
109 });
110
111 test_v({
112 '901' => [ { 'b' => 42 } ]
113 }, qw/901 subfield extra b/);
114
115 test_v({
116 '902' => [ { 'b' => 1 }, { 'c' => 2 } ]
117 });
118
119 test_v({
120 '902' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
121 }, qw/902 subfield extra a/);
122
123 test_v({
124 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 } ]
125 });
126
127 test_v({
128 '903' => [ { 'a' => 0 }, { 'b' => 1 }, { 'c' => 2 }, { 'd' => 3 } ]
129 }, qw/903 subfield extra d/);
130
131 is_deeply(
132 test_v({
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');
136
137 test_v({
138 '904' => [ { 'a' => 1, } ]
139 });
140
141 test_v({
142 '904' => [ { 'b' => 1 } ]
143 }, qw/904 subfield extra b/);
144
145 test_v({
146 '904' => [ { 'a' => [ 1,2 ] } ]
147 }, qw/904 subfield extra_repeatable a/);
148
149 test_v({
150 '905' => [ { 'a' => [ 1,2 ] } ]
151 });
152
153 test_v({
154 '905' => [ ]
155 });
156
157 test_v({
158 '906' => [ ]
159 });
160
161 test_v({
162 '906' => [ { '0' => 'foo' } ]
163 });
164
165 my $expected_error = {
166 900 => { not_repeatable => "probably bug in parsing input data" },
167 901 => { missing_subfield => "a required", "dump" => "baz" },
168 902 => {
169 "dump" => "^a1^b1^b2",
170 subfield => { extra => { a => 1 }, extra_repeatable => { b => 1 } },
171 },
172 903 => {
173 "dump" => "^a1^a2^c1",
174 subfield => { extra_repeatable => { a => 1 } },
175 },
176 904 => { subfield => { extra => { b => 1 }, missing => { a => 1 } } },
177 };
178
179
180 is_deeply(
181 test_v({
182 '900' => 'foo',
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 } ],
188 }, undef),
189 $expected_error, 'validate without subfields');
190
191 ok(my $r1 = $v->report, 'report');
192
193 diag "report: $r1" if ( $debug );
194
195 is_deeply(
196 test_v({
197 '900' => 'foo',
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/ ] } ],
203 }, undef),
204 $expected_error, 'validate with subfields');
205
206 ok(my $r2 = $v->report, 'report');
207
208 cmp_ok($r1, 'eq', $r2, 'subfields same as non-subfields');

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26