/[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 811 by dpavlin, Sun Apr 1 21:47:40 2007 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4    
5  use Test::More tests => 153;  use Test::More tests => 322;
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' }] },
# Line 657  sub test_s { Line 784  sub test_s {
784                          } ],                          } ],
785                  },                  },
786                  qq{                  qq{
787                          marc_original_order(200,900);                          marc_original_order(900,200);
788                  },                  },
789                  [                  [
790                          [ '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', ],
791                          [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],                          [ '900', ' ', ' ', 'a', 'a3', 'a', 'a4', 'b', 'b3', 'c', 'c3', 'a', 'a5', ],
792                  ],                  ],
793          );          );
794    
795            test_rule(
796                    'rec1 skips subfields',
797                    {
798                            '200' => [ {
799                                    a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
800                                    subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
801                            }, {
802                                    a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
803                                    subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
804                            } ],
805                    },
806                    qq{
807                            rec1(200);
808                    },
809                    ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
810            );
811    
812            is_deeply(
813                    [ _pack_subfields_hash({
814                            a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
815                            subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
816                    }) ],
817                    ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
818                    '_pack_subfields_hash( $h )'
819            );
820    
821            cmp_ok(
822                    _pack_subfields_hash({
823                            a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
824                            subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
825                    }, 1),
826                    'eq',
827                    '^aa1^bb1^aa2^bb2^cc1^cc2',
828                    '_pack_subfields_hash( $h, 1 )'
829            );
830  }  }
831    

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

  ViewVC Help
Powered by ViewVC 1.1.26