2 |
|
|
3 |
use strict; |
use strict; |
4 |
|
|
5 |
use Test::More tests => 99; |
use Test::More tests => 332; |
6 |
use Test::Exception; |
use Test::Exception; |
7 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
8 |
use blib; |
use blib; |
32 |
$abs_path =~ s#/[^/]*$#/#; |
$abs_path =~ s#/[^/]*$#/#; |
33 |
diag "abs_path: $abs_path" if ($debug); |
diag "abs_path: $abs_path" if ($debug); |
34 |
|
|
|
#throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup"; |
|
|
|
|
35 |
my $rec1 = { |
my $rec1 = { |
36 |
'200' => [{ |
'200' => [{ |
37 |
'a' => '200a', |
'a' => '200a', |
107 |
}; |
}; |
108 |
|
|
109 |
|
|
110 |
my $lookup1 = { |
my $lookup_hash1 = { |
111 |
'00900' => [ |
'db1' => { |
112 |
'lookup 1', |
'input1' => { |
113 |
'lookup 2', |
'key1' => { 1 => 1 }, |
114 |
], |
'key2' => { 2 => 1 }, |
115 |
|
}, |
116 |
|
'input2' => { |
117 |
|
'key3' => { 3 => 1 }, |
118 |
|
'key4' => { 4 => 1 }, |
119 |
|
}, |
120 |
|
}, |
121 |
|
'db2' => { |
122 |
|
'input3' => { |
123 |
|
'key5' => { 5 => 1 }, |
124 |
|
'key6' => { 6 => 1 }, |
125 |
|
}, |
126 |
|
} |
127 |
}; |
}; |
128 |
|
|
129 |
my $lookup2 = { |
my $lookup_hash2 = { |
130 |
'00900' => 'lookup', |
'db3' => { |
131 |
|
'input4' => { |
132 |
|
'key7' => { 7 => 1 }, |
133 |
|
'key8' => { 8 => 1 }, |
134 |
|
}, |
135 |
|
} |
136 |
}; |
}; |
137 |
|
|
|
|
|
138 |
sub test { |
sub test { |
139 |
print dump( @_ ), ("-" x 78), "\n"; |
print dump( @_ ), ("-" x 78), "\n"; |
140 |
ok( defined(@_) ); |
ok( defined(@_) ); |
164 |
$eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output); |
$eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output); |
165 |
$eval_t =~ s/\\/\\\\/gs; |
$eval_t =~ s/\\/\\\\/gs; |
166 |
|
|
167 |
my $v = eval "$t"; |
my @__ret; |
168 |
ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t"); |
eval "\@__ret = $t"; |
169 |
$v; |
ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret)); |
170 |
|
return \@__ret; |
171 |
} |
} |
172 |
|
|
173 |
{ |
{ |
194 |
cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix'); |
cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix'); |
195 |
cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround'); |
cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround'); |
196 |
|
|
197 |
|
# count |
198 |
|
my @el; |
199 |
|
for my $i ( 0 .. 10 ) { |
200 |
|
cmp_ok( count( @el ), '==', $i, "count($i)"); |
201 |
|
push @el, "element $i"; |
202 |
|
} |
203 |
|
|
204 |
|
# lookups |
205 |
|
|
206 |
|
throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()'; |
207 |
|
|
208 |
|
ok(_set_load_row(sub { |
209 |
|
my ($database,$input,$mfn) = @_; |
210 |
|
diag "load_row( $database, $input, $mfn )" if ($debug); |
211 |
|
cmp_ok( $#_, '==', 2, 'have 3 arguments'); |
212 |
|
ok($database, '_load_row database'); |
213 |
|
ok($input, '_load_row input'); |
214 |
|
ok($mfn, '_load_row mfn'); |
215 |
|
return { |
216 |
|
'900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }], |
217 |
|
} |
218 |
|
|
219 |
|
}), '_set_load_row'); |
220 |
|
|
221 |
|
my @v = qw/foo bar baz aaa bbb ccc ddd/; |
222 |
|
|
223 |
|
my @accumulated; |
224 |
|
|
225 |
|
for my $i ( 0 .. $#v ) { |
226 |
|
|
227 |
|
my $mfn = 1000 + $i; |
228 |
|
|
229 |
|
ok(WebPAC::Normalize::_set_config({ '_mfn' => $mfn }), "_set_config _mfn=$mfn"); |
230 |
|
|
231 |
|
my $size = $#v + 1; |
232 |
|
|
233 |
|
cmp_ok( |
234 |
|
save_into_lookup('db','input','key', sub { @v }), |
235 |
|
'==', $size, "save_into_lookup $size values" |
236 |
|
); |
237 |
|
|
238 |
|
ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup'); |
239 |
|
diag "_get_lookup = ", dump($l) if ($debug); |
240 |
|
|
241 |
|
my @lookup; |
242 |
|
|
243 |
|
ok(@lookup = lookup( |
244 |
|
sub { |
245 |
|
diag "in show" if ($debug); |
246 |
|
rec('900','x'); |
247 |
|
}, |
248 |
|
'db','input','key', |
249 |
|
sub { |
250 |
|
return @v; |
251 |
|
} |
252 |
|
), |
253 |
|
"lookup db/input/key"); |
254 |
|
|
255 |
|
push @accumulated, '900x-' . $mfn; |
256 |
|
|
257 |
|
is_deeply(\@lookup, \@accumulated, "lookup db/input/key"); |
258 |
|
|
259 |
|
shift @v; |
260 |
|
|
261 |
|
} |
262 |
|
|
263 |
|
ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup'); |
264 |
|
diag "_get_lookup = ", dump($l) if ($debug); |
265 |
|
|
266 |
|
is_deeply( $l, { |
267 |
|
db => { |
268 |
|
input => { |
269 |
|
key => { |
270 |
|
foo => { 1000 => 1 }, |
271 |
|
bar => { 1000 => 1, 1001 => 1 }, |
272 |
|
baz => { 1000 => 1, 1001 => 1, 1002 => 1 }, |
273 |
|
aaa => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1 }, |
274 |
|
bbb => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1 }, |
275 |
|
ccc => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1 }, |
276 |
|
ddd => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1, 1006 => 1 }, |
277 |
|
}, |
278 |
|
}, |
279 |
|
}, |
280 |
|
}, 'lookup data'); |
281 |
|
|
282 |
|
####### |
283 |
|
|
284 |
|
diag "lookup_hash1 = ", dump($lookup_hash1) if ($debug); |
285 |
|
ok(_set_lookup( $lookup_hash1 ), '_set_lookup $lookup_hash1'); |
286 |
|
|
287 |
|
throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()'; |
288 |
|
|
289 |
|
ok(_set_load_row(sub { |
290 |
|
my ($database,$input,$mfn) = @_; |
291 |
|
diag "load_row( $database, $input, $mfn )"; |
292 |
|
cmp_ok( $#_, '==', 2, 'have 3 arguments'); |
293 |
|
ok($database, 'database'); |
294 |
|
ok($input, 'input'); |
295 |
|
ok($mfn, 'mfn'); |
296 |
|
|
297 |
|
}), '_set_load_row'); |
298 |
|
|
299 |
|
|
300 |
|
# cmp_ok(lookup( |
301 |
|
# sub { |
302 |
|
# 'found' |
303 |
|
# }, |
304 |
|
# 'db1','input1','key1', |
305 |
|
# sub { |
306 |
|
# rec('200','a') |
307 |
|
# } |
308 |
|
# ), 'eq', 'found', 'lookup db1/input1/key1'); |
309 |
|
|
310 |
|
|
|
_set_lookup( $lookup1 ); |
|
311 |
|
|
312 |
cmp_ok( |
# cmp_ok( |
313 |
join_with(" i ", |
# lookup( |
314 |
lookup( |
# ), |
315 |
regex( 's/^/00/', |
# 'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2'); |
|
rec2('902','z') |
|
|
) |
|
|
) |
|
|
), |
|
|
'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2'); |
|
316 |
|
|
317 |
# check join_with operations |
# check join_with operations |
318 |
|
|
356 |
|
|
357 |
# test lookups |
# test lookups |
358 |
|
|
359 |
_set_lookup( $lookup2 ); |
_set_lookup( $lookup_hash2 ); |
360 |
|
|
361 |
|
throws_ok { lookup() } qr/need/, 'empty lookup'; |
362 |
|
|
363 |
is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' ); |
#is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' ); |
364 |
|
|
365 |
ok(! lookup('non-existent'), 'lookup non-existant' ); |
#ok(! lookup('non-existent'), 'lookup non-existant' ); |
366 |
|
|
367 |
_set_rec( $rec2 ); |
_set_rec( $rec2 ); |
368 |
|
|
483 |
# |
# |
484 |
# MARC |
# MARC |
485 |
# |
# |
486 |
|
#_debug( 4 ); |
487 |
|
|
488 |
test_s(qq{ marc_indicators('900',1,2) }); |
test_s(qq{ marc_indicators('900',1,2) }); |
489 |
test_s(qq{ marc('900','a', rec('200') ) }); |
test_s(qq{ marc('900','a', rec('200') ) }); |
490 |
my @marc; |
my $marc; |
491 |
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
492 |
diag dump( \@marc ) if ($debug); |
diag dump( $marc ) if ($debug); |
493 |
|
|
494 |
is_deeply( \@marc, [ |
is_deeply( $marc, [ |
495 |
[ '900', 1, 2, 'a', '200a' ], |
[ '900', 1, 2, 'a', '200a' ], |
496 |
[ '900', 1, 2, 'a', '200-solo' ] |
[ '900', 1, 2, 'a', '200-solo' ] |
497 |
], 'correct marc with indicators'); |
], 'correct marc with indicators'); |
499 |
test_s(qq{ marc_indicators('900',' ',9) }); |
test_s(qq{ marc_indicators('900',' ',9) }); |
500 |
test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) }); |
test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) }); |
501 |
|
|
502 |
ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
503 |
diag dump( \@marc ) if ($debug); |
diag dump( $marc ) if ($debug); |
504 |
|
|
505 |
is_deeply( \@marc, [ |
is_deeply( $marc, [ |
506 |
[ '900', 1, 2, 'a', '200a', 'a', '200-solo' ], |
[ '900', 1, 2, 'a', '200a', 'a', '200-solo' ], |
507 |
[ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ] |
[ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ] |
508 |
], 'correct marc with repetable subfield'); |
], 'correct marc with repetable subfield'); |
524 |
test_s($r) if ($r); |
test_s($r) if ($r); |
525 |
} |
} |
526 |
|
|
527 |
ok(my @marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
ok(my $marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields"); |
528 |
diag dump( \@marc ) if ($debug); |
diag dump( $marc ) if ($debug); |
529 |
diag "expects:\n", dump($struct) if ($debug > 1); |
diag "expects:\n", dump($struct) if ($debug > 1); |
530 |
is_deeply( \@marc, $struct, $msg ); |
is_deeply( $marc, $struct, $msg ); |
531 |
} |
} |
532 |
|
|
533 |
test_rec_rules( |
test_rec_rules( |
602 |
], |
], |
603 |
); |
); |
604 |
|
|
605 |
|
test_rec_rules( |
606 |
|
'marc_compose with + subfields', |
607 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
608 |
|
qq{ |
609 |
|
marc_compose('900', |
610 |
|
'a', rec(200,'a'), |
611 |
|
'+', prefix(" * ", rec(200,'c')), |
612 |
|
'b', rec(200,'b'), |
613 |
|
'+', prefix(" : ", rec(200,'c')), |
614 |
|
); |
615 |
|
}, |
616 |
|
[ |
617 |
|
[ '900', ' ', ' ', 'a', 'foo ; bar * baz', 'b', '42 : baz' ] |
618 |
|
], |
619 |
|
); |
620 |
|
|
621 |
# |
# |
622 |
# test rules |
# test rules |
623 |
# |
# |
637 |
'split_rec_on', |
'split_rec_on', |
638 |
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
639 |
qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) }, |
qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) }, |
640 |
'foo', |
[ 'foo' ], |
641 |
); |
); |
642 |
test_rule( |
test_rule( |
643 |
'split_rec_on', |
'split_rec_on', |
644 |
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
645 |
qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) }, |
qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) }, |
646 |
'bar', |
[ 'bar' ], |
647 |
|
); |
648 |
|
test_rule( |
649 |
|
'split_rec_on no part', |
650 |
|
{ '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] }, |
651 |
|
qq{ split_rec_on('200','a', qr/\\s*;\\s*/) }, |
652 |
|
[ 'foo', 'bar' ], |
653 |
|
); |
654 |
|
test_rule( |
655 |
|
'split_rec_on no record', |
656 |
|
{}, |
657 |
|
qq{ split_rec_on('200','a', qr/\\s*;\\s*/) }, |
658 |
|
[ '' ], |
659 |
); |
); |
660 |
|
|
661 |
test_rec_rules( |
test_rec_rules( |
678 |
] |
] |
679 |
], |
], |
680 |
); |
); |
681 |
|
|
682 |
|
cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader'); |
683 |
|
cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader'); |
684 |
|
ok(marc_leader(), 'marc_leader get'); |
685 |
|
diag "leader: ", dump(marc_leader()) if ($debug); |
686 |
|
is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full"); |
687 |
|
|
688 |
|
test_rule( |
689 |
|
'rec1(000)', |
690 |
|
{ '000' => [ 42 ]}, |
691 |
|
qq{ rec('000') }, |
692 |
|
[ 42 ], |
693 |
|
); |
694 |
|
|
695 |
|
test_rec_rules( |
696 |
|
'marc(001,rec(000))', |
697 |
|
{ '000' => [ 42 ]}, |
698 |
|
qq{ |
699 |
|
marc('001', rec('000') ); |
700 |
|
}, |
701 |
|
[ |
702 |
|
[ '001', 42, ] |
703 |
|
], |
704 |
|
); |
705 |
|
|
706 |
|
test_rec_rules( |
707 |
|
'marc_remove subfield', |
708 |
|
{ '200' => [{ a => 42, b => 'bar', c => 'baz' }] }, |
709 |
|
qq{ |
710 |
|
marc('900', 'a', rec('200','a') ); |
711 |
|
marc('900', 'b', rec('200','b') ); |
712 |
|
marc_remove('900','b'); |
713 |
|
marc('900', 'b', rec('200','c') ); |
714 |
|
marc_remove('900','a'); |
715 |
|
}, |
716 |
|
[ |
717 |
|
[ '900', ' ', ' ', 'b', 'baz' ], |
718 |
|
], |
719 |
|
); |
720 |
|
|
721 |
|
test_rec_rules( |
722 |
|
'marc_remove field', |
723 |
|
{ '200' => [{ a => 42, b => 'bar', c => 'baz' }] }, |
724 |
|
qq{ |
725 |
|
marc('900', 'a', rec('200','a') ); |
726 |
|
marc('900', 'b', rec('200','b') ); |
727 |
|
marc('901', 'b', rec('200','b') ); |
728 |
|
marc('901', 'c', rec('200','c') ); |
729 |
|
marc_remove('900'); |
730 |
|
}, |
731 |
|
[ |
732 |
|
[ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ], |
733 |
|
], |
734 |
|
); |
735 |
|
|
736 |
|
test_s(qq{ marc_remove('*'); }); |
737 |
|
ok(! WebPAC::Normalize::_get_marc_fields(), 'marc_remove(*)'); |
738 |
|
|
739 |
|
test_rec_rules( |
740 |
|
'marc_duplicate', |
741 |
|
{ '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] }, |
742 |
|
qq{ |
743 |
|
marc_leader('06',42); |
744 |
|
marc_leader('11',0); |
745 |
|
marc('900', 'a', rec('200','a') ); |
746 |
|
marc('900', 'b', rec('200','b') ); |
747 |
|
marc_duplicate; |
748 |
|
marc_leader('11',1); |
749 |
|
marc_remove('900','b'); |
750 |
|
marc('900', 'b', rec('200','c') ); |
751 |
|
marc_duplicate; |
752 |
|
marc_leader('11',2); |
753 |
|
marc_remove('900','b'); |
754 |
|
marc('900', 'b', rec('200','d') ); |
755 |
|
marc_duplicate; |
756 |
|
marc_leader('11',3); |
757 |
|
marc_remove('900','b'); |
758 |
|
marc('900', 'b', rec('200','e') ); |
759 |
|
}, |
760 |
|
[ |
761 |
|
# this will return FIRST record |
762 |
|
[ '900', ' ', ' ', 'a', 42, 'b', 'bar' ], |
763 |
|
], |
764 |
|
); |
765 |
|
|
766 |
|
cmp_ok( marc_count(), '==', 3, 'marc_count' ); |
767 |
|
|
768 |
|
my $i = 0; |
769 |
|
foreach my $v ( qw/bar baz bing bong/ ) { |
770 |
|
|
771 |
|
ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ), |
772 |
|
"_get_marc_fields( offset => $i )" |
773 |
|
); |
774 |
|
diag "marc $i = ", dump( $marc ) if ($debug); |
775 |
|
is_deeply( $marc, |
776 |
|
[ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ], |
777 |
|
"MARC copy $i has $v", |
778 |
|
); |
779 |
|
is_deeply(WebPAC::Normalize::_get_marc_leader(), { '06' => 42, 11 => $i }, "_get_marc_leader copy $i"); |
780 |
|
$i++; |
781 |
|
} |
782 |
|
|
783 |
|
test_rec_rules( |
784 |
|
'marc_original_order', |
785 |
|
{ |
786 |
|
'200' => [ { |
787 |
|
a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ], |
788 |
|
subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ], |
789 |
|
}, { |
790 |
|
a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3', |
791 |
|
subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ], |
792 |
|
} ], |
793 |
|
}, |
794 |
|
qq{ |
795 |
|
marc_original_order(900,200); |
796 |
|
}, |
797 |
|
[ |
798 |
|
[ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ], |
799 |
|
[ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ], |
800 |
|
], |
801 |
|
); |
802 |
|
|
803 |
|
test_rule( |
804 |
|
'rec1 skips subfields', |
805 |
|
{ |
806 |
|
'200' => [ { |
807 |
|
a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ], |
808 |
|
subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ], |
809 |
|
}, { |
810 |
|
a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3', |
811 |
|
subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ], |
812 |
|
} ], |
813 |
|
}, |
814 |
|
qq{ |
815 |
|
rec1(200); |
816 |
|
}, |
817 |
|
['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ], |
818 |
|
); |
819 |
|
|
820 |
|
is_deeply( |
821 |
|
[ _pack_subfields_hash({ |
822 |
|
a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ], |
823 |
|
subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ], |
824 |
|
}) ], |
825 |
|
['a1', 'b1', 'a2', 'b2', 'c1', 'c2'], |
826 |
|
'_pack_subfields_hash( $h )' |
827 |
|
); |
828 |
|
|
829 |
|
cmp_ok( |
830 |
|
_pack_subfields_hash({ |
831 |
|
a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ], |
832 |
|
subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ], |
833 |
|
}, 1), |
834 |
|
'eq', |
835 |
|
'^aa1^bb1^aa2^bb2^cc1^cc2', |
836 |
|
'_pack_subfields_hash( $h, 1 )' |
837 |
|
); |
838 |
} |
} |
839 |
|
|