a try at mocking of inputs in WebPAC::Input::Test
[webpac2] / t / 2-input.t
index afc0b1a..cb26b19 100755 (executable)
 #!/usr/bin/perl -w
 
-use Test::More tests => 49;
+use Test::More tests => 104;
 use Test::Exception;
 use Cwd qw/abs_path/;
 use blib;
 use strict;
 
+use Data::Dump qw/dump/;
+
 BEGIN {
 use_ok( 'WebPAC::Input::ISIS' );
+use_ok( 'WebPAC::Input::MARC' );
+use_ok( 'WebPAC::Input::Test' );
 }
 
+my $debug = shift @ARGV;
+my $no_log = $debug ? 0 : 1;
+
 ok(my $abs_path = abs_path($0), "abs_path");
 $abs_path =~ s#/[^/]*$#/#;
 
 my $module = 'WebPAC::Input::ISIS';
+diag "testing with $module";
 
 throws_ok { my $input = new WebPAC::Input( ) } qr/module/, "need module";
-ok(my $input = new WebPAC::Input( module => $module, no_log => 0 ), "new");
-ok(my $input_lm = new WebPAC::Input( module => $module, low_mem => 1, no_log => 1 ), "new");
+ok(my $input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, stats => 1 ), "new $module");
+ok(my $input_lm = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
 
 throws_ok { $input->open( ) } qr/path/, "need path";
 
 throws_ok { $input->open( path => '/dev/null', ) } qr/can't find database/ , "open";
 
-ok($input->open( path => "$abs_path/winisis/BIBL" ), "open");
-ok($input_lm->open( path => "$abs_path/winisis/BIBL", low_mem => 1 ), "open");
-
-cmp_ok($input->pos, '==', -1, "mfn");
+my $store;
+
+ok($input->open( path => "$abs_path/winisis/BIBL" ), "open winisis");
+ok($input_lm->open(
+       path => "$abs_path/winisis/BIBL",
+       save_row => sub {
+               my $a = shift;
+               $store->{ $a->{id} } = $a->{row};
+       },
+       load_row => sub {
+               my $a = shift;
+               return defined($store->{ $a->{id} }) &&
+                       $store->{ $a->{id} };
+       },
+), "open winisis");
+
+cmp_ok( keys %$store, '==', 5, 'have 5 rows');
+
+foreach my $i ( 1 .. 5 ) {
+       ok(my $r = $store->{$i}, "row $i");
+       ok($r->{'000'}, "have 000");
+       isa_ok($r->{'000'}, 'ARRAY', "is ARRAY");
+       cmp_ok($r->{'000'}->[0], '==', $i, 'sane value');
+}
 
-ok(my $size = $input->size, "size");
+diag "store = ",dump( $store ) if ($debug);
 
-my @db1;
+sub test_after_open($) {
+       my $input = shift;
 
-foreach my $mfn ( 1 ... $size ) {
-       ok(my $rec = $input->fetch, "fetch");
-       cmp_ok($input->pos, '==', $mfn, "rec $mfn");
-       push @db1, $rec;
+       cmp_ok($input->pos, '==', -1, "mfn");
+       ok(my $size = $input->size, "size");
+       return $size;
 }
 
-my @db2;
+test_after_open($input);
+my $size = test_after_open($input_lm);
+
+sub test_fetch($$) {
+       my ($input, $size) = @_;
+
+       my @db;
+
+       foreach my $mfn ( 1 ... $size ) {
+               ok(my $rec = $input->fetch, "fetch $mfn");
+               cmp_ok($input->pos, '==', $mfn, "pos $mfn");
+               push @db, $rec;
+               ok(my $dump = $input->dump_ascii, "dump_ascii $mfn");
+               diag $dump if ($debug);
+       }
 
-foreach my $mfn ( 1 ... $size ) {
-       ok($input_lm->seek($mfn), "seek");
-       ok(my $rec = $input_lm->fetch, "fetch");
-       cmp_ok($input_lm->pos, '==', $mfn, "rec $mfn");
-       push @db2, $rec;
+       return @db;
 }
 
+my @db1 = test_fetch($input, $size);
+my @db2 = test_fetch($input_lm, $size);
+
 is_deeply(\@db1, \@db2, "seek working");
 
-sub test_start_limit($$$) {
-       my ($s,$l,$e) = @_;
+sub test_start_limit($$$$) {
+       my ($input, $s,$l,$e) = @_;
 
        diag "offset $s, limit: $l, expected: $e";
 
-       ok($s = $input->open( path => "$abs_path/winisis/BIBL", offset => $s, limit => $l, debug => 1 ), "open");
+       ok($s = $input->open( path => "$abs_path/winisis/BIBL", offset => $s, limit => $l, debug => $debug ), "open winisis");
        cmp_ok($s, '==', $size, "db size from open = $size");
        cmp_ok($input->size, '==', $e, "input->size = $e");
 }
 
-test_start_limit(1, 3, 3);
-test_start_limit($size, 3, 0);
-test_start_limit(3, $size, $size - 2);
-test_start_limit(1, $size + 2, $size);
+test_start_limit($input, 1, 3, 3);
+test_start_limit($input, $size, 3, 0);
+test_start_limit($input, 3, $size, $size - 2);
+test_start_limit($input, 1, $size + 2, $size);
+
+ok(my $s = $input->stats, 'stats');
+diag "stats:\n$s" if ($debug);
+
+$module = 'WebPAC::Input::MARC';
+diag "testing with $module";
+
+ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
+
+ok($input->open( path => "$abs_path/data/marc.iso" ), "open marc.iso");
+
+test_after_open($input);
+
+test_fetch($input, $input->size);
+
+# test modify_record
+$module = 'WebPAC::Input::Test';
+ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, debug => $debug ), "new $module");
+
+ok($input->open( path => "$abs_path/modify_isis/LIBRI", ), "open modify_isis (plain)");
+
+$WebPAC::Input::Test::rec = {
+       '200' => [ {
+               'a' => 'foo',
+               'b' => 'bar',
+               }, {
+               'a' => 'baz',
+               } ],
+       '900' => [
+               'foobar',
+               ],
+};
+
+ok($input->size, 'size');
+
+ok(my $rec_p = $input->fetch, 'fetch');
+
+# modify_records
+
+ok($input->open(
+       path => "$abs_path/modify_isis/LIBRI",
+       modify_records => {
+               200 => {
+                       '*' => { '^c' => '. ' },
+                       '^f' => { ' : ' => ' / ' },
+               }
+       },
+), "open modify_isis (with modify_records)");
+
+ok(my $rec = $input->fetch, 'fetch');
+diag "fetched rec field 200 = ", dump($rec->{200}) if ($debug);
+
+cmp_ok($rec_p->{200}->[0]->{f} . '. ' . $rec_p->{200}->[0]->{c}, 'eq' ,$rec->{200}->[0]->{f}, 'modify_records working');
+
+diag "input = ",dump($input->{data}) if ($debug);
+
+# break encapsulation, bad! bad!
+$input->{ll_db}->{_isis_db}->{record} = {
+       900 => 'foo ; bar ; baz',
+};
+
+$input->{modify_record} = {
+       900 => {
+               '*' => [
+                       { ' ; ' => 'a' },
+                       { ' ; ' => 'b' },
+                       { ' ; ' => 'c' },
+               ],
+       }
+};
+
+diag "hacked: ",dump($input, $input->fetch) if ($debug);
+
+# seek
+throws_ok { $input->seek } qw/without/, 'seek without position';
+cmp_ok($input->seek(0), '==', -1, 'seek');
 
+ok(my $rec = $input->fetch, 'fetch');
+diag "fetched rec = ", dump($rec) if ($debug);