/[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 571 by dpavlin, Mon Jul 3 14:30:22 2006 UTC revision 725 by dpavlin, Fri Sep 29 18:55:41 2006 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4    
5  use Test::More tests => 112;  use Test::More tests => 309;
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 180  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_ds() } qr/CODE/, 'empty _set_load_ds()';
200    
201            ok(_set_load_ds(sub {
202                    my ($database,$input,$mfn) = @_;
203                    diag "load_ds( $database, $input, $mfn )";
204                    cmp_ok( $#_, '==', 2, 'have 3 arguments');
205                    ok($database, '_load_ds database');
206                    ok($input, '_load_ds input');
207                    ok($mfn, '_load_ds mfn');
208                    return {
209                            '900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }],
210                    }
211    
212            }), '_set_load_ds');
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(my @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_ds() } qr/CODE/, 'empty _set_load_ds()';
281    
282            ok(_set_load_ds(sub {
283                    my ($database,$input,$mfn) = @_;
284                    diag "load_ds( $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_ds');
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 235  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            throws_ok { lookup() } qr/need/, 'empty lookup';
355    
356          is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );          #is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
357    
358          ok(! lookup('non-existent'), 'lookup non-existant' );          #ok(! lookup('non-existent'), 'lookup non-existant' );
359    
360          _set_rec( $rec2 );          _set_rec( $rec2 );
361    
# Line 360  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 375  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 400  sub test_s { Line 517  sub test_s {
517                          test_s($r) if ($r);                          test_s($r) if ($r);
518                  }                  }
519    
520                  ok(my @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);                  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 478  sub test_s { Line 595  sub test_s {
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          # test rules
616          #          #
# Line 545  sub test_s { Line 678  sub test_s {
678          diag "leader: ", dump(marc_leader()) if ($debug);          diag "leader: ", dump(marc_leader()) if ($debug);
679          is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");          is_deeply(marc_leader(), { '06' => 42, 11 => 5 }, "marc_leader full");
680    
         _debug(2);  
681          test_rule(          test_rule(
682                  'rec1(000)',                  'rec1(000)',
683                  { '000' => [ 42 ]},                  { '000' => [ 42 ]},
# Line 554  sub test_s { Line 686  sub test_s {
686          );          );
687    
688          test_rec_rules(          test_rec_rules(
689                  'marc_compose+split_rec_on',                  'marc(001,rec(000))',
690                  { '000' => [ 42 ]},                  { '000' => [ 42 ]},
691                  qq{                  qq{
692                          marc('001', rec('000') );                          marc('001', rec('000') );
693                  },                  },
694                  [                  [
695                          [ '001', ' ', ' ', 42, ]                          [ '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            test_rec_rules(
729                    'marc_duplicate',
730                    { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
731                    qq{
732                            marc('900', 'a', rec('200','a') );
733                            marc('900', 'b', rec('200','b') );
734                            marc_duplicate;
735                            marc_remove('900','b');
736                            marc('900', 'b', rec('200','c') );
737                            marc_duplicate;
738                            marc_remove('900','b');
739                            marc('900', 'b', rec('200','d') );
740                            marc_duplicate;
741                            marc_remove('900','b');
742                            marc('900', 'b', rec('200','e') );
743                    },
744                    [
745                            # this will return FIRST record
746                            [ '900', ' ', ' ', 'a', 42, 'b', 'bar' ],
747                    ],
748            );
749    
750            my $i = 0;
751            foreach my $v ( qw/bar baz bing bong/ ) {
752    
753                    ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ),
754                            "_get_marc_fields( offset => $i )"
755                    );
756                    diag "marc $i = ", dump( $marc ) if ($debug);
757                    is_deeply( $marc,
758                            [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
759                            "MARC copy $i has $v",
760                    );
761                    $i++;
762            }
763    
764            test_rec_rules(
765                    'marc_original_order',
766                    {
767                            '200' => [ {
768                                    a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
769                                    subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
770                            }, {
771                                    a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
772                                    subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
773                            } ],
774                    },
775                    qq{
776                            marc_original_order(900,200);
777                    },
778                    [
779                            [ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ],
780                            [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],
781                  ],                  ],
782          );          );
783    
784            test_rule(
785                    'rec1 skips subfields',
786                    {
787                            '200' => [ {
788                                    a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
789                                    subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
790                            }, {
791                                    a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
792                                    subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
793                            } ],
794                    },
795                    qq{
796                            rec1(200);
797                    },
798                    ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
799            );
800    
801            is_deeply(
802                    [ _pack_subfields_hash({
803                            a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
804                            subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
805                    }) ],
806                    ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
807                    '_pack_subfields_hash( $h )'
808            );
809    
810            cmp_ok(
811                    _pack_subfields_hash({
812                            a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
813                            subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
814                    }, 1),
815                    'eq',
816                    '^aa1^bb1^aa2^bb2^cc1^cc2',
817                    '_pack_subfields_hash( $h, 1 )'
818            );
819  }  }
820    

Legend:
Removed from v.571  
changed lines
  Added in v.725

  ViewVC Help
Powered by ViewVC 1.1.26