1 /* $Id: testlib.c,v 1.29 2005-12-15 13:28:32 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);
107 TL_ASSERT2(zs, "zebra_start_failed. Missing config file?");
112 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
113 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
121 yaz_log(log_level,"========= Exiting with return code %d", retcode);
123 yaz_log(log_level,"========= All tests OK");
129 /** inits the database and inserts test data */
131 void init_data(ZebraHandle zh, const char **recs)
136 zebra_select_database(zh, "Default");
137 yaz_log(log_level, "going to call init");
139 yaz_log(log_level, "init_data returned %d", i);
142 yaz_log(log_level, "init_data: zebra_init failed with %d", i);
143 printf("init_data failed with %d\n", i);
144 zebra_result(zh, &i, &addinfo);
145 yaz_log(log_level, "Error %d %s", i, addinfo);
146 printf(" Error %d %s\n", i, addinfo);
151 zebra_begin_trans (zh, 1);
152 for (i = 0; recs[i]; i++)
153 zebra_add_record(zh, recs[i], strlen(recs[i]));
160 int do_query_x(int lineno, ZebraHandle zh, const char *query, zint exphits,
164 YAZ_PQF_Parser parser;
166 const char *setname="rsetname";
170 yaz_log(log_level, "======================================");
171 yaz_log(log_level, "qry[%d]: %s", lineno, query);
172 odr = odr_createmem (ODR_DECODE);
174 parser = yaz_pqf_create();
175 rpn = yaz_pqf_parse(parser, odr, query);
176 yaz_pqf_destroy(parser);
177 TL_ASSERT2(rpn, "Parse of pqf failed");
178 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
182 if (rc != ZEBRA_FAIL)
184 yaz_log(log_level, "search returned %d (OK), but error was "
186 printf("Error: search returned %d (OK), but error was expected\n"
188 TL_ASSERT(rc == ZEBRA_FAIL);
190 code = zebra_errCode(zh);
191 if (code != experror)
193 yaz_log(log_level, "search returned error code %d, but error %d "
194 "was expected", code, experror);
195 printf("Error: search returned error code %d, but error %d was "
197 code, experror, query);
198 TL_ASSERT(code == experror);
203 if (rc == ZEBRA_FAIL) {
204 int code = zebra_errCode(zh);
205 yaz_log(log_level, "search returned %d. Code %d", rc, code);
207 printf("Error: search returned %d. Code %d\n%s\n", rc,
211 if (exphits != -1 && hits != exphits)
213 yaz_log(log_level, "search returned " ZINT_FORMAT
214 " hits instead of " ZINT_FORMAT, hits, exphits);
215 printf("Error: search returned " ZINT_FORMAT
216 " hits instead of " ZINT_FORMAT "\n%s\n",
217 hits, exphits, query);
226 int do_query(int lineno, ZebraHandle zh, const char *query, zint exphits)
228 return do_query_x(lineno, zh, query, exphits, 0);
231 void do_scan(int lineno, ZebraHandle zh, const char *query,
233 int exp_pos, int exp_num, int exp_partial,
234 const char **exp_entries)
236 ODR odr = odr_createmem(ODR_ENCODE);
237 ZebraScanEntry *entries = 0;
241 yaz_log(log_level, "======================================");
242 yaz_log(log_level, "scan[%d]: pos=%d num=%d %s", lineno, pos, num, query);
244 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
248 printf("Error: scan returned %d (FAIL), but no error was expected\n"
257 printf("Error: scan returned OK, but partial was not set\n"
261 if (partial != exp_partial)
263 printf("Error: scan returned OK, with partial/expected %d/%d\n"
264 "%s\n", partial, exp_partial, query);
269 printf("Error: scan returned OK, with num/expected %d/%d\n"
270 "%s\n", num, exp_num, query);
275 printf("Error: scan returned OK, with pos/expected %d/%d\n"
276 "%s\n", pos, exp_pos, query);
285 for (i = 0; i<num; i++)
287 if (strcmp(exp_entries[i], entries[i].term))
289 printf("Error: scan OK, but entry %d term/exp %s/%s\n"
291 i, entries[i].term, exp_entries[i], query);
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 void ranking_query(int lineno, ZebraHandle zh, char *query,
307 int exphits, char *firstrec, int firstscore)
309 ZebraRetrievalRecord retrievalRecord[10];
310 ODR odr_output = odr_createmem (ODR_ENCODE);
311 const char *setname="rsetname";
316 hits = do_query(lineno, zh, query, exphits);
318 for (i = 0; i<10; i++)
319 retrievalRecord[i].position = i+1;
321 rc = zebra_records_retrieve (zh, odr_output, setname, 0,
322 VAL_TEXT_XML, hits, retrievalRecord);
326 printf("Error: retrieve returned %d \n%s\n",rc,query);
330 if (!strstr(retrievalRecord[0].buf, firstrec))
332 printf("Error: Got the wrong record first\n");
333 printf("Expected '%s' but got\n", firstrec);
334 printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
338 if (retrievalRecord[0].score != firstscore)
340 printf("Error: first rec got score %d instead of %d\n",
341 retrievalRecord[0].score, firstscore);
344 odr_destroy (odr_output);
347 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
350 ZebraMetaRecord *meta;
351 ODR odr_output = odr_createmem (ODR_ENCODE);
352 const char *setname="rsetname";
353 zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
357 hits = do_query(lineno, zh, query, exphits);
359 for (i = 0; i<exphits; i++)
362 meta = zebra_meta_records_create (zh, setname, exphits, positions);
366 printf("Error: retrieve returned error\n%s\n", query);
370 for (i = 0; i<exphits; i++)
372 if (meta[i].sysno != ids[i])
374 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
375 ids[i], meta[i].sysno);
379 zebra_meta_records_destroy(zh, meta, exphits);
380 odr_destroy (odr_output);
384 void do_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
386 ZebraMetaRecord *recs;
389 zint min_val_recs = 0;
390 zint min_val_exp = 0;
393 do_query(__LINE__, zh, query, hits);
395 recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
398 fprintf(stderr, "recs==0\n");
401 /* find min for each sequence to get proper base offset */
402 for (i = 0; i<hits; i++)
404 if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
405 min_val_recs = recs[i].sysno;
406 if (min_val_exp == 0 || exp[i] < min_val_exp)
407 min_val_exp = exp[i];
410 /* compare sequences using base offset */
411 for (i = 0; i<hits; i++)
412 if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
416 printf("Sequence not in right order for query\n%s\ngot exp\n",
418 for (i = 0; i<hits; i++)
419 printf(" " ZINT_FORMAT " " ZINT_FORMAT "\n",
420 recs[i].sysno, exp[i]);
422 zebra_meta_records_destroy (zh, recs, 4);
434 static void filter_cb(void *cd, const char *name)
436 struct finfo *f = (struct finfo*) cd;
437 if (!strcmp(f->name, name))
441 void check_filter(ZebraService zs, const char *name)
447 zebra_filter_info(zs, &f, filter_cb);
450 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);