/[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 290 by dpavlin, Sun Dec 18 23:10:02 2005 UTC revision 820 by dpavlin, Wed Apr 11 12:22:31 2007 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use Test::More tests => 61;  use Test::More tests => 111;
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' );  use_ok( 'WebPAC::Input::MARC' );
14    use_ok( 'WebPAC::Input::Test' );
15  }  }
16    
17    my $debug = shift @ARGV;
18    my $no_log = $debug ? 0 : 1;
19    
20  ok(my $abs_path = abs_path($0), "abs_path");  ok(my $abs_path = abs_path($0), "abs_path");
21  $abs_path =~ s#/[^/]*$#/#;  $abs_path =~ s#/[^/]*$#/#;
22    
# Line 18  my $module = 'WebPAC::Input::ISIS'; Line 24  my $module = 'WebPAC::Input::ISIS';
24  diag "testing with $module";  diag "testing with $module";
25    
26  throws_ok { my $input = new WebPAC::Input( ) } qr/module/, "need module";  throws_ok { my $input = new WebPAC::Input( ) } qr/module/, "need module";
27  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");
28  ok(my $input_lm = new WebPAC::Input( module => $module, low_mem => 1, no_log => 1 ), "new $module");  ok(my $input_lm = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
29    
30  throws_ok { $input->open( ) } qr/path/, "need path";  throws_ok { $input->open( ) } qr/path/, "need path";
31    
32  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";
33    
34  ok($input->open( path => "$abs_path/winisis/BIBL" ), "open");  my $store;
35  ok($input_lm->open( path => "$abs_path/winisis/BIBL", low_mem => 1 ), "open");  
36    ok($input->open( path => "$abs_path/winisis/BIBL" ), "open winisis");
37    ok($input_lm->open(
38            path => "$abs_path/winisis/BIBL",
39            save_row => sub {
40                    my $a = shift;
41                    $store->{ $a->{id} } = $a->{row};
42            },
43            load_row => sub {
44                    my $a = shift;
45                    return defined($store->{ $a->{id} }) &&
46                            $store->{ $a->{id} };
47            },
48    ), "open winisis");
49    
50    cmp_ok( keys %$store, '==', 5, 'have 5 rows');
51    
52    foreach my $i ( 1 .. 5 ) {
53            ok(my $r = $store->{$i}, "row $i");
54            ok($r->{'000'}, "have 000");
55            isa_ok($r->{'000'}, 'ARRAY', "is ARRAY");
56            cmp_ok($r->{'000'}->[0], '==', $i, 'sane value');
57    }
58    
59    diag "store = ",dump( $store ) if ($debug);
60    
61  sub test_after_open($) {  sub test_after_open($) {
62          my $input = shift;          my $input = shift;
# Line 48  sub test_fetch($$) { Line 78  sub test_fetch($$) {
78                  ok(my $rec = $input->fetch, "fetch $mfn");                  ok(my $rec = $input->fetch, "fetch $mfn");
79                  cmp_ok($input->pos, '==', $mfn, "pos $mfn");                  cmp_ok($input->pos, '==', $mfn, "pos $mfn");
80                  push @db, $rec;                  push @db, $rec;
81                    ok(my $dump = $input->dump_ascii, "dump_ascii $mfn");
82                    diag $dump if ($debug);
83          }          }
84    
85          return @db;          return @db;
# Line 63  sub test_start_limit($$$$) { Line 95  sub test_start_limit($$$$) {
95    
96          diag "offset $s, limit: $l, expected: $e";          diag "offset $s, limit: $l, expected: $e";
97    
98          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");
99          cmp_ok($s, '==', $size, "db size from open = $size");          cmp_ok($s, '==', $size, "db size from open = $size");
100          cmp_ok($input->size, '==', $e, "input->size = $e");          cmp_ok($input->size, '==', $e, "input->size = $e");
101  }  }
# Line 73  test_start_limit($input, $size, 3, 0); Line 105  test_start_limit($input, $size, 3, 0);
105  test_start_limit($input, 3, $size, $size - 2);  test_start_limit($input, 3, $size, $size - 2);
106  test_start_limit($input, 1, $size + 2, $size);  test_start_limit($input, 1, $size + 2, $size);
107    
108    ok(my $s = $input->stats, 'stats');
109    diag "stats:\n$s" if ($debug);
110    
111  $module = 'WebPAC::Input::MARC';  $module = 'WebPAC::Input::MARC';
112  diag "testing with $module";  diag "testing with $module";
113    
114  ok($input = new WebPAC::Input( module => $module, low_mem => 1, no_log => 1 ), "new $module");  ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
115    
116  ok($input->open( path => "$abs_path/data/marc.iso" ), "open");  ok($input->open( path => "$abs_path/data/marc.iso" ), "open marc.iso");
117    
118  test_after_open($input);  test_after_open($input);
119    
120  test_fetch($input, $input->size);  test_fetch($input, $input->size);
121    
122    # test modify_record
123    $module = 'WebPAC::Input::Test';
124    ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, debug => $debug ), "new $module");
125    
126    $WebPAC::Input::Test::rec = {
127            '200' => [
128                    { 'a' => '[200 a]', 'b' => '[200 b]', 'c' => '[200 c]', 'f' => '[200 f] test : test' },
129            ],
130            '900' => [
131                    { 'x' => 'foobar', },
132            ],
133    };
134    
135    $WebPAC::Input::Test::size = 42;
136    
137    ok($input->open( path => "/fake/path", ), "open modify_isis (plain)");
138    
139    cmp_ok($input->size, '==', 42, 'size');
140    
141    ok(my $rec_p = $input->fetch, 'fetch');
142    
143    # modify_records
144    
145    ok($input->open(
146            path => "/another/fake/path",
147            modify_records => {
148                    200 => {
149                            '*' => { '^c' => '. ' },
150                            '^f' => { ' : ' => ' / ' },
151                    }
152            },
153    ), "open (with modify_records)");
154    
155    # seek
156    throws_ok { $input->seek } qr/without/, 'seek without position';
157    cmp_ok($input->seek(0), '==', -1, 'seek');
158    
159    my $f = $WebPAC::Input::Test::filter_coderef;
160    ok(ref($f) eq 'CODE', 'filter_coderef');
161    
162    cmp_ok(
163            $f->(   '^afoo^cbar^fbing : bong',      200),
164            'eq',   '^afoo. bar^fbing / bong',
165            'modify 200'
166    );
167    
168    # modify_file
169    
170    my $modify_file = "$abs_path/conf/modify/test.pl";
171    
172    ok($input->open(
173            path => "/and/another/fake/path",
174            modify_file => $modify_file,
175    ), "open (with modify_file $modify_file)");
176    
177    my $f = $WebPAC::Input::Test::filter_coderef;
178    ok(ref($f) eq 'CODE', 'filter_coderef');
179    
180    diag "regexps = ", dump($input->modify_file_regexps( $modify_file )) if ($debug);
181    
182    sub test_filter {
183            my ($field, $from, $to) = @_;
184            cmp_ok( $f->( $from, $field, 1 ), 'eq', $to, "filter $field |$from| -> |$to|" );
185    }
186    
187    test_filter(200,
188            '^a foo ; bar = baz : zzz',
189            '^a foo^kbar^dbaz : zzz',
190    );
191    
192    # empty subfield removal
193    test_filter(901,
194            '^a^efoo',
195            '^efoo',
196    );

Legend:
Removed from v.290  
changed lines
  Added in v.820

  ViewVC Help
Powered by ViewVC 1.1.26