Three new tests of connectionless create and explicit connect.
[ZOOM-Perl-moved-to-github.git] / t / 2-ZOOM.t
1 # $Id: 2-ZOOM.t,v 1.6 2005-10-17 13:50:06 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 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('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 = ZOOM::diag_str(ZOOM::Error::INVALID_QUERY);
20 ok($msg eq "Invalid query", "diagnostic string lookup works");
21
22 my $host = "no.such.host";
23 my $conn;
24 eval { $conn = new ZOOM::Connection($host, 0) };
25 ok($@ && $@->isa("ZOOM::Exception") &&
26    $@->code() == ZOOM::Error::CONNECT && $@->addinfo() eq $host,
27    "connection to non-existent host '$host' fails");
28
29 $host = "indexdata.com/gils";
30 eval { $conn = new ZOOM::Connection($host, 0) };
31 ok(!$@, "connection to '$host'");
32
33 $conn->destroy();
34 ok(1, "destroyed connection");
35
36 my $options = new ZOOM::Options();
37 eval { $conn = create ZOOM::Connection($options) };
38 ok(!$@, "unconnected connection object created");
39 eval { $conn->connect($host, 0) };
40 ok(!$@, "delayed connection to '$host'");
41
42 my $val1 = "foo";
43 my $val2 = "$val1\0bar";
44 $conn->option(xyz => $val2);
45 my $val = $conn->option("xyz");
46 ok($val eq $val1, "option() treats value as NUL-terminated");
47 $conn->option_binary(xyz => $val2, length($val2));
48 $val = $conn->option_binary("xyz");
49 ok($val eq $val2, "option_setl() treats value as opaque chunk, val='$val'");
50
51 my $syntax = "usmarc";
52 $conn->option(preferredRecordSyntax => $syntax);
53 $val = $conn->option("preferredRecordSyntax");
54 ok($val eq $syntax, "preferred record syntax set to '$val'");
55
56 my $query = '@attr @and 1=4 minerals';
57 my $rs;
58 eval { $rs = $conn->search_pqf($query) };
59 ok($@ && $@->isa("ZOOM::Exception") &&
60    $@->code() == ZOOM::Error::INVALID_QUERY,
61    "search for invalid query '$query' fails");
62
63 my($xcode, $xmsg, $xinfo, $xset) = $conn->error_x();
64 ok($xcode == $@->code() && $xmsg eq $@->message() && $xinfo eq $@->addinfo() &&
65    $xset eq "ZOOM", "error_x() consistent with exception");
66 ok($conn->errcode() == $@->code(),
67    "errcode() consistent with exception");
68 ok($conn->errmsg() eq $@->message(),
69    "errmsg() consistent with exception");
70 ok($conn->addinfo() eq $@->addinfo(),
71    "addinfo() consistent with exception");
72 ### No $conn->diagset() yet, due to lack of underlying support
73
74 $query = '@attr 1=4 minerals';
75 eval { $rs = $conn->search_pqf($query) };
76 ok(!$@, "search for '$query'");
77
78 my $n = $rs->size($rs);
79 ok($n == 1, "found 1 record as expected");
80
81 my $rec = $rs->record(0);
82 my $data = $rec->render();
83 ok($data =~ /245 +\$a ISOTOPIC DATES OF ROCKS AND MINERALS/,
84    "rendered record has expected title");
85 my $raw = $rec->raw();
86 ok($raw =~ /^00981n/, "raw record contains expected header");
87
88 $rs->destroy();
89 ok(1, "destroyed result-set");
90 $conn->destroy();
91 ok(1, "destroyed connection");