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