3 use Test::More tests => 111;
9 use Data::Dump qw/dump/;
12 use_ok( 'WebPAC::Input::ISIS' );
13 use_ok( 'WebPAC::Input::MARC' );
14 use_ok( 'WebPAC::Input::Test' );
17 my $debug = shift @ARGV;
18 my $no_log = $debug ? 0 : 1;
20 ok(my $abs_path = abs_path($0), "abs_path");
21 $abs_path =~ s#/[^/]*$#/#;
23 my $module = 'WebPAC::Input::ISIS';
24 diag "testing with $module";
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");
30 throws_ok { $input->open( ) } qr/path/, "need path";
32 throws_ok { $input->open( path => '/dev/null', ) } qr/can't find database/ , "open";
36 ok($input->open( path => "$abs_path/winisis/BIBL" ), "open winisis");
38 path => "$abs_path/winisis/BIBL",
41 $store->{ $a->{id} } = $a->{row};
45 return defined($store->{ $a->{id} }) &&
50 cmp_ok( keys %$store, '==', 5, 'have 5 rows');
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');
59 diag "store = ",dump( $store ) if ($debug);
61 sub test_after_open($) {
64 cmp_ok($input->pos, '==', -1, "mfn");
65 ok(my $size = $input->size, "size");
69 test_after_open($input);
70 my $size = test_after_open($input_lm);
73 my ($input, $size) = @_;
77 foreach my $mfn ( 1 ... $size ) {
78 ok(my $rec = $input->fetch, "fetch $mfn");
79 cmp_ok($input->pos, '==', $mfn, "pos $mfn");
81 ok(my $dump = $input->dump_ascii, "dump_ascii $mfn");
82 diag $dump if ($debug);
88 my @db1 = test_fetch($input, $size);
89 my @db2 = test_fetch($input_lm, $size);
91 is_deeply(\@db1, \@db2, "seek working");
93 sub test_start_limit($$$$) {
94 my ($input, $s,$l,$e) = @_;
96 diag "offset $s, limit: $l, expected: $e";
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");
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);
108 ok(my $s = $input->stats, 'stats');
109 diag "stats:\n$s" if ($debug);
111 $module = 'WebPAC::Input::MARC';
112 diag "testing with $module";
114 ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");
116 ok($input->open( path => "$abs_path/data/marc.iso" ), "open marc.iso");
118 test_after_open($input);
120 test_fetch($input, $input->size);
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");
126 $WebPAC::Input::Test::rec = {
128 { 'a' => '[200 a]', 'b' => '[200 b]', 'c' => '[200 c]', 'f' => '[200 f] test : test' },
131 { 'x' => 'foobar', },
135 $WebPAC::Input::Test::size = 42;
137 ok($input->open( path => "$abs_path/modify_isis/LIBRI", ), "open modify_isis (plain)");
139 cmp_ok($input->size, '==', 42, 'size');
141 ok(my $rec_p = $input->fetch, 'fetch');
146 path => "$abs_path/modify_isis/LIBRI",
149 '*' => { '^c' => '. ' },
150 '^f' => { ' : ' => ' / ' },
153 ), "open (with modify_records)");
156 throws_ok { $input->seek } qr/without/, 'seek without position';
157 cmp_ok($input->seek(0), '==', -1, 'seek');
159 my $f = $WebPAC::Input::Test::filter_coderef;
160 ok(ref($f) eq 'CODE', 'filter_coderef');
163 $f->( '^afoo^cbar^fbing : bong', 200),
164 'eq', '^afoo. bar^fbing / bong',
170 my $modify_file = "$abs_path/conf/modify/test.pl";
173 path => "$abs_path/modify_isis/LIBRI",
174 modify_file => $modify_file,
175 ), "open (with modify_file $modify_file)");
177 my $f = $WebPAC::Input::Test::filter_coderef;
178 ok(ref($f) eq 'CODE', 'filter_coderef');
180 diag "regexps = ", dump($input->modify_file_regexps( $modify_file )) if ($debug);
183 my ($field, $from, $to) = @_;
184 cmp_ok( $f->( $from, $field, 1 ), 'eq', $to, "filter $field |$from| -> |$to|" );
188 '^a foo ; bar = baz : zzz',
189 '^a foo^kbar^dbaz : zzz',