/[webpac2]/trunk/t/3-normalize.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/t/3-normalize.t

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 555 by dpavlin, Sat Jul 1 10:19:39 2006 UTC revision 787 by dpavlin, Sun Dec 10 12:56:05 2006 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4    
5  use Test::More tests => 89;  use Test::More tests => 311;
6  use Test::Exception;  use Test::Exception;
7  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
8  use blib;  use blib;
# Line 32  ok(my $abs_path = abs_path($0), "abs_pat Line 32  ok(my $abs_path = abs_path($0), "abs_pat
32  $abs_path =~ s#/[^/]*$#/#;  $abs_path =~ s#/[^/]*$#/#;
33  diag "abs_path: $abs_path" if ($debug);  diag "abs_path: $abs_path" if ($debug);
34    
 #throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup";  
   
35  my $rec1 = {  my $rec1 = {
36          '200' => [{          '200' => [{
37                  'a' => '200a',                  'a' => '200a',
# Line 109  my $rec2 = { Line 107  my $rec2 = {
107  };  };
108    
109    
110  my $lookup1 = {  my $lookup_hash1 = {
111          '00900' => [          'db1' => {
112                  'lookup 1',                  'input1' => {
113                  'lookup 2',                          'key1' => { 1 => 1 },
114          ],                          'key2' => { 2 => 1 },
115                    },
116                    'input2' => {
117                            'key3' => { 3 => 1 },
118                            'key4' => { 4 => 1 },
119                    },
120            },
121            'db2' => {
122                    'input3' => {
123                            'key5' => { 5 => 1 },
124                            'key6' => { 6 => 1 },
125                    },
126            }
127  };  };
128    
129  my $lookup2 = {  my $lookup_hash2 = {
130          '00900' => 'lookup',          'db3' => {
131                    'input4' => {
132                            'key7' => { 7 => 1 },
133                            'key8' => { 8 => 1 },
134                    },
135            }
136  };  };
137    
   
138  sub test {  sub test {
139          print dump( @_ ), ("-" x 78), "\n";          print dump( @_ ), ("-" x 78), "\n";
140          ok( defined(@_) );          ok( defined(@_) );
# Line 148  sub test_s { Line 162  sub test_s {
162          my $eval_t = $t;          my $eval_t = $t;
163          $eval_t =~ s/[\n\r\s]+/ /gs;          $eval_t =~ s/[\n\r\s]+/ /gs;
164          $eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output);          $eval_t = substr($eval_t,0,$max_eval_output) . '...' if (length($eval_t) > $max_eval_output);
165            $eval_t =~ s/\\/\\\\/gs;
166    
167          eval "$t";          my @__ret;
168          ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t");          eval "\@__ret = $t";
169            ok(! $@, $@ ? dump_error($@, $t) : "eval: $eval_t = " . dump(@__ret));
170            return \@__ret;
171  }  }
172    
173  {  {
# Line 177  sub test_s { Line 194  sub test_s {
194          cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');          cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
195          cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');          cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
196    
197            # lookups
198    
199            throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
200    
201            ok(_set_load_row(sub {
202                    my ($database,$input,$mfn) = @_;
203                    diag "load_row( $database, $input, $mfn )";
204                    cmp_ok( $#_, '==', 2, 'have 3 arguments');
205                    ok($database, '_load_row database');
206                    ok($input, '_load_row input');
207                    ok($mfn, '_load_row mfn');
208                    return {
209                            '900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }],
210                    }
211    
212            }), '_set_load_row');
213    
214            my @v = qw/foo bar baz aaa bbb ccc ddd/;
215    
216            my @accumulated;
217    
218            for my $i ( 0 .. $#v ) {
219    
220                    my $mfn = 1000 + $i;
221    
222                    ok(WebPAC::Normalize::_set_config({ '_mfn' => $mfn }), "_set_config _mfn=$mfn");
223    
224                    my $size = $#v + 1;
225    
226                    cmp_ok(
227                            save_into_lookup('db','input','key', sub { @v }),
228                            '==', $size, "save_into_lookup $size values"
229                    );
230    
231                    ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
232                    diag "_get_lookup = ", dump($l);
233    
234                    my @lookup;
235    
236                    ok(@lookup = lookup(
237                                    sub {
238                                            diag "in show";
239                                            rec('900','x');
240                                    },
241                                    'db','input','key',
242                                    sub {
243                                            return @v;
244                                    }
245                            ),
246                    "lookup db/input/key");
247    
248                    push @accumulated, '900x-' . $mfn;
249    
250                    is_deeply(\@lookup, \@accumulated, "lookup db/input/key");
251    
252                    shift @v;
253    
254            }
255    
256            ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
257            diag "_get_lookup = ", dump($l);
258    
259            is_deeply( $l, {
260                    db => {
261                            input => {
262                                    key => {
263                                            foo => { 1000 => 1 },
264                                            bar => { 1000 => 1, 1001 => 1 },
265                                            baz => { 1000 => 1, 1001 => 1, 1002 => 1 },
266                                            aaa => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1 },
267                                            bbb => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1 },
268                                            ccc => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1 },
269                                            ddd => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1, 1006 => 1 },
270                                    },
271                            },
272                    },
273            }, 'lookup data');
274    
275    #######
276    
277            diag "lookup_hash1 = ", dump($lookup_hash1);
278            ok(_set_lookup( $lookup_hash1 ), '_set_lookup $lookup_hash1');
279    
280            throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
281    
282            ok(_set_load_row(sub {
283                    my ($database,$input,$mfn) = @_;
284                    diag "load_row( $database, $input, $mfn )";
285                    cmp_ok( $#_, '==', 2, 'have 3 arguments');
286                    ok($database, 'database');
287                    ok($input, 'input');
288                    ok($mfn, 'mfn');
289    
290            }), '_set_load_row');
291    
292    
293    #       cmp_ok(lookup(
294    #               sub {
295    #                       'found'
296    #               },
297    #               'db1','input1','key1',
298    #               sub {
299    #                       rec('200','a')
300    #               }
301    #       ), 'eq', 'found', 'lookup db1/input1/key1');
302    
303    
         _set_lookup( $lookup1 );  
304                    
305          cmp_ok(  #       cmp_ok(
306                  join_with(" i ",  #               lookup(
307                          lookup(  #               ),
308                                  regex( 's/^/00/',  #       'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
                                         rec2('902','z')  
                                 )  
                         )  
                 ),  
         'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');  
309    
310          # check join_with operations          # check join_with operations
311    
# Line 232  sub test_s { Line 349  sub test_s {
349    
350          # test lookups          # test lookups
351    
352          _set_lookup( $lookup2 );          _set_lookup( $lookup_hash2 );
353    
354          is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );          throws_ok { lookup() } qr/need/, 'empty lookup';
355    
356          ok(! lookup('non-existent'), 'lookup non-existant' );          #is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
357    
358            #ok(! lookup('non-existent'), 'lookup non-existant' );
359    
360          _set_rec( $rec2 );          _set_rec( $rec2 );
361    
# Line 357  sub test_s { Line 476  sub test_s {
476          #          #
477          # MARC          # MARC
478          #          #
479            #_debug( 4 );
480    
481          test_s(qq{ marc_indicators('900',1,2) });          test_s(qq{ marc_indicators('900',1,2) });
482          test_s(qq{ marc('900','a', rec('200') ) });          test_s(qq{ marc('900','a', rec('200') ) });
483          my @marc;          my $marc;
484          ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");          ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
485          diag dump( \@marc ) if ($debug);          diag dump( $marc ) if ($debug);
486    
487          is_deeply( \@marc, [          is_deeply( $marc, [
488                  [ '900', 1, 2, 'a', '200a' ],                  [ '900', 1, 2, 'a', '200a' ],
489                  [ '900', 1, 2, 'a', '200-solo' ]                  [ '900', 1, 2, 'a', '200-solo' ]
490          ], 'correct marc with indicators');          ], 'correct marc with indicators');
# Line 372  sub test_s { Line 492  sub test_s {
492          test_s(qq{ marc_indicators('900',' ',9) });          test_s(qq{ marc_indicators('900',' ',9) });
493          test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });          test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
494    
495          ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");          ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
496          diag dump( \@marc ) if ($debug);          diag dump( $marc ) if ($debug);
497    
498          is_deeply( \@marc, [          is_deeply( $marc, [
499                  [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],                  [ '900', 1, 2, 'a', '200a', 'a', '200-solo' ],
500                  [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]                  [ '900', ' ', 9, 'a', '200a', 'a', '200-solo' ]
501          ], 'correct marc with repetable subfield');          ], 'correct marc with repetable subfield');
# Line 397  sub test_s { Line 517  sub test_s {
517                          test_s($r) if ($r);                          test_s($r) if ($r);
518                  }                  }
519    
520                  ok(@marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");                  ok(my $marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
521                  diag dump( \@marc ) if ($debug);                  diag dump( $marc ) if ($debug);
522                    diag "expects:\n", dump($struct) if ($debug > 1);
523                  is_deeply( \@marc, $struct, $msg );                  is_deeply( $marc, $struct, $msg );
524          }          }
525    
526          test_rec_rules(          test_rec_rules(
# Line 459  sub test_s { Line 579  sub test_s {
579                          [ '900', 1, 0, 'c', '200c-3' ],                          [ '900', 1, 0, 'c', '200c-3' ],
580                  ],                  ],
581          );          );
582    
583            test_rec_rules(
584                    'marc_compose',
585                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
586                    qq{
587                            marc_compose('900',
588                                    'c', rec(200,'b'),
589                                    'b', rec(200,'a'),
590                                    'a', rec(200,'c'),
591                            );
592                    },
593                    [
594                            [ '900', ' ', ' ', 'c', 42, 'b', 'foo ; bar', 'a', 'baz' ]
595                    ],
596            );
597    
598            test_rec_rules(
599                    'marc_compose with + subfields',
600                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
601                    qq{
602                            marc_compose('900',
603                                    'a', rec(200,'a'),
604                                    '+', prefix(" * ", rec(200,'c')),
605                                    'b', rec(200,'b'),
606                                    '+', prefix(" : ", rec(200,'c')),
607                            );
608                    },
609                    [
610                            [ '900', ' ', ' ', 'a', 'foo ; bar * baz', 'b', '42 : baz' ]
611                    ],
612            );
613    
614            #
615            # test rules
616            #
617            sub test_rule {
618                    my ($msg, $rec, $rule, $struct) = @_;
619                    _clean_ds();
620                    _set_rec( $rec );
621                    $rule =~ s/\\/\\/gs;
622                    my $r = test_s( $rule );
623                    diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
624                    diag dump($struct) if ($debug);
625                    is_deeply( $r, $struct, $msg );
626            }
627    
628            # test split_rec_on
629            test_rule(
630                    'split_rec_on',
631                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
632                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 1) },
633                    [ 'foo' ],
634            );
635            test_rule(
636                    'split_rec_on',
637                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
638                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/, 2) },
639                    [ 'bar' ],
640            );
641            test_rule(
642                    'split_rec_on no part',
643                    { '200' => [{ a => 'foo ; bar', b => 42, c => 'baz' }] },
644                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
645                    [ 'foo', 'bar' ],
646            );
647            test_rule(
648                    'split_rec_on no record',
649                    {},
650                    qq{ split_rec_on('200','a', qr/\\s*;\\s*/) },
651                    [ '' ],
652            );
653    
654            test_rec_rules(
655                    'marc_compose+split_rec_on',
656                    { '200' => [{ a => 'foo ! bar', b => 42, c => 'baz' }] },
657                    qq{
658                            marc_compose('900',
659                                    'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 1),
660                                    'c', rec(200,'c'),
661                                    'a', split_rec_on(200,'a', qr/\\s*!\\s*/, 2),
662                                    'b', rec(200,'b'),
663                            );
664                    },
665                    [
666                            [ '900', ' ', ' ',
667                                    'a', 'foo',
668                                    'c', 'baz',
669                                    'a', 'bar',
670                                    'b', 42,
671                            ]
672                    ],
673            );
674    
675            cmp_ok(marc_leader('06',42), '==', 42, 'marc_leader');
676            cmp_ok(marc_leader('11',5), '==', 5, 'marc_leader');
677            ok(marc_leader(), 'marc_leader get');
678            diag "leader: ", dump(marc_leader()) if ($debug);
679            is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
680    
681            test_rule(
682                    'rec1(000)',
683                    { '000' => [ 42 ]},
684                    qq{ rec('000') },
685                    [ 42 ],
686            );
687    
688            test_rec_rules(
689                    'marc(001,rec(000))',
690                    { '000' => [ 42 ]},
691                    qq{
692                            marc('001', rec('000') );
693                    },
694                    [
695                            [ '001', 42, ]
696                    ],
697            );
698    
699            test_rec_rules(
700                    'marc_remove subfield',
701                    { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
702                    qq{
703                            marc('900', 'a', rec('200','a') );
704                            marc('900', 'b', rec('200','b') );
705                            marc_remove('900','b');
706                            marc('900', 'b', rec('200','c') );
707                            marc_remove('900','a');
708                    },
709                    [
710                            [ '900', ' ', ' ', 'b', 'baz' ],
711                    ],
712            );
713    
714            test_rec_rules(
715                    'marc_remove field',
716                    { '200' => [{ a => 42, b => 'bar', c => 'baz' }] },
717                    qq{
718                            marc('900', 'a', rec('200','a') );
719                            marc('900', 'b', rec('200','b') );
720                            marc('901', 'b', rec('200','b') );
721                            marc('901', 'c', rec('200','c') );
722                            marc_remove('900');
723                    },
724                    [
725                            [ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ],
726                    ],
727            );
728    
729            test_s(qq{ marc_remove('*'); });
730            ok(! WebPAC::Normalize::_get_marc_fields(), 'marc_remove(*)');
731    
732            test_rec_rules(
733                    'marc_duplicate',
734                    { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
735                    qq{
736                            marc('900', 'a', rec('200','a') );
737                            marc('900', 'b', rec('200','b') );
738                            marc_duplicate;
739                            marc_remove('900','b');
740                            marc('900', 'b', rec('200','c') );
741                            marc_duplicate;
742                            marc_remove('900','b');
743                            marc('900', 'b', rec('200','d') );
744                            marc_duplicate;
745                            marc_remove('900','b');
746                            marc('900', 'b', rec('200','e') );
747                    },
748                    [
749                            # this will return FIRST record
750                            [ '900', ' ', ' ', 'a', 42, 'b', 'bar' ],
751                    ],
752            );
753    
754            my $i = 0;
755            foreach my $v ( qw/bar baz bing bong/ ) {
756    
757                    ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ),
758                            "_get_marc_fields( offset => $i )"
759                    );
760                    diag "marc $i = ", dump( $marc ) if ($debug);
761                    is_deeply( $marc,
762                            [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
763                            "MARC copy $i has $v",
764                    );
765                    $i++;
766            }
767    
768            test_rec_rules(
769                    'marc_original_order',
770                    {
771                            '200' => [ {
772                                    a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
773                                    subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
774                            }, {
775                                    a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
776                                    subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
777                            } ],
778                    },
779                    qq{
780                            marc_original_order(900,200);
781                    },
782                    [
783                            [ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ],
784                            [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],
785                    ],
786            );
787    
788            test_rule(
789                    'rec1 skips subfields',
790                    {
791                            '200' => [ {
792                                    a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
793                                    subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
794                            }, {
795                                    a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
796                                    subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
797                            } ],
798                    },
799                    qq{
800                            rec1(200);
801                    },
802                    ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
803            );
804    
805            is_deeply(
806                    [ _pack_subfields_hash({
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                    ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
811                    '_pack_subfields_hash( $h )'
812            );
813    
814            cmp_ok(
815                    _pack_subfields_hash({
816                            a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
817                            subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
818                    }, 1),
819                    'eq',
820                    '^aa1^bb1^aa2^bb2^cc1^cc2',
821                    '_pack_subfields_hash( $h, 1 )'
822            );
823  }  }
824    

Legend:
Removed from v.555  
changed lines
  Added in v.787

  ViewVC Help
Powered by ViewVC 1.1.26