1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
use Test::More tests => 104; |
use Test::More tests => 111; |
4 |
use Test::Exception; |
use Test::Exception; |
5 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
6 |
use blib; |
use blib; |
11 |
BEGIN { |
BEGIN { |
12 |
use_ok( 'WebPAC::Input::ISIS' ); |
use_ok( 'WebPAC::Input::ISIS' ); |
13 |
use_ok( 'WebPAC::Input::MARC' ); |
use_ok( 'WebPAC::Input::MARC' ); |
14 |
|
use_ok( 'WebPAC::Input::Test' ); |
15 |
} |
} |
16 |
|
|
17 |
my $debug = shift @ARGV; |
my $debug = shift @ARGV; |
120 |
test_fetch($input, $input->size); |
test_fetch($input, $input->size); |
121 |
|
|
122 |
# test modify_record |
# test modify_record |
123 |
$module = 'WebPAC::Input::ISIS'; |
$module = 'WebPAC::Input::Test'; |
124 |
ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module"); |
ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, debug => $debug ), "new $module"); |
125 |
|
|
126 |
|
$WebPAC::Input::Test::rec = { |
127 |
|
'200' => [ |
128 |
|
{ 'a' => '[200 a]', 'b' => '[200 b]', 'c' => '[200 c]', 'f' => '[200 f] test : test' }, |
129 |
|
], |
130 |
|
'900' => [ |
131 |
|
{ 'x' => 'foobar', }, |
132 |
|
], |
133 |
|
}; |
134 |
|
|
135 |
|
$WebPAC::Input::Test::size = 42; |
136 |
|
|
137 |
|
ok($input->open( path => "/fake/path", ), "open modify_isis (plain)"); |
138 |
|
|
139 |
|
cmp_ok($input->size, '==', 42, 'size'); |
140 |
|
|
|
ok($input->open( path => "$abs_path/modify_isis/LIBRI", ), "open modify_isis (plain)"); |
|
141 |
ok(my $rec_p = $input->fetch, 'fetch'); |
ok(my $rec_p = $input->fetch, 'fetch'); |
142 |
|
|
143 |
# modify_records |
# modify_records |
144 |
|
|
145 |
ok($input->open( |
ok($input->open( |
146 |
path => "$abs_path/modify_isis/LIBRI", |
path => "/another/fake/path", |
147 |
modify_records => { |
modify_records => { |
148 |
200 => { |
200 => { |
149 |
'*' => { '^c' => '. ' }, |
'*' => { '^c' => '. ' }, |
150 |
'^f' => { ' : ' => ' / ' }, |
'^f' => { ' : ' => ' / ' }, |
151 |
} |
} |
152 |
}, |
}, |
153 |
), "open modify_isis (with modify_records)"); |
), "open (with modify_records)"); |
154 |
|
|
155 |
ok(my $rec = $input->fetch, 'fetch'); |
# seek |
156 |
diag "fetched rec field 200 = ", dump($rec->{200}) if ($debug); |
throws_ok { $input->seek } qr/without/, 'seek without position'; |
157 |
|
cmp_ok($input->seek(0), '==', -1, 'seek'); |
|
cmp_ok($rec_p->{200}->[0]->{f} . '. ' . $rec_p->{200}->[0]->{c}, 'eq' ,$rec->{200}->[0]->{f}, 'modify_records working'); |
|
158 |
|
|
159 |
diag "input = ",dump($input->{data}) if ($debug); |
my $f = $WebPAC::Input::Test::filter_coderef; |
160 |
|
ok(ref($f) eq 'CODE', 'filter_coderef'); |
161 |
|
|
162 |
# break encapsulation, bad! bad! |
cmp_ok( |
163 |
$input->{ll_db}->{_isis_db}->{record} = { |
$f->( '^afoo^cbar^fbing : bong', 200), |
164 |
900 => 'foo ; bar ; baz', |
'eq', '^afoo. bar^fbing / bong', |
165 |
}; |
'modify 200' |
166 |
|
); |
167 |
|
|
168 |
$input->{modify_record} = { |
# modify_file |
|
900 => { |
|
|
'*' => [ |
|
|
{ ' ; ' => 'a' }, |
|
|
{ ' ; ' => 'b' }, |
|
|
{ ' ; ' => 'c' }, |
|
|
], |
|
|
} |
|
|
}; |
|
169 |
|
|
170 |
diag "hacked: ",dump($input, $input->fetch) if ($debug); |
my $modify_file = "$abs_path/conf/modify/test.pl"; |
171 |
|
|
172 |
# seek |
ok($input->open( |
173 |
throws_ok { $input->seek } qw/without/, 'seek without position'; |
path => "/and/another/fake/path", |
174 |
cmp_ok($input->seek(0), '==', -1, 'seek'); |
modify_file => $modify_file, |
175 |
|
), "open (with modify_file $modify_file)"); |
176 |
|
|
177 |
|
my $f = $WebPAC::Input::Test::filter_coderef; |
178 |
|
ok(ref($f) eq 'CODE', 'filter_coderef'); |
179 |
|
|
180 |
|
diag "regexps = ", dump($input->modify_file_regexps( $modify_file )) if ($debug); |
181 |
|
|
182 |
|
sub test_filter { |
183 |
|
my ($field, $from, $to) = @_; |
184 |
|
cmp_ok( $f->( $from, $field, 1 ), 'eq', $to, "filter $field |$from| -> |$to|" ); |
185 |
|
} |
186 |
|
|
187 |
ok(my $rec = $input->fetch, 'fetch'); |
test_filter(200, |
188 |
diag "fetched rec = ", dump($rec) if ($debug); |
'^a foo ; bar = baz : zzz', |
189 |
|
'^a foo^kbar^dbaz : zzz', |
190 |
|
); |
191 |
|
|
192 |
|
# empty subfield removal |
193 |
|
test_filter(901, |
194 |
|
'^a^efoo', |
195 |
|
'^efoo', |
196 |
|
); |