a try at mocking of inputs in WebPAC::Input::Test
[webpac2] / t / 2-input.t
index a228448..cb26b19 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-use Test::More tests => 89;
+use Test::More tests => 104;
 use Test::Exception;
 use Cwd qw/abs_path/;
 use blib;
@@ -11,6 +11,7 @@ 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;
@@ -77,6 +78,8 @@ sub test_fetch($$) {
                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);
        }
 
        return @db;
@@ -103,7 +106,7 @@ 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";
+diag "stats:\n$s" if ($debug);
 
 $module = 'WebPAC::Input::MARC';
 diag "testing with $module";
@@ -117,22 +120,66 @@ test_after_open($input);
 test_fetch($input, $input->size);
 
 # test modify_record
-$module = 'WebPAC::Input::ISIS';
-ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
+$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);