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