better utf8 test records
[webpac2] / t / 3-normalize.t
index 30421d9..0eb6f2a 100755 (executable)
@@ -1,25 +1,15 @@
 #!/usr/bin/perl -w
 
 use strict;
-
-use Test::More tests => 155;
-use Test::Exception;
-use Cwd qw/abs_path/;
 use blib;
-use File::Slurp;
-use Getopt::Long;
+
+use Test::More tests => 355;
 
 BEGIN {
+       use_ok( 'WebPAC::Test' );
        use_ok( 'WebPAC::Normalize' );
 }
 
-use Data::Dump qw/dump/;
-
-my $debug = 0;
-GetOptions(
-       "debug+", \$debug
-);
-
 cmp_ok(_debug(1), '==', 1, '_debug level');
 cmp_ok(_debug(0), '==', 0, '_debug level');
 
@@ -28,12 +18,6 @@ if ($debug > 2) {
        diag "debug level for WebPAC::Normalize is ", _debug( $debug - 2 );
 }
 
-ok(my $abs_path = abs_path($0), "abs_path");
-$abs_path =~ s#/[^/]*$#/#;
-diag "abs_path: $abs_path" if ($debug);
-
-#throws_ok { new WebPAC::Normalize::XML( lookup_regex => 'foo' ) } qr/pair/, "lookup_regex without lookup";
-
 my $rec1 = {
        '200' => [{
                'a' => '200a',
@@ -109,18 +93,34 @@ my $rec2 = {
 };
 
 
-my $lookup1 = {
-       '00900' => [
-               'lookup 1',
-               'lookup 2',
-       ],
+my $lookup_hash1 = {
+       'db1' => {
+               'input1' => {
+                       'key1' => { 1 => 1 },
+                       'key2' => { 2 => 1 },
+               },
+               'input2' => {
+                       'key3' => { 3 => 1 },
+                       'key4' => { 4 => 1 },
+               },
+       },
+       'db2' => {
+               'input3' => {
+                       'key5' => { 5 => 1 },
+                       'key6' => { 6 => 1 },
+               },
+       }
 };
 
-my $lookup2 = {
-       '00900' => 'lookup',
+my $lookup_hash2 = {
+       'db3' => {
+               'input4' => {
+                       'key7' => { 7 => 1 },
+                       'key8' => { 8 => 1 },
+               },
+       }
 };
 
-
 sub test {
        print dump( @_ ), ("-" x 78), "\n";
        ok( defined(@_) );
@@ -162,7 +162,7 @@ sub test_s {
 
        ok(! _set_lookup( undef ), "set_lookup(undef)");
 
-       _set_rec( $rec1 );
+       _set_ds( $rec1 );
 
        cmp_ok( join(",", rec2('200','a') ), 'eq', '200a,200a*2', 'join rec2' );
        cmp_ok( join(",", rec2('200','a'), rec2('200','b') ), 'eq', '200a,200a*2,200b', 'join rec2 rec2' );
@@ -177,21 +177,136 @@ sub test_s {
 
        # simple list manipulatons
        cmp_ok( join('', prefix('ab', 'cd') ), 'eq', 'abcd', 'prefix');
+       cmp_ok( join('-', prefix('', 'x', 'y') ), 'eq', 'x-y', 'prefix empty');
+       cmp_ok( join('-', prefix(0, 'x', 'y') ), 'eq', '0x-0y', 'prefix 0');
+
        cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix');
+       cmp_ok( join('-', suffix('', 'x', 'y' ) ), 'eq', 'x-y', 'suffix empty');
+       cmp_ok( join('-', suffix(0, 'x', 'y' ) ), 'eq', 'x0-y0', 'suffix 0');
+
        cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround');
+       cmp_ok( join('-', surround('', '', 'x','y','z') ), 'eq', 'x-y-z', 'surround empty');
+       cmp_ok( join('-', surround(0, 0, 'x','y','z') ), 'eq', '0x0-0y0-0z0', 'surround 0 0');
+
+       # count
+       my @el;
+       for my $i ( 0 .. 10 ) {
+               cmp_ok( count( @el ), '==', $i, "count($i)");
+               push @el, "element $i";
+       }
+
+       # lookups
+
+       throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
+
+       ok(_set_load_row(sub {
+               my ($database,$input,$mfn) = @_;
+               diag "load_row( $database, $input, $mfn )" if ($debug);
+               cmp_ok( $#_, '==', 2, 'have 3 arguments');
+               ok($database, '_load_row database');
+               ok($input, '_load_row input');
+               ok($mfn, '_load_row mfn');
+               return {
+                       '900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }],
+               }
+
+       }), '_set_load_row');
+
+       my @v = qw/foo bar baz aaa bbb ccc ddd/;
+
+       my @accumulated;
+
+       for my $i ( 0 .. $#v ) {
+
+               my $mfn = 1000 + $i;
+
+               ok(WebPAC::Normalize::_set_config({ '_mfn' => $mfn }), "_set_config _mfn=$mfn");
+
+               my $size = $#v + 1;
+
+               cmp_ok(
+                       save_into_lookup('db','input','key', sub { @v }),
+                       '==', $size, "save_into_lookup $size values"
+               );
+
+               ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
+               diag "_get_lookup = ", dump($l) if ($debug);
+
+               my @lookup;
+
+               ok(@lookup = lookup(
+                               sub {
+                                       diag "in show" if ($debug);
+                                       rec('900','x');
+                               },
+                               'db','input','key',
+                               sub {
+                                       return @v;
+                               }
+                       ),
+               "lookup db/input/key");
+
+               push @accumulated, '900x-' . $mfn;
+
+               is_deeply(\@lookup, \@accumulated, "lookup db/input/key");
+
+               shift @v;
+
+       }
+
+       ok(my $l = WebPAC::Normalize::_get_lookup(), '_get_lookup');
+       diag "_get_lookup = ", dump($l) if ($debug);
+
+       is_deeply( $l, {
+               db => {
+                       input => {
+                               key => {
+                                       foo => { 1000 => 1 },
+                                       bar => { 1000 => 1, 1001 => 1 },
+                                       baz => { 1000 => 1, 1001 => 1, 1002 => 1 },
+                                       aaa => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1 },
+                                       bbb => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1 },
+                                       ccc => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1 },
+                                       ddd => { 1000 => 1, 1001 => 1, 1002 => 1, 1003 => 1, 1004 => 1, 1005 => 1, 1006 => 1 },
+                               },
+                       },
+               },
+       }, 'lookup data');
+
+#######
+
+       diag "lookup_hash1 = ", dump($lookup_hash1) if ($debug);
+       ok(_set_lookup( $lookup_hash1 ), '_set_lookup $lookup_hash1');
+
+       throws_ok { _set_load_row() } qr/CODE/, 'empty _set_load_row()';
+
+       ok(_set_load_row(sub {
+               my ($database,$input,$mfn) = @_;
+               diag "load_row( $database, $input, $mfn )";
+               cmp_ok( $#_, '==', 2, 'have 3 arguments');
+               ok($database, 'database');
+               ok($input, 'input');
+               ok($mfn, 'mfn');
+
+       }), '_set_load_row');
+
+
+#      cmp_ok(lookup(
+#              sub {
+#                      'found'
+#              },
+#              'db1','input1','key1',
+#              sub {
+#                      rec('200','a')
+#              }
+#      ), 'eq', 'found', 'lookup db1/input1/key1');
 
 
-       _set_lookup( $lookup1 );
        
-       cmp_ok(
-               join_with(" i ",
-                       lookup( 
-                               regex( 's/^/00/',
-                                       rec2('902','z')
-                               )
-                       ) 
-               ),
-       'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
+#      cmp_ok(
+#              lookup( 
+#              ),
+#      'eq', 'lookup 1 i lookup 2', 'join lookup regex rec2');
 
        # check join_with operations
 
@@ -235,21 +350,23 @@ sub test_s {
 
        # test lookups
 
-       _set_lookup( $lookup2 );
+       _set_lookup( $lookup_hash2 );
+
+       throws_ok { lookup() } qr/need/, 'empty lookup';
 
-       is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
+       #is_deeply( \[ lookup( prefix( '00', rec('902') ) ) ], \[ 'lookup' ], 'lookup prefix' );
 
-       ok(! lookup('non-existent'), 'lookup non-existant' );
+       #ok(! lookup('non-existent'), 'lookup non-existant' );
 
-       _set_rec( $rec2 );
+       _set_ds( $rec2 );
 
        test_s(qq{
-               tag('Title',
+               search_display('Title',
                        rec('200','a')
                );
        });
        test_s(qq{
-               tag('Who',
+               search_display('Who',
                        join_with(" ",
                                rec('702','a'),
                                rec('702','b')
@@ -300,11 +417,16 @@ sub test_s {
        test_check_ds('display');
 
        _clean_ds();
-       test_s(qq{ tag('something', '42'); });
-       test_s(qq{ tag('empty', ''); });
+       test_s(qq{ search_display('something', '42'); });
+       test_s(qq{ search_display('empty', ''); });
        test_check_ds('search');
        test_check_ds('display');
 
+       _clean_ds();
+       test_s(qq{ sorted('something', '42'); });
+       test_s(qq{ sorted('empty', ''); });
+       test_check_ds('sorted');
+
        _clean_ds();
        my $n = read_file( "$abs_path/data/normalize.pl" );
        $n .= "\n1;\n";
@@ -323,13 +445,12 @@ sub test_s {
        my $rules = qq{ search('mixed', rec('200') ) };
        
        _clean_ds();
-       _set_rec( $rec );
+       _set_ds( $rec );
        test_s( $rules );
        ok($ds = _get_ds(), "get_ds");
        is_deeply( $ds, {
                'mixed' => {
                        'search' => [ '200a', '200b' ],
-                       'tag' => 'mixed'
                }
        }, 'correct get_ds');
 
@@ -341,7 +462,7 @@ sub test_s {
 
        # wird and non-valid structure which is supported anyway
        _clean_ds();
-       _set_rec({
+       _set_ds({
                '200' => [{
                        'a' => '200a',
                },
@@ -353,19 +474,18 @@ sub test_s {
        is_deeply( $ds, {
                'mixed' => {
                        'search' => [ '200a', '200-solo' ],
-                       'tag' => 'mixed'
                }
        }, 'correct get_ds');
 
        #
        # MARC
        #
-       _debug( 4 );
+       #_debug( 4 );
 
        test_s(qq{ marc_indicators('900',1,2) });
        test_s(qq{ marc('900','a', rec('200') ) });
        my $marc;
-       ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
+       ok($marc = WebPAC::Normalize::MARC::_get_marc_fields(), "_get_marc_fields");
        diag dump( $marc ) if ($debug);
 
        is_deeply( $marc, [
@@ -376,7 +496,7 @@ sub test_s {
        test_s(qq{ marc_indicators('900',' ',9) });
        test_s(qq{ marc_repeatable_subfield('900','a', rec('200') ) });
 
-       ok($marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
+       ok($marc = WebPAC::Normalize::MARC::_get_marc_fields(), "_get_marc_fields");
        diag dump( $marc ) if ($debug);
 
        is_deeply( $marc, [
@@ -392,17 +512,18 @@ sub test_s {
                my ($msg, $rec, $rules, $struct) = @_;
 
                _clean_ds();
-               _set_rec($rec);
+               _set_ds($rec);
 
-               foreach my $r (split(/;/, $rules)) {
+               foreach my $r (split(/;\s*$/, $rules)) {
                        $r =~ s/[\s\n\r]+/ /gs;
                        $r =~ s/^\s+//gs;
                        $r =~ s/\s+$//gs;
+                       diag "rule: $r" if $debug;
                        test_s($r) if ($r);
                }
 
-               ok(my $marc = WebPAC::Normalize::_get_marc_fields(), "_get_marc_fields");
-               diag dump( $marc ) if ($debug);
+               ok(my $marc = WebPAC::Normalize::MARC::_get_marc_fields(), "_get_marc_fields");
+               diag dump( $marc ) if $debug;
                diag "expects:\n", dump($struct) if ($debug > 1);
                is_deeply( $marc, $struct, $msg );
        }
@@ -501,7 +622,7 @@ sub test_s {
        sub test_rule {
                my ($msg, $rec, $rule, $struct) = @_;
                _clean_ds();
-               _set_rec( $rec );
+               _set_ds( $rec );
                $rule =~ s/\\/\\/gs;
                my $r = test_s( $rule );
                diag "for ", dump($rec), " got:\n", dump($r), "\nexpect:\n" if ($debug > 1);
@@ -609,19 +730,28 @@ sub test_s {
                        [ '901', ' ', ' ', 'b', 'bar', 'c', 'baz' ],
                ],
        );
+
+       test_s(qq{ marc_remove('*'); });
+       ok(! WebPAC::Normalize::MARC::_get_marc_fields(), 'marc_remove(*)');
+
        test_rec_rules(
                'marc_duplicate',
                { '200' => [{ a => 42, b => 'bar', c => 'baz', d => 'bing', e => 'bong' }] },
                qq{
+                       marc_leader('06',42);
+                       marc_leader('11',0);
                        marc('900', 'a', rec('200','a') );
                        marc('900', 'b', rec('200','b') );
                        marc_duplicate;
+                       marc_leader('11',1);
                        marc_remove('900','b');
                        marc('900', 'b', rec('200','c') );
                        marc_duplicate;
+                       marc_leader('11',2);
                        marc_remove('900','b');
                        marc('900', 'b', rec('200','d') );
                        marc_duplicate;
+                       marc_leader('11',3);
                        marc_remove('900','b');
                        marc('900', 'b', rec('200','e') );
                },
@@ -631,10 +761,12 @@ sub test_s {
                ],
        );
 
+       cmp_ok( marc_count(), '==', 3, 'marc_count' );
+
        my $i = 0;
        foreach my $v ( qw/bar baz bing bong/ ) {
 
-               ok($marc = WebPAC::Normalize::_get_marc_fields( offset => $i ),
+               ok($marc = WebPAC::Normalize::MARC::_get_marc_fields( offset => $i ),
                        "_get_marc_fields( offset => $i )"
                );
                diag "marc $i = ", dump( $marc ) if ($debug);
@@ -642,6 +774,7 @@ sub test_s {
                        [ [ '900', ' ', ' ', 'a', 42, 'b', $v ] ],
                        "MARC copy $i has $v",
                );
+               is_deeply(WebPAC::Normalize::MARC::_get_marc_leader(), { '06' => 42, 11 => $i }, "_get_marc_leader copy $i");
                $i++;
        }
 
@@ -679,7 +812,109 @@ sub test_s {
                qq{
                        rec1(200);
                },
-               ["a1", "b1", "a2", "b2", "c1", "c2"],
+               ['a1', 'b1', 'a2', 'b2', 'c1', 'c2', 'a3', 'a4', 'b3', 'c3', 'a5' ],
+       );
+
+       is_deeply(
+               [ _pack_subfields_hash({
+                       a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
+                       subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
+               }) ],
+               ['a1', 'b1', 'a2', 'b2', 'c1', 'c2'],
+               '_pack_subfields_hash( $h )'
+       );
+
+       cmp_ok(
+               _pack_subfields_hash({
+                       a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
+                       subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
+               }, 1),
+               'eq',
+               '^aa1^bb1^aa2^bb2^cc1^cc2',
+               '_pack_subfields_hash( $h, 1 )'
+       );
+
+       _clean_ds();
+       test_s(qq{
+               marc_fixed('008', 0, 'abcdef');
+               marc_fixed('000', 5, '5');
+               marc_fixed('000', 10, 'A');
+               marc_fixed('000', 0, '0');
+       });
+       ok( my $m = WebPAC::Normalize::MARC::_get_marc_fields(), '_get_marc_fields');
+       diag dump( $m );
+       is_deeply( WebPAC::Normalize::MARC::_get_marc_fields(),
+               [
+                       ["008", "abcdef"], 
+                       #        0....5....10
+                       ["000", "0    5    A"]
+               ]
        );
+
+       test_s(qq{ isbn_13( '1558607013', '978-1558607019' ) });
+       test_s(qq{ isbn_10( '1558607013', '978-1558607019' ) });
+
+       is_deeply(
+               [ isbn_13( '1558607013', '978-1558607019' ) ],
+               [ '978-1-55860-701-9', '978-1-55860-701-9', ],
+       'isbn_13' );
+
+       is_deeply(
+               [ isbn_10( '1558607013', '978-1558607019' ) ],
+               [ '1-55860-701-3', '1-55860-701-3' ],
+       'isbn_10' );
+
+       # frec
+
+       $rec = {
+                       '200' => [ {
+                               a => [ 'a1', 'a2' ], b => [ 'b1', 'b2' ], c => [ 'c1', 'c2' ],
+                               subfields => [ qw/a 0 b 0 a 1 b 1 c 0 c 1/ ],
+                               i1 => '0', i2 => '1',
+                       }, {
+                               a => [ 'a3', 'a4', 'a5' ], b => 'b3', c => 'c3',
+                               subfields => [ qw/a 0 a 1 b 0 c 0 a 2/ ],
+                       } ],
+       };
+
+       test_rule( 'frec', $rec, qq{ frec(200) }, [ 'a1' ] );
+       test_rule( 'frec', $rec, qq{ frec(200,'a') }, [ 'a1' ] );
+       test_rule( 'frec', $rec, qq{ frec(200,'b') }, [ 'b1' ] );
+       test_rule( 'frec', $rec, qq{ frec(200,'c') }, [ 'c1' ] );
+
+       $rec->{'900'} = $rec->{'200'};
+       foreach my $sf ( qw/a b c/ ) {
+               ok( frec_eq( '200' => $sf, '900' => $sf ), "frec_eq 200 == 900 $sf");
+               ok( ! frec_ne( '200' => $sf, '900' => $sf ), "! frec_ne 200 == 900 $sf");
+       }
+
+       foreach my $sf ( qw/a b/ ) {
+               ok( ! frec_eq( '200' => $sf, '200' => 'c' ), "! frec_eq 200 $sf == 200 c");
+               ok( frec_ne( '200' => $sf, '200' => 'c' ), "frec_ne 200 $sf == 200 c");
+       }
+
+       test_rule( 'rec(200,i1)', $rec, qq{ rec(200,'i1') }, [ '0' ] );
+       test_rule( 'rec(200,i2)', $rec, qq{ rec(200,'i2') }, [ '1' ] );
+
+       my $hash = { a => '[a]', 'b' => '[b]', subfields => [ 'a', 0, 'b', 0 ] };
+       is_deeply([ _pack_subfields_hash( $hash ) ], [ '[a]', '[b]' ], '_pack_subfields_hash' );
+       ok( $hash->{subfields}, 'subfields exist' );
+       cmp_ok( _pack_subfields_hash( $hash, 1 ), 'eq', '^a[a]^b[b]', '_pack_subfields_hash' );
+       ok( $hash->{subfields}, 'subfields exist' );
+
+       $rec = { 'arr' => [ 1, 2, 3 ] };
+       test_rule( 'rec_array', $rec, qq{ rec_array('arr') }, $rec->{arr} );
+
+       _clean_ds();
+       _set_ds( $rec );
+       test_s(q{
+                       row( 'table', e => $_ ) foreach ( rec_array('arr') );
+       });
+       ok( my $rows = _get_ds->{_rows}->{table}, 'ds have _rows' );
+
+       foreach my $i ( 1 .. 3 ) {
+               cmp_ok( $rows->[ $i - 1 ]->{e}, '==', $i, "e $i" );
+       }
+
 }