getl() test emits recovered length in commentary.
[ZOOM-Perl-moved-to-github.git] / t / 1-Net-Z3950-ZOOM.t
1 # $Id: 1-Net-Z3950-ZOOM.t,v 1.10 2005-10-17 16:18:27 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 1-Net-Z3950-ZOOM.t'
5
6 #########################
7
8 # change 'tests => 1' to 'tests => last_test_to_print';
9
10 use strict;
11 use Test::More tests => 21;
12 BEGIN { use_ok('Net::Z3950::ZOOM') };
13
14 #########################
15
16 # Insert your test code below, the Test::More module is use()ed here so read
17 # its man page ( perldoc Test::More ) for help writing this test script.
18
19 my $msg = Net::Z3950::ZOOM::diag_str(Net::Z3950::ZOOM::ERROR_INVALID_QUERY);
20 ok($msg eq "Invalid query", "diagnostic string lookup works");
21
22 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
23
24 my $host = "no.such.host";
25 my $conn = Net::Z3950::ZOOM::connection_new($host, 0);
26 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
27 ok($errcode == Net::Z3950::ZOOM::ERROR_CONNECT && $addinfo eq $host,
28    "connection to non-existent host '$host' fails");
29
30 $host = "indexdata.com/gils";
31 $conn = Net::Z3950::ZOOM::connection_new($host, 0);
32 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
33 ok($errcode == 0, "connection to '$host'");
34
35 Net::Z3950::ZOOM::connection_destroy($conn);
36 ok(1, "destroyed connection");
37
38 my $options = Net::Z3950::ZOOM::options_create();
39 $conn = Net::Z3950::ZOOM::connection_create($options);
40 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
41 ok($errcode == 0, "unconnected connection object created");
42 Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
43 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
44 ok($errcode == 0, "delayed connection to '$host'");
45
46 my $val1 = "foo";
47 my $val2 = "$val1\0bar";
48 Net::Z3950::ZOOM::connection_option_set($conn, xyz => $val2);
49 my $val = Net::Z3950::ZOOM::connection_option_get($conn, "xyz");
50 ok($val eq $val1, "option_set() treats value as NUL-terminated");
51 Net::Z3950::ZOOM::connection_option_setl($conn, xyz => $val2, length($val2));
52 my $vallen = 0;
53 $val = Net::Z3950::ZOOM::connection_option_getl($conn, "xyz", $vallen);
54 ok($val eq $val2, "option_setl() treats value as opaque chunk, val='$val' len=$vallen");
55
56 my $syntax = "usmarc";
57 Net::Z3950::ZOOM::connection_option_set($conn,
58                                         preferredRecordSyntax => $syntax);
59 $val = Net::Z3950::ZOOM::connection_option_get($conn, "preferredRecordSyntax");
60 ok($val eq $syntax, "preferred record syntax set to '$val'");
61
62 my $query = '@attr @and 1=4 minerals';
63 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
64 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
65 ok($errcode == Net::Z3950::ZOOM::ERROR_INVALID_QUERY,
66    "search for invalid query '$query' fails");
67
68 my($xcode, $xmsg, $xinfo, $xset) = (undef, "dummy", "dummy", "dummy");
69 $xcode = Net::Z3950::ZOOM::connection_error_x($conn, $xmsg, $xinfo, $xset);
70 ok($xcode == $errcode && $xmsg eq $errmsg && $xinfo eq $addinfo &&
71    $xset eq "ZOOM", "error_x() consistent with error()");
72 ok(Net::Z3950::ZOOM::connection_errcode($conn) == $errcode,
73    "errcode() consistent with error()");
74 ok(Net::Z3950::ZOOM::connection_errmsg($conn) eq $errmsg,
75    "errmsg() consistent with error()");
76 ok(Net::Z3950::ZOOM::connection_addinfo($conn) eq $addinfo,
77    "addinfo() consistent with error()");
78 ### These is no ZOOM_connection_diagset() -- surely that's a mistake?
79
80 $query = '@attr 1=4 minerals';
81 $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
82 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
83 ok($errcode == 0, "search for '$query'");
84
85 my $n = Net::Z3950::ZOOM::resultset_size($rs);
86 ok($n == 1, "found 1 record as expected");
87
88 my $rec = Net::Z3950::ZOOM::resultset_record($rs, 0);
89 my $len = 0;
90 my $data = Net::Z3950::ZOOM::record_get($rec, "render", $len);
91 ok($data =~ /245 +\$a ISOTOPIC DATES OF ROCKS AND MINERALS/,
92    "rendered record has expected title");
93 my $raw = Net::Z3950::ZOOM::record_get($rec, "raw", $len);
94 ok($raw =~ /^00981n/, "raw record contains expected header");
95
96 Net::Z3950::ZOOM::resultset_destroy($rs);
97 ok(1, "destroyed result-set");
98 Net::Z3950::ZOOM::connection_destroy($conn);
99 ok(1, "destroyed connection");