/[webpac2]/trunk/t/2-input.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/2-input.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 908 - (show annotations)
Mon Oct 29 23:20:13 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: application/x-troff
File size: 5091 byte(s)
leader from WebPAC::Input::MARC is now available as rec('leader')

for mondifications within leader, use substr(rec('leader'),from,to)
instead of proposed leader(field,nr) syntax

1 #!/usr/bin/perl -w
2
3 use Test::More tests => 123;
4 use Test::Exception;
5 use Cwd qw/abs_path/;
6 use blib;
7 use strict;
8
9 use Data::Dump qw/dump/;
10
11 BEGIN {
12 use_ok( 'WebPAC::Input::ISIS' );
13 use_ok( 'WebPAC::Input::MARC' );
14 use_ok( 'WebPAC::Input::Test' );
15 }
16
17 my $debug = shift @ARGV;
18 my $no_log = $debug ? 0 : 1;
19
20 ok(my $abs_path = abs_path($0), "abs_path");
21 $abs_path =~ s#/[^/]*$#/#; #vim
22
23 my $module = 'WebPAC::Input::ISIS';
24 diag "testing with $module";
25
26 throws_ok { my $input = new WebPAC::Input( no_log => $no_log ) } qr/module/, "need module";
27 ok(my $input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, stats => 1 ), "new $module");
28 ok(my $input_lm = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
29
30 throws_ok { $input->open( ) } qr/path/, "need path";
31
32 throws_ok { $input->open( path => '/dev/null', ) } qr/can't find database/ , "open";
33
34 my $store;
35
36 ok($input->open( path => "$abs_path/winisis/BIBL" ), "open winisis");
37 ok($input_lm->open(
38 path => "$abs_path/winisis/BIBL",
39 save_row => sub {
40 my $a = shift;
41 $store->{ $a->{id} } = $a->{row};
42 },
43 load_row => sub {
44 my $a = shift;
45 return defined($store->{ $a->{id} }) &&
46 $store->{ $a->{id} };
47 },
48 ), "open winisis");
49
50 cmp_ok( keys %$store, '==', 5, 'have 5 rows');
51
52 foreach my $i ( 1 .. 5 ) {
53 ok(my $r = $store->{$i}, "row $i");
54 ok($r->{'000'}, "have 000");
55 isa_ok($r->{'000'}, 'ARRAY', "is ARRAY");
56 cmp_ok($r->{'000'}->[0], '==', $i, 'sane value');
57 }
58
59 diag "store = ",dump( $store ) if ($debug);
60
61 sub test_after_open($) {
62 my $input = shift;
63
64 cmp_ok($input->pos, '==', -1, "mfn");
65 ok(my $size = $input->size, "size");
66 return $size;
67 }
68
69 test_after_open($input);
70 my $size = test_after_open($input_lm);
71
72 sub test_fetch($$) {
73 my ($input, $size) = @_;
74
75 my @db;
76
77 foreach my $mfn ( 1 ... $size ) {
78 ok(my $rec = $input->fetch, "fetch $mfn");
79 cmp_ok($input->pos, '==', $mfn, "pos $mfn");
80 push @db, $rec;
81 ok(my $dump = $input->dump_ascii, "dump_ascii $mfn");
82 # XXX test count will help us keep this test in-line :-)
83 ok($rec->{leader}, "leader $mfn") if $rec->{leader};
84 diag $dump if ($debug);
85 }
86
87 return @db;
88 }
89
90 my @db1 = test_fetch($input, $size);
91 my @db2 = test_fetch($input_lm, $size);
92
93 is_deeply(\@db1, \@db2, "seek working");
94
95 sub test_start_limit($$$$) {
96 my ($input, $s,$l,$e) = @_;
97
98 diag "offset $s, limit: $l, expected: $e";
99
100 ok($s = $input->open( path => "$abs_path/winisis/BIBL", offset => $s, limit => $l, debug => $debug ), "open winisis");
101 cmp_ok($s, '==', $size, "db size from open = $size");
102 cmp_ok($input->size, '==', $e, "input->size = $e");
103 }
104
105 test_start_limit($input, 1, 3, 3);
106 test_start_limit($input, $size, 3, 0);
107 test_start_limit($input, 3, $size, $size - 2);
108 test_start_limit($input, 1, $size + 2, $size);
109
110 ok(my $s = $input->stats, 'stats');
111 diag "stats:\n$s" if ($debug);
112
113 $module = 'WebPAC::Input::MARC';
114 diag "testing with $module";
115
116 ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
117
118 ok($input->open( path => "$abs_path/data/marc.iso" ), "open marc.iso");
119
120 test_after_open($input);
121
122 test_fetch($input, $input->size);
123
124 # test modify_record
125 $module = 'WebPAC::Input::Test';
126 ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, debug => $debug ), "new $module");
127
128 $WebPAC::Input::Test::rec = {
129 '200' => [
130 { 'a' => '[200 a]', 'b' => '[200 b]', 'c' => '[200 c]', 'f' => '[200 f] test : test' },
131 ],
132 '900' => [
133 { 'x' => 'foobar', },
134 ],
135 };
136
137 $WebPAC::Input::Test::size = 42;
138
139 ok($input->open( path => "/fake/path", ), "open modify_isis (plain)");
140
141 cmp_ok($input->size, '==', 42, 'size');
142
143 ok(my $rec_p = $input->fetch, 'fetch');
144
145 # modify_records
146
147 ok($input->open(
148 path => "/another/fake/path",
149 modify_records => {
150 200 => {
151 '*' => { '^c' => '. ' },
152 '^f' => { ' : ' => ' / ' },
153 }
154 },
155 ), "open (with modify_records)");
156
157 # seek
158 throws_ok { $input->seek } qr/without/, 'seek without position';
159 cmp_ok($input->seek(0), '==', -1, 'seek');
160
161 sub test_filter {
162
163 my $f = $WebPAC::Input::Test::filter_coderef;
164 ok(ref($f) eq 'CODE', 'filter_coderef');
165
166 my ($field, $from, $to) = @_;
167 cmp_ok( $f->( $from, $field, 1 ), 'eq', $to, "filter $field |$from| -> |$to|" );
168 }
169
170 test_filter(200,
171 '^afoo^cbar^fbing : bong',
172 '^afoo. bar^fbing / bong',
173 );
174
175 # modify_file
176
177 my $modify_file = "$abs_path/conf/modify/test.pl";
178
179 ok($input->open(
180 path => "/and/another/fake/path",
181 modify_file => $modify_file,
182 ), "open (with modify_file $modify_file)");
183
184 diag "regexps = ", dump($input->modify_file_regexps( $modify_file )) if ($debug);
185
186 test_filter(200,
187 '^a foo ; bar = baz : zzz',
188 '^a foo^kbar^dbaz : zzz',
189 );
190
191 # empty subfield removal
192
193 ok($input->open(
194 path => "/another/fake/path",
195 modify_records => {
196 900 => {
197 '^a' => { '^e' => ' : ^e' },
198 },
199 901 => {
200 '^a' => { 'foo' => 'baz' },
201 },
202 },
203 ), "open (with modify_records for empty subfields)");
204
205 test_filter(900,
206 '^a^ebar',
207 '^a^ebar',
208 );
209
210 test_filter(900,
211 '^afoo^ebar',
212 '^afoo : ^ebar',
213 );
214
215 test_filter(901,
216 '^afoo^ebar',
217 '^abaz^ebar',
218 );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26