ff145c1984d4d00945ad0a0de7ef4d7b545b7147
[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($disp eq $term, "display term $i identical to term");
32 }
33
34 $ss->destroy();
35 ok(1, "destroyed scanset");
36 eval { $ss->destroy() };
37 ok(defined $@ && $@ =~ /been destroy\(\)ed/,
38    "can't re-destroy scanset");
39
40 # Now re-scan, but only for words that occur in the title
41 # This time, use a Query object for the start-term
42 ($ss, $n) = scan($conn, 1, new ZOOM::Query::PQF('@attr 1=4 w'), 6);
43
44 $previous = "";         # Sorts before all legitimate terms
45 foreach my $i (1 .. $n) {
46     my($term, $occ) = $ss->term($i-1);
47     ok(defined $term,
48        "got title term $i of $n: '$term' ($occ occurences)");
49     ok($term ge $previous, "title term '$term' ge previous '$previous'");
50     $previous = $term;
51     ok((grep { $term eq $_ } @terms), "title term was in term list");
52 }
53
54 $ss->destroy();
55 ok(1, "destroyed second scanset");
56
57 # Now re-do the same scan, but limiting the results to four terms at a
58 # time.  This time, use a CQL query
59 $conn->option(number => 4);
60 $conn->option(cqlfile => "samples/cql/pqf.properties");
61
62 ($ss, $n) = scan($conn, 1, new ZOOM::Query::CQL2RPN('title=w', $conn), 4);
63 # Get last term and use it as seed for next scan
64 my($term, $occ) = $ss->term($n-1);
65 ok($ss->option("position") == 1,
66    "seed-term is start of returned list");
67 ok(defined $term,
68    "got last title term '$term' to use as seed");
69
70 $ss->destroy();
71 ok(1, "destroyed third scanset");
72
73 $conn->option(cclfile => "samples/ccl/default.bib");
74 ($ss, $n) = scan($conn, 1, new ZOOM::Query::CCL2RPN('ti=w', $conn), 4);
75 # Get last term and use it as seed for next scan
76 ($term, $occ) = $ss->term($n-1);
77 ok($ss->option("position") == 1,
78    "seed-term is start of returned list");
79 ok(defined $term,
80    "got last title term '$term' to use as seed");
81
82 $ss->destroy();
83 ok(1, "destroyed fourth scanset");
84
85 # We want the seed-term to be in "position zero", i.e. just before the start
86 $conn->option(position => 0);
87 ($ss, $n) = scan($conn, 0, "\@attr 1=4 $term", 2);
88 ok($ss->option("position") == 0,
89    "seed-term before start of returned list");
90
91 # Silly test of option setting and getting
92 $ss->option(position => "fruit");
93 ok($ss->option("position") eq "fruit",
94    "option setting/getting works");
95
96 $ss->destroy();
97 ok(1, "destroyed fifth scanset");
98
99 # Some more testing still to do: see comment in "15-scan.t"
100
101
102 sub scan {
103     my($conn, $startterm_is_query, $startterm, $nexpected) = @_;
104
105     my $ss;
106     eval {
107         if ($startterm_is_query) {
108             $ss = $conn->scan($startterm);
109         } else {
110             $ss = $conn->scan_pqf($startterm);
111         }
112     };
113     ok(!$@, "scan for '$startterm'");
114
115     my $n = $ss->size();
116     ok(defined $n, "got size");
117     ok($n == $nexpected, "got $n terms for '$startterm' (expected $nexpected)");
118     return ($ss, $n);
119 }