cb26b192d17014c462218d4500919df49afd1f3a
[webpac2] / t / 2-input.t
1 #!/usr/bin/perl -w
2
3 use Test::More tests => 104;
4 use Test::Exception;
5 use Cwd qw/abs_path/;
6 use blib;
7 use strict;
8
9 use Data::Dump qw/dump/;
10
11 BEGIN {
12 use_ok( 'WebPAC::Input::ISIS' );
13 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");
21 $abs_path =~ s#/[^/]*$#/#;
22
23 my $module = 'WebPAC::Input::ISIS';
24 diag "testing with $module";
25
26 throws_ok { my $input = new WebPAC::Input( ) } qr/module/, "need module";
27 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, no_log => $no_log, no_progress_bar => 1 ), "new $module");
29
30 throws_ok { $input->open( ) } qr/path/, "need path";
31
32 throws_ok { $input->open( path => '/dev/null', ) } qr/can't find database/ , "open";
33
34 my $store;
35
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($) {
62         my $input = shift;
63
64         cmp_ok($input->pos, '==', -1, "mfn");
65         ok(my $size = $input->size, "size");
66         return $size;
67 }
68
69 test_after_open($input);
70 my $size = test_after_open($input_lm);
71
72 sub test_fetch($$) {
73         my ($input, $size) = @_;
74
75         my @db;
76
77         foreach my $mfn ( 1 ... $size ) {
78                 ok(my $rec = $input->fetch, "fetch $mfn");
79                 cmp_ok($input->pos, '==', $mfn, "pos $mfn");
80                 push @db, $rec;
81                 ok(my $dump = $input->dump_ascii, "dump_ascii $mfn");
82                 diag $dump if ($debug);
83         }
84
85         return @db;
86 }
87
88 my @db1 = test_fetch($input, $size);
89 my @db2 = test_fetch($input_lm, $size);
90
91 is_deeply(\@db1, \@db2, "seek working");
92
93 sub test_start_limit($$$$) {
94         my ($input, $s,$l,$e) = @_;
95
96         diag "offset $s, limit: $l, expected: $e";
97
98         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");
100         cmp_ok($input->size, '==', $e, "input->size = $e");
101 }
102
103 test_start_limit($input, 1, 3, 3);
104 test_start_limit($input, $size, 3, 0);
105 test_start_limit($input, 3, $size, $size - 2);
106 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';
112 diag "testing with $module";
113
114 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 marc.iso");
117
118 test_after_open($input);
119
120 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 ok($input->open( path => "$abs_path/modify_isis/LIBRI", ), "open modify_isis (plain)");
127
128 $WebPAC::Input::Test::rec = {
129         '200' => [ {
130                 'a' => 'foo',
131                 'b' => 'bar',
132                 }, {
133                 'a' => 'baz',
134                 } ],
135         '900' => [
136                 'foobar',
137                 ],
138 };
139
140 ok($input->size, 'size');
141
142 ok(my $rec_p = $input->fetch, 'fetch');
143
144 # modify_records
145
146 ok($input->open(
147         path => "$abs_path/modify_isis/LIBRI",
148         modify_records => {
149                 200 => {
150                         '*' => { '^c' => '. ' },
151                         '^f' => { ' : ' => ' / ' },
152                 }
153         },
154 ), "open modify_isis (with modify_records)");
155
156 ok(my $rec = $input->fetch, 'fetch');
157 diag "fetched rec field 200 = ", dump($rec->{200}) if ($debug);
158
159 cmp_ok($rec_p->{200}->[0]->{f} . '. ' . $rec_p->{200}->[0]->{c}, 'eq' ,$rec->{200}->[0]->{f}, 'modify_records working');
160
161 diag "input = ",dump($input->{data}) if ($debug);
162
163 # break encapsulation, bad! bad!
164 $input->{ll_db}->{_isis_db}->{record} = {
165         900 => 'foo ; bar ; baz',
166 };
167
168 $input->{modify_record} = {
169         900 => {
170                 '*' => [
171                         { ' ; ' => 'a' },
172                         { ' ; ' => 'b' },
173                         { ' ; ' => 'c' },
174                 ],
175         }
176 };
177
178 diag "hacked: ",dump($input, $input->fetch) if ($debug);
179
180 # seek
181 throws_ok { $input->seek } qw/without/, 'seek without position';
182 cmp_ok($input->seek(0), '==', -1, 'seek');
183
184 ok(my $rec = $input->fetch, 'fetch');
185 diag "fetched rec = ", dump($rec) if ($debug);