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'
6 use Test::More tests => 41;
7 BEGIN { use_ok('ZOOM') };
9 #ZOOM::Log::init_level(ZOOM::Log::mask_str("zoom"));
12 eval { $q = new ZOOM::Query() };
13 ok(defined $@ && $@ =~ /can.t create ZOOM::Query/,
14 "instantiation of ZOOM::Query base class rejected");
16 ok(1, "[no query to destroy]");
18 ok(1, "[no need to recreate empty query]");
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
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");
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");
33 eval { $q = new ZOOM::Query::PQF('@and @attr 1=1003 pike @attr 1=21 unix') };
34 ok(!$@, "set PQF into query");
36 eval { $q->sortby("") };
37 ok($@ && $@->isa("ZOOM::Exception") &&
38 $@->code() == ZOOM::Error::SORTBY,
39 "zero-length sort criteria rejected");
41 eval { $q->sortby("foo bar baz") };
42 ok(!$@, "sort criteria accepted");
45 ok(1, "destroyed complex query");
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.
52 my $host = "z3950.indexdata.com/gils";
54 eval { $conn = new ZOOM::Connection($host, 0,
55 preferredRecordSyntax => "usmarc") };
56 ok(!$@, "connection to '$host'");
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);
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");
70 eval { $rs = $conn->search($q) };
71 ok($@ && $@->isa("ZOOM::Exception") &&
72 $@->code() == 107 && $@->diagset() eq "Bib-1",
73 "query rejected: error " . $@->code());
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',
80 ok($@ && $@->isa("ZOOM::Exception") &&
81 $@->code() == ZOOM::Error::CQL_TRANSFORM && $@->diagset() eq "ZOOM",
82 "can't make CQL2RPN query: error " . $@->code());
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',
89 ok(!$@, "created CQL2RPN query");
90 check_record($conn, $q);
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());
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);
109 ok(1, "destroyed all objects");
116 eval { $rs = $conn->search($q) };
121 ok($n == 1, "found 1 record as expected");
123 my $rec = $rs->record(0);
124 ok(1, "got record idenfified by query");
126 my $data = $rec->render();
127 ok(1, "rendered record");
128 ok($data =~ /^035 +\$a ESDD0006$/m, "record is the expected one");