Synchronised with 22-query.t
[ZOOM-Perl-moved-to-github.git] / t / 22-query.t
1 # $Id: 22-query.t,v 1.5 2005-12-22 09:11:30 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 22-query.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 32;
9 BEGIN { use_ok('ZOOM') };
10
11 #ZOOM::Log::init_level(ZOOM::Log::mask_str("zoom"));
12
13 my $q;
14 eval { $q = new ZOOM::Query() };
15 ok(defined $@ && $@ =~ /can.t create ZOOM::Query/,
16    "instantiation of ZOOM::Query base class rejected");
17
18 ok(1, "[no query to destroy]");
19
20 ok(1, "[no need to recreate 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 $q = new ZOOM::Query::CQL("creator=pike and");
26 ok(defined $q, "invalid CQL accepted (pass-through)");
27 $q = new ZOOM::Query::CQL("creator=pike and subject=unix");
28 ok(defined $q, "valid CQL accepted");
29
30 eval { $q = new ZOOM::Query::PQF('@and @attr 1=1003 pike') };
31 ok($@ && $@->isa("ZOOM::Exception") &&
32    $@->code() == ZOOM::Error::QUERY_PQF,
33    "invalid PQF rejected");
34
35 eval { $q = new ZOOM::Query::PQF('@and @attr 1=1003 pike @attr 1=21 unix') };
36 ok(!$@, "set PQF into query");
37
38 eval { $q->sortby("") };
39 ok($@ && $@->isa("ZOOM::Exception") &&
40    $@->code() == ZOOM::Error::SORTBY,
41    "zero-length sort criteria rejected");
42
43 eval { $q->sortby("foo bar baz") };
44 ok(!$@, "sort criteria accepted");
45
46 $q->destroy();
47 ok(1, "destroyed complex query");
48
49 # Up till now, we have been doing query management.  Now to actually
50 # use the query.  This is done using Connection::search() -- there are
51 # no other uses of query objects -- but we need to establish a
52 # connection for it to work on first.
53
54 my $host = "indexdata.com/gils";
55 my $conn;
56 eval { $conn = new ZOOM::Connection($host, 0) };
57 ok(!$@, "connection to '$host'");
58 $conn->option(preferredRecordSyntax => "usmarc");
59
60 ok(1, "[no need to create empty query]");
61 eval { $q = new ZOOM::Query::PQF('@and @attr 1=4 utah @attr 1=62 epicenter') };
62 ok(!$@, "created PQF query");
63 check_record($conn, $q);
64 $q->destroy();
65
66 # Now try a CQL query: this will fail due to lack of server support
67 ok(1, "[no need to create empty query]");
68 eval { $q = new ZOOM::Query::CQL('title=utah and description=epicenter') };
69 ok(!$@, "created CQL query");
70 check_failure($conn, $q, 107, "Bib-1");
71 $q->destroy();
72
73 # Client-side compiled CQL: this will fail due to lack of config-file
74 ok(1, "[no need to create empty query]");
75 eval { $q = new ZOOM::Query::CQL2RPN('title=utah and description=epicenter',
76                                      $conn) };
77 ok($@ && $@->isa("ZOOM::Exception") &&
78    $@->code() == ZOOM::Error::CQL_TRANSFORM && $@->diagset() eq "ZOOM",
79    "can't make CQL2RPN query: error " . $@->code());
80
81 # Finally, do a successful client-compiled CQL search
82 ok(1, "[no need to create empty query]");
83 $conn->option(cqlfile => "samples/cql/pqf.properties");
84 eval { $q = new ZOOM::Query::CQL2RPN('title=utah and description=epicenter',
85                                      $conn) };
86 ok(!$@, "created CQL2RPN query: \@=$@");
87 check_record($conn, $q);
88 $q->destroy();
89
90 $conn->destroy();
91 ok(1, "destroyed all objects");
92
93
94 sub check_record {
95     my($conn, $q) = @_;
96
97     my $rs;
98     eval { $rs = $conn->search($q) };
99     ok(!$@, "search");
100     die $@ if $@;
101
102     my $n = $rs->size();
103     ok($n == 1, "found 1 record as expected");
104
105     my $rec = $rs->record(0);
106     ok(1, "got record idenfified by query");
107
108     my $data = $rec->render();
109     ok(1, "rendered record");
110     ok($data =~ /^035 +\$a ESDD0006$/m, "record is the expected one");
111
112     $rs->destroy();
113 }
114
115
116 sub check_failure {
117     my($conn, $q, $expected_error, $expected_dset) = @_;
118
119     my $rs;
120     eval { $rs = $conn->search($q) };
121     ok($@ && $@->isa("ZOOM::Exception") &&
122        $@->code() == $expected_error && $@->diagset() eq $expected_dset,
123        "query rejected: error " . $@->code());
124 }