5 use Test::More tests => 347;
13 use_ok( 'WebPAC::Normalize' );
16 use Data::Dump qw/dump/;
23 cmp_ok(_debug(1), '==', 1, '_debug level');
24 cmp_ok(_debug(0), '==', 0, '_debug level');
26 diag "debug level for $0 is $debug" if ($debug);
28 diag "debug level for WebPAC::Normalize is ", _debug( $debug - 2 );
31 ok(my $abs_path = abs_path($0), "abs_path");
32 $abs_path =~ s#/[^/]*$#/#;
33 diag "abs_path: $abs_path" if ($debug);
66 'c' => 'New York University press',
83 'a' => 'xix, 202 str',
92 'e' => 'from Freud and Ferenczi to Michael balint',
93 'a' => 'Controversies in psychoanalytic method',
94 'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern',
95 'f' => 'by André E. Haynal'
97 '610' => [ 'povijest psihoanalize' ],
103 '320' => [ 'Kazalo' ],
106 '300' => [ 'Prijevod djela: ' ],
113 'key1' => { 1 => 1 },
114 'key2' => { 2 => 1 },
117 'key3' => { 3 => 1 },
118 'key4' => { 4 => 1 },
123 'key5' => { 5 => 1 },
124 'key6' => { 6 => 1 },
132 'key7' => { 7 => 1 },
133 'key8' => { 8 => 1 },
139 print dump( @_ ), ("-" x 78), "\n";
143 # how much of string evaled to display?
144 my $max_eval_output = 170;
147 my ($msg,$code) = @_;
149 my @l = split(/[\n\r]/, $code);
152 foreach my $i ( 0 .. $#l ) {
153 $out .= sprintf("%2d: %s\n", $i, $l[$i]);
160 my $t = shift || die;
163 $eval_t =~ s/[\n\r\s]+/ /gs;
164 $eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output);
165 $eval_t =~ s/\\/\\\\/gs;
169 ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret));
175 use WebPAC::Normalize;
177 ok(! _set_lookup( undef ), "set_lookup(undef)");
181 cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
182 cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
183 cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' );
184 diag "is_deeply checks\n";
185 is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] );
186 is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]);
187 is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]);
188 is_deeply( \[ rec('902') ], \[ '900' ] );
190 cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
192 # simple list manipulatons
193 cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
194 cmp_ok( join('-', prefix('', 'x', 'y') ), 'eq', 'x-y', 'prefix empty');
195 cmp_ok( join('-', prefix(0, 'x', 'y') ), 'eq', '0x-0y', 'prefix 0');
197 cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
198 cmp_ok( join('-', suffix('', 'x', 'y' ) ), 'eq', 'x-y', 'suffix empty');
199 cmp_ok( join('-', suffix(0, 'x', 'y' ) ), 'eq', 'x0-y0', 'suffix 0');
201 cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
202 cmp_ok( join('-', surround('', '', 'x','y','z') ), 'eq', 'x-y-z', 'surround empty');
203 cmp_ok( join('-', surround(0, 0, 'x','y','z') ), 'eq', '0x0-0y0-0z0', 'surround 0 0');
207 for my $i ( 0 .. 10 ) {
208 cmp_ok( count( @el ), '==', $i, "count($i)");
209 push @el, "element $i";
214 throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
216 ok(_set_load_row(sub {
217 my ($database,$input,$mfn) = @_;
218 diag "load_row( $database, $input, $mfn )" if ($debug);
219 cmp_ok( $#_, '==', 2, 'have 3 arguments');
220 ok($database, '_load_row database');
221 ok($input, '_load_row input');
222 ok($mfn, '_load_row mfn');
224 '900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }],
227 }), '_set_load_row');
229 my @v = qw/foo bar baz aaa bbb ccc ddd/;
233 for my $i ( 0 .. $#v ) {
237 ok(WebPAC::Normalize::_set_config({ '_mfn' => $mfn }), "_set_config _mfn=$mfn");
242 save_into_lookup('db','input','key', sub { @v }),
243 '==', $size, "save_into_lookup $size values"
246 ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
247 diag "_get_lookup = ", dump($l) if ($debug);
253 diag "in show" if ($debug);
261 "lookup db/input/key");
263 push @accumulated, '900x-' . $mfn;
265 is_deeply(\@lookup, \@accumulated, "lookup db/input/key");
271 ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
272 diag "_get_lookup = ", dump($l) if ($debug);
278 foo => { 1000 => 1 },
279 bar => { 1000 => 1, 1001 => 1 },
280 baz => { 1000 => 1, 1001 => 1, 1002 => 1 },
281 aaa => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1 },
282 bbb => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1 },
283 ccc => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1 },
284 ddd => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1, 1006 => 1 },
292 diag "lookup_hash1 = ", dump($lookup_hash1) if ($debug);
293 ok(_set_lookup( $lookup_hash1 ), '_set_lookup $lookup_hash1');
295 throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
297 ok(_set_load_row(sub {
298 my ($database,$input,$mfn) = @_;
299 diag "load_row( $database, $input, $mfn )";
300 cmp_ok( $#_, '==', 2, 'have 3 arguments');
301 ok($database, 'database');
305 }), '_set_load_row');
312 # 'db1','input1','key1',
316 # ), 'eq', 'found', 'lookup db1/input1/key1');
323 # 'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
325 # check join_with operations
327 sub test_join_with_2 {
335 'eq', $e, "join_with $a <1> $b = $e");
338 test_join_with_2('_','_','');
339 test_join_with_2('x','_','201x');
340 test_join_with_2('_','x','201x');
341 test_join_with_2('x','y','201x <1> 201y');
343 sub test_join_with_3 {
344 my ($a,$b,$c,$e) = @_;
347 join_with(" <1> ", rec('201',$a),
348 join_with(" <2> ", rec('201',$b),
352 'eq', $e, "join_with $a <1> $b <2> $c = $e");
355 test_join_with_3('_','_','_','');
356 test_join_with_3('x','_','_','201x');
357 test_join_with_3('_','x','_','201x');
358 test_join_with_3('_','_','x','201x');
359 test_join_with_3('x','y','_','201x <1> 201y');
360 test_join_with_3('x','_','y','201x <1> 201y');
361 test_join_with_3('_','x','y','201x <2> 201y');
362 test_join_with_3('x','_','y','201x <1> 201y');
363 test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
367 _set_lookup( $lookup_hash2 );
369 throws_ok { lookup() } qr/need/, 'empty lookup';
371 #is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
373 #ok(! lookup('non-existent'), 'lookup non-existant' );
378 search_display('Title',
383 search_display('Who',
399 regex( 's/[^\\d]+//',
405 ok(my $ds = _get_ds(), "get_ds");
406 diag "ds = ", dump($ds) if ($debug);
413 ok($ds = _get_ds(), 'get_ds');
414 diag dump( $ds ) if ($debug);
416 ok( $ds && $ds->{something}, 'get_ds->something exists' );
417 ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
418 ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
424 test_s(qq{ search('something', '42'); });
425 test_s(qq{ search('empty', ''); });
426 test_check_ds('search');
429 test_s(qq{ display('something', '42'); });
430 test_s(qq{ display('empty', ''); });
431 test_check_ds('display');
434 test_s(qq{ search_display('something', '42'); });
435 test_s(qq{ search_display('empty', ''); });
436 test_check_ds('search');
437 test_check_ds('display');
440 test_s(qq{ sorted('something', '42'); });
441 test_s(qq{ sorted('empty', ''); });
442 test_check_ds('sorted');
445 my $n = read_file( "$abs_path/data/normalize.pl" );
447 #diag "normalize code:\n$n\n";
450 ok($ds = _get_ds(), "get_ds");
451 diag "ds = ", dump($ds) if ($debug);
459 my $rules = qq{ search('mixed', rec('200') ) };
464 ok($ds = _get_ds(), "get_ds");
467 'search' => [ '200a', '200b' ],
469 }, 'correct get_ds');
471 ok(my $ds2 = WebPAC::Normalize::data_structure(
474 ), 'data_structure');
475 is_deeply( $ds, $ds2, 'data_structure(s) same');
477 # wird and non-valid structure which is supported anyway
486 test_s(qq{ search('mixed', rec('200') ) });
487 ok($ds = _get_ds(), "get_ds");
490 'search' => [ '200a', '200-solo' ],
492 }, 'correct get_ds');
499 test_s(qq{ marc_indicators('900',1,2) });
500 test_s(qq{ marc('900','a', rec('200') ) });
502 ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
503 diag dump( $marc ) if ($debug);
506 [ '900', 1, 2, 'a', '200a' ],
507 [ '900', 1, 2, 'a', '200-solo' ]
508 ], 'correct marc with indicators');
510 test_s(qq{ marc_indicators('900',' ',9) });
511 test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
513 ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
514 diag dump( $marc ) if ($debug);
517 [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
518 [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
519 ], 'correct marc with repetable subfield');
522 # test magic re-ordering of input data
526 my ($msg, $rec, $rules, $struct) = @_;
531 foreach my $r (split(/;/, $rules)) {
532 $r =~ s/[\s\n\r]+/ /gs;
538 ok(my $marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
539 diag dump( $marc ) if ($debug);
540 diag "expects:\n", dump($struct) if ($debug > 1);
541 is_deeply( $marc, $struct, $msg );
545 'correct marc with repetable subfield',
559 marc_indicators('900',1 ,0);
560 marc('900','a', rec('200','a') );
561 marc('900','b', rec('200','b') );
562 marc('900','c', rec('200','c') );
565 [ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ],
566 [ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ],
567 [ '900', 1, 0, 'a', '200a-3' ],
573 'marc_repeatable_subfield',
589 marc_indicators('900',1 ,0);
590 marc_repeatable_subfield('900','a', rec('200','a') );
591 marc('900','b', rec('200','b') );
592 marc('900','c', rec('200','c') );
595 [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],
596 [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],
597 [ '900', 1, 0, 'c', '200c-3' ],
603 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
612 [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
617 'marc_compose with + subfields',
618 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
622 '+', prefix(" * ", rec(200,'c')),
624 '+', prefix(" : ", rec(200,'c')),
628 [ '900', ' ', ' ', 'a', 'foo ; bar * baz', 'b', '42 : baz' ]
636 my ($msg, $rec, $rule, $struct) = @_;
640 my $r = test_s( $rule );
641 diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
642 diag dump($struct) if ($debug);
643 is_deeply( $r, $struct, $msg );
649 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
650 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
655 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
656 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
660 'split_rec_on no part',
661 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
662 qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
666 'split_rec_on no record',
668 qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
673 'marc_compose+split_rec_on',
674 { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
677 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
679 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
693 cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
694 cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
695 ok(marc_leader(), 'marc_leader get');
696 diag "leader: ", dump(marc_leader()) if ($debug);
697 is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
707 'marc(001,rec(000))',
710 marc('001', rec('000') );
718 'marc_remove subfield',
719 { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
721 marc('900', 'a', rec('200','a') );
722 marc('900', 'b', rec('200','b') );
723 marc_remove('900','b');
724 marc('900', 'b', rec('200','c') );
725 marc_remove('900','a');
728 [ '900', ' ', ' ', 'b', 'baz' ],
734 { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
736 marc('900', 'a', rec('200','a') );
737 marc('900', 'b', rec('200','b') );
738 marc('901', 'b', rec('200','b') );
739 marc('901', 'c', rec('200','c') );
743 [ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ],
747 test_s(qq{ marc_remove('*'); });
748 ok(! WebPAC::Normalize::_get_marc_fields(), 'marc_remove(*)');
752 { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
754 marc_leader('06',42);
756 marc('900', 'a', rec('200','a') );
757 marc('900', 'b', rec('200','b') );
760 marc_remove('900','b');
761 marc('900', 'b', rec('200','c') );
764 marc_remove('900','b');
765 marc('900', 'b', rec('200','d') );
768 marc_remove('900','b');
769 marc('900', 'b', rec('200','e') );
772 # this will return FIRST record
773 [ '900', ' ', ' ', 'a', 42, 'b', 'bar' ],
777 cmp_ok( marc_count(), '==', 3, 'marc_count' );
780 foreach my $v ( qw/bar baz bing bong/ ) {
782 ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ),
783 "_get_marc_fields( offset => $i )"
785 diag "marc $i = ", dump( $marc ) if ($debug);
787 [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
788 "MARC copy $i has $v",
790 is_deeply(WebPAC::Normalize::_get_marc_leader(), { '06' => 42, 11 => $i }, "_get_marc_leader copy $i");
795 'marc_original_order',
798 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
799 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
801 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
802 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
806 marc_original_order(900,200);
809 [ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ],
810 [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],
815 'rec1 skips subfields',
818 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
819 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
821 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
822 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
828 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
832 [ _pack_subfields_hash({
833 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
834 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
836 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
837 '_pack_subfields_hash( $h )'
841 _pack_subfields_hash({
842 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
843 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
846 '^aa1^bb1^aa2^bb2^cc1^cc2',
847 '_pack_subfields_hash( $h, 1 )'
852 marc_fixed('008', 0, 'abcdef');
853 marc_fixed('000', 5, '5');
854 marc_fixed('000', 10, 'A');
855 marc_fixed('000', 0, '0');
857 ok( my $m = WebPAC::Normalize::_get_marc_fields(), '_get_marc_fields');
859 is_deeply( WebPAC::Normalize::_get_marc_fields(),