/[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

Annotation of /trunk/t/2-input.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 909 - (hide annotations)
Tue Oct 30 01:46:41 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: application/x-troff
File size: 5187 byte(s)
 r1362@llin:  dpavlin | 2007-10-30 02:46:05 +0100
 Show indicators (available when using WebPAC::Input::MARC) as
 first two subfileds in statistics insted in alphabetical order

1 dpavlin 286 #!/usr/bin/perl -w
2    
3 dpavlin 908 use Test::More tests => 123;
4 dpavlin 286 use Test::Exception;
5     use Cwd qw/abs_path/;
6     use blib;
7     use strict;
8    
9 dpavlin 761 use Data::Dump qw/dump/;
10 dpavlin 506
11 dpavlin 286 BEGIN {
12     use_ok( 'WebPAC::Input::ISIS' );
13 dpavlin 290 use_ok( 'WebPAC::Input::MARC' );
14 dpavlin 796 use_ok( 'WebPAC::Input::Test' );
15 dpavlin 286 }
16    
17 dpavlin 761 my $debug = shift @ARGV;
18 dpavlin 506 my $no_log = $debug ? 0 : 1;
19    
20 dpavlin 286 ok(my $abs_path = abs_path($0), "abs_path");
21 dpavlin 908 $abs_path =~ s#/[^/]*$#/#; #vim
22 dpavlin 286
23     my $module = 'WebPAC::Input::ISIS';
24 dpavlin 290 diag "testing with $module";
25 dpavlin 286
26 dpavlin 908 throws_ok { my $input = new WebPAC::Input( no_log => $no_log ) } qr/module/, "need module";
27 dpavlin 761 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 dpavlin 286
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 dpavlin 761 my $store;
35    
36 dpavlin 599 ok($input->open( path => "$abs_path/winisis/BIBL" ), "open winisis");
37 dpavlin 761 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 dpavlin 286
50 dpavlin 761 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 dpavlin 290 sub test_after_open($) {
62     my $input = shift;
63 dpavlin 286
64 dpavlin 290 cmp_ok($input->pos, '==', -1, "mfn");
65     ok(my $size = $input->size, "size");
66     return $size;
67     }
68 dpavlin 286
69 dpavlin 290 test_after_open($input);
70     my $size = test_after_open($input_lm);
71 dpavlin 286
72 dpavlin 290 sub test_fetch($$) {
73     my ($input, $size) = @_;
74 dpavlin 286
75 dpavlin 290 my @db;
76 dpavlin 286
77 dpavlin 290 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 dpavlin 774 ok(my $dump = $input->dump_ascii, "dump_ascii $mfn");
82 dpavlin 908 # XXX test count will help us keep this test in-line :-)
83     ok($rec->{leader}, "leader $mfn") if $rec->{leader};
84 dpavlin 771 diag $dump if ($debug);
85 dpavlin 290 }
86    
87     return @db;
88 dpavlin 286 }
89    
90 dpavlin 290 my @db1 = test_fetch($input, $size);
91     my @db2 = test_fetch($input_lm, $size);
92    
93 dpavlin 286 is_deeply(\@db1, \@db2, "seek working");
94    
95 dpavlin 290 sub test_start_limit($$$$) {
96     my ($input, $s,$l,$e) = @_;
97 dpavlin 286
98     diag "offset $s, limit: $l, expected: $e";
99    
100 dpavlin 761 ok($s = $input->open( path => "$abs_path/winisis/BIBL", offset => $s, limit => $l, debug => $debug ), "open winisis");
101 dpavlin 286 cmp_ok($s, '==', $size, "db size from open = $size");
102     cmp_ok($input->size, '==', $e, "input->size = $e");
103     }
104    
105 dpavlin 290 test_start_limit($input, 1, 3, 3);
106     test_start_limit($input, $size, 3, 0);
107     test_start_limit($input, 3, $size, $size - 2);
108     test_start_limit($input, 1, $size + 2, $size);
109 dpavlin 286
110 dpavlin 909 ok(my $s = $input->stats, "$module stats");
111 dpavlin 794 diag "stats:\n$s" if ($debug);
112 dpavlin 506
113 dpavlin 290 $module = 'WebPAC::Input::MARC';
114     diag "testing with $module";
115    
116 dpavlin 909 ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, stats => 1 ), "new $module");
117 dpavlin 290
118 dpavlin 599 ok($input->open( path => "$abs_path/data/marc.iso" ), "open marc.iso");
119 dpavlin 290
120     test_after_open($input);
121    
122     test_fetch($input, $input->size);
123    
124 dpavlin 909 ok(my $s = $input->stats, "$module stats");
125    
126     diag "stats:\n$s" if ($debug);
127 dpavlin 599 # test modify_record
128 dpavlin 796 $module = 'WebPAC::Input::Test';
129     ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, debug => $debug ), "new $module");
130 dpavlin 599
131 dpavlin 796 $WebPAC::Input::Test::rec = {
132 dpavlin 797 '200' => [
133     { 'a' => '[200 a]', 'b' => '[200 b]', 'c' => '[200 c]', 'f' => '[200 f] test : test' },
134     ],
135 dpavlin 796 '900' => [
136 dpavlin 797 { 'x' => 'foobar', },
137     ],
138 dpavlin 796 };
139    
140 dpavlin 797 $WebPAC::Input::Test::size = 42;
141 dpavlin 796
142 dpavlin 820 ok($input->open( path => "/fake/path", ), "open modify_isis (plain)");
143 dpavlin 797
144     cmp_ok($input->size, '==', 42, 'size');
145    
146 dpavlin 599 ok(my $rec_p = $input->fetch, 'fetch');
147    
148 dpavlin 784 # modify_records
149    
150 dpavlin 599 ok($input->open(
151 dpavlin 820 path => "/another/fake/path",
152 dpavlin 599 modify_records => {
153     200 => {
154     '*' => { '^c' => '. ' },
155 dpavlin 794 '^f' => { ' : ' => ' / ' },
156 dpavlin 599 }
157     },
158 dpavlin 799 ), "open (with modify_records)");
159 dpavlin 599
160 dpavlin 797 # seek
161     throws_ok { $input->seek } qr/without/, 'seek without position';
162     cmp_ok($input->seek(0), '==', -1, 'seek');
163 dpavlin 599
164 dpavlin 821 sub test_filter {
165 dpavlin 761
166 dpavlin 821 my $f = $WebPAC::Input::Test::filter_coderef;
167     ok(ref($f) eq 'CODE', 'filter_coderef');
168    
169     my ($field, $from, $to) = @_;
170     cmp_ok( $f->( $from, $field, 1 ), 'eq', $to, "filter $field |$from| -> |$to|" );
171     }
172    
173     test_filter(200,
174     '^afoo^cbar^fbing : bong',
175     '^afoo. bar^fbing / bong',
176 dpavlin 797 );
177 dpavlin 784
178 dpavlin 799 # modify_file
179    
180     my $modify_file = "$abs_path/conf/modify/test.pl";
181    
182     ok($input->open(
183 dpavlin 820 path => "/and/another/fake/path",
184 dpavlin 799 modify_file => $modify_file,
185     ), "open (with modify_file $modify_file)");
186    
187 dpavlin 800 diag "regexps = ", dump($input->modify_file_regexps( $modify_file )) if ($debug);
188 dpavlin 799
189     test_filter(200,
190     '^a foo ; bar = baz : zzz',
191 dpavlin 800 '^a foo^kbar^dbaz : zzz',
192 dpavlin 799 );
193    
194 dpavlin 820 # empty subfield removal
195 dpavlin 821
196     ok($input->open(
197     path => "/another/fake/path",
198     modify_records => {
199     900 => {
200     '^a' => { '^e' => ' : ^e' },
201 dpavlin 823 },
202     901 => {
203     '^a' => { 'foo' => 'baz' },
204     },
205 dpavlin 821 },
206     ), "open (with modify_records for empty subfields)");
207    
208     test_filter(900,
209     '^a^ebar',
210     '^a^ebar',
211 dpavlin 820 );
212 dpavlin 821
213     test_filter(900,
214     '^afoo^ebar',
215     '^afoo : ^ebar',
216     );
217 dpavlin 823
218     test_filter(901,
219     '^afoo^ebar',
220     '^abaz^ebar',
221     );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26