/[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 604 by dpavlin, Sun Jul 30 14:19:54 2006 UTC revision 813 by dpavlin, Sun Apr 1 21:47:47 2007 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4    
5  use Test::More tests => 153;  use Test::More tests => 332;
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            # count
198            my @el;
199            for my $i ( 0 .. 10 ) {
200                    cmp_ok( count( @el ), '==', $i, "count($i)");
201                    push @el, "element $i";
202            }
203    
204            # lookups
205    
206            throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
207    
208            ok(_set_load_row(sub {
209                    my ($database,$input,$mfn) = @_;
210                    diag "load_row( $database, $input, $mfn )" if ($debug);
211                    cmp_ok( $#_, '==', 2, 'have 3 arguments');
212                    ok($database, '_load_row database');
213                    ok($input, '_load_row input');
214                    ok($mfn, '_load_row mfn');
215                    return {
216                            '900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }],
217                    }
218    
219            }), '_set_load_row');
220    
221            my @v = qw/foo bar baz aaa bbb ccc ddd/;
222    
223            my @accumulated;
224    
225            for my $i ( 0 .. $#v ) {
226    
227                    my $mfn = 1000 + $i;
228    
229                    ok(WebPAC::Normalize::_set_config({ '_mfn' => $mfn }), "_set_config _mfn=$mfn");
230    
231                    my $size = $#v + 1;
232    
233                    cmp_ok(
234                            save_into_lookup('db','input','key', sub { @v }),
235                            '==', $size, "save_into_lookup $size values"
236                    );
237    
238                    ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
239                    diag "_get_lookup = ", dump($l) if ($debug);
240    
241                    my @lookup;
242    
243                    ok(@lookup = lookup(
244                                    sub {
245                                            diag "in show" if ($debug);
246                                            rec('900','x');
247                                    },
248                                    'db','input','key',
249                                    sub {
250                                            return @v;
251                                    }
252                            ),
253                    "lookup db/input/key");
254    
255                    push @accumulated, '900x-' . $mfn;
256    
257                    is_deeply(\@lookup, \@accumulated, "lookup db/input/key");
258    
259                    shift @v;
260    
261            }
262    
263            ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
264            diag "_get_lookup = ", dump($l) if ($debug);
265    
266            is_deeply( $l, {
267                    db => {
268                            input => {
269                                    key => {
270                                            foo => { 1000 => 1 },
271                                            bar => { 1000 => 1, 1001 => 1 },
272                                            baz => { 1000 => 1, 1001 => 1, 1002 => 1 },
273                                            aaa => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1 },
274                                            bbb => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1 },
275                                            ccc => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1 },
276                                            ddd => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1, 1006 => 1 },
277                                    },
278                            },
279                    },
280            }, 'lookup data');
281    
282    #######
283    
284            diag "lookup_hash1 = ", dump($lookup_hash1) if ($debug);
285            ok(_set_lookup( $lookup_hash1 ), '_set_lookup $lookup_hash1');
286    
287            throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
288    
289            ok(_set_load_row(sub {
290                    my ($database,$input,$mfn) = @_;
291                    diag "load_row( $database, $input, $mfn )";
292                    cmp_ok( $#_, '==', 2, 'have 3 arguments');
293                    ok($database, 'database');
294                    ok($input, 'input');
295                    ok($mfn, 'mfn');
296    
297            }), '_set_load_row');
298    
299    
300    #       cmp_ok(lookup(
301    #               sub {
302    #                       'found'
303    #               },
304    #               'db1','input1','key1',
305    #               sub {
306    #                       rec('200','a')
307    #               }
308    #       ), 'eq', 'found', 'lookup db1/input1/key1');
309    
310    
         _set_lookup( $lookup1 );  
311                    
312          cmp_ok(  #       cmp_ok(
313                  join_with(" i ",  #               lookup(
314                          lookup(  #               ),
315                                  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');  
316    
317          # check join_with operations          # check join_with operations
318    
# Line 235  sub test_s { Line 356  sub test_s {
356    
357          # test lookups          # test lookups
358    
359          _set_lookup( $lookup2 );          _set_lookup( $lookup_hash2 );
360    
361            throws_ok { lookup() } qr/need/, 'empty lookup';
362    
363          is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );          #is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
364    
365          ok(! lookup('non-existent'), 'lookup non-existant' );          #ok(! lookup('non-existent'), 'lookup non-existant' );
366    
367          _set_rec( $rec2 );          _set_rec( $rec2 );
368    
# Line 360  sub test_s { Line 483  sub test_s {
483          #          #
484          # MARC          # MARC
485          #          #
486          _debug( 4 );          #_debug( 4 );
487    
488          test_s(qq{ marc_indicators('900',1,2) });          test_s(qq{ marc_indicators('900',1,2) });
489          test_s(qq{ marc('900','a', rec('200') ) });          test_s(qq{ marc('900','a', rec('200') ) });
# Line 609  sub test_s { Line 732  sub test_s {
732                          [ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ],                          [ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ],
733                  ],                  ],
734          );          );
735    
736            test_s(qq{ marc_remove('*'); });
737            ok(! WebPAC::Normalize::_get_marc_fields(), 'marc_remove(*)');
738    
739          test_rec_rules(          test_rec_rules(
740                  'marc_duplicate',                  'marc_duplicate',
741                  { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },                  { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
742                  qq{                  qq{
743                            marc_leader('06',42);
744                            marc_leader('11',0);
745                          marc('900', 'a', rec('200','a') );                          marc('900', 'a', rec('200','a') );
746                          marc('900', 'b', rec('200','b') );                          marc('900', 'b', rec('200','b') );
747                          marc_duplicate;                          marc_duplicate;
748                            marc_leader('11',1);
749                          marc_remove('900','b');                          marc_remove('900','b');
750                          marc('900', 'b', rec('200','c') );                          marc('900', 'b', rec('200','c') );
751                          marc_duplicate;                          marc_duplicate;
752                            marc_leader('11',2);
753                          marc_remove('900','b');                          marc_remove('900','b');
754                          marc('900', 'b', rec('200','d') );                          marc('900', 'b', rec('200','d') );
755                          marc_duplicate;                          marc_duplicate;
756                            marc_leader('11',3);
757                          marc_remove('900','b');                          marc_remove('900','b');
758                          marc('900', 'b', rec('200','e') );                          marc('900', 'b', rec('200','e') );
759                  },                  },
# Line 631  sub test_s { Line 763  sub test_s {
763                  ],                  ],
764          );          );
765    
766            cmp_ok( marc_count(), '==', 3, 'marc_count' );
767    
768          my $i = 0;          my $i = 0;
769          foreach my $v ( qw/bar baz bing bong/ ) {          foreach my $v ( qw/bar baz bing bong/ ) {
770    
# Line 642  sub test_s { Line 776  sub test_s {
776                          [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],                          [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
777                          "MARC copy $i has $v",                          "MARC copy $i has $v",
778                  );                  );
779                    is_deeply(WebPAC::Normalize::_get_marc_leader(), { '06' => 42, 11 => $i }, "_get_marc_leader copy $i");
780                  $i++;                  $i++;
781          }          }
782    
# Line 657  sub test_s { Line 792  sub test_s {
792                          } ],                          } ],
793                  },                  },
794                  qq{                  qq{
795                          marc_original_order(200,900);                          marc_original_order(900,200);
796                  },                  },
797                  [                  [
798                          [ '900', ' ', ' ', 'a', 'a1', 'b', 'b1', 'a', 'a2', 'b', 'b2', 'c', 'c1', 'c', 'c2', ],                          [ '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', ],                          [ '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    

Legend:
Removed from v.604  
changed lines
  Added in v.813

  ViewVC Help
Powered by ViewVC 1.1.26