8f0df0163948a773a5f742b37c3b1e8694ddba4f
[webpac2] / t / 3-normalize.t
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Test::More tests => 322;
6 use Test::Exception;
7 use Cwd qw/abs_path/;
8 use blib;
9 use File::Slurp;
10 use Getopt::Long;
11
12 BEGIN {
13         use_ok( 'WebPAC::Normalize' );
14 }
15
16 use Data::Dump qw/dump/;
17
18 my $debug = 0;
19 GetOptions(
20         "debug+", \$debug
21 );
22
23 cmp_ok(_debug(1), '==', 1, '_debug level');
24 cmp_ok(_debug(0), '==', 0, '_debug level');
25
26 diag "debug level for $0 is $debug" if ($debug);
27 if ($debug > 2) {
28         diag "debug level for WebPAC::Normalize is ", _debug( $debug - 2 );
29 }
30
31 ok(my $abs_path = abs_path($0), "abs_path");
32 $abs_path =~ s#/[^/]*$#/#;
33 diag "abs_path: $abs_path" if ($debug);
34
35 my $rec1 = {
36         '200' => [{
37                 'a' => '200a',
38                 'b' => '200b',
39                 },{
40                 'c' => '200c',
41                 'd' => '200d',
42                 },{
43                 'a' => '200a*2',
44                 'd' => '200d*2',
45                 }],
46         '201' => [{
47                 'x' => '201x',
48                 'y' => '201y',
49                 }],
50         '900' => [
51                 '900-no_subfield'
52                 ],
53         '901' => [{
54                 'a' => '900a',
55                 }],
56         '902' => [{
57                 'z' => '900',
58                 }],
59 };
60
61 my $rec2 = {
62  '675' => [ {
63               'a' => '159.9'
64             } ],
65  '210' => [ {
66               'c' => 'New York University press',
67               'a' => 'New York',
68               'd' => 'cop. 1988'
69             } ],
70  '700' => [ {
71               'a' => 'Haynal',
72               'b' => 'AndrĂ©'
73             } ],
74  '801' => [ 'FFZG' ],
75  '991' => [ '8302' ],
76  '000' => [ 1 ],
77  '702' => [ {
78               'a' => 'Holder',
79               'b' => 'Elizabeth'
80             } ],
81  '215' => [ {
82               'c' => 'ilustr',
83               'a' => 'xix, 202 str',
84               'd' => '23cm'
85             } ],
86  '990' => [
87             '2140',
88             '88',
89             'HAY'
90           ],
91  '200' => [ {
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'
96             } ],
97  '610' => [ 'povijest psihoanalize' ],
98  '994' => [ {
99               'c' => '',
100               'a' => 'PS',
101               'b' => 'MG'
102             } ],
103  '320' => [ 'Kazalo' ],
104  '101' => [ 'ENG' ],
105  '686' => [ '2140' ],
106  '300' => [ 'Prijevod djela: ' ],
107 };
108
109
110 my $lookup_hash1 = {
111         'db1' => {
112                 'input1' => {
113                         '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 $lookup_hash2 = {
130         'db3' => {
131                 'input4' => {
132                         'key7' => { 7 => 1 },
133                         'key8' => { 8 => 1 },
134                 },
135         }
136 };
137
138 sub test {
139         print dump( @_ ), ("-" x 78), "\n";
140         ok( defined(@_) );
141 }
142
143 # how much of string evaled to display?
144 my $max_eval_output = 170;
145
146 sub dump_error {
147         my ($msg,$code) = @_;
148
149         my @l = split(/[\n\r]/, $code);
150         my $out = "$msg\n";
151
152         foreach my $i ( 0 .. $#l ) {
153                 $out .= sprintf("%2d: %s\n", $i, $l[$i]);
154         }
155
156         return $out;
157 }
158
159 sub test_s {
160         my $t = shift || die;
161
162         my $eval_t = $t;
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;
166
167         my @__ret;
168         eval "\@__ret = $t";
169         ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret));
170         return \@__ret;
171 }
172
173 {
174         no strict 'subs';
175         use WebPAC::Normalize;
176
177         ok(! _set_lookup( undef ), "set_lookup(undef)");
178
179         _set_rec( $rec1 );
180
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' ] );
189
190         cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
191
192         # simple list manipulatons
193         cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
194         cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
195         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
311         
312 #       cmp_ok(
313 #               lookup( 
314 #               ),
315 #       'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
316
317         # check join_with operations
318
319         sub test_join_with_2 {
320                 my ($a,$b,$e) = @_;
321
322                 cmp_ok(
323                         join_with(" <1> ",
324                                 rec('201',$a),
325                                 rec('201',$b),
326                         ), 
327                 'eq', $e, "join_with $a <1> $b = $e");
328         }
329
330         test_join_with_2('_','_','');
331         test_join_with_2('x','_','201x');
332         test_join_with_2('_','x','201x');
333         test_join_with_2('x','y','201x <1> 201y');
334
335         sub test_join_with_3 {
336                 my ($a,$b,$c,$e) = @_;
337
338                 cmp_ok(
339                         join_with(" <1> ", rec('201',$a),
340                                 join_with(" <2> ", rec('201',$b),
341                                         rec('201',$c),
342                                 )
343                         ), 
344                 'eq', $e, "join_with $a <1> $b <2> $c = $e");
345         };
346
347         test_join_with_3('_','_','_','');
348         test_join_with_3('x','_','_','201x');
349         test_join_with_3('_','x','_','201x');
350         test_join_with_3('_','_','x','201x');
351         test_join_with_3('x','y','_','201x <1> 201y');
352         test_join_with_3('x','_','y','201x <1> 201y');
353         test_join_with_3('_','x','y','201x <2> 201y');
354         test_join_with_3('x','_','y','201x <1> 201y');
355         test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
356
357         # test lookups
358
359         _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' );
364
365         #ok(! lookup('non-existent'), 'lookup non-existant' );
366
367         _set_rec( $rec2 );
368
369         test_s(qq{
370                 tag('Title',
371                         rec('200','a')
372                 );
373         });
374         test_s(qq{
375                 tag('Who',
376                         join_with(" ",
377                                 rec('702','a'),
378                                 rec('702','b')
379                         )
380                 );
381         });
382
383         test_s(qq{
384                 display('Publisher',
385                         rec('210','c')
386                 )
387         });
388         
389         test_s(qq{
390                 search('Year',
391                         regex( 's/[^\\d]+//',
392                                 rec('210','d')
393                         )
394                 )
395         });
396
397         ok(my $ds = _get_ds(), "get_ds");
398         diag "ds = ", dump($ds) if ($debug);
399
400
401         sub test_check_ds {
402
403                 my $t = shift;
404
405                 ok($ds = _get_ds(), 'get_ds');
406                 diag dump( $ds ) if ($debug);
407
408                 ok( $ds && $ds->{something}, 'get_ds->something exists' );
409                 ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
410                 ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
411
412                 return $ds;
413         }
414
415         _clean_ds();
416         test_s(qq{ search('something', '42'); });
417         test_s(qq{ search('empty', ''); });
418         test_check_ds('search');
419
420         _clean_ds();
421         test_s(qq{ display('something', '42'); });
422         test_s(qq{ display('empty', ''); });
423         test_check_ds('display');
424
425         _clean_ds();
426         test_s(qq{ tag('something', '42'); });
427         test_s(qq{ tag('empty', ''); });
428         test_check_ds('search');
429         test_check_ds('display');
430
431         _clean_ds();
432         my $n = read_file( "$abs_path/data/normalize.pl" );
433         $n .= "\n1;\n";
434         #diag "normalize code:\n$n\n";
435         test_s( $n );
436
437         ok($ds = _get_ds(), "get_ds");
438         diag "ds = ", dump($ds) if ($debug);
439
440         my $rec = {
441                 '200' => [{
442                         'a' => '200a',
443                         'b' => '200b',
444                 }],
445         };
446         my $rules = qq{ search('mixed', rec('200') ) };
447         
448         _clean_ds();
449         _set_rec( $rec );
450         test_s( $rules );
451         ok($ds = _get_ds(), "get_ds");
452         is_deeply( $ds, {
453                 'mixed' => {
454                         'search' => [ '200a', '200b' ],
455                         'tag' => 'mixed'
456                 }
457         }, 'correct get_ds');
458
459         ok(my $ds2 = WebPAC::Normalize::data_structure(
460                 row => $rec,
461                 rules => $rules,
462         ), 'data_structure');
463         is_deeply( $ds, $ds2, 'data_structure(s) same');
464
465         # wird and non-valid structure which is supported anyway
466         _clean_ds();
467         _set_rec({
468                 '200' => [{
469                         'a' => '200a',
470                 },
471                         '200-solo'
472                 ]
473         });
474         test_s(qq{ search('mixed', rec('200') ) });
475         ok($ds = _get_ds(), "get_ds");
476         is_deeply( $ds, {
477                 'mixed' => {
478                         'search' => [ '200a', '200-solo' ],
479                         'tag' => 'mixed'
480                 }
481         }, 'correct get_ds');
482
483         #
484         # MARC
485         #
486         #_debug( 4 );
487
488         test_s(qq{ marc_indicators('900',1,2) });
489         test_s(qq{ marc('900','a', rec('200') ) });
490         my $marc;
491         ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
492         diag dump( $marc ) if ($debug);
493
494         is_deeply( $marc, [
495                 [ '900', 1, 2, 'a', '200a' ],
496                 [ '900', 1, 2, 'a', '200-solo' ]
497         ], 'correct marc with indicators');
498
499         test_s(qq{ marc_indicators('900',' ',9) });
500         test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
501
502         ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
503         diag dump( $marc ) if ($debug);
504
505         is_deeply( $marc, [
506                 [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
507                 [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
508         ], 'correct marc with repetable subfield');
509
510         #
511         # test magic re-ordering of input data
512         #
513
514         sub test_rec_rules {
515                 my ($msg, $rec, $rules, $struct) = @_;
516
517                 _clean_ds();
518                 _set_rec($rec);
519
520                 foreach my $r (split(/;/, $rules)) {
521                         $r =~ s/[\s\n\r]+/ /gs;
522                         $r =~ s/^\s+//gs;
523                         $r =~ s/\s+$//gs;
524                         test_s($r) if ($r);
525                 }
526
527                 ok(my $marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
528                 diag dump( $marc ) if ($debug);
529                 diag "expects:\n", dump($struct) if ($debug > 1);
530                 is_deeply( $marc, $struct, $msg );
531         }
532
533         test_rec_rules(
534                 'correct marc with repetable subfield',
535                 {
536                         '200' => [{
537                                 'a' => '200a-1',
538                                 'b' => '200b-1',
539                                 'c' => '200c-1',
540                         }, {
541                                 'a' => '200a-2',
542                                 'b' => '200b-2',
543                         }, {
544                                 'a' => '200a-3',
545                         }],
546                 },
547                 qq{
548                         marc_indicators('900',1 ,0);
549                         marc('900','a', rec('200','a') );
550                         marc('900','b', rec('200','b') );
551                         marc('900','c', rec('200','c') );
552                 },
553                 [
554                         [ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ],
555                         [ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ],
556                         [ '900', 1, 0, 'a', '200a-3' ],
557                 ],
558         );
559
560
561         test_rec_rules(
562                 'marc_repeatable_subfield',
563                 {
564                         '200' => [{
565                                 'a' => '200a-1',
566                                 'b' => '200b-1',
567                                 'c' => '200c-1',
568                         }, {
569                                 'a' => '200a-2',
570                                 'b' => '200b-2',
571                                 'c' => '200c-2',
572                         }, {
573                                 'a' => '200a-3',
574                                 'c' => '200c-3',
575                         }],
576                 },
577                 qq{
578                         marc_indicators('900',1 ,0);
579                         marc_repeatable_subfield('900','a', rec('200','a') );
580                         marc('900','b', rec('200','b') );
581                         marc('900','c', rec('200','c') );
582                 },
583                 [
584                         [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],
585                         [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],
586                         [ '900', 1, 0, 'c', '200c-3' ],
587                 ],
588         );
589
590         test_rec_rules(
591                 'marc_compose',
592                 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
593                 qq{
594                         marc_compose('900',
595                                 'c', rec(200,'b'),
596                                 'b', rec(200,'a'),
597                                 'a', rec(200,'c'),
598                         );
599                 },
600                 [
601                         [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
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
623         #
624         sub test_rule {
625                 my ($msg, $rec, $rule, $struct) = @_;
626                 _clean_ds();
627                 _set_rec( $rec );
628                 $rule =~ s/\\/\\/gs;
629                 my $r = test_s( $rule );
630                 diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
631                 diag dump($struct) if ($debug);
632                 is_deeply( $r, $struct, $msg );
633         }
634
635         # test split_rec_on
636         test_rule(
637                 'split_rec_on',
638                 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
639                 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
640                 [ 'foo' ],
641         );
642         test_rule(
643                 'split_rec_on',
644                 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
645                 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
646                 [ '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(
662                 'marc_compose+split_rec_on',
663                 { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
664                 qq{
665                         marc_compose('900',
666                                 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
667                                 'c', rec(200,'c'),
668                                 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
669                                 'b', rec(200,'b'),
670                         );
671                 },
672                 [
673                         [ '900', ' ', ' ',
674                                 'a', 'foo',
675                                 'c', 'baz',
676                                 'a', 'bar',
677                                 'b', 42,
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('900', 'a', rec('200','a') );
744                         marc('900', 'b', rec('200','b') );
745                         marc_duplicate;
746                         marc_remove('900','b');
747                         marc('900', 'b', rec('200','c') );
748                         marc_duplicate;
749                         marc_remove('900','b');
750                         marc('900', 'b', rec('200','d') );
751                         marc_duplicate;
752                         marc_remove('900','b');
753                         marc('900', 'b', rec('200','e') );
754                 },
755                 [
756                         # this will return FIRST record
757                         [ '900', ' ', ' ', 'a', 42, 'b', 'bar' ],
758                 ],
759         );
760
761         my $i = 0;
762         foreach my $v ( qw/bar baz bing bong/ ) {
763
764                 ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ),
765                         "_get_marc_fields( offset => $i )"
766                 );
767                 diag "marc $i = ", dump( $marc ) if ($debug);
768                 is_deeply( $marc,
769                         [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
770                         "MARC copy $i has $v",
771                 );
772                 $i++;
773         }
774
775         test_rec_rules(
776                 'marc_original_order',
777                 {
778                         '200' => [ {
779                                 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
780                                 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
781                         }, {
782                                 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
783                                 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
784                         } ],
785                 },
786                 qq{
787                         marc_original_order(900,200);
788                 },
789                 [
790                         [ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ],
791                         [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],
792                 ],
793         );
794
795         test_rule(
796                 'rec1 skips subfields',
797                 {
798                         '200' => [ {
799                                 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
800                                 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
801                         }, {
802                                 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
803                                 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
804                         } ],
805                 },
806                 qq{
807                         rec1(200);
808                 },
809                 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
810         );
811
812         is_deeply(
813                 [ _pack_subfields_hash({
814                         a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
815                         subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
816                 }) ],
817                 ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
818                 '_pack_subfields_hash( $h )'
819         );
820
821         cmp_ok(
822                 _pack_subfields_hash({
823                         a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
824                         subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
825                 }, 1),
826                 'eq',
827                 '^aa1^bb1^aa2^bb2^cc1^cc2',
828                 '_pack_subfields_hash( $h, 1 )'
829         );
830 }
831