Add tests for CQL scanning
[ZOOM-Perl-moved-to-github.git] / t / 15-scan.t
1 # $Id: 15-scan.t,v 1.8 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 15-scan.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 81;
9
10 BEGIN { use_ok('Net::Z3950::ZOOM') };
11
12 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
13
14 #my $host = "indexdata.com/gils";
15 my $host = "localhost:9999/default";
16 my $conn = Net::Z3950::ZOOM::connection_new($host, 0);
17 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
18 ok($errcode == 0, "connection to '$host'");
19
20 my($ss, $n) = scan($conn, 0, "w", 10);
21
22 my @terms = ();
23 my($occ, $len) = (0, 0);
24
25 my $previous = "";              # Sorts before all legitimate terms
26 foreach my $i (1 .. $n) {
27     my $term = Net::Z3950::ZOOM::scanset_term($ss, $i-1, $occ, $len);
28     ok(defined $term && $len eq length($term),
29        "got term $i of $n: '$term' ($occ occurences)");
30     ok($term ge $previous, "term '$term' ge previous '$previous'");
31     $previous = $term;
32     push @terms, $term;
33     my $disp = Net::Z3950::ZOOM::scanset_display_term($ss, $i-1, $occ, $len);
34     ok(defined $disp && $len eq length($disp),
35        "display term $i of $n: '$disp' ($occ occurences)");
36     ok($disp eq $term, "display term $i identical to term");
37 }
38
39 Net::Z3950::ZOOM::scanset_destroy($ss);
40 ok(1, "destroyed scanset");
41 ok(1, "(can't re-destroy scanset)"); # Only meaningful in OO API.
42
43 # Now re-scan, but only for words that occur in the title
44 # This time, use a Query object for the start-term
45 my $q = Net::Z3950::ZOOM::query_create();
46 Net::Z3950::ZOOM::query_prefix($q, '@attr 1=4 w');
47 ($ss, $n) = scan($conn, 1, $q, 6);
48
49 $previous = "";         # Sorts before all legitimate terms
50 foreach my $i (1 .. $n) {
51     my $term = Net::Z3950::ZOOM::scanset_term($ss, $i-1, $occ, $len);
52     ok(defined $term && $len eq length($term),
53        "got title term $i of $n: '$term' ($occ occurences)");
54     ok($term ge $previous, "title term '$term' ge previous '$previous'");
55     $previous = $term;
56     ok((grep { $term eq $_ } @terms), "title term was in term list");
57 }
58
59 Net::Z3950::ZOOM::scanset_destroy($ss);
60 ok(1, "destroyed second scanset");
61
62 # Now re-do the same scan, but limiting the results to four terms at a
63 # time.  This time, use a CQL query
64 Net::Z3950::ZOOM::connection_option_set($conn, number => 4);
65 Net::Z3950::ZOOM::connection_option_set($conn, cqlfile =>
66                                         "samples/cql/pqf.properties");
67
68 $q = Net::Z3950::ZOOM::query_create();
69 Net::Z3950::ZOOM::query_cql($q, 'title=w');
70 ($ss, $n) = scan($conn, 1, $q, 4);
71 # Get last term and use it as seed for next scan
72 my $term = Net::Z3950::ZOOM::scanset_term($ss, $n-1, $occ, $len);
73 ok(Net::Z3950::ZOOM::scanset_option_get($ss, "position") == 1,
74    "seed-term is start of returned list");
75 ok(defined $term && $len eq length($term),
76    "got last title term '$term' to use as seed");
77
78 Net::Z3950::ZOOM::scanset_destroy($ss);
79 ok(1, "destroyed third scanset");
80
81 # We want the seed-term to be in "position zero", i.e. just before the start
82 Net::Z3950::ZOOM::connection_option_set($conn, position => 0);
83 ($ss, $n) = scan($conn, 0, "\@attr 1=4 $term", 2);
84 ok(Net::Z3950::ZOOM::scanset_option_get($ss, "position") == 0,
85    "seed-term before start of returned list");
86
87 # Silly test of option setting and getting
88 Net::Z3950::ZOOM::scanset_option_set($ss, position => "fruit");
89 ok(Net::Z3950::ZOOM::scanset_option_get($ss, "position") eq "fruit",
90    "option setting/getting works");
91
92 Net::Z3950::ZOOM::scanset_destroy($ss);
93 ok(1, "destroyed fourth scanset");
94
95 # There is no obvious use for scanset_option_set(), and little to be
96 # done with scanset_option_get(); and I can't find a server that
97 # returns display terms different from its terms.
98
99
100 sub scan {
101     my($conn, $startterm_is_query, $startterm, $nexpected) = @_;
102
103     my $ss;
104     if ($startterm_is_query) {
105         $ss = Net::Z3950::ZOOM::connection_scan1($conn, $startterm);
106     } else {
107         $ss = Net::Z3950::ZOOM::connection_scan($conn, $startterm);
108     }
109
110     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
111     ok($errcode == 0, "scan for '$startterm'");
112
113     my $n = Net::Z3950::ZOOM::scanset_size($ss);
114     ok(defined $n, "got size");
115     ok($n == $nexpected, "got $n terms (expected $nexpected)");
116     return ($ss, $n);
117 }