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