1 # $Id: 22-query.t,v 1.10 2006-11-02 17:04:36 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 22-query.t'
8 use Test::More tests => 41;
9 BEGIN { use_ok('ZOOM') };
11 #ZOOM::Log::init_level(ZOOM::Log::mask_str("zoom"));
14 eval { $q = new ZOOM::Query() };
15 ok(defined $@ && $@ =~ /can.t create ZOOM::Query/,
16 "instantiation of ZOOM::Query base class rejected");
18 ok(1, "[no query to destroy]");
20 ok(1, "[no need to recreate 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 $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");
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");
35 eval { $q = new ZOOM::Query::PQF('@and @attr 1=1003 pike @attr 1=21 unix') };
36 ok(!$@, "set PQF into query");
38 eval { $q->sortby("") };
39 ok($@ && $@->isa("ZOOM::Exception") &&
40 $@->code() == ZOOM::Error::SORTBY,
41 "zero-length sort criteria rejected");
43 eval { $q->sortby("foo bar baz") };
44 ok(!$@, "sort criteria accepted");
47 ok(1, "destroyed complex query");
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.
54 my $host = "bagel.indexdata.com/gils";
56 eval { $conn = new ZOOM::Connection($host, 0,
57 preferredRecordSyntax => "usmarc") };
58 ok(!$@, "connection to '$host'");
61 ok(1, "[no need to create empty query]");
62 eval { $q = new ZOOM::Query::PQF('@and @attr 1=4 utah @attr 1=62 epicenter') };
63 ok(!$@, "created PQF query");
64 check_record($conn, $q);
67 # Now try a CQL query: this will fail due to lack of server support
68 ok(1, "[no need to create empty query]");
69 eval { $q = new ZOOM::Query::CQL('title=utah and description=epicenter') };
70 ok(!$@, "created CQL query");
72 eval { $rs = $conn->search($q) };
73 ok($@ && $@->isa("ZOOM::Exception") &&
74 $@->code() == 107 && $@->diagset() eq "Bib-1",
75 "query rejected: error " . $@->code());
78 # Client-side compiled CQL: this will fail due to lack of config-file
79 ok(1, "[no need to create empty query]");
80 eval { $q = new ZOOM::Query::CQL2RPN('title=utah and description=epicenter',
82 ok($@ && $@->isa("ZOOM::Exception") &&
83 $@->code() == ZOOM::Error::CQL_TRANSFORM && $@->diagset() eq "ZOOM",
84 "can't make CQL2RPN query: error " . $@->code());
86 # Do a successful client-compiled CQL search
87 ok(1, "[no need to create empty query]");
88 $conn->option(cqlfile => "samples/cql/pqf.properties");
89 eval { $q = new ZOOM::Query::CQL2RPN('title=utah and description=epicenter',
91 ok(!$@, "created CQL2RPN query");
92 check_record($conn, $q);
95 # Client-side compiled CCL: this will fail due to lack of config-file
96 ok(1, "[no need to create empty query]");
97 eval { $q = new ZOOM::Query::CCL2RPN('ti=utah and ab=epicenter', $conn) };
98 ok($@ && $@->isa("ZOOM::Exception") &&
99 $@->code() == ZOOM::Error::CCL_CONFIG && $@->diagset() eq "ZOOM",
100 "can't make CCL2RPN query: error " . $@->code());
102 # Do a successful client-compiled CCL search
103 ok(1, "[no need to create empty query]");
104 $conn->option(cclfile => "samples/ccl/default.bib");
105 eval { $q = new ZOOM::Query::CCL2RPN('ti=utah and ab=epicenter', $conn) };
106 ok(!$@, "created CCL2RPN query");
107 check_record($conn, $q);
111 ok(1, "destroyed all objects");
118 eval { $rs = $conn->search($q) };
123 ok($n == 1, "found 1 record as expected");
125 my $rec = $rs->record(0);
126 ok(1, "got record idenfified by query");
128 my $data = $rec->render();
129 ok(1, "rendered record");
130 ok($data =~ /^035 +\$a ESDD0006$/m, "record is the expected one");