Recognise TIMEOUT errors as well as CONNECT errors in the attempt to connect to no...
[ZOOM-Perl-moved-to-github.git] / t / 2-ZOOM.t
1 # $Id: 2-ZOOM.t,v 1.13 2007-09-14 10:36:13 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 2-ZOOM.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 23;
9 BEGIN { use_ok('ZOOM') };
10
11 my $msg = ZOOM::diag_str(ZOOM::Error::INVALID_QUERY);
12 ok($msg eq "Invalid query", "diagnostic string lookup works");
13
14 $msg = ZOOM::diag_srw_str(27);
15 ok($msg eq "Empty term unsupported", "SRW diagnostic string lookup works");
16
17 my $host = "no.such.host";
18 my $conn;
19 eval { $conn = new ZOOM::Connection($host, 0) };
20 # For some reason, Red Hat signals this as a TIMEOUT rather than a CONNECT
21 ok($@ && $@->isa("ZOOM::Exception") &&
22    ($@->code() == ZOOM::Error::CONNECT ||
23     $@->code() == ZOOM::Error::TIMEOUT) && $@->addinfo() eq $host,
24    "connection to non-existent host '$host' fails: \$\@=$@");
25
26 $host = "z3950.indexdata.com/gils";
27 eval { $conn = new ZOOM::Connection($host, 0) };
28 ok(!$@, "connection to '$host'");
29
30 $conn->destroy();
31 ok(1, "destroyed connection");
32
33 eval { $conn = create ZOOM::Connection() };
34 ok(!$@, "unconnected connection object created");
35 eval { $conn->connect($host, 0) };
36 ok(!$@, "delayed connection to '$host'");
37
38 my $val1 = "foo";
39 my $val2 = "$val1\0bar";
40 $conn->option(xyz => $val2);
41 my $val = $conn->option("xyz");
42 ok($val eq $val1, "option() treats value as NUL-terminated");
43 $conn->option_binary(xyz => $val2, length($val2));
44 $val = $conn->option_binary("xyz");
45 ok($val eq $val2, "option_setl() treats value as opaque chunk, val='$val'");
46
47 my $syntax = "usmarc";
48 $conn->option(preferredRecordSyntax => $syntax);
49 $val = $conn->option("preferredRecordSyntax");
50 ok($val eq $syntax, "preferred record syntax set to '$val'");
51
52 my $query = '@attr @and 1=4 minerals';
53 my $rs;
54 eval { $rs = $conn->search_pqf($query) };
55 ok($@ && $@->isa("ZOOM::Exception") &&
56    $@->code() == ZOOM::Error::INVALID_QUERY,
57    "search for invalid query '$query' fails");
58
59 my($xcode, $xmsg, $xinfo, $xset) = $conn->error_x();
60 ok($xcode == $@->code() && $xmsg eq $@->message() && $xinfo eq $@->addinfo() &&
61    $xset eq $@->diagset(), "error_x() consistent with exception");
62 ok($conn->errcode() == $@->code(),
63    "errcode() consistent with exception");
64 ok($conn->errmsg() eq $@->message(),
65    "errmsg() consistent with exception");
66 ok($conn->addinfo() eq $@->addinfo(),
67    "addinfo() consistent with exception");
68 ok($conn->diagset() eq $@->diagset(),
69    "diagset() consistent with exception");
70
71 $query = '@attr 1=4 minerals';
72 eval { $rs = $conn->search_pqf($query) };
73 ok(!$@, "search for '$query'");
74
75 my $n = $rs->size($rs);
76 ok($n == 1, "found 1 record as expected");
77
78 my $rec = $rs->record(0);
79 my $data = $rec->render();
80 ok($data =~ /^245 +\$a ISOTOPIC DATES OF ROCKS AND MINERALS$/m,
81    "rendered record has expected title");
82 my $raw = $rec->raw();
83 ok($raw =~ /^00966n/, "raw record contains expected header");
84
85 $rs->destroy();
86 ok(1, "destroyed result-set");
87 $conn->destroy();
88 ok(1, "destroyed connection");