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