--- trunk/t/3-normalize.t 2006/09/11 14:29:01 669 +++ trunk/t/3-normalize.t 2006/09/29 18:55:41 725 @@ -2,7 +2,7 @@ use strict; -use Test::More tests => 157; +use Test::More tests => 309; use Test::Exception; use Cwd qw/abs_path/; use blib; @@ -32,8 +32,6 @@ $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 +107,34 @@ }; -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(@_) ); @@ -180,18 +194,118 @@ cmp_ok( join('', suffix('xy', 'cd') ), 'eq', 'cdxy', 'suffix'); cmp_ok( join('', surround('->', '<-', 'a','b','c') ), 'eq', '->a<-->b<-->c<-', 'surround'); + # lookups + + throws_ok { _set_load_ds() } qr/CODE/, 'empty _set_load_ds()'; + + ok(_set_load_ds(sub { + my ($database,$input,$mfn) = @_; + diag "load_ds( $database, $input, $mfn )"; + cmp_ok( $#_, '==', 2, 'have 3 arguments'); + ok($database, '_load_ds database'); + ok($input, '_load_ds input'); + ok($mfn, '_load_ds mfn'); + return { + '900' => [{ x => '900x-' . $mfn , y => '900y-' . $mfn }], + } + + }), '_set_load_ds'); + + 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); + + my @lookup; + + ok(my @lookup = lookup( + sub { + diag "in show"; + 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); + + 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); + ok(_set_lookup( $lookup_hash1 ), '_set_lookup $lookup_hash1'); + + throws_ok { _set_load_ds() } qr/CODE/, 'empty _set_load_ds()'; + + ok(_set_load_ds(sub { + my ($database,$input,$mfn) = @_; + diag "load_ds( $database, $input, $mfn )"; + cmp_ok( $#_, '==', 2, 'have 3 arguments'); + ok($database, 'database'); + ok($input, 'input'); + ok($mfn, 'mfn'); + + }), '_set_load_ds'); + + +# 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,11 +349,13 @@ # 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 ); @@ -360,7 +476,7 @@ # # MARC # - _debug( 4 ); + #_debug( 4 ); test_s(qq{ marc_indicators('900',1,2) }); test_s(qq{ marc('900','a', rec('200') ) });