New.
[ZOOM-Perl-moved-to-github.git] / t / 22-query.t
1 # $Id: 22-query.t,v 1.1 2005-10-31 15:11:08 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
21
22
23
24
25
26
27 my $res;                        ### not needed in OO interface
28
29
30
31
32
33
34 # Invalid CQL is not recognised as such, because ZOOM-C does not
35 # attempt to parse it: it just gets passed to the server when the
36 # query is used.
37 $q = new ZOOM::Query::CQL("creator=pike and");
38 ok(defined $q, "invalid CQL accepted (pass-through)");
39 $q = new ZOOM::Query::CQL("creator=pike and subject=unix");
40 ok(defined $q, "valid CQL accepted");
41
42 eval { $q = new ZOOM::Query::PQF('@and @attr 1=1003 pike') };
43 ok($@ && $@->isa("ZOOM::Exception") &&
44    $@->code() == ZOOM::Error::QUERY_PQF,
45    "invalid PQF rejected");
46
47 eval { $q = new ZOOM::Query::PQF('@and @attr 1=1003 pike @attr 1=21 unix') };
48 ok(!$@, "set PQF into query");
49
50 eval { $q->sortby("") };
51 ok($@ && $@->isa("ZOOM::Exception") &&
52    $@->code() == ZOOM::Error::SORTBY,
53    "zero-length sort criteria rejected");
54
55 eval { $q->sortby("foo bar baz") };
56 ok(!$@, "sort criteria accepted");
57
58 $q->destroy();
59 ok(1, "destroyed complex query");
60
61 # Up till now, we have been doing query management.  Now to actually
62 # use the query.  This is done using Connection::search() -- there are
63 # no other uses of query objects -- but we need to establish a
64 # connection for it to work on first.
65
66 my $host = "indexdata.com/gils";
67 my $conn;
68 eval { $conn = new ZOOM::Connection($host, 0) };
69 ok(!$@, "connection to '$host'");
70 $conn->option(preferredRecordSyntax => "usmarc");
71
72 ok(1, "[no need to create empty query]");
73 eval { $q = new ZOOM::Query::PQF('@and @attr 1=4 utah @attr 1=62 epicenter') };
74 ok(!$@, "created PQF query");
75
76 my $rs;
77 eval { $rs = $conn->search($q) };
78 ok(!$@, "search");
79
80 my $n = $rs->size();
81 ok($n == 1, "found 1 record as expected");
82
83 my $rec = $rs->record(0);
84 ok(1, "got record idenfified by query");
85
86 my $data = $rec->render();
87 ok(1, "rendered record");
88 ok($data =~ /^035 +\$a ESDD0006$/m, "record is the expected one");
89
90 $rs->destroy();
91 $q->destroy();
92 $conn->destroy();
93 ok(1, "destroyed all objects");