1 # $Id: 12-query.t,v 1.7 2006-06-15 15:43:19 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 => 41;
9 BEGIN { use_ok('Net::Z3950::ZOOM') };
11 # Net::Z3950::ZOOM::yaz_log_init_level(Net::Z3950::ZOOM::yaz_log_mask_str("zoom"));
13 my $q = Net::Z3950::ZOOM::query_create();
14 ok(defined $q, "create empty query");
16 Net::Z3950::ZOOM::query_destroy($q);
17 ok(1, "destroyed empty query");
19 $q = Net::Z3950::ZOOM::query_create();
20 ok(defined $q, "recreated empty query");
22 # Invalid CQL is not recognised as such, because ZOOM-C does not
23 # attempt to parse it: it just gets passed to the server when the
25 my $res = Net::Z3950::ZOOM::query_cql($q, "creator=pike and");
26 ok($res == 0, "invalid CQL accepted (pass-through)");
27 $res = Net::Z3950::ZOOM::query_cql($q, "creator=pike and subject=unix");
28 ok($res == 0, "valid CQL accepted");
30 $res = Net::Z3950::ZOOM::query_prefix($q, '@and @attr 1=1003 pike');
31 ok($res < 0, "invalid PQF rejected");
32 $res = Net::Z3950::ZOOM::query_prefix($q, '@and @attr 1=1003 pike @attr 1=21 unix');
33 ok($res == 0, "set PQF into query");
35 $res = Net::Z3950::ZOOM::query_sortby($q, "");
36 ok($res < 0, "zero-length sort criteria rejected");
38 $res = Net::Z3950::ZOOM::query_sortby($q, "foo bar baz");
39 ok($res == 0, "sort criteria accepted");
41 Net::Z3950::ZOOM::query_destroy($q);
42 ok(1, "destroyed complex query");
44 # Up till now, we have been doing query management. Now to actually
45 # use the query. This is done using connection_search() -- there are
46 # no other uses of query objects -- but we need to establish a
47 # connection for it to work on first.
49 my $host = "indexdata.com/gils";
50 my $conn = Net::Z3950::ZOOM::connection_new($host, 0);
51 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
52 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
53 ok($errcode == 0, "connection to '$host'");
55 Net::Z3950::ZOOM::connection_option_set($conn,
56 preferredRecordSyntax => "usmarc");
58 $q = Net::Z3950::ZOOM::query_create();
59 ok(defined $q, "create empty query");
60 $res = Net::Z3950::ZOOM::query_prefix($q,
61 '@and @attr 1=4 utah @attr 1=62 epicenter');
62 ok($res == 0, "set PQF into query");
63 check_record($conn, $q);
64 Net::Z3950::ZOOM::query_destroy($q);
66 # Now try a CQL query: this will fail due to lack of server support
67 $q = Net::Z3950::ZOOM::query_create();
68 ok(defined $q, "create empty query");
69 $res = Net::Z3950::ZOOM::query_cql($q, 'title=utah and description=epicenter');
70 ok($res == 0, "valid CQL accepted");
71 my $rs = Net::Z3950::ZOOM::connection_search($conn, $q);
72 my $diagset = "dummy";
73 $errcode = Net::Z3950::ZOOM::connection_error_x($conn, $errmsg, $addinfo,
75 ok($errcode == 107 && $diagset eq "Bib-1",
76 "query rejected: error " . $errcode);
77 Net::Z3950::ZOOM::query_destroy($q);
79 # Client-side compiled CQL: this will fail due to lack of config-file
80 $q = Net::Z3950::ZOOM::query_create();
81 ok(defined $q, "create empty query");
82 $res = Net::Z3950::ZOOM::query_cql2rpn($q,
83 'title=utah and description=epicenter',
85 $errcode = Net::Z3950::ZOOM::connection_error_x($conn, $errmsg, $addinfo,
88 $errcode == Net::Z3950::ZOOM::ERROR_CQL_TRANSFORM &&
90 "can't make CQL2RPN query: error " . $errcode);
91 Net::Z3950::ZOOM::query_destroy($q);
93 # Do a successful client-compiled CQL search
94 $q = Net::Z3950::ZOOM::query_create();
95 ok(defined $q, "create empty query");
96 Net::Z3950::ZOOM::connection_option_set($conn, cqlfile =>
97 "samples/cql/pqf.properties");
98 $res = Net::Z3950::ZOOM::query_cql2rpn($q,
99 'title=utah and description=epicenter',
101 ok($res == 0, "created CQL2RPN query");
102 check_record($conn, $q);
103 Net::Z3950::ZOOM::query_destroy($q);
105 # Client-side compiled CCL: this will fail due to incorrect syntax
106 $q = Net::Z3950::ZOOM::query_create();
107 ok(defined $q, "create empty query");
108 my($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0, "", 0);
110 ### change documentation
111 $res = Net::Z3950::ZOOM::query_ccl2rpn($q,
113 "ti u=4 s=pw\nab u=62 s=pw",
114 $ccl_errcode, $ccl_errstr, $ccl_errpos);
116 $ccl_errcode == Net::Z3950::ZOOM::CCL_ERR_TERM_EXPECTED,
117 "can't make CCL2RPN query: error $ccl_errcode ($ccl_errstr)");
118 Net::Z3950::ZOOM::query_destroy($q);
120 # Do a successful client-compiled CCL search
121 $q = Net::Z3950::ZOOM::query_create();
122 ok(defined $q, "create empty query");
123 Net::Z3950::ZOOM::connection_option_set($conn, cclfile =>
124 "samples/ccl/default.bib");
125 $res = Net::Z3950::ZOOM::query_ccl2rpn($q,
126 'ti=utah and ab=epicenter',
127 "ti u=4 s=pw\nab u=62 s=pw",
131 ok($res == 0, "created CCL2RPN query");
132 check_record($conn, $q);
133 Net::Z3950::ZOOM::query_destroy($q);
135 Net::Z3950::ZOOM::connection_destroy($conn);
136 ok(1, "destroyed all objects");
142 my $rs = Net::Z3950::ZOOM::connection_search($conn, $q);
143 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
144 $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
145 ok($errcode == 0, "search");
147 my $n = Net::Z3950::ZOOM::resultset_size($rs);
148 ok($n == 1, "found 1 record as expected");
150 my $rec = Net::Z3950::ZOOM::resultset_record($rs, 0);
151 ok(1, "got record idenfified by query");
154 my $data = Net::Z3950::ZOOM::record_get($rec, "render", $len);
155 ok(1, "rendered record");
156 ok($data =~ /^035 \$a ESDD0006$/m, "record is the expected one");
158 Net::Z3950::ZOOM::resultset_destroy($rs);