/[webpac2]/trunk/t/2-input.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/2-input.t

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 286 by dpavlin, Sun Dec 18 21:06:46 2005 UTC revision 761 by dpavlin, Wed Oct 25 17:10:08 2006 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use Test::More tests => 49;  use Test::More tests => 89;
4  use Test::Exception;  use Test::Exception;
5  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
6  use blib;  use blib;
7  use strict;  use strict;
8    
9    use Data::Dump qw/dump/;
10    
11  BEGIN {  BEGIN {
12  use_ok( 'WebPAC::Input::ISIS' );  use_ok( 'WebPAC::Input::ISIS' );
13    use_ok( 'WebPAC::Input::MARC' );
14  }  }
15    
16    my $debug = shift @ARGV;
17    my $no_log = $debug ? 0 : 1;
18    
19  ok(my $abs_path = abs_path($0), "abs_path");  ok(my $abs_path = abs_path($0), "abs_path");
20  $abs_path =~ s#/[^/]*$#/#;  $abs_path =~ s#/[^/]*$#/#;
21    
22  my $module = 'WebPAC::Input::ISIS';  my $module = 'WebPAC::Input::ISIS';
23    diag "testing with $module";
24    
25  throws_ok { my $input = new WebPAC::Input( ) } qr/module/, "need module";  throws_ok { my $input = new WebPAC::Input( ) } qr/module/, "need module";
26  ok(my $input = new WebPAC::Input( module => $module, no_log => 0 ), "new");  ok(my $input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, stats => 1 ), "new $module");
27  ok(my $input_lm = new WebPAC::Input( module => $module, low_mem => 1, no_log => 1 ), "new");  ok(my $input_lm = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
28    
29  throws_ok { $input->open( ) } qr/path/, "need path";  throws_ok { $input->open( ) } qr/path/, "need path";
30    
31  throws_ok { $input->open( path => '/dev/null', ) } qr/can't find database/ , "open";  throws_ok { $input->open( path => '/dev/null', ) } qr/can't find database/ , "open";
32    
33  ok($input->open( path => "$abs_path/winisis/BIBL" ), "open");  my $store;
 ok($input_lm->open( path => "$abs_path/winisis/BIBL", low_mem => 1 ), "open");  
34    
35  cmp_ok($input->pos, '==', -1, "mfn");  ok($input->open( path => "$abs_path/winisis/BIBL" ), "open winisis");
36    ok($input_lm->open(
37            path => "$abs_path/winisis/BIBL",
38            save_row => sub {
39                    my $a = shift;
40                    $store->{ $a->{id} } = $a->{row};
41            },
42            load_row => sub {
43                    my $a = shift;
44                    return defined($store->{ $a->{id} }) &&
45                            $store->{ $a->{id} };
46            },
47    ), "open winisis");
48    
49    cmp_ok( keys %$store, '==', 5, 'have 5 rows');
50    
51    foreach my $i ( 1 .. 5 ) {
52            ok(my $r = $store->{$i}, "row $i");
53            ok($r->{'000'}, "have 000");
54            isa_ok($r->{'000'}, 'ARRAY', "is ARRAY");
55            cmp_ok($r->{'000'}->[0], '==', $i, 'sane value');
56    }
57    
58  ok(my $size = $input->size, "size");  diag "store = ",dump( $store ) if ($debug);
59    
60  my @db1;  sub test_after_open($) {
61            my $input = shift;
62    
63  foreach my $mfn ( 1 ... $size ) {          cmp_ok($input->pos, '==', -1, "mfn");
64          ok(my $rec = $input->fetch, "fetch");          ok(my $size = $input->size, "size");
65          cmp_ok($input->pos, '==', $mfn, "rec $mfn");          return $size;
         push @db1, $rec;  
66  }  }
67    
68  my @db2;  test_after_open($input);
69    my $size = test_after_open($input_lm);
70    
71    sub test_fetch($$) {
72            my ($input, $size) = @_;
73    
74            my @db;
75    
76  foreach my $mfn ( 1 ... $size ) {          foreach my $mfn ( 1 ... $size ) {
77          ok($input_lm->seek($mfn), "seek");                  ok(my $rec = $input->fetch, "fetch $mfn");
78          ok(my $rec = $input_lm->fetch, "fetch");                  cmp_ok($input->pos, '==', $mfn, "pos $mfn");
79          cmp_ok($input_lm->pos, '==', $mfn, "rec $mfn");                  push @db, $rec;
80          push @db2, $rec;          }
81    
82            return @db;
83  }  }
84    
85    my @db1 = test_fetch($input, $size);
86    my @db2 = test_fetch($input_lm, $size);
87    
88  is_deeply(\@db1, \@db2, "seek working");  is_deeply(\@db1, \@db2, "seek working");
89    
90  sub test_start_limit($$$) {  sub test_start_limit($$$$) {
91          my ($s,$l,$e) = @_;          my ($input, $s,$l,$e) = @_;
92    
93          diag "offset $s, limit: $l, expected: $e";          diag "offset $s, limit: $l, expected: $e";
94    
95          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");
96          cmp_ok($s, '==', $size, "db size from open = $size");          cmp_ok($s, '==', $size, "db size from open = $size");
97          cmp_ok($input->size, '==', $e, "input->size = $e");          cmp_ok($input->size, '==', $e, "input->size = $e");
98  }  }
99    
100  test_start_limit(1, 3, 3);  test_start_limit($input, 1, 3, 3);
101  test_start_limit($size, 3, 0);  test_start_limit($input, $size, 3, 0);
102  test_start_limit(3, $size, $size - 2);  test_start_limit($input, 3, $size, $size - 2);
103  test_start_limit(1, $size + 2, $size);  test_start_limit($input, 1, $size + 2, $size);
104    
105    ok(my $s = $input->stats, 'stats');
106    diag "stats:\n$s";
107    
108    $module = 'WebPAC::Input::MARC';
109    diag "testing with $module";
110    
111    ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
112    
113    ok($input->open( path => "$abs_path/data/marc.iso" ), "open marc.iso");
114    
115    test_after_open($input);
116    
117    test_fetch($input, $input->size);
118    
119    # test modify_record
120    $module = 'WebPAC::Input::ISIS';
121    ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
122    
123    ok($input->open( path => "$abs_path/modify_isis/LIBRI", ), "open modify_isis (plain)");
124    ok(my $rec_p = $input->fetch, 'fetch');
125    
126    ok($input->open(
127            path => "$abs_path/modify_isis/LIBRI",
128            modify_records => {
129                    200 => {
130                            '*' => { '^c' => '. ' },
131                    }
132            },
133    ), "open modify_isis (with modify_records)");
134    
135    ok(my $rec = $input->fetch, 'fetch');
136    
137    cmp_ok($rec_p->{200}->[0]->{f} . '. ' . $rec_p->{200}->[0]->{c}, 'eq' ,$rec->{200}->[0]->{f}, 'modify_records working');
138    

Legend:
Removed from v.286  
changed lines
  Added in v.761

  ViewVC Help
Powered by ViewVC 1.1.26