1 /* $Id: testlib.c,v 1.28 2005-11-09 12:01:09 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(const char *cfgname)
100 const char *srcdir = get_srcdir();
102 if (!cfgname || ! *cfgname )
105 sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
106 zs = zebra_start(cfg);
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 yaz_log(log_level, "init_data: zebra_init failed with %d", i);
148 printf("init_data failed with %d\n", i);
149 zebra_result(zh, &i, &addinfo);
150 yaz_log(log_level, "Error %d %s", i, addinfo);
151 printf(" Error %d %s\n", i, addinfo);
156 zebra_begin_trans (zh, 1);
157 for (i = 0; recs[i]; i++)
158 zebra_add_record(zh, recs[i], strlen(recs[i]));
165 int do_query_x(int lineno, ZebraHandle zh, const char *query, zint exphits,
169 YAZ_PQF_Parser parser;
171 const char *setname="rsetname";
175 yaz_log(log_level, "======================================");
176 yaz_log(log_level, "qry[%d]: %s", lineno, query);
177 odr = odr_createmem (ODR_DECODE);
179 parser = yaz_pqf_create();
180 rpn = yaz_pqf_parse(parser, odr, query);
181 yaz_pqf_destroy(parser);
183 yaz_log(log_level, "PQF Parse failed\n");
184 printf("Error: Parse failed \n%s\n", query);
187 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
191 if (rc != ZEBRA_FAIL)
193 yaz_log(log_level, "search returned %d (OK), but error was "
195 printf("Error: search returned %d (OK), but error was expected\n"
199 code = zebra_errCode(zh);
200 if (code != experror)
202 yaz_log(log_level, "search returned error code %d, but error %d "
203 "was expected", code, experror);
204 printf("Error: search returned error code %d, but error %d was "
206 code, experror, query);
212 if (rc == ZEBRA_FAIL) {
213 int code = zebra_errCode(zh);
214 yaz_log(log_level, "search returned %d. Code %d", rc, code);
216 printf("Error: search returned %d. Code %d\n%s\n", rc,
220 if (exphits != -1 && hits != exphits)
222 yaz_log(log_level, "search returned " ZINT_FORMAT
223 " hits instead of " ZINT_FORMAT, hits, exphits);
224 printf("Error: search returned " ZINT_FORMAT
225 " hits instead of " ZINT_FORMAT "\n%s\n",
226 hits, exphits, query);
235 int do_query(int lineno, ZebraHandle zh, const char *query, zint exphits)
237 return do_query_x(lineno, zh, query, exphits, 0);
240 void do_scan(int lineno, ZebraHandle zh, const char *query,
242 int exp_pos, int exp_num, int exp_partial,
243 const char **exp_entries)
245 ODR odr = odr_createmem(ODR_ENCODE);
246 ZebraScanEntry *entries = 0;
250 yaz_log(log_level, "======================================");
251 yaz_log(log_level, "scan[%d]: pos=%d num=%d %s", lineno, pos, num, query);
253 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
257 printf("Error: scan returned %d (FAIL), but no error was expected\n"
266 printf("Error: scan returned OK, but partial was not set\n"
270 if (partial != exp_partial)
272 printf("Error: scan returned OK, with partial/expected %d/%d\n"
273 "%s\n", partial, exp_partial, query);
278 printf("Error: scan returned OK, with num/expected %d/%d\n"
279 "%s\n", num, exp_num, query);
284 printf("Error: scan returned OK, with pos/expected %d/%d\n"
285 "%s\n", pos, exp_pos, query);
294 for (i = 0; i<num; i++)
296 if (strcmp(exp_entries[i], entries[i].term))
298 printf("Error: scan OK, but entry %d term/exp %s/%s\n"
300 i, entries[i].term, exp_entries[i], query);
312 * makes a query, checks number of hits, and for the first hit, that
313 * it contains the given string, and that it gets the right score
315 void ranking_query(int lineno, ZebraHandle zh, char *query,
316 int exphits, char *firstrec, int firstscore)
318 ZebraRetrievalRecord retrievalRecord[10];
319 ODR odr_output = odr_createmem (ODR_ENCODE);
320 const char *setname="rsetname";
325 hits = do_query(lineno, zh, query, exphits);
327 for (i = 0; i<10; i++)
328 retrievalRecord[i].position = i+1;
330 rc = zebra_records_retrieve (zh, odr_output, setname, 0,
331 VAL_TEXT_XML, hits, retrievalRecord);
335 printf("Error: retrieve returned %d \n%s\n",rc,query);
339 if (!strstr(retrievalRecord[0].buf, firstrec))
341 printf("Error: Got the wrong record first\n");
342 printf("Expected '%s' but got\n", firstrec);
343 printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
347 if (retrievalRecord[0].score != firstscore)
349 printf("Error: first rec got score %d instead of %d\n",
350 retrievalRecord[0].score, firstscore);
353 odr_destroy (odr_output);
356 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
359 ZebraMetaRecord *meta;
360 ODR odr_output = odr_createmem (ODR_ENCODE);
361 const char *setname="rsetname";
362 zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
366 hits = do_query(lineno, zh, query, exphits);
368 for (i = 0; i<exphits; i++)
371 meta = zebra_meta_records_create (zh, setname, exphits, positions);
375 printf("Error: retrieve returned error\n%s\n", query);
379 for (i = 0; i<exphits; i++)
381 if (meta[i].sysno != ids[i])
383 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
384 ids[i], meta[i].sysno);
388 zebra_meta_records_destroy(zh, meta, exphits);
389 odr_destroy (odr_output);
393 void do_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
395 ZebraMetaRecord *recs;
398 zint min_val_recs = 0;
399 zint min_val_exp = 0;
402 do_query(__LINE__, zh, query, hits);
404 recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
407 fprintf(stderr, "recs==0\n");
410 /* find min for each sequence to get proper base offset */
411 for (i = 0; i<hits; i++)
413 if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
414 min_val_recs = recs[i].sysno;
415 if (min_val_exp == 0 || exp[i] < min_val_exp)
416 min_val_exp = exp[i];
419 /* compare sequences using base offset */
420 for (i = 0; i<hits; i++)
421 if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
425 printf("Sequence not in right order for query\n%s\ngot exp\n",
427 for (i = 0; i<hits; i++)
428 printf(" " ZINT_FORMAT " " ZINT_FORMAT "\n",
429 recs[i].sysno, exp[i]);
431 zebra_meta_records_destroy (zh, recs, 4);
443 static void filter_cb(void *cd, const char *name)
445 struct finfo *f = (struct finfo*) cd;
446 if (!strcmp(f->name, name))
450 void check_filter(ZebraService zs, const char *name)
456 zebra_filter_info(zs, &f, filter_cb);
459 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);