Nine new tests for client-side CCL support.
[ZOOM-Perl-moved-to-github.git] / t / 12-query.t
1 # $Id: 12-query.t,v 1.6 2006-06-13 16:15:12 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 12-query.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 41;
9 BEGIN { use_ok('Net::Z3950::ZOOM') };
10
11 # Net::Z3950::ZOOM::yaz_log_init_level(Net::Z3950::ZOOM::yaz_log_mask_str("zoom"));
12
13 my $q = Net::Z3950::ZOOM::query_create();
14 ok(defined $q, "create empty query");
15
16 Net::Z3950::ZOOM::query_destroy($q);
17 ok(1, "destroyed empty query");
18
19 $q = Net::Z3950::ZOOM::query_create();
20 ok(defined $q, "recreated empty query");
21
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
24 # query is used.
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");
29
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");
34
35 $res = Net::Z3950::ZOOM::query_sortby($q, "");
36 ok($res < 0, "zero-length sort criteria rejected");
37
38 $res = Net::Z3950::ZOOM::query_sortby($q, "foo bar baz");
39 ok($res == 0, "sort criteria accepted");
40
41 Net::Z3950::ZOOM::query_destroy($q);
42 ok(1, "destroyed complex query");
43
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.
48
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'");
54
55 Net::Z3950::ZOOM::connection_option_set($conn,
56                                         preferredRecordSyntax => "usmarc");
57
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);
65
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,
74                                                 $diagset);
75 ok($errcode == 107 && $diagset eq "Bib-1",
76    "query rejected: error " . $errcode);
77 Net::Z3950::ZOOM::query_destroy($q);
78
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',
84                                        $conn);
85 $errcode = Net::Z3950::ZOOM::connection_error_x($conn, $errmsg, $addinfo,
86                                                 $diagset);
87 ok($res < 0 &&
88    $errcode == Net::Z3950::ZOOM::ERROR_CQL_TRANSFORM &&
89    $diagset eq "ZOOM",
90    "can't make CQL2RPN query: error " . $errcode);
91 Net::Z3950::ZOOM::query_destroy($q);
92
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',
100                                        $conn);
101 ok($res == 0, "created CQL2RPN query");
102 check_record($conn, $q);
103 Net::Z3950::ZOOM::query_destroy($q);
104
105 # Client-side compiled CCL: this will fail due to lack of qualifier-file
106 $q = Net::Z3950::ZOOM::query_create();
107 ok(defined $q, "create empty query");
108 $res = Net::Z3950::ZOOM::query_ccl2rpn($q,
109                                        'ti=utah and ab=epicenter',
110                                        $conn);
111 $errcode = Net::Z3950::ZOOM::connection_error_x($conn, $errmsg, $addinfo,
112                                                 $diagset);
113 ok($res < 0 &&
114    $errcode == Net::Z3950::ZOOM::ERROR_CCL_CONFIG &&
115    $diagset eq "ZOOM",
116    "can't make CCL2RPN query: error " . $errcode);
117 Net::Z3950::ZOOM::query_destroy($q);
118
119 # Do a successful client-compiled CCL search
120 $q = Net::Z3950::ZOOM::query_create();
121 ok(defined $q, "create empty query");
122 Net::Z3950::ZOOM::connection_option_set($conn, cclfile =>
123                                         "samples/ccl/default.bib");
124 $res = Net::Z3950::ZOOM::query_ccl2rpn($q,
125                                        'ti=utah and ab=epicenter',
126                                        $conn);
127 ok($res == 0, "created CCL2RPN query");
128 check_record($conn, $q);
129 Net::Z3950::ZOOM::query_destroy($q);
130
131 Net::Z3950::ZOOM::connection_destroy($conn);
132 ok(1, "destroyed all objects");
133
134
135 sub check_record {
136     my($conn, $q) = @_;
137
138     my $rs = Net::Z3950::ZOOM::connection_search($conn, $q);
139     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
140     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
141     ok($errcode == 0, "search");
142
143     my $n = Net::Z3950::ZOOM::resultset_size($rs);
144     ok($n == 1, "found 1 record as expected");
145
146     my $rec = Net::Z3950::ZOOM::resultset_record($rs, 0);
147     ok(1, "got record idenfified by query");
148
149     my $len = 0;
150     my $data = Net::Z3950::ZOOM::record_get($rec, "render", $len);
151     ok(1, "rendered record");
152     ok($data =~ /^035    \$a ESDD0006$/m, "record is the expected one");
153
154     Net::Z3950::ZOOM::resultset_destroy($rs);
155 }