8c345e9528641f0b57c7bdc6741d6d3caecda25f
[idzebra-moved-to-github.git] / perl / t / 08_scan.t
1 #!perl
2 # =============================================================================
3 # $Id: 08_scan.t,v 1.2 2004-07-28 08:15:47 adam 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 => 17;
18
19 # ----------------------------------------------------------------------------
20 # Session opening and closing
21 BEGIN {
22     use IDZebra;
23     unlink("test08.log");
24     IDZebra::logFile("test08.log");
25 #  IDZebra::logLevel(15);
26     use_ok('IDZebra::Session'); 
27     use_ok('pod');
28 }
29
30
31 # ----------------------------------------------------------------------------
32 # Session opening and closing
33 my $sess = IDZebra::Session->open(configFile => 'demo/zebra.cfg',
34                                   groupName => 'demo1');
35
36 $sess->databases('demo1');
37
38 our $filecount = 8;
39 # -----------------------------------------------------------------------------
40 # Scan titles in multiple databases
41
42 my $sl1 = $sess->scan(expression => "\@attr 1=4 \@attr 6=2 a",
43                       databases => [qw(demo1 demo2)]);
44
45 &test_list($sl1,$filecount, $filecount*2,1);
46 # -----------------------------------------------------------------------------
47 # Scan titles in a single and default database
48 my $sl2 = $sess->scan(expression => "\@attr 1=4 \@attr 6=2 a");
49 &test_list($sl2,$filecount, $filecount,1);
50
51
52 # -----------------------------------------------------------------------------
53 # Scan long list, with position...
54 my $sl3 = $sess->scan(expression  => "\@attr 1=1016 a");
55
56 my @entries = $sl3->entries(position    => 5,
57                             num_entries => 10000);
58
59 my $count = $#entries + 1;
60 ok (($sl3->errCode == 0),"scan successful");
61 ok (($sl3->num_entries == $count),"fetched $count entries");
62 my $i = 1;
63 my $posok = 1;
64 foreach my $se (@entries) {
65     $posok = 0 if ($se->position != $i); 
66     $i++;
67 }
68 ok (($posok),"position of each term");
69
70
71 # -----------------------------------------------------------------------------
72 # Scan error
73 eval {my $sl4 = $sess->scan(expression => "\@attr 1=9999 a");};
74 ok (($@ ne ""),"Wrong scan die");
75 ok (($sess->errCode != 0), 
76     "Error reported in session: ".$sess->errCode.
77     " (". $sess->errString. ")");
78
79
80 # ----------------------------------------------------------------------------
81 # Close session
82 $sess->close;
83
84 # ============================================================================
85 sub test_list {
86     my ($sl, $ecount, $occ, $offset) = @_;
87     my @entries = $sl->entries();
88     my $count = $#entries + 1;
89     ok (($sl->errCode == 0),"scan successfull");
90     ok (($sl->num_entries == $ecount),
91         "number of entries is ".$sl->num_entries);
92     ok (($count == $sl->num_entries),"fetched $count entries");
93     
94     my $occcount=0; 
95     my $posok = 1;
96     my $i = $offset;
97     foreach my $se (@entries) {
98         $occcount += $se->occurrences();
99         $posok = 0 if ($se->position != $i); 
100         $i++;
101     }
102     
103     ok ($occcount == $occ,"occurrences: $occcount");
104     ok (($posok),"position of each term");
105 }