r772@llin: dpavlin | 2006-07-02 22:14:37 +0200
[webpac2] / t / 3-normalize.t
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Test::More tests => 103;
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 #throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup";
36
37 my $rec1 = {
38         '200' => [{
39                 'a' => '200a',
40                 'b' => '200b',
41                 },{
42                 'c' => '200c',
43                 'd' => '200d',
44                 },{
45                 'a' => '200a*2',
46                 'd' => '200d*2',
47                 }],
48         '201' => [{
49                 'x' => '201x',
50                 'y' => '201y',
51                 }],
52         '900' => [
53                 '900-no_subfield'
54                 ],
55         '901' => [{
56                 'a' => '900a',
57                 }],
58         '902' => [{
59                 'z' => '900',
60                 }],
61 };
62
63 my $rec2 = {
64  '675' => [ {
65               'a' => '159.9'
66             } ],
67  '210' => [ {
68               'c' => 'New York University press',
69               'a' => 'New York',
70               'd' => 'cop. 1988'
71             } ],
72  '700' => [ {
73               'a' => 'Haynal',
74               'b' => 'AndrĂ©'
75             } ],
76  '801' => [ 'FFZG' ],
77  '991' => [ '8302' ],
78  '000' => [ 1 ],
79  '702' => [ {
80               'a' => 'Holder',
81               'b' => 'Elizabeth'
82             } ],
83  '215' => [ {
84               'c' => 'ilustr',
85               'a' => 'xix, 202 str',
86               'd' => '23cm'
87             } ],
88  '990' => [
89             '2140',
90             '88',
91             'HAY'
92           ],
93  '200' => [ {
94               'e' => 'from Freud and Ferenczi to Michael balint',
95               'a' => 'Controversies in psychoanalytic method',
96               'g' => 'translated by Elizabeth Holder on the basisi of a first draft by Archie Hooton ; with a preface by Daniel N. Stern',
97               'f' => 'by AndrĂ© E. Haynal'
98             } ],
99  '610' => [ 'povijest psihoanalize' ],
100  '994' => [ {
101               'c' => '',
102               'a' => 'PS',
103               'b' => 'MG'
104             } ],
105  '320' => [ 'Kazalo' ],
106  '101' => [ 'ENG' ],
107  '686' => [ '2140' ],
108  '300' => [ 'Prijevod djela: ' ],
109 };
110
111
112 my $lookup1 = {
113         '00900' => [
114                 'lookup 1',
115                 'lookup 2',
116         ],
117 };
118
119 my $lookup2 = {
120         '00900' => 'lookup',
121 };
122
123
124 sub test {
125         print dump( @_ ), ("-" x 78), "\n";
126         ok( defined(@_) );
127 }
128
129 # how much of string evaled to display?
130 my $max_eval_output = 170;
131
132 sub dump_error {
133         my ($msg,$code) = @_;
134
135         my @l = split(/[\n\r]/, $code);
136         my $out = "$msg\n";
137
138         foreach my $i ( 0 .. $#l ) {
139                 $out .= sprintf("%2d: %s\n", $i, $l[$i]);
140         }
141
142         return $out;
143 }
144
145 sub test_s {
146         my $t = shift || die;
147
148         my $eval_t = $t;
149         $eval_t =~ s/[\n\r\s]+/ /gs;
150         $eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output);
151         $eval_t =~ s/\\/\\\\/gs;
152
153         my $v = eval "$t";
154         ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t");
155         $v;
156 }
157
158 {
159         no strict 'subs';
160         use WebPAC::Normalize;
161
162         ok(! _set_lookup( undef ), "set_lookup(undef)");
163
164         _set_rec( $rec1 );
165
166         cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
167         cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
168         cmp_ok( join(" * ", sort(rec1('200'), rec1('201') )), 'eq', '200a * 200a*2 * 200b * 200c * 200d * 200d*2 * 201x * 201y', 'join sort rec1 rec1' );
169         diag "is_deeply checks\n";
170         is_deeply( \[ rec1('200') ], \[ qw/200a 200b 200c 200d 200a*2 200d*2/ ] );
171         is_deeply( \[ regex( 's/0/o/g', rec1('200') ) ],  \[ qw/2ooa 2oob 2ooc 2ood 2ooa*2 2ood*2/ ]);
172         is_deeply( \[ grep { /\*/ } regex( 's/0/o/g', rec1('200') ) ], \[ qw/2ooa*2 2ood*2/ ]);
173         is_deeply( \[ rec('902') ], \[ '900' ] );
174
175         cmp_ok( rec('902'), 'eq', rec('902','z'), 'rec sf' );
176
177         # simple list manipulatons
178         cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
179         cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
180         cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
181
182
183         _set_lookup( $lookup1 );
184         
185         cmp_ok(
186                 join_with(" i ",
187                         lookup( 
188                                 regex( 's/^/00/',
189                                         rec2('902','z')
190                                 )
191                         ) 
192                 ),
193         'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
194
195         # check join_with operations
196
197         sub test_join_with_2 {
198                 my ($a,$b,$e) = @_;
199
200                 cmp_ok(
201                         join_with(" <1> ",
202                                 rec('201',$a),
203                                 rec('201',$b),
204                         ), 
205                 'eq', $e, "join_with $a <1> $b = $e");
206         }
207
208         test_join_with_2('_','_','');
209         test_join_with_2('x','_','201x');
210         test_join_with_2('_','x','201x');
211         test_join_with_2('x','y','201x <1> 201y');
212
213         sub test_join_with_3 {
214                 my ($a,$b,$c,$e) = @_;
215
216                 cmp_ok(
217                         join_with(" <1> ", rec('201',$a),
218                                 join_with(" <2> ", rec('201',$b),
219                                         rec('201',$c),
220                                 )
221                         ), 
222                 'eq', $e, "join_with $a <1> $b <2> $c = $e");
223         };
224
225         test_join_with_3('_','_','_','');
226         test_join_with_3('x','_','_','201x');
227         test_join_with_3('_','x','_','201x');
228         test_join_with_3('_','_','x','201x');
229         test_join_with_3('x','y','_','201x <1> 201y');
230         test_join_with_3('x','_','y','201x <1> 201y');
231         test_join_with_3('_','x','y','201x <2> 201y');
232         test_join_with_3('x','_','y','201x <1> 201y');
233         test_join_with_3('x','y','x','201x <1> 201y <2> 201x');
234
235         # test lookups
236
237         _set_lookup( $lookup2 );
238
239         is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
240
241         ok(! lookup('non-existent'), 'lookup non-existant' );
242
243         _set_rec( $rec2 );
244
245         test_s(qq{
246                 tag('Title',
247                         rec('200','a')
248                 );
249         });
250         test_s(qq{
251                 tag('Who',
252                         join_with(" ",
253                                 rec('702','a'),
254                                 rec('702','b')
255                         )
256                 );
257         });
258
259         test_s(qq{
260                 display('Publisher',
261                         rec('210','c')
262                 )
263         });
264         
265         test_s(qq{
266                 search('Year',
267                         regex( 's/[^\\d]+//',
268                                 rec('210','d')
269                         )
270                 )
271         });
272
273         ok(my $ds = _get_ds(), "get_ds");
274         diag "ds = ", dump($ds) if ($debug);
275
276
277         sub test_check_ds {
278
279                 my $t = shift;
280
281                 ok($ds = _get_ds(), 'get_ds');
282                 diag dump( $ds ) if ($debug);
283
284                 ok( $ds && $ds->{something}, 'get_ds->something exists' );
285                 ok( $ds && $ds->{something}->{$t}, 'get_ds->something->'.$t.' exists') if ($t);
286                 ok( $ds && !$ds->{empty}, 'get_ds->empty doesn\'t' );
287
288                 return $ds;
289         }
290
291         _clean_ds();
292         test_s(qq{ search('something', '42'); });
293         test_s(qq{ search('empty', ''); });
294         test_check_ds('search');
295
296         _clean_ds();
297         test_s(qq{ display('something', '42'); });
298         test_s(qq{ display('empty', ''); });
299         test_check_ds('display');
300
301         _clean_ds();
302         test_s(qq{ tag('something', '42'); });
303         test_s(qq{ tag('empty', ''); });
304         test_check_ds('search');
305         test_check_ds('display');
306
307         _clean_ds();
308         my $n = read_file( "$abs_path/data/normalize.pl" );
309         $n .= "\n1;\n";
310         #diag "normalize code:\n$n\n";
311         test_s( $n );
312
313         ok($ds = _get_ds(), "get_ds");
314         diag "ds = ", dump($ds) if ($debug);
315
316         my $rec = {
317                 '200' => [{
318                         'a' => '200a',
319                         'b' => '200b',
320                 }],
321         };
322         my $rules = qq{ search('mixed', rec('200') ) };
323         
324         _clean_ds();
325         _set_rec( $rec );
326         test_s( $rules );
327         ok($ds = _get_ds(), "get_ds");
328         is_deeply( $ds, {
329                 'mixed' => {
330                         'search' => [ '200a', '200b' ],
331                         'tag' => 'mixed'
332                 }
333         }, 'correct get_ds');
334
335         ok(my $ds2 = WebPAC::Normalize::data_structure(
336                 row => $rec,
337                 rules => $rules,
338         ), 'data_structure');
339         is_deeply( $ds, $ds2, 'data_structure(s) same');
340
341         # wird and non-valid structure which is supported anyway
342         _clean_ds();
343         _set_rec({
344                 '200' => [{
345                         'a' => '200a',
346                 },
347                         '200-solo'
348                 ]
349         });
350         test_s(qq{ search('mixed', rec('200') ) });
351         ok($ds = _get_ds(), "get_ds");
352         is_deeply( $ds, {
353                 'mixed' => {
354                         'search' => [ '200a', '200-solo' ],
355                         'tag' => 'mixed'
356                 }
357         }, 'correct get_ds');
358
359         #
360         # MARC
361         #
362
363         test_s(qq{ marc_indicators('900',1,2) });
364         test_s(qq{ marc('900','a', rec('200') ) });
365         my @marc;
366         ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
367         diag dump( \@marc ) if ($debug);
368
369         is_deeply( \@marc, [
370                 [ '900', 1, 2, 'a', '200a' ],
371                 [ '900', 1, 2, 'a', '200-solo' ]
372         ], 'correct marc with indicators');
373
374         test_s(qq{ marc_indicators('900',' ',9) });
375         test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
376
377         ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
378         diag dump( \@marc ) if ($debug);
379
380         is_deeply( \@marc, [
381                 [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
382                 [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
383         ], 'correct marc with repetable subfield');
384
385         #
386         # test magic re-ordering of input data
387         #
388
389         sub test_rec_rules {
390                 my ($msg, $rec, $rules, $struct) = @_;
391
392                 _clean_ds();
393                 _set_rec($rec);
394
395                 foreach my $r (split(/;/, $rules)) {
396                         $r =~ s/[\s\n\r]+/ /gs;
397                         $r =~ s/^\s+//gs;
398                         $r =~ s/\s+$//gs;
399                         test_s($r) if ($r);
400                 }
401
402                 ok(my @marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
403                 diag dump( \@marc ) if ($debug);
404                 diag "expects:\n", dump($struct) if ($debug > 1);
405                 is_deeply( \@marc, $struct, $msg );
406         }
407
408         test_rec_rules(
409                 'correct marc with repetable subfield',
410                 {
411                         '200' => [{
412                                 'a' => '200a-1',
413                                 'b' => '200b-1',
414                                 'c' => '200c-1',
415                         }, {
416                                 'a' => '200a-2',
417                                 'b' => '200b-2',
418                         }, {
419                                 'a' => '200a-3',
420                         }],
421                 },
422                 qq{
423                         marc_indicators('900',1 ,0);
424                         marc('900','a', rec('200','a') );
425                         marc('900','b', rec('200','b') );
426                         marc('900','c', rec('200','c') );
427                 },
428                 [
429                         [ '900', 1, 0, 'a', '200a-1', 'b', '200b-1', 'c', '200c-1' ],
430                         [ '900', 1, 0, 'a', '200a-2', 'b', '200b-2' ],
431                         [ '900', 1, 0, 'a', '200a-3' ],
432                 ],
433         );
434
435
436         test_rec_rules(
437                 'marc_repeatable_subfield',
438                 {
439                         '200' => [{
440                                 'a' => '200a-1',
441                                 'b' => '200b-1',
442                                 'c' => '200c-1',
443                         }, {
444                                 'a' => '200a-2',
445                                 'b' => '200b-2',
446                                 'c' => '200c-2',
447                         }, {
448                                 'a' => '200a-3',
449                                 'c' => '200c-3',
450                         }],
451                 },
452                 qq{
453                         marc_indicators('900',1 ,0);
454                         marc_repeatable_subfield('900','a', rec('200','a') );
455                         marc('900','b', rec('200','b') );
456                         marc('900','c', rec('200','c') );
457                 },
458                 [
459                         [ '900', 1, 0, 'a', '200a-1', 'a', '200a-2', 'a', '200a-3', 'b', '200b-1', 'c', '200c-1' ],
460                         [ '900', 1, 0, 'b', '200b-2', 'c', '200c-2' ],
461                         [ '900', 1, 0, 'c', '200c-3' ],
462                 ],
463         );
464
465         test_rec_rules(
466                 'marc_compose',
467                 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
468                 qq{
469                         marc_compose('900',
470                                 'c', rec(200,'b'),
471                                 'b', rec(200,'a'),
472                                 'a', rec(200,'c'),
473                         );
474                 },
475                 [
476                         [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
477                 ],
478         );
479
480         #
481         # test rules
482         #
483         sub test_rule {
484                 my ($msg, $rec, $rule, $struct) = @_;
485                 _clean_ds();
486                 _set_rec( $rec );
487                 $rule =~ s/\\/\\/gs;
488                 my $r = test_s( $rule );
489                 diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
490                 diag dump($struct) if ($debug);
491                 is_deeply( $r, $struct, $msg );
492         }
493
494         # test split_rec_on
495         test_rule(
496                 'split_rec_on',
497                 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
498                 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
499                 'foo',
500         );
501         test_rule(
502                 'split_rec_on',
503                 { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
504                 qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
505                 'bar',
506         );
507
508         test_rec_rules(
509                 'marc_compose+split_rec_on',
510                 { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
511                 qq{
512                         marc_compose('900',
513                                 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
514                                 'c', rec(200,'c'),
515                                 'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
516                                 'b', rec(200,'b'),
517                         );
518                 },
519                 [
520                         [ '900', ' ', ' ',
521                                 'a', 'foo',
522                                 'c', 'baz',
523                                 'a', 'bar',
524                                 'b', 42,
525                         ]
526                 ],
527         );
528
529         cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
530         cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
531         ok(marc_leader(), 'marc_leader get');
532         diag "leader: ", dump(marc_leader()) if ($debug);
533         is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
534 }
535