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