Cleaned test scripts to be (nearly?) atomic
[idzebra-moved-to-github.git] / perl / t / 08_scan.t
1 #!perl
2 # =============================================================================
3 # $Id: 08_scan.t,v 1.3 2004-09-15 14:11:06 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 => 17;
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 $sess->begin_trans;
45 $sess->databases('demo1', 'demo2');
46 $ret=$sess->end_trans;
47
48 $sess->begin_trans;
49 $sess->databases('demo1', 'demo2');
50 for $F (<lib/IDZebra/*.pm>)
51 {
52     ($ret,$sysno)=$sess->insert_record (file=>$F, recordType => 'grs.perl.pod');
53     ok( $ret==0, "inserted $F");
54     #print STDERR "Inserted $F ok. ret=$ret sys=$sysno\n";
55     $filecount++;
56 }
57 $ret=$sess->end_trans;
58 ok($filecount>0,"Inserted files");
59 is($ret->{inserted},$filecount, "Inserted all");
60 $sess->databases('demo1');
61
62 # -----------------------------------------------------------------------------
63 # Scan titles in multiple databases
64
65 my $sl1 = $sess->scan(expression => "\@attr 1=4 \@attr 6=2 a",
66                       databases => [qw(demo1 demo2)]);
67
68 &test_list($sl1,$filecount, $filecount*2,1);
69 # -----------------------------------------------------------------------------
70 # Scan titles in a single and default database
71 my $sl2 = $sess->scan(expression => "\@attr 1=4 \@attr 6=2 a");
72 &test_list($sl2,$filecount, $filecount,1);
73
74
75 # -----------------------------------------------------------------------------
76 # Scan long list, with position...
77 my $sl3 = $sess->scan(expression  => "\@attr 1=1016 a");
78
79 my @entries = $sl3->entries(position    => 5,
80                             num_entries => 10000);
81
82 my $count = $#entries + 1;
83 ok (($sl3->errCode == 0),"scan successful");
84 ok (($sl3->num_entries == $count),"fetched $count entries");
85 my $i = 1;
86 my $posok = 1;
87 foreach my $se (@entries) {
88     $posok = 0 if ($se->position != $i); 
89     $i++;
90 }
91 ok (($posok),"position of each term");
92
93
94 # -----------------------------------------------------------------------------
95 # Scan error
96 eval {my $sl4 = $sess->scan(expression => "\@attr 1=9999 a");};
97 ok (($@ ne ""),"Wrong scan die");
98 ok (($sess->errCode != 0), 
99     "Error reported in session: ".$sess->errCode.
100     " (". $sess->errString. ")");
101
102
103 # ----------------------------------------------------------------------------
104 # Close session
105 $sess->close;
106
107 # ============================================================================
108 sub test_list {
109     my ($sl, $ecount, $occ, $offset) = @_;
110     my @entries = $sl->entries();
111     my $count = $#entries + 1;
112     ok (($sl->errCode == 0),"scan successfull");
113     ok (($sl->num_entries == $ecount),
114         "number of entries is ".$sl->num_entries);
115     ok (($count == $sl->num_entries),"fetched $count entries");
116     
117     my $occcount=0; 
118     my $posok = 1;
119     my $i = $offset;
120     foreach my $se (@entries) {
121         $occcount += $se->occurrences();
122         $posok = 0 if ($se->position != $i); 
123         $i++;
124     }
125     
126     ok ($occcount == $occ,"occurrences: $occcount");
127     ok (($posok),"position of each term");
128 }