Perl tests work
[idzebra-moved-to-github.git] / perl / t / 08_scan.t
1 #!perl
2 # =============================================================================
3 # $Id: 08_scan.t,v 1.4 2004-09-20 15:59:48 heikki Exp $
4 #
5 # Perl API header
6 # =============================================================================
7 BEGIN {
8     if ($ENV{PERL_CORE}) {
9         chdir 't' if -d 't';
10     }
11     push (@INC,'demo','blib/lib','blib/arch');
12 }
13
14 use strict;
15 use warnings;
16
17 use Test::More tests => 32;
18 #use Test::More skip_all => "Something rotten with scan.";
19
20 # ----------------------------------------------------------------------------
21 # Session opening and closing
22 BEGIN {
23     use IDZebra;
24     unlink("test08.log");
25     IDZebra::logFile("test08.log");
26   IDZebra::logLevel(15);
27     use_ok('IDZebra::Session'); 
28     use_ok('pod');
29 }
30
31
32 # ----------------------------------------------------------------------------
33 # Session opening and closing
34 my $sess = IDZebra::Session->open(configFile => 'demo/zebra.cfg',
35                                   groupName => 'demo1');
36
37 # ----------------------------------------------------------------------------
38 # Insert some test data
39 my $ret;
40 my $sysno;
41 my $F;
42 my $filecount=0;
43 $sess->init;
44
45 $sess->begin_trans;
46 $sess->databases('demo1', 'demo2');
47 for $F (<lib/IDZebra/*.pm>)
48 {
49     ($ret,$sysno)=$sess->insert_record (file=>$F, recordType => 'grs.perl.pod');
50     ok( $ret==0, "inserted $F");
51     $filecount++;
52 }
53 $ret=$sess->end_trans;
54 ok($filecount>0,"Inserted files");
55 is($ret->{inserted},$filecount, "Inserted all");
56 $sess->databases('demo1');
57
58 # -----------------------------------------------------------------------------
59 # Scan titles in a single (default) database
60 $sess->begin_trans;
61 IDZebra::logMsg(2,"t08: Starting to scan");
62 my $sl0 = $sess->scan(expression => "\@attr 1=4 \@attr 6=1 a");
63 IDZebra::logMsg(2,"t08: scan done");
64
65 my @ent=$sl0->entries(position    => 1,
66                       num_entries => 3);
67 my $nent=@ent;
68 is ($nent,3,"got 3 entries");
69
70 my $cnt= $sl0->num_entries;
71
72 is($cnt,3,"num_entries");
73 is($ent[0]->term(),"a", "first entry");
74 is($ent[1]->term(),"an", "second entry");
75 is($ent[2]->term(),"and", "third entry");
76
77 $sess->end_trans;
78 # -----------------------------------------------------------------------------
79 # Scan titles in a named database
80
81 my $sl1 = $sess->scan(expression => "\@attr 1=4 \@attr 6=2 a",
82                       databases => [qw(demo1)]);
83 &test_list($sl1,$filecount, $filecount,1);
84
85 # FIXME - Should test multiple databases, but I can't get that to work
86
87
88 # -----------------------------------------------------------------------------
89 # Scan titles in a single and default database
90 my $sl2 = $sess->scan(expression => "\@attr 1=4 \@attr 6=2 a");
91 &test_list($sl2,$filecount, $filecount,1);
92
93
94 # -----------------------------------------------------------------------------
95 # Scan long list, with position...
96 my $sl3 = $sess->scan(expression  => "\@attr 1=1016 a");
97
98 my @entries = $sl3->entries(position    => 5,
99                             num_entries => 10000);
100
101 my $count = $#entries + 1;
102 ok (($sl3->errCode == 0),"scan successful");
103 ok (($sl3->num_entries == $count),"fetched $count entries");
104 my $i = 1;
105 my $posok = 1;
106 foreach my $se (@entries) {
107     $posok = 0 if ($se->position != $i); 
108     $i++;
109 }
110 ok (($posok),"position of each term");
111
112
113 # -----------------------------------------------------------------------------
114 # Scan error
115 eval {my $sl4 = $sess->scan(expression => "\@attr 1=9999 a");};
116 ok (($@ ne ""),"Wrong scan die");
117 ok (($sess->errCode != 0), 
118     "Error reported in session: ".$sess->errCode.
119     " (". $sess->errString. ")");
120
121
122 # ----------------------------------------------------------------------------
123 # Close session
124 $sess->close;
125
126 # ============================================================================
127 sub test_list {
128     my ($sl, $ecount, $occ, $offset) = @_;
129     my @entries = $sl->entries();
130     my $count = $#entries + 1;
131     ok (($sl->errCode == 0),"scan successfull");
132     ok (($sl->num_entries == $ecount),
133         "number of entries is ".$sl->num_entries);
134     is ($count,$sl->num_entries,"fetched $count entries");
135     
136     my $occcount=0; 
137     my $posok = 1;
138     my $i = $offset;
139     foreach my $se (@entries) {
140         $occcount += $se->occurrences();
141         $posok = 0 if ($se->position != $i); 
142         $i++;
143     }
144     
145     is ($occcount, $occ,"occurrences: $occcount");
146     ok (($posok),"position of each term");
147 }