Test for new scan1()
[ZOOM-Perl-moved-to-github.git] / t / 15-scan.t
1 # $Id: 15-scan.t,v 1.7 2005-12-19 17:39:58 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 $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, "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 ($ss, $n) = scan($conn, '@attr 1=4 w', 6);
44
45 $previous = "";         # Sorts before all legitimate terms
46 foreach my $i (1 .. $n) {
47     my $term = Net::Z3950::ZOOM::scanset_term($ss, $i-1, $occ, $len);
48     ok(defined $term && $len eq length($term),
49        "got title term $i of $n: '$term' ($occ occurences)");
50     ok($term ge $previous, "title term '$term' ge previous '$previous'");
51     $previous = $term;
52     ok((grep { $term eq $_ } @terms), "title term was in term list");
53 }
54
55 Net::Z3950::ZOOM::scanset_destroy($ss);
56 ok(1, "destroyed second scanset");
57
58 # Now re-do the same scan, but limiting the results to four terms at a
59 # time.
60 Net::Z3950::ZOOM::connection_option_set($conn, number => 4);
61 ($ss, $n) = scan($conn, '@attr 1=4 w', 4);
62 # Get last term and use it as seed for next scan
63 my $term = Net::Z3950::ZOOM::scanset_term($ss, $n-1, $occ, $len);
64 ok(Net::Z3950::ZOOM::scanset_option_get($ss, "position") == 1,
65    "seed-term is start of returned list");
66 ok(defined $term && $len eq length($term),
67    "got last title term '$term' to use as seed");
68
69 Net::Z3950::ZOOM::scanset_destroy($ss);
70 ok(1, "destroyed third scanset");
71
72 # We want the seed-term to be in "position zero", i.e. just before the start
73 Net::Z3950::ZOOM::connection_option_set($conn, position => 0);
74 ($ss, $n) = scan($conn, "\@attr 1=4 $term", 2);
75 ok(Net::Z3950::ZOOM::scanset_option_get($ss, "position") == 0,
76    "seed-term before start of returned list");
77
78 # Silly test of option setting and getting
79 Net::Z3950::ZOOM::scanset_option_set($ss, position => "fruit");
80 ok(Net::Z3950::ZOOM::scanset_option_get($ss, "position") eq "fruit",
81    "option setting/getting works");
82
83 Net::Z3950::ZOOM::scanset_destroy($ss);
84 ok(1, "destroyed fourth scanset");
85
86 # There is no obvious use for scanset_option_set(), and little to be
87 # done with scanset_option_get(); and I can't find a server that
88 # returns display terms different from its terms.
89
90
91 my $use_query_for_scan = 0;
92 sub scan {
93     my($conn, $startterm, $nexpected) = @_;
94
95     # Alternately use the original scan() interface and new scan1()
96     my $ss;
97     if ($use_query_for_scan) {
98         my $q = Net::Z3950::ZOOM::query_create();
99         Net::Z3950::ZOOM::query_prefix($q, $startterm);
100         $ss = Net::Z3950::ZOOM::connection_scan1($conn, $q);
101     } else {
102         $ss = Net::Z3950::ZOOM::connection_scan($conn, $startterm);
103     }
104     $use_query_for_scan = !$use_query_for_scan;
105
106     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
107     ok($errcode == 0, "scan for '$startterm'");
108     $n = Net::Z3950::ZOOM::scanset_size($ss);
109     ok(defined $n, "got size");
110     ok($n == $nexpected, "got $n terms (expected $nexpected)");
111     return ($ss, $n);
112 }