1 /* This file is part of the Zebra server.
2 Copyright (C) Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /** testlib - utilities for the api tests */
28 #if HAVE_SYS_RESOURCE_H
29 #include <sys/resource.h>
37 #include <yaz/pquery.h>
38 #include <yaz/oid_db.h>
39 #include <idzebra/api.h>
42 int log_level = YLOG_LOG;
45 * tl_start_up : do common start things, and a zebra_start
46 * - build the name of logfile from argv[0], and open it
47 * if no argv passed, do not open a log
48 * - read zebra.cfg from env var srcdir if it exists; otherwise current dir
49 * default to zebra.cfg, if no name is given
51 ZebraService tl_start_up(char *cfgname, int argc, char **argv)
53 #if HAVE_SYS_RESOURCE_H
58 setrlimit(RLIMIT_CPU, &rlim);
61 return tl_zebra_start(cfgname);
65 * get_srcdir: return env srcdir or . (if does does not exist)
67 const char *tl_get_srcdir(void)
69 const char *srcdir = getenv("srcdir");
70 if (!srcdir || ! *srcdir)
75 /** tl_zebra_start - do a zebra_start with a decent config name */
76 ZebraService tl_zebra_start(const char *cfgname)
79 const char *srcdir = tl_get_srcdir();
80 if (!cfgname || ! *cfgname )
83 sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
84 return zebra_start(cfg);
87 /** tl_close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
88 int tl_close_down(ZebraHandle zh, ZebraService zs)
99 /** inits the database and inserts test data */
101 int tl_init_data(ZebraHandle zh, const char **recs)
108 if (zebra_select_database(zh, "Default") != ZEBRA_OK)
111 yaz_log(log_level, "going to call init");
112 res = zebra_init(zh);
113 if (res == ZEBRA_FAIL)
115 yaz_log(log_level, "init_data: zebra_init failed with %d", res);
116 printf("init_data failed with %d\n", res);
122 if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
124 for (i = 0; recs[i]; i++)
126 if (zebra_add_record(zh, recs[i], strlen(recs[i])) != ZEBRA_OK)
128 if (zebra_end_trans(zh) != ZEBRA_OK)
133 if (zebra_end_trans(zh) != ZEBRA_OK)
140 int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
143 YAZ_PQF_Parser parser;
145 const char *setname="rsetname";
149 yaz_log(log_level, "======================================");
150 yaz_log(log_level, "query: %s", query);
151 odr = odr_createmem (ODR_DECODE);
155 parser = yaz_pqf_create();
156 rpn = yaz_pqf_parse(parser, odr, query);
157 yaz_pqf_destroy(parser);
160 yaz_log(log_level, "could not parse pqf query %s\n", query);
161 printf("could not parse pqf query %s\n", query);
166 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
171 if (rc != ZEBRA_FAIL)
173 yaz_log(log_level, "search returned %d (OK), but error was "
175 printf("Error: search returned %d (OK), but error was expected\n"
179 code = zebra_errCode(zh);
180 if (code != experror)
182 yaz_log(log_level, "search returned error code %d, but error %d "
183 "was expected", code, experror);
184 printf("Error: search returned error code %d, but error %d was "
186 code, experror, query);
192 if (rc == ZEBRA_FAIL) {
193 int code = zebra_errCode(zh);
194 yaz_log(log_level, "search returned %d. Code %d", rc, code);
196 printf("Error: search returned %d. Code %d\n%s\n", rc,
200 if (exphits != -1 && hits != exphits)
202 yaz_log(log_level, "search returned " ZINT_FORMAT
203 " hits instead of " ZINT_FORMAT, hits, exphits);
204 printf("Error: search returned " ZINT_FORMAT
205 " hits instead of " ZINT_FORMAT "\n%s\n",
206 hits, exphits, query);
214 int tl_query(ZebraHandle zh, const char *query, zint exphits)
216 return tl_query_x(zh, query, exphits, 0);
219 int tl_scan(ZebraHandle zh, const char *query,
221 int exp_pos, int exp_num, int exp_partial,
222 const char **exp_entries)
225 ODR odr = odr_createmem(ODR_ENCODE);
226 ZebraScanEntry *entries = 0;
230 yaz_log(log_level, "======================================");
231 yaz_log(log_level, "scan: pos=%d num=%d %s", pos, num, query);
233 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
238 printf("Error: scan returned OK, but partial was not set\n"
242 if (partial != exp_partial)
244 printf("Error: scan OK, with partial/expected %d/%d\n",
245 partial, exp_partial);
248 if (res != ZEBRA_OK) /* failure */
252 printf("Error: scan failed, but no error was expected\n");
260 printf("Error: scan OK, but error was expected\n");
268 printf("Error: scan OK, with num/expected %d/%d\n",
274 printf("Error: scan OK, with pos/expected %d/%d\n",
283 for (i = 0; i<num; i++)
285 if (strcmp(exp_entries[i], entries[i].term))
287 printf("Error: scan OK of %s, no %d got=%s exp=%s\n",
288 query, i, entries[i].term, exp_entries[i]);
303 * makes a query, checks number of hits, and for the first hit, that
304 * it contains the given string, and that it gets the right score
306 int tl_ranking_query(ZebraHandle zh, char *query,
307 int exphits, char *firstrec, int firstscore)
309 ZebraRetrievalRecord retrievalRecord[10];
311 const char *setname = "rsetname";
316 if (!tl_query(zh, query, exphits))
319 for (i = 0; i<10; i++)
320 retrievalRecord[i].position = i+1;
322 odr_output = odr_createmem(ODR_ENCODE);
323 rc = zebra_records_retrieve(zh, odr_output, setname, 0,
324 yaz_oid_recsyn_xml, exphits, retrievalRecord);
327 else if (!strstr(retrievalRecord[0].buf, firstrec))
329 printf("Error: Got the wrong record first\n");
330 printf("Expected '%s' but got\n", firstrec);
331 printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
334 else if (retrievalRecord[0].score != firstscore)
336 printf("Error: first rec got score %d instead of %d\n",
337 retrievalRecord[0].score, firstscore);
340 odr_destroy (odr_output);
344 int tl_meta_query(ZebraHandle zh, char *query, int exphits,
347 ZebraMetaRecord *meta;
348 const char *setname= "rsetname";
352 if (!tl_query(zh, query, exphits))
355 positions = (zint *) xmalloc(1 + (exphits * sizeof(zint)));
356 for (i = 0; i<exphits; i++)
359 meta = zebra_meta_records_create(zh, setname, exphits, positions);
363 printf("Error: retrieve returned error\n%s\n", query);
368 for (i = 0; i<exphits; i++)
370 if (meta[i].sysno != ids[i])
372 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
373 ids[i], meta[i].sysno);
377 zebra_meta_records_destroy(zh, meta, exphits);
382 int tl_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
384 ZebraMetaRecord *recs;
387 zint min_val_recs = 0;
388 zint min_val_exp = 0;
391 if (!tl_query(zh, query, hits))
394 recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
398 /* find min for each sequence to get proper base offset */
399 for (i = 0; i<hits; i++)
401 if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
402 min_val_recs = recs[i].sysno;
403 if (min_val_exp == 0 || exp[i] < min_val_exp)
404 min_val_exp = exp[i];
407 /* compare sequences using base offset */
408 for (i = 0; i<hits; i++)
409 if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
413 printf("Sequence not in right order for query\n%s\ngot exp\n",
415 for (i = 0; i<hits; i++)
416 printf(" " ZINT_FORMAT " " ZINT_FORMAT "\n",
417 recs[i].sysno, exp[i]);
419 zebra_meta_records_destroy (zh, recs, 4);
432 static void filter_cb(void *cd, const char *name)
434 struct finfo *f = (struct finfo*) cd;
435 if (!strcmp(f->name, name))
439 void tl_check_filter(ZebraService zs, const char *name)
445 zebra_filter_info(zs, &f, filter_cb);
448 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
453 ZEBRA_RES tl_fetch(ZebraHandle zh, int position, const char *element_set,
454 const Odr_oid * format, ODR odr,
455 const char **rec_buf, size_t *rec_len)
457 ZebraRetrievalRecord retrievalRecord[1];
458 Z_RecordComposition *comp;
461 retrievalRecord[0].position = position;
463 yaz_set_esn(&comp, element_set, odr->mem);
465 res = zebra_records_retrieve(zh, odr, "rsetname", comp, format, 1,
469 int code = zebra_errCode(zh);
470 yaz_log(YLOG_FATAL, "zebra_records_retrieve returned error %d",
475 *rec_buf = retrievalRecord[0].buf;
476 *rec_len = retrievalRecord[0].len;
481 ZEBRA_RES tl_fetch_compare(ZebraHandle zh,
482 int position, const char *element_set,
483 const Odr_oid *format, const char *cmp_rec)
485 const char *rec_buf = 0;
487 ODR odr = odr_createmem(ODR_ENCODE);
488 ZEBRA_RES res = tl_fetch(zh, position, element_set, format, odr,
492 if (strlen(cmp_rec) != rec_len)
494 else if (memcmp(cmp_rec, rec_buf, rec_len))
496 if (res == ZEBRA_FAIL)
499 yaz_log(YLOG_LOG, "Expected: %s", cmp_rec);
500 yaz_log(YLOG_LOG, "Got: %.*s", l, rec_buf);
507 ZEBRA_RES tl_fetch_first_compare(ZebraHandle zh,
508 const char *element_set,
509 const Odr_oid *format, const char *cmp_rec)
511 return tl_fetch_compare(zh, 1, element_set, format, cmp_rec);
517 * c-file-style: "Stroustrup"
518 * indent-tabs-mode: nil
520 * vim: shiftwidth=4 tabstop=8 expandtab