Remove extraneous declaration.
[ZOOM-Perl-moved-to-github.git] / t / 22-query.t
1 # $Id: 22-query.t,v 1.2 2005-11-26 16:58:03 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 => 20;
9 BEGIN { use_ok('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 = "indexdata.com/gils";
53 my $conn;
54 eval { $conn = new ZOOM::Connection($host, 0) };
55 ok(!$@, "connection to '$host'");
56 $conn->option(preferredRecordSyntax => "usmarc");
57
58 ok(1, "[no need to create empty query]");
59 eval { $q = new ZOOM::Query::PQF('@and @attr 1=4 utah @attr 1=62 epicenter') };
60 ok(!$@, "created PQF query");
61
62 my $rs;
63 eval { $rs = $conn->search($q) };
64 ok(!$@, "search");
65
66 my $n = $rs->size();
67 ok($n == 1, "found 1 record as expected");
68
69 my $rec = $rs->record(0);
70 ok(1, "got record idenfified by query");
71
72 my $data = $rec->render();
73 ok(1, "rendered record");
74 ok($data =~ /^035 +\$a ESDD0006$/m, "record is the expected one");
75
76 $rs->destroy();
77 $q->destroy();
78 $conn->destroy();
79 ok(1, "destroyed all objects");