1 /* $Id: testlib.c,v 1.26 2005-08-18 12:50:18 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /** testlib - utilities for the api tests */
28 #if HAVE_SYS_RESOURCE_H
29 #include <sys/resource.h>
37 #include <yaz/pquery.h>
38 #include <idzebra/api.h>
41 /** start_log: open a log file */
42 /* FIXME - parse command line arguments to set log levels etc */
43 int log_level=0; /* not static, t*.c may use it */
45 void start_log(int argc, char **argv)
53 sprintf(logname, "%s.log", argv[0]);
54 yaz_log_init_file(logname);
55 log_level = yaz_log_mask_str_x(argv[0], 0);
57 cmd_level |= yaz_log_mask_str_x(argv[1], 0);
58 yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level | cmd_level);
59 yaz_log(log_level, "starting %s", argv[0]);
63 * start_up : do common start things, and a zebra_start
65 * - build the name of logfile from argv[0], and open it
66 * if no argv passed, do not open a log
67 * - read zebra.cfg from env var srcdir if it exists; otherwise current dir
68 * default to zebra.cfg, if no name is given
70 ZebraService start_up(char *cfgname, int argc, char **argv)
72 #if HAVE_SYS_RESOURCE_H
77 setrlimit(RLIMIT_CPU, &rlim);
81 start_log(argc, argv);
82 return start_service(cfgname);
86 * get_srcdir: return env srcdir or . (if does does not exist)
88 const char *get_srcdir()
90 const char *srcdir = getenv("srcdir");
91 if (!srcdir || ! *srcdir)
96 /** start_service - do a zebra_start with a decent config name */
97 ZebraService start_service(char *cfgname)
100 const char *srcdir = get_srcdir();
102 if (!cfgname || ! *cfgname )
105 sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
109 printf("zebra_start failed, probably because missing config file \n"
117 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
118 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
126 yaz_log(log_level,"========= Exiting with return code %d", retcode);
128 yaz_log(log_level,"========= All tests OK");
134 /** inits the database and inserts test data */
136 void init_data(ZebraHandle zh, const char **recs)
141 zebra_select_database(zh, "Default");
142 yaz_log(log_level, "going to call init");
144 yaz_log(log_level, "init_data returned %d", i);
147 printf("init_data failed with %d\n",i);
148 zebra_result(zh, &i, &addinfo);
149 printf(" Error %d %s\n", i, addinfo);
154 zebra_begin_trans (zh, 1);
155 for (i = 0; recs[i]; i++)
156 zebra_add_record(zh, recs[i], strlen(recs[i]));
163 int do_query_x(int lineno, ZebraHandle zh, const char *query, zint exphits,
167 YAZ_PQF_Parser parser;
169 const char *setname="rsetname";
173 yaz_log(log_level, "======================================");
174 yaz_log(log_level, "qry[%d]: %s", lineno, query);
175 odr = odr_createmem (ODR_DECODE);
177 parser = yaz_pqf_create();
178 rpn = yaz_pqf_parse(parser, odr, query);
179 yaz_pqf_destroy(parser);
181 printf("Error: Parse failed \n%s\n", query);
184 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
188 if (rc != ZEBRA_FAIL)
190 printf("Error: search returned %d (OK), but error was expected\n"
194 code = zebra_errCode(zh);
195 if (code != experror)
197 printf("Error: search returned error code %d, but error %d was "
199 code, experror, query);
205 if (rc == ZEBRA_FAIL) {
206 int code = zebra_errCode(zh);
207 printf("Error: search returned %d. Code %d\n%s\n", rc,
211 if (exphits != -1 && hits != exphits) {
212 printf("Error: search returned " ZINT_FORMAT
213 " hits instead of " ZINT_FORMAT "\n%s\n",
214 hits, exphits, query);
223 int do_query(int lineno, ZebraHandle zh, const char *query, zint exphits)
225 return do_query_x(lineno, zh, query, exphits, 0);
228 void do_scan(int lineno, ZebraHandle zh, const char *query,
230 int exp_pos, int exp_num, int exp_partial,
231 const char **exp_entries)
233 ODR odr = odr_createmem(ODR_ENCODE);
234 ZebraScanEntry *entries = 0;
238 yaz_log(log_level, "======================================");
239 yaz_log(log_level, "scan[%d]: pos=%d num=%d %s", lineno, pos, num, query);
241 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
245 printf("Error: scan returned %d (FAIL), but no error was expected\n"
254 printf("Error: scan returned OK, but partial was not set\n"
258 if (partial != exp_partial)
260 printf("Error: scan returned OK, with partial/expected %d/%d\n"
261 "%s\n", partial, exp_partial, query);
266 printf("Error: scan returned OK, with num/expected %d/%d\n"
267 "%s\n", num, exp_num, query);
272 printf("Error: scan returned OK, with pos/expected %d/%d\n"
273 "%s\n", pos, exp_pos, query);
282 for (i = 0; i<num; i++)
284 if (strcmp(exp_entries[i], entries[i].term))
286 printf("Error: scan OK, but entry %d term/exp %s/%s\n"
288 i, entries[i].term, exp_entries[i], query);
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 void ranking_query(int lineno, ZebraHandle zh, char *query,
304 int exphits, char *firstrec, int firstscore)
306 ZebraRetrievalRecord retrievalRecord[10];
307 ODR odr_output = odr_createmem (ODR_ENCODE);
308 const char *setname="rsetname";
313 hits = do_query(lineno, zh, query, exphits);
315 for (i = 0; i<10; i++)
316 retrievalRecord[i].position = i+1;
318 rc = zebra_records_retrieve (zh, odr_output, setname, 0,
319 VAL_TEXT_XML, hits, retrievalRecord);
323 printf("Error: retrieve returned %d \n%s\n",rc,query);
327 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);
335 if (retrievalRecord[0].score != firstscore)
337 printf("Error: first rec got score %d instead of %d\n",
338 retrievalRecord[0].score, firstscore);
341 odr_destroy (odr_output);
344 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
347 ZebraMetaRecord *meta;
348 ODR odr_output = odr_createmem (ODR_ENCODE);
349 const char *setname="rsetname";
350 zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
354 hits = do_query(lineno, zh, query, exphits);
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);
367 for (i = 0; i<exphits; i++)
369 if (meta[i].sysno != ids[i])
371 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
372 ids[i], meta[i].sysno);
376 zebra_meta_records_destroy(zh, meta, exphits);
377 odr_destroy (odr_output);
381 void do_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
383 ZebraMetaRecord *recs;
386 zint min_val_recs = 0;
387 zint min_val_exp = 0;
390 do_query(__LINE__, zh, query, hits);
392 recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
395 fprintf(stderr, "recs==0\n");
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);
431 static void filter_cb(void *cd, const char *name)
433 struct finfo *f = (struct finfo*) cd;
434 if (!strcmp(f->name, name))
438 void check_filter(ZebraService zs, const char *name)
444 zebra_filter_info(zs, &f, filter_cb);
447 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);