1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 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 */
25 #if HAVE_SYS_RESOURCE_H
26 #include <sys/resource.h>
34 #include <yaz/pquery.h>
35 #include <yaz/oid_db.h>
36 #include <idzebra/api.h>
39 int log_level = YLOG_LOG;
42 * tl_start_up : do common start things, and a zebra_start
43 * - build the name of logfile from argv[0], and open it
44 * if no argv passed, do not open a log
45 * - read zebra.cfg from env var srcdir if it exists; otherwise current dir
46 * default to zebra.cfg, if no name is given
48 ZebraService tl_start_up(char *cfgname, int argc, char **argv)
50 #if HAVE_SYS_RESOURCE_H
55 setrlimit(RLIMIT_CPU, &rlim);
58 return tl_zebra_start(cfgname);
62 * get_srcdir: return env srcdir or . (if does does not exist)
64 const char *tl_get_srcdir(void)
66 const char *srcdir = getenv("srcdir");
67 if (!srcdir || ! *srcdir)
72 /** tl_zebra_start - do a zebra_start with a decent config name */
73 ZebraService tl_zebra_start(const char *cfgname)
76 const char *srcdir = tl_get_srcdir();
77 if (!cfgname || ! *cfgname )
80 sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
81 return zebra_start(cfg);
84 /** tl_close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
85 int tl_close_down(ZebraHandle zh, ZebraService zs)
96 /** inits the database and inserts test data */
98 int tl_init_data(ZebraHandle zh, const char **recs)
105 if (zebra_select_database(zh, "Default") != ZEBRA_OK)
108 yaz_log(log_level, "going to call init");
109 res = zebra_init(zh);
110 if (res == ZEBRA_FAIL)
112 yaz_log(log_level, "init_data: zebra_init failed with %d", res);
113 printf("init_data failed with %d\n", res);
119 if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
121 for (i = 0; recs[i]; i++)
123 if (zebra_add_record(zh, recs[i], strlen(recs[i])) != ZEBRA_OK)
125 if (zebra_end_trans(zh) != ZEBRA_OK)
130 if (zebra_end_trans(zh) != ZEBRA_OK)
137 int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
140 YAZ_PQF_Parser parser;
142 const char *setname="rsetname";
146 yaz_log(log_level, "======================================");
147 yaz_log(log_level, "query: %s", query);
148 odr = odr_createmem (ODR_DECODE);
152 parser = yaz_pqf_create();
153 rpn = yaz_pqf_parse(parser, odr, query);
154 yaz_pqf_destroy(parser);
157 yaz_log(log_level, "could not parse pqf query %s\n", query);
158 printf("could not parse pqf query %s\n", query);
163 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
168 if (rc != ZEBRA_FAIL)
170 yaz_log(log_level, "search returned %d (OK), but error was "
172 printf("Error: search returned %d (OK), but error was expected\n"
176 code = zebra_errCode(zh);
177 if (code != experror)
179 yaz_log(log_level, "search returned error code %d, but error %d "
180 "was expected", code, experror);
181 printf("Error: search returned error code %d, but error %d was "
183 code, experror, query);
189 if (rc == ZEBRA_FAIL) {
190 int code = zebra_errCode(zh);
191 yaz_log(log_level, "search returned %d. Code %d", rc, code);
193 printf("Error: search returned %d. Code %d\n%s\n", rc,
197 if (exphits != -1 && hits != exphits)
199 yaz_log(log_level, "search returned " ZINT_FORMAT
200 " hits instead of " ZINT_FORMAT, hits, exphits);
201 printf("Error: search returned " ZINT_FORMAT
202 " hits instead of " ZINT_FORMAT "\n%s\n",
203 hits, exphits, query);
211 int tl_query(ZebraHandle zh, const char *query, zint exphits)
213 return tl_query_x(zh, query, exphits, 0);
216 int tl_scan(ZebraHandle zh, const char *query,
218 int exp_pos, int exp_num, int exp_partial,
219 const char **exp_entries)
222 ODR odr = odr_createmem(ODR_ENCODE);
223 ZebraScanEntry *entries = 0;
227 yaz_log(log_level, "======================================");
228 yaz_log(log_level, "scan: pos=%d num=%d %s", pos, num, query);
230 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
235 printf("Error: scan returned OK, but partial was not set\n"
239 if (partial != exp_partial)
241 printf("Error: scan OK, with partial/expected %d/%d\n",
242 partial, exp_partial);
245 if (res != ZEBRA_OK) /* failure */
249 printf("Error: scan failed, but no error was expected\n");
257 printf("Error: scan OK, but error was expected\n");
265 printf("Error: scan OK, with num/expected %d/%d\n",
271 printf("Error: scan OK, with pos/expected %d/%d\n",
280 for (i = 0; i<num; i++)
282 if (strcmp(exp_entries[i], entries[i].term))
284 printf("Error: scan OK of %s, no %d got=%s exp=%s\n",
285 query, i, entries[i].term, exp_entries[i]);
300 * makes a query, checks number of hits, and for the first hit, that
301 * it contains the given string, and that it gets the right score
303 int tl_ranking_query(ZebraHandle zh, char *query,
304 int exphits, char *firstrec, int firstscore)
306 ZebraRetrievalRecord retrievalRecord[10];
308 const char *setname = "rsetname";
313 if (!tl_query(zh, query, exphits))
316 for (i = 0; i<10; i++)
317 retrievalRecord[i].position = i+1;
319 odr_output = odr_createmem(ODR_ENCODE);
320 rc = zebra_records_retrieve(zh, odr_output, setname, 0,
321 yaz_oid_recsyn_xml, exphits, retrievalRecord);
324 else if (!strstr(retrievalRecord[0].buf, firstrec))
326 printf("Error: Got the wrong record first\n");
327 printf("Expected '%s' but got\n", firstrec);
328 printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
331 else if (retrievalRecord[0].score != firstscore)
333 printf("Error: first rec got score %d instead of %d\n",
334 retrievalRecord[0].score, firstscore);
337 odr_destroy (odr_output);
341 int tl_meta_query(ZebraHandle zh, char *query, int exphits,
344 ZebraMetaRecord *meta;
345 const char *setname= "rsetname";
349 if (!tl_query(zh, query, exphits))
352 positions = (zint *) xmalloc(1 + (exphits * sizeof(zint)));
353 for (i = 0; i<exphits; i++)
356 meta = zebra_meta_records_create(zh, setname, exphits, positions);
360 printf("Error: retrieve returned error\n%s\n", query);
365 for (i = 0; i<exphits; i++)
367 if (meta[i].sysno != ids[i])
369 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
370 ids[i], meta[i].sysno);
374 zebra_meta_records_destroy(zh, meta, exphits);
379 int tl_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
381 ZebraMetaRecord *recs;
384 zint min_val_recs = 0;
385 zint min_val_exp = 0;
388 if (!tl_query(zh, query, hits))
391 recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
395 /* find min for each sequence to get proper base offset */
396 for (i = 0; i<hits; i++)
398 if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
399 min_val_recs = recs[i].sysno;
400 if (min_val_exp == 0 || exp[i] < min_val_exp)
401 min_val_exp = exp[i];
404 /* compare sequences using base offset */
405 for (i = 0; i<hits; i++)
406 if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
410 printf("Sequence not in right order for query\n%s\ngot exp\n",
412 for (i = 0; i<hits; i++)
413 printf(" " ZINT_FORMAT " " ZINT_FORMAT "\n",
414 recs[i].sysno, exp[i]);
416 zebra_meta_records_destroy (zh, recs, 4);
429 static void filter_cb(void *cd, const char *name)
431 struct finfo *f = (struct finfo*) cd;
432 if (!strcmp(f->name, name))
436 void tl_check_filter(ZebraService zs, const char *name)
442 zebra_filter_info(zs, &f, filter_cb);
445 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
450 ZEBRA_RES tl_fetch(ZebraHandle zh, int position, const char *element_set,
451 const Odr_oid * format, ODR odr,
452 const char **rec_buf, size_t *rec_len)
454 ZebraRetrievalRecord retrievalRecord[1];
455 Z_RecordComposition *comp;
458 retrievalRecord[0].position = position;
460 yaz_set_esn(&comp, element_set, odr->mem);
462 res = zebra_records_retrieve(zh, odr, "rsetname", comp, format, 1,
466 int code = zebra_errCode(zh);
467 yaz_log(YLOG_FATAL, "zebra_records_retrieve returned error %d",
472 *rec_buf = retrievalRecord[0].buf;
473 *rec_len = retrievalRecord[0].len;
478 ZEBRA_RES tl_fetch_compare(ZebraHandle zh,
479 int position, const char *element_set,
480 const Odr_oid *format, const char *cmp_rec)
482 const char *rec_buf = 0;
484 ODR odr = odr_createmem(ODR_ENCODE);
485 ZEBRA_RES res = tl_fetch(zh, position, element_set, format, odr,
489 if (strlen(cmp_rec) != rec_len)
491 else if (memcmp(cmp_rec, rec_buf, rec_len))
493 if (res == ZEBRA_FAIL)
496 yaz_log(YLOG_LOG, "Expected: %s", cmp_rec);
497 yaz_log(YLOG_LOG, "Got: %.*s", l, rec_buf);
504 ZEBRA_RES tl_fetch_first_compare(ZebraHandle zh,
505 const char *element_set,
506 const Odr_oid *format, const char *cmp_rec)
508 return tl_fetch_compare(zh, 1, element_set, format, cmp_rec);
514 * c-file-style: "Stroustrup"
515 * indent-tabs-mode: nil
517 * vim: shiftwidth=4 tabstop=8 expandtab