Many additional tests, for field-specific scanning, non-default
[ZOOM-Perl-moved-to-github.git] / t / 25-scan.t
1 # $Id: 25-scan.t,v 1.2 2005-11-09 17:08:31 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 => 81;
9
10 BEGIN { use_ok('ZOOM') };
11
12 my $host = "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, "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 ($ss, $n) = scan($conn, '@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.
59 $conn->option(number => 4);
60 ($ss, $n) = scan($conn, '@attr 1=4 w', 4);
61 # Get last term and use it as seed for next scan
62 my($term, $occ) = $ss->term($n-1);
63 ok($ss->option("position") == 1,
64    "seed-term is start of returned list");
65 ok(defined $term,
66    "got last title term '$term' to use as seed");
67
68 $ss->destroy();
69 ok(1, "destroyed third scanset");
70
71 # We want the seed-term to be in "position zero", i.e. just before the start
72 $conn->option(position => 0);
73 ($ss, $n) = scan($conn, "\@attr 1=4 $term", 2);
74 ok($ss->option("position") == 0,
75    "seed-term before start of returned list");
76
77 # Silly test of option setting and getting
78 $ss->option(position => "fruit");
79 ok($ss->option("position") eq "fruit",
80    "option setting/getting works");
81
82 $ss->destroy();
83 ok(1, "destroyed fourth scanset");
84
85 # Some more testing still to do: see comment in "15-scan.t"
86
87 sub scan {
88     my($conn, $startterm, $nexpected) = @_;
89
90     my $ss;
91     eval { $ss = $conn->scan($startterm) };
92     ok(!$@, "scan for '$startterm'");
93
94     my $n = $ss->size();
95     ok(defined $n, "got size");
96     ok($n == $nexpected, "got $n terms (expected $nexpected)");
97     return ($ss, $n);
98 }