a6da2924f05395564cb14147b32a0f3c01cbe04a
[ZOOM-Perl-moved-to-github.git] / t / 25-scan.t
1 # $Id: 25-scan.t,v 1.11 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 25-scan.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 87;
9
10 BEGIN { use_ok('ZOOM') };
11
12 my $host = "z3950.indexdata.com/gils";
13 my $conn;
14 eval { $conn = new ZOOM::Connection($host, 0) };
15 ok(!$@, "connection to '$host'");
16
17 my($ss, $n) = scan($conn, 0, "w", 10);
18
19 my @terms = ();
20
21 my $previous = "";              # Sorts before all legitimate terms
22 foreach my $i (1 .. $n) {
23     my($term, $occ) = $ss->term($i-1);
24     ok(defined $term,
25        "got term $i of $n: '$term' ($occ occurences)");
26     ok($term ge $previous, "term '$term' ge previous '$previous'");
27     $previous = $term;
28     push @terms, $term;
29     (my $disp, $occ) = $ss->display_term($i-1);
30     ok(defined $disp,
31        "display term $i of $n: '$disp' ($occ occurences)");
32     ok($disp eq $term, "display term $i identical to term");
33 }
34
35 $ss->destroy();
36 ok(1, "destroyed scanset");
37 eval { $ss->destroy() };
38 ok(defined $@ && $@ =~ /been destroy\(\)ed/,
39    "can't re-destroy scanset");
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 ($ss, $n) = scan($conn, 1, new ZOOM::Query::PQF('@attr 1=4 w'), 6);
44
45 $previous = "";         # Sorts before all legitimate terms
46 foreach my $i (1 .. $n) {
47     my($term, $occ) = $ss->term($i-1);
48     ok(defined $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 $ss->destroy();
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.  This time, use a CQL query
60 $conn->option(number => 4);
61 $conn->option(cqlfile => "samples/cql/pqf.properties");
62
63 ($ss, $n) = scan($conn, 1, new ZOOM::Query::CQL2RPN('title=w', $conn), 4);
64 # Get last term and use it as seed for next scan
65 my($term, $occ) = $ss->term($n-1);
66 ok($ss->option("position") == 1,
67    "seed-term is start of returned list");
68 ok(defined $term,
69    "got last title term '$term' to use as seed");
70
71 $ss->destroy();
72 ok(1, "destroyed third scanset");
73
74 $conn->option(cclfile => "samples/ccl/default.bib");
75 ($ss, $n) = scan($conn, 1, new ZOOM::Query::CCL2RPN('ti=w', $conn), 4);
76 # Get last term and use it as seed for next scan
77 ($term, $occ) = $ss->term($n-1);
78 ok($ss->option("position") == 1,
79    "seed-term is start of returned list");
80 ok(defined $term,
81    "got last title term '$term' to use as seed");
82
83 $ss->destroy();
84 ok(1, "destroyed fourth scanset");
85
86 # We want the seed-term to be in "position zero", i.e. just before the start
87 $conn->option(position => 0);
88 ($ss, $n) = scan($conn, 0, "\@attr 1=4 $term", 2);
89 ok($ss->option("position") == 0,
90    "seed-term before start of returned list");
91
92 # Silly test of option setting and getting
93 $ss->option(position => "fruit");
94 ok($ss->option("position") eq "fruit",
95    "option setting/getting works");
96
97 $ss->destroy();
98 ok(1, "destroyed fifth scanset");
99
100 # Some more testing still to do: see comment in "15-scan.t"
101
102
103 sub scan {
104     my($conn, $startterm_is_query, $startterm, $nexpected) = @_;
105
106     my $ss;
107     eval {
108         if ($startterm_is_query) {
109             $ss = $conn->scan($startterm);
110         } else {
111             $ss = $conn->scan_pqf($startterm);
112         }
113     };
114     ok(!$@, "scan for '$startterm'");
115
116     my $n = $ss->size();
117     ok(defined $n, "got size");
118     ok($n == $nexpected, "got $n terms (expected $nexpected)");
119     return ($ss, $n);
120 }