Add tests for CQL scanning
[ZOOM-Perl-moved-to-github.git] / t / 25-scan.t
1 # $Id: 25-scan.t,v 1.6 2005-12-21 00:16:50 mike Exp $
2
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl 25-scan.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 81;
9
10 BEGIN { use_ok('ZOOM') };
11
12 #my $host = "indexdata.com/gils";
13 my $host = "localhost:9999/default";
14 my $conn;
15 eval { $conn = new ZOOM::Connection($host, 0) };
16 ok(!$@, "connection to '$host'");
17
18 my($ss, $n) = scan($conn, 0, "w", 10);
19
20 my @terms = ();
21
22 my $previous = "";              # Sorts before all legitimate terms
23 foreach my $i (1 .. $n) {
24     my($term, $occ) = $ss->term($i-1);
25     ok(defined $term,
26        "got term $i of $n: '$term' ($occ occurences)");
27     ok($term ge $previous, "term '$term' ge previous '$previous'");
28     $previous = $term;
29     push @terms, $term;
30     (my $disp, $occ) = $ss->display_term($i-1);
31     ok(defined $disp,
32        "display term $i of $n: '$disp' ($occ occurences)");
33     ok($disp eq $term, "display term $i identical to term");
34 }
35
36 $ss->destroy();
37 ok(1, "destroyed scanset");
38 eval { $ss->destroy() };
39 ok(defined $@ && $@ =~ /been destroy\(\)ed/,
40    "can't re-destroy scanset");
41
42 # Now re-scan, but only for words that occur in the title
43 # This time, use a Query object for the start-term
44 ($ss, $n) = scan($conn, 1, new ZOOM::Query::PQF('@attr 1=4 w'), 6);
45
46 $previous = "";         # Sorts before all legitimate terms
47 foreach my $i (1 .. $n) {
48     my($term, $occ) = $ss->term($i-1);
49     ok(defined $term,
50        "got title term $i of $n: '$term' ($occ occurences)");
51     ok($term ge $previous, "title term '$term' ge previous '$previous'");
52     $previous = $term;
53     ok((grep { $term eq $_ } @terms), "title term was in term list");
54 }
55
56 $ss->destroy();
57 ok(1, "destroyed second scanset");
58
59 # Now re-do the same scan, but limiting the results to four terms at a
60 # time.  This time, use a CQL query
61 $conn->option(number => 4);
62 $conn->option(cqlfile => "samples/cql/pqf.properties");
63
64 ($ss, $n) = scan($conn, 1, new ZOOM::Query::CQL('title=w'), 4);
65 # Get last term and use it as seed for next scan
66 my($term, $occ) = $ss->term($n-1);
67 ok($ss->option("position") == 1,
68    "seed-term is start of returned list");
69 ok(defined $term,
70    "got last title term '$term' to use as seed");
71
72 $ss->destroy();
73 ok(1, "destroyed third scanset");
74
75 # We want the seed-term to be in "position zero", i.e. just before the start
76 $conn->option(position => 0);
77 ($ss, $n) = scan($conn, 0, "\@attr 1=4 $term", 2);
78 ok($ss->option("position") == 0,
79    "seed-term before start of returned list");
80
81 # Silly test of option setting and getting
82 $ss->option(position => "fruit");
83 ok($ss->option("position") eq "fruit",
84    "option setting/getting works");
85
86 $ss->destroy();
87 ok(1, "destroyed fourth scanset");
88
89 # Some more testing still to do: see comment in "15-scan.t"
90
91
92 sub scan {
93     my($conn, $startterm_is_query, $startterm, $nexpected) = @_;
94
95     my $ss;
96     eval {
97         if ($startterm_is_query) {
98             $ss = $conn->scan($startterm);
99         } else {
100             $ss = $conn->scan_pqf($startterm);
101         }
102     };
103     ok(!$@, "scan for '$startterm'");
104
105     my $n = $ss->size();
106     ok(defined $n, "got size");
107     ok($n == $nexpected, "got $n terms (expected $nexpected)");
108     return ($ss, $n);
109 }