Three more functions tested:
[ZOOM-Perl-moved-to-github.git] / t / 1-Net-Z3950-ZOOM.t
1 # $Id: 1-Net-Z3950-ZOOM.t,v 1.6 2005-10-13 13:31:11 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'");
34
35 Net::Z3950::ZOOM::connection_destroy($conn);
36 ok(1, "destroyed connection");
37
38 my $options = Net::Z3950::ZOOM::options_create();
39 print STDERR "options='$options'\n";
40
41 $conn = Net::Z3950::ZOOM::connection_create($options);
42 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
43 ok($errcode == 0, "unconnected connection object created");
44 Net::Z3950::ZOOM::connection_connect($conn, $host, 0);
45 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
46 ok($errcode == 0, "delayed connection to '$host'");
47
48 my $syntax = "usmarc";
49 Net::Z3950::ZOOM::connection_option_set($conn,
50                                         preferredRecordSyntax => $syntax);
51 my $val = Net::Z3950::ZOOM::connection_option_get($conn,
52                                                   "preferredRecordSyntax");
53 ok($val eq $syntax, "preferred record syntax set to '$val'");
54
55 my $query = '@attr @and 1=4 minerals';
56 my $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
57 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
58 ok($errcode == Net::Z3950::ZOOM::ERROR_INVALID_QUERY,
59    "search for invalid query '$query' fails");
60
61 $query = '@attr 1=4 minerals';
62 $rs = Net::Z3950::ZOOM::connection_search_pqf($conn, $query);
63 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
64 ok($errcode == 0, "search for '$query'");
65
66 my $n = Net::Z3950::ZOOM::resultset_size($rs);
67 ok($n == 1, "found 1 record as expected");
68
69 my $rec = Net::Z3950::ZOOM::resultset_record($rs, 0);
70 my $len = 0;
71 my $data = Net::Z3950::ZOOM::record_get($rec, "render", $len);
72 ok($data =~ /245 +\$a ISOTOPIC DATES OF ROCKS AND MINERALS/,
73    "rendered record has expected title");
74 my $raw = Net::Z3950::ZOOM::record_get($rec, "raw", $len);
75 ok($raw =~ /^00981n/, "raw record contains expected header");
76
77 Net::Z3950::ZOOM::resultset_destroy($rs);
78 ok(1, "destroyed result-set");
79 Net::Z3950::ZOOM::connection_destroy($conn);
80 ok(1, "destroyed connection");