Add diag_str() test.
[ZOOM-Perl-moved-to-github.git] / t / 1-Net-Z3950-ZOOM.t
1 # $Id: 1-Net-Z3950-ZOOM.t,v 1.5 2005-10-12 16:13:34 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 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 => 12;
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' OK");
34
35 my $syntax = "usmarc";
36 Net::Z3950::ZOOM::connection_option_set($conn,
37                                         preferredRecordSyntax => $syntax);
38 my $val = Net::Z3950::ZOOM::connection_option_get($conn,
39                                                   "preferredRecordSyntax");
40 ok($val eq $syntax, "preferred record syntax set to '$val'");
41
42 my $query = '@attr @and 1=4 minerals';
43 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
44 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
45 ok($errcode == Net::Z3950::ZOOM::ERROR_INVALID_QUERY,
46    "search for invalid query '$query' fails");
47
48 $query = '@attr 1=4 minerals';
49 $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
50 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
51 ok($errcode == 0, "search for '$query' OK");
52
53 my $n = Net::Z3950::ZOOM::resultset_size($rs);
54 ok($n == 1, "found 1 record as expected");
55
56 my $rec = Net::Z3950::ZOOM::resultset_record($rs, 0);
57 my $len = 0;
58 my $data = Net::Z3950::ZOOM::record_get($rec, "render", $len);
59 ok($data =~ /245 +\$a ISOTOPIC DATES OF ROCKS AND MINERALS/,
60    "rendered record has expected title");
61 my $raw = Net::Z3950::ZOOM::record_get($rec, "raw", $len);
62 ok($raw =~ /^00981n/, "raw record contains expected header");
63
64 Net::Z3950::ZOOM::resultset_destroy($rs);
65 ok(1, "destroyed result-set");
66 Net::Z3950::ZOOM::connection_destroy($conn);
67 ok(1, "destroyed connection");