2 * Copyright (c) 2004-2007, Index Data.
3 * See the file LICENSE for details.
5 * $Id: test-search-01.cpp,v 1.7 2007-04-12 15:00:33 adam Exp $
9 #include <yaz/pquery.h>
10 #include <yaz/sortspec.h>
13 #include <yaz/oid_db.h>
15 static const char *try_query[] = {
16 "@attr 1=4 petersson",
17 "@attr 1=1016 petersson",
19 "@attr 1=1016 kingdom",
27 "@attr 1=1016 computer",
35 static const char *try_syntax [] = {
46 static const char *try_sort [] = {
54 Zlint_test_search_01::Zlint_test_search_01()
57 m_record_syntax_no = 0;
62 Zlint_test_search_01::~Zlint_test_search_01()
66 Zlint_code Zlint_test_search_01::init(Zlint *z)
69 Z_APDU *apdu = z->create_Z_PDU(Z_APDU_initRequest);
70 Z_InitRequest *init = apdu->u.initRequest;
72 z->msg_check_for("search and retrieve");
74 ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
76 ODR_MASK_SET(init->options, Z_Options_namedResultSets);
77 ODR_MASK_SET(init->options, Z_Options_sort);
79 int r = z->send_Z_PDU(apdu, &len);
82 z->msg_check_fail("unable to send init request");
88 Zlint_code Zlint_test_search_01::sendTest(Zlint *z)
90 if (!m_got_result_set)
92 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_searchRequest);
94 sr = apdu->u.searchRequest;
95 sr->query = (Z_Query *) odr_malloc(z->odr_encode(), sizeof(*sr->query));
96 if (try_query[m_query_no] && sr)
98 sr->query->which = Z_Query_type_1;
100 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
102 z->getDatabase(&sr->databaseNames, &sr->num_databaseNames);
104 rpn = yaz_pqf_parse(pqf_parser, z->odr_encode(),
105 try_query[m_query_no]);
107 yaz_pqf_destroy (pqf_parser);
111 z->msg_check_fail("Query %s invalid", try_query[m_query_no]);
112 return TEST_FINISHED;
115 sr->query->u.type_1 = rpn;
116 z->send_Z_PDU(apdu, &len);
120 z->msg_check_notapp();
121 z->msg_check_info("unable to get any hit count");
122 return TEST_FINISHED;
125 else if (m_got_result_set && try_syntax[m_record_syntax_no])
128 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_presentRequest);
129 Z_PresentRequest *pr = apdu->u.presentRequest;
130 *pr->numberOfRecordsRequested = 1;
131 *pr->resultSetStartPoint = 1;
133 z->msg_check_for("record syntax %s", try_syntax[m_record_syntax_no]);
134 pr->preferredRecordSyntax =
135 yaz_string_to_oid_odr(yaz_oid_std(),
136 CLASS_RECSYN, try_syntax[m_record_syntax_no],
138 z->send_Z_PDU(apdu, &len);
139 return TEST_CONTINUE;
141 else if(m_got_result_set && !try_syntax[m_record_syntax_no])
143 Z_APDU *apdu = zget_APDU(z->odr_encode(), Z_APDU_sortRequest);
144 if (apdu && try_sort[m_sort_no])
146 z->msg_check_for("sort %s", try_sort[m_sort_no]);
148 char *setstring = "default";
150 Z_SortRequest *sr = apdu->u.sortRequest;
152 sr->num_inputResultSetNames = 1;
153 sr->num_inputResultSetNames = 1;
154 sr->inputResultSetNames = (Z_InternationalString **)
155 odr_malloc (z->odr_encode(), sizeof(*sr->inputResultSetNames));
156 sr->inputResultSetNames[0] = odr_strdup (z->odr_encode(), setstring);
157 sr->sortedResultSetName = odr_strdup(z->odr_encode(), setstring);
158 sr->sortSequence = yaz_sort_spec(z->odr_encode(), try_sort[m_sort_no]);
159 z->send_Z_PDU(apdu, &len);
162 return TEST_FINISHED;
166 printf ("finished...\n");
167 return TEST_FINISHED;
169 return TEST_CONTINUE;
172 Zlint_code Zlint_test_search_01::recv_gdu(Zlint *z, Z_GDU *gdu)
174 if (gdu->which == Z_GDU_Z3950 &&
175 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_initResponse)
177 Z_InitResponse *init = gdu->u.z3950->u.initResponse;
178 int result = init->result ? *init->result : 0;
181 z->msg_check_notapp();
182 z->msg_check_info ("init rejected (result false)");
183 return TEST_FINISHED;
187 else if (gdu->which == Z_GDU_Z3950 &&
188 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_searchResponse)
190 Z_SearchResponse *sr = gdu->u.z3950->u.searchResponse;
191 if (sr->records && (sr->records->which == Z_Records_NSD
193 sr->records->which == Z_Records_multipleNSD))
195 else if (!sr->resultCount || *sr->resultCount == 0)
200 z->msg_check_info("got %d result count with %s", *sr->resultCount,
201 try_query[m_query_no]);
202 m_got_result_set = 1;
206 else if (gdu->which == Z_GDU_Z3950 &&
207 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_presentResponse)
209 Z_PresentResponse *sr = gdu->u.z3950->u.presentResponse;
210 if (sr->records && (sr->records->which == Z_Records_NSD
212 sr->records->which == Z_Records_multipleNSD))
215 z->msg_check_info("present returned NSD for %s",
216 try_syntax[m_record_syntax_no]);
218 else if (sr->records && sr->records->which == Z_Records_DBOSD
219 && sr->records->u.databaseOrSurDiagnostics->num_records>0
220 && sr->records->u.databaseOrSurDiagnostics->records[0])
222 if (sr->records->u.databaseOrSurDiagnostics->records[0]->which == Z_NamePlusRecord_databaseRecord)
224 Z_External *ext = sr->records->u.databaseOrSurDiagnostics->records[0]->u.databaseRecord;
225 Odr_oid *expectRecordSyntax =
226 yaz_string_to_oid_odr(
227 yaz_oid_std(), CLASS_RECSYN,
228 try_syntax[m_record_syntax_no], z->odr_decode());
229 if (oid_oidcmp(expectRecordSyntax,
230 ext->direct_reference))
232 z->msg_check_fail("Got Record in different syntax "
233 "from that requested %s",
234 try_syntax[m_record_syntax_no]);
239 else if (sr->records->u.databaseOrSurDiagnostics->records[0]->which == Z_NamePlusRecord_surrogateDiagnostic)
242 z->msg_check_info("present returned SD %s",
243 try_syntax[m_record_syntax_no]);
248 z->msg_check_info("present returned fragment %s",
249 try_syntax[m_record_syntax_no]);
254 z->msg_check_fail("present returned no records or diagnostics");
256 m_record_syntax_no++;
259 else if (gdu->which == Z_GDU_Z3950 &&
260 gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_sortResponse)
262 Z_SortResponse *sr = gdu->u.z3950->u.sortResponse;
265 z->msg_check_info( "sort NSD for %s", try_sort[m_sort_no]);
270 z->msg_check_fail("did not receive init/search/present response "
272 return TEST_FINISHED;
275 Zlint_code Zlint_test_search_01::recv_fail(Zlint *z, int reason)
277 z->msg_check_fail("target closed connection");
278 return TEST_FINISHED;
283 * indent-tabs-mode: nil
285 * vim: shiftwidth=4 tabstop=8 expandtab