1 # $Id: 12-query.t,v 1.3 2005-10-31 15:04:04 mike Exp $
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl 12-query.t'
8 use Test::More tests => 20;
9 BEGIN { use_ok('Net::Z3950::ZOOM') };
11 my $q = Net::Z3950::ZOOM::query_create();
12 ok(defined $q, "create empty query");
14 Net::Z3950::ZOOM::query_destroy($q);
15 ok(1, "destroyed empty query");
17 $q = Net::Z3950::ZOOM::query_create();
18 ok(defined $q, "recreated empty query");
20 # Invalid CQL is not recognised as such, because ZOOM-C does not
21 # attempt to parse it: it just gets passed to the server when the
23 my $res = Net::Z3950::ZOOM::query_cql($q, "creator=pike and");
24 ok($res == 0, "invalid CQL accepted (pass-through)");
25 $res = Net::Z3950::ZOOM::query_cql($q, "creator=pike and subject=unix");
26 ok($res == 0, "valid CQL accepted");
28 $res = Net::Z3950::ZOOM::query_prefix($q, '@and @attr 1=1003 pike');
29 ok($res < 0, "invalid PQF rejected");
30 $res = Net::Z3950::ZOOM::query_prefix($q, '@and @attr 1=1003 pike @attr 1=21 unix');
31 ok($res == 0, "set PQF into query");
33 $res = Net::Z3950::ZOOM::query_sortby($q, "");
34 ok($res < 0, "zero-length sort criteria rejected");
36 $res = Net::Z3950::ZOOM::query_sortby($q, "foo bar baz");
37 ok($res == 0, "sort criteria accepted");
39 Net::Z3950::ZOOM::query_destroy($q);
40 ok(1, "destroyed complex query");
42 # Up till now, we have been doing query management. Now to actually
43 # use the query. This is done using connection_search() -- there are
44 # no other uses of query objects -- but we need to establish a
45 # connection for it to work on first.
47 my $host = "indexdata.com/gils";
48 my $conn = Net::Z3950::ZOOM::connection_new($host, 0);
49 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
50 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
51 ok($errcode == 0, "connection to '$host'");
53 Net::Z3950::ZOOM::connection_option_set($conn,
54 preferredRecordSyntax => "usmarc");
56 $q = Net::Z3950::ZOOM::query_create();
57 ok(defined $q, "create empty query");
58 $res = Net::Z3950::ZOOM::query_prefix($q,
59 '@and @attr 1=4 utah @attr 1=62 epicenter');
60 ok($res == 0, "set PQF into query");
62 my $rs = Net::Z3950::ZOOM::connection_search($conn, $q);
63 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
64 ok($errcode == 0, "search");
66 my $n = Net::Z3950::ZOOM::resultset_size($rs);
67 ok($n == 1, "found 1 record as expected");
69 my $rec = Net::Z3950::ZOOM::resultset_record($rs, 0);
70 ok(1, "got record idenfified by query");
73 my $data = Net::Z3950::ZOOM::record_get($rec, "render", $len);
74 ok(1, "rendered record");
75 ok($data =~ /^035 \$a ESDD0006$/m, "record is the expected one");
77 Net::Z3950::ZOOM::resultset_destroy($rs);
78 Net::Z3950::ZOOM::query_destroy($q);
79 Net::Z3950::ZOOM::connection_destroy($conn);
80 ok(1, "destroyed all objects");