import OAI repository
[webpac2] / t / 3-normalize.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib 'lib';
5
6 use Test::More tests => 357;
7
8 use utf8;
9
10 BEGIN {
11         use_ok( 'WebPAC::Test' );
12         use_ok( 'WebPAC::Normalize' );
13 }
14
15 cmp_ok(_debug(1), '==', 1, '_debug level');
16 cmp_ok(_debug(0), '==', 0, '_debug level');
17
18 diag "debug level for $0 is $debug" if ($debug);
19 if ($debug > 2) {
20         diag "debug level for WebPAC::Normalize is ", _debug( $debug - 2 );
21 }
22
23 my $rec1 = {
24         '200' => [{
25                 'a' => '200a',
26                 'b' => '200b',
27                 },{
28                 'c' => '200c',
29                 'd' => '200d',
30                 },{
31                 'a' => '200a*2',
32                 'd' => '200d*2',
33                 }],
34         '201' => [{
35                 'x' => '201x',
36                 'y' => '201y',
37                 }],
38         '900' => [
39                 '900-no_subfield'
40                 ],
41         '901' => [{
42                 'a' => '900a',
43                 }],
44         '902' => [{
45                 'z' => '900',
46                 }],
47 };
48
49 my $rec2 = {
50  '675' => [ {
51               'a' => '159.9'
52             } ],
53  '210' => [ {
54               'c' => 'New York University press',
55               'a' => 'New York',
56               'd' => 'cop. 1988'
57             } ],
58  '700' => [ {
59               'a' => 'Haynal',
60               'b' => 'André'
61             } ],
62  '801' => [ 'FFZG' ],
63  '991' => [ '8302' ],
64  '000' => [ 1 ],
65  '702' => [ {
66               'a' => 'Holder',
67               'b' => 'Elizabeth'
68             } ],
69  '215' => [ {
70               'c' => 'ilustr',
71               'a' => 'xix, 202 str',
72               'd' => '23cm'
73             } ],
74  '990' => [
75             '2140',
76             '88',
77             'HAY'
78           ],
79  '200' => [ {
80               'e' => 'from Freud and Ferenczi to Michael balint',
81               'a' => 'Controversies in psychoanalytic method',
82               'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern',
83               'f' => 'by André E. Haynal'
84             } ],
85  '610' => [ 'povijest psihoanalize' ],
86  '994' => [ {
87               'c' => '',
88               'a' => 'PS',
89               'b' => 'MG'
90             } ],
91  '320' => [ 'Kazalo' ],
92  '101' => [ 'ENG' ],
93  '686' => [ '2140' ],
94  '300' => [ 'Prijevod djela: ' ],
95 };
96
97
98 my $lookup_hash1 = {
99         'db1' => {
100                 'input1' => {
101                         'key1' => { 1 => 1 },
102                         'key2' => { 2 => 1 },
103                 },
104                 'input2' => {
105                         'key3' => { 3 => 1 },
106                         'key4' => { 4 => 1 },
107                 },
108         },
109         'db2' => {
110                 'input3' => {
111                         'key5' => { 5 => 1 },
112                         'key6' => { 6 => 1 },
113                 },
114         }
115 };
116
117 my $lookup_hash2 = {
118         'db3' => {
119                 'input4' => {
120                         'key7' => { 7 => 1 },
121                         'key8' => { 8 => 1 },
122                 },
123         }
124 };
125
126 sub test {
127         print dump( @_ ), ("-" x 78), "\n";
128         ok( defined(@_) );
129 }
130
131 # how much of string evaled to display?
132 my $max_eval_output = 170;
133
134 sub dump_error {
135         my ($msg,$code) = @_;
136
137         my @l = split(/[\n\r]/, $code);
138         my $out = "$msg\n";
139
140         foreach my $i ( 0 .. $#l ) {
141                 $out .= sprintf("%2d: %s\n", $i, $l[$i]);
142         }
143
144         return $out;
145 }
146
147 sub test_s {
148         my $t = shift || die;
149
150         my $eval_t = $t;
151         $eval_t =~ s/[\n\r\s]+/ /gs;
152         $eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output);
153         $eval_t =~ s/\\/\\\\/gs;
154
155         my @__ret;
156         eval "\@__ret = $t";
157         ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret));
158         return \@__ret;
159 }
160
161 {
162         no strict 'subs';
163         use WebPAC::Normalize;
164
165         ok(! _set_lookup( undef ), "set_lookup(undef)");
166
167         _set_ds( $rec1 );
168
169         cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
170         cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
171         cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' );
172         diag "is_deeply checks\n";
173         is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] );
174         is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ],  \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]);
175         is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]);
176         is_deeply( \[ rec('902') ], \[ '900' ] );
177
178         cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
179
180         # simple list manipulatons
181         cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
182         cmp_ok( join('-', prefix('', 'x', 'y') ), 'eq', 'x-y', 'prefix empty');
183         cmp_ok( join('-', prefix(0, 'x', 'y') ), 'eq', '0x-0y', 'prefix 0');
184
185         cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
186         cmp_ok( join('-', suffix('', 'x', 'y' ) ), 'eq', 'x-y', 'suffix empty');
187         cmp_ok( join('-', suffix(0, 'x', 'y' ) ), 'eq', 'x0-y0', 'suffix 0');
188
189         cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
190         cmp_ok( join('-', surround('', '', 'x','y','z') ), 'eq', 'x-y-z', 'surround empty');
191         cmp_ok( join('-', surround(0, 0, 'x','y','z') ), 'eq', '0x0-0y0-0z0', 'surround 0 0');
192
193         # count
194         my @el;
195         for my $i ( 0 .. 10 ) {
196                 cmp_ok( count( @el ), '==', $i, "count($i)");
197                 push @el, "element $i";
198         }
199
200         # lookups
201
202         throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
203
204         ok(_set_load_row(sub {
205                 my ($database,$input,$mfn) = @_;
206                 diag "load_row( $database, $input, $mfn )" if ($debug);
207                 cmp_ok( $#_, '==', 2, 'have 3 arguments');
208                 ok($database, '_load_row database');
209                 ok($input, '_load_row input');
210                 ok($mfn, '_load_row mfn');
211                 return {
212                         '900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }],
213                 }
214
215         }), '_set_load_row');
216
217         my @v = qw/foo bar baz aaa bbb ccc ddd/;
218
219         my @accumulated;
220
221         for my $i ( 0 .. $#v ) {
222
223                 my $mfn = 1000 + $i;
224
225                 ok(WebPAC::Normalize::_set_config({ '_mfn' => $mfn }), "_set_config _mfn=$mfn");
226
227                 my $size = $#v + 1;
228
229                 cmp_ok(
230                         save_into_lookup('db','input','key', sub { @v }),
231                         '==', $size, "save_into_lookup $size values"
232                 );
233
234                 ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
235                 diag "_get_lookup = ", dump($l) if ($debug);
236
237                 my @lookup;
238
239                 ok(@lookup = lookup(
240                                 sub {
241                                         diag "in show" if ($debug);
242                                         rec('900','x');
243                                 },
244                                 'db','input','key',
245                                 sub {
246                                         return @v;
247                                 }
248                         ),
249                 "lookup db/input/key");
250
251                 push @accumulated, '900x-' . $mfn;
252
253                 is_deeply(\@lookup, \@accumulated, "lookup db/input/key");
254
255                 shift @v;
256
257         }
258
259         ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
260         diag "_get_lookup = ", dump($l) if ($debug);
261
262         is_deeply( $l, {
263                 db => {
264                         input => {
265                                 key => {
266                                         foo => { 1000 => 1 },
267                                         bar => { 1000 => 1, 1001 => 1 },
268                                         baz => { 1000 => 1, 1001 => 1, 1002 => 1 },
269                                         aaa => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1 },
270                                         bbb => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1 },
271                                         ccc => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1 },
272                                         ddd => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1, 1006 => 1 },
273                                 },
274                         },
275                 },
276         }, 'lookup data');
277
278 #######
279
280         diag "lookup_hash1 = ", dump($lookup_hash1) if ($debug);
281         ok(_set_lookup( $lookup_hash1 ), '_set_lookup $lookup_hash1');
282
283         throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
284
285         ok(_set_load_row(sub {
286                 my ($database,$input,$mfn) = @_;
287                 diag "load_row( $database, $input, $mfn )";
288                 cmp_ok( $#_, '==', 2, 'have 3 arguments');
289                 ok($database, 'database');
290                 ok($input, 'input');
291                 ok($mfn, 'mfn');
292
293         }), '_set_load_row');
294
295
296 #       cmp_ok(lookup(
297 #               sub {
298 #                       'found'
299 #               },
300 #               'db1','input1','key1',
301 #               sub {
302 #                       rec('200','a')
303 #               }
304 #       ), 'eq', 'found', 'lookup db1/input1/key1');
305
306
307         
308 #       cmp_ok(
309 #               lookup( 
310 #               ),
311 #       'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
312
313         # check join_with operations
314
315         sub test_join_with_2 {
316                 my ($a,$b,$e) = @_;
317
318                 cmp_ok(
319                         join_with(" <1> ",
320                                 rec('201',$a),
321                                 rec('201',$b),
322                         ), 
323                 'eq', $e, "join_with $a <1> $b = $e");
324         }
325
326         test_join_with_2('_','_','');
327         test_join_with_2('x','_','201x');
328         test_join_with_2('_','x','201x');
329         test_join_with_2('x','y','201x <1> 201y');
330
331         sub test_join_with_3 {
332                 my ($a,$b,$c,$e) = @_;
333
334                 cmp_ok(
335                         join_with(" <1> ", rec('201',$a),
336                                 join_with(" <2> ", rec('201',$b),
337                                         rec('201',$c),
338                                 )
339                         ), 
340                 'eq', $e, "join_with $a <1> $b <2> $c = $e");
341         };
342
343         test_join_with_3('_','_','_','');
344         test_join_with_3('x','_','_','201x');
345         test_join_with_3('_','x','_','201x');
346         test_join_with_3('_','_','x','201x');
347         test_join_with_3('x','y','_','201x <1> 201y');
348         test_join_with_3('x','_','y','201x <1> 201y');
349         test_join_with_3('_','x','y','201x <2> 201y');
350         test_join_with_3('x','_','y','201x <1> 201y');
351         test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
352
353         # test lookups
354
355         _set_lookup( $lookup_hash2 );
356
357         throws_ok { lookup() } qr/need/, 'empty lookup';
358
359         #is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
360
361         #ok(! lookup('non-existent'), 'lookup non-existant' );
362
363         _set_ds( $rec2 );
364
365         test_s(qq{
366                 search_display('Title',
367                         rec('200','a')
368                 );
369         });
370         test_s(qq{
371                 search_display('Who',
372                         join_with(" ",
373                                 rec('702','a'),
374                                 rec('702','b')
375                         )
376                 );
377         });
378
379         test_s(qq{
380                 display('Publisher',
381                         rec('210','c')
382                 )
383         });
384         
385         test_s(qq{
386                 search('Year',
387                         regex( 's/[^\\d]+//',
388                                 rec('210','d')
389                         )
390                 )
391         });
392
393         ok(my $ds = _get_ds(), "get_ds");
394         diag "ds = ", dump($ds) if ($debug);
395
396
397         sub test_check_ds {
398
399                 my $t = shift;
400
401                 ok($ds = _get_ds(), 'get_ds');
402                 diag dump( $ds ) if ($debug);
403
404                 ok( $ds && $ds->{something}, 'get_ds->something exists' );
405                 ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
406                 ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
407
408                 return $ds;
409         }
410
411         _clean_ds();
412         test_s(qq{ search('something', '42'); });
413         test_s(qq{ search('empty', ''); });
414         test_check_ds('search');
415
416         _clean_ds();
417         test_s(qq{ display('something', '42'); });
418         test_s(qq{ display('empty', ''); });
419         test_check_ds('display');
420
421         _clean_ds();
422         test_s(qq{ search_display('something', '42'); });
423         test_s(qq{ search_display('empty', ''); });
424         test_check_ds('search');
425         test_check_ds('display');
426
427         _clean_ds();
428         test_s(qq{ sorted('something', '42'); });
429         test_s(qq{ sorted('empty', ''); });
430         test_check_ds('sorted');
431
432         _clean_ds();
433         my $n = read_file( "$abs_path/data/normalize.pl" );
434         $n .= "\n1;\n";
435         #diag "normalize code:\n$n\n";
436         test_s( $n );
437
438         ok($ds = _get_ds(), "get_ds");
439         diag "ds = ", dump($ds) if ($debug);
440
441         my $rec = {
442                 '200' => [{
443                         'a' => '200a',
444                         'b' => '200b',
445                 }],
446         };
447         my $rules = qq{ search('mixed', rec('200') ) };
448         
449         _clean_ds();
450         _set_ds( $rec );
451         test_s( $rules );
452         ok($ds = _get_ds(), "get_ds");
453         is_deeply( $ds, {
454                 'mixed' => {
455                         'search' => [ '200a', '200b' ],
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_ds({
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                 }
480         }, 'correct get_ds');
481
482         #
483         # MARC
484         #
485         #_debug( 4 );
486
487         test_s(qq{ marc_indicators('900',1,2) });
488         test_s(qq{ marc('900','a', rec('200') ) });
489         my $marc;
490         ok($marc = WebPAC::Normalize::MARC::_get_marc_fields(), "_get_marc_fields");
491         diag dump( $marc ) if ($debug);
492
493         is_deeply( $marc, [
494                 [ '900', 1, 2, 'a', '200a' ],
495                 [ '900', 1, 2, 'a', '200-solo' ]
496         ], 'correct marc with indicators');
497
498         test_s(qq{ marc_indicators('900',' ',9) });
499         test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
500
501         ok($marc = WebPAC::Normalize::MARC::_get_marc_fields(), "_get_marc_fields");
502         diag dump( $marc ) if ($debug);
503
504         is_deeply( $marc, [
505                 [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
506                 [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
507         ], 'correct marc with repetable subfield');
508
509         #
510         # test magic re-ordering of input data
511         #
512
513         sub test_rec_rules {
514                 my ($msg, $rec, $rules, $struct) = @_;
515
516                 _clean_ds();
517                 _set_ds($rec);
518
519                 foreach my $r (split(/;\s*$/, $rules)) {
520                         $r =~ s/[\s\n\r]+/ /gs;
521                         $r =~ s/^\s+//gs;
522                         $r =~ s/\s+$//gs;
523                         diag "rule: $r" if $debug;
524                         test_s($r) if ($r);
525                 }
526
527                 ok(my $marc = WebPAC::Normalize::MARC::_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_ds( $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::MARC::_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::MARC::_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::MARC::_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         _clean_ds();
840         test_s(qq{
841                 marc_fixed('008', 0, 'abcdef');
842                 marc_fixed('000', 5, '5');
843                 marc_fixed('000', 10, 'A');
844                 marc_fixed('000', 0, '0');
845         });
846         ok( my $m = WebPAC::Normalize::MARC::_get_marc_fields(), '_get_marc_fields');
847         diag dump( $m );
848         is_deeply( WebPAC::Normalize::MARC::_get_marc_fields(),
849                 [
850                         ["008", "abcdef"], 
851                         #        0....5....10
852                         ["000", "0    5    A"]
853                 ]
854         );
855
856         test_s(qq{ isbn_13( '1558607013', '978-1558607019' ) });
857         test_s(qq{ isbn_10( '1558607013', '978-1558607019' ) });
858
859         is_deeply(
860                 [ isbn_13( '1558607013', '978-1558607019' ) ],
861                 [ '978-1-55860-701-9', '978-1-55860-701-9', ],
862         'isbn_13' );
863
864         is_deeply(
865                 [ isbn_10( '1558607013', '978-1558607019' ) ],
866                 [ '1-55860-701-3', '1-55860-701-3' ],
867         'isbn_10' );
868
869         # frec
870
871         $rec = {
872                         '200' => [ {
873                                 a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
874                                 subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
875                                 i1 => '0', i2 => '1',
876                         }, {
877                                 a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
878                                 subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
879                         } ],
880         };
881
882         test_rule( 'frec', $rec, qq{ frec(200) }, [ 'a1' ] );
883         test_rule( 'frec', $rec, qq{ frec(200,'a') }, [ 'a1' ] );
884         test_rule( 'frec', $rec, qq{ frec(200,'b') }, [ 'b1' ] );
885         test_rule( 'frec', $rec, qq{ frec(200,'c') }, [ 'c1' ] );
886
887         $rec->{'900'} = $rec->{'200'};
888         foreach my $sf ( qw/a b c/ ) {
889                 ok( frec_eq( '200' => $sf, '900' => $sf ), "frec_eq 200 == 900 $sf");
890                 ok( ! frec_ne( '200' => $sf, '900' => $sf ), "! frec_ne 200 == 900 $sf");
891         }
892
893         foreach my $sf ( qw/a b/ ) {
894                 ok( ! frec_eq( '200' => $sf, '200' => 'c' ), "! frec_eq 200 $sf == 200 c");
895                 ok( frec_ne( '200' => $sf, '200' => 'c' ), "frec_ne 200 $sf == 200 c");
896         }
897
898         test_rule( 'rec(200,i1)', $rec, qq{ rec(200,'i1') }, [ '0' ] );
899         test_rule( 'rec(200,i2)', $rec, qq{ rec(200,'i2') }, [ '1' ] );
900
901         my $hash = { a => '[a]', 'b' => '[b]', subfields => [ 'a', 0, 'b', 0 ] };
902         is_deeply([ _pack_subfields_hash( $hash ) ], [ '[a]', '[b]' ], '_pack_subfields_hash' );
903         ok( $hash->{subfields}, 'subfields exist' );
904         cmp_ok( _pack_subfields_hash( $hash, 1 ), 'eq', '^a[a]^b[b]', '_pack_subfields_hash' );
905         ok( $hash->{subfields}, 'subfields exist' );
906
907         $rec = { 'arr' => [ 1, 2, 3 ] };
908         test_rule( 'rec_array', $rec, qq{ rec_array('arr') }, $rec->{arr} );
909
910         _clean_ds();
911         _set_ds( $rec );
912         test_s(q{
913                         row( 'table', e => $_ ) foreach ( rec_array('arr') );
914         });
915         ok( my $rows = _get_ds->{_rows}->{table}, 'ds have _rows' );
916
917         foreach my $i ( 1 .. 3 ) {
918                 cmp_ok( $rows->[ $i - 1 ]->{e}, '==', $i, "e $i" );
919         }
920
921         test_rule( 'utf-8'
922                 , { '900' => [{ a => 'Čev', b => 'ić' }] }
923                 , qq{ join_with('', rec(900,'a'), 'apč', rec(900,'b') ) }
924                 , [ "\x{10C}evap\x{10D}i\x{107}" ]
925         );
926
927 }
928