1 /* $Id: testlib.c,v 1.31 2006-04-04 00:02:34 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 tl_start_log(int argc, char **argv)
53 sprintf(logname, "%s.log", argv[0]);
57 yaz_log_init_file(logname);
59 log_level = yaz_log_mask_str_x(argv[1], 0);
61 yaz_log_time_format(argv[2]);
62 yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level | cmd_level);
63 yaz_log(log_level, "starting %s", argv[0]);
67 * tl_start_up : do common start things, and a zebra_start
69 * - build the name of logfile from argv[0], and open it
70 * if no argv passed, do not open a log
71 * - read zebra.cfg from env var srcdir if it exists; otherwise current dir
72 * default to zebra.cfg, if no name is given
74 ZebraService tl_start_up(char *cfgname, int argc, char **argv)
76 #if HAVE_SYS_RESOURCE_H
81 setrlimit(RLIMIT_CPU, &rlim);
85 tl_start_log(argc, argv);
86 return tl_zebra_start(cfgname);
90 * get_srcdir: return env srcdir or . (if does does not exist)
92 const char *tl_get_srcdir()
94 const char *srcdir = getenv("srcdir");
95 if (!srcdir || ! *srcdir)
100 /** tl_zebra_start - do a zebra_start with a decent config name */
101 ZebraService tl_zebra_start(const char *cfgname)
104 const char *srcdir = tl_get_srcdir();
105 if (!cfgname || ! *cfgname )
108 sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
109 return zebra_start(cfg);
112 /** tl_close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
113 int tl_close_down(ZebraHandle zh, ZebraService zs)
125 /** inits the database and inserts test data */
127 int tl_init_data(ZebraHandle zh, const char **recs)
134 if (zebra_select_database(zh, "Default") != ZEBRA_OK)
137 yaz_log(log_level, "going to call init");
138 res = zebra_init(zh);
139 if (res == ZEBRA_FAIL)
141 yaz_log(log_level, "init_data: zebra_init failed with %d", res);
142 printf("init_data failed with %d\n", res);
148 if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
150 for (i = 0; recs[i]; i++)
151 zebra_add_record(zh, recs[i], strlen(recs[i]));
152 if (zebra_end_trans(zh) != ZEBRA_OK)
159 int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
162 YAZ_PQF_Parser parser;
164 const char *setname="rsetname";
168 yaz_log(log_level, "======================================");
169 yaz_log(log_level, "query: %s", query);
170 odr = odr_createmem (ODR_DECODE);
174 parser = yaz_pqf_create();
175 rpn = yaz_pqf_parse(parser, odr, query);
176 yaz_pqf_destroy(parser);
183 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
187 if (rc != ZEBRA_FAIL)
189 yaz_log(log_level, "search returned %d (OK), but error was "
191 printf("Error: search returned %d (OK), but error was expected\n"
195 code = zebra_errCode(zh);
196 if (code != experror)
198 yaz_log(log_level, "search returned error code %d, but error %d "
199 "was expected", code, experror);
200 printf("Error: search returned error code %d, but error %d was "
202 code, experror, query);
208 if (rc == ZEBRA_FAIL) {
209 int code = zebra_errCode(zh);
210 yaz_log(log_level, "search returned %d. Code %d", rc, code);
212 printf("Error: search returned %d. Code %d\n%s\n", rc,
216 if (exphits != -1 && hits != exphits)
218 yaz_log(log_level, "search returned " ZINT_FORMAT
219 " hits instead of " ZINT_FORMAT, hits, exphits);
220 printf("Error: search returned " ZINT_FORMAT
221 " hits instead of " ZINT_FORMAT "\n%s\n",
222 hits, exphits, query);
231 int tl_query(ZebraHandle zh, const char *query, zint exphits)
233 return tl_query_x(zh, query, exphits, 0);
236 int tl_scan(ZebraHandle zh, const char *query,
238 int exp_pos, int exp_num, int exp_partial,
239 const char **exp_entries)
241 ODR odr = odr_createmem(ODR_ENCODE);
242 ZebraScanEntry *entries = 0;
246 yaz_log(log_level, "======================================");
247 yaz_log(log_level, "scan: pos=%d num=%d %s", pos, num, query);
249 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
253 printf("Error: scan returned %d (FAIL), but no error was expected\n"
262 printf("Error: scan returned OK, but partial was not set\n"
266 if (partial != exp_partial)
268 printf("Error: scan returned OK, with partial/expected %d/%d\n"
269 "%s\n", partial, exp_partial, query);
274 printf("Error: scan returned OK, with num/expected %d/%d\n"
275 "%s\n", num, exp_num, query);
280 printf("Error: scan returned OK, with pos/expected %d/%d\n"
281 "%s\n", pos, exp_pos, query);
290 for (i = 0; i<num; i++)
292 if (strcmp(exp_entries[i], entries[i].term))
294 printf("Error: scan OK, but entry %d term/exp %s/%s\n"
296 i, entries[i].term, exp_entries[i], query);
309 * makes a query, checks number of hits, and for the first hit, that
310 * it contains the given string, and that it gets the right score
312 int tl_ranking_query(ZebraHandle zh, char *query,
313 int exphits, char *firstrec, int firstscore)
315 ZebraRetrievalRecord retrievalRecord[10];
316 ODR odr_output = odr_createmem (ODR_ENCODE);
317 const char *setname="rsetname";
321 if (!tl_query(zh, query, exphits))
324 for (i = 0; i<10; i++)
325 retrievalRecord[i].position = i+1;
327 rc = zebra_records_retrieve (zh, odr_output, setname, 0,
328 VAL_TEXT_XML, exphits, retrievalRecord);
332 if (!strstr(retrievalRecord[0].buf, firstrec))
334 printf("Error: Got the wrong record first\n");
335 printf("Expected '%s' but got\n", firstrec);
336 printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
340 if (retrievalRecord[0].score != firstscore)
342 printf("Error: first rec got score %d instead of %d\n",
343 retrievalRecord[0].score, firstscore);
346 odr_destroy (odr_output);
350 int tl_meta_query(ZebraHandle zh, char *query, int exphits,
353 ZebraMetaRecord *meta;
354 ODR odr_output = odr_createmem (ODR_ENCODE);
355 const char *setname="rsetname";
356 zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
359 if (!tl_query(zh, query, exphits))
362 for (i = 0; i<exphits; i++)
365 meta = zebra_meta_records_create (zh, setname, exphits, positions);
369 printf("Error: retrieve returned error\n%s\n", query);
373 for (i = 0; i<exphits; i++)
375 if (meta[i].sysno != ids[i])
377 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
378 ids[i], meta[i].sysno);
382 zebra_meta_records_destroy(zh, meta, exphits);
383 odr_destroy (odr_output);
388 int tl_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
390 ZebraMetaRecord *recs;
393 zint min_val_recs = 0;
394 zint min_val_exp = 0;
397 if (!tl_query(zh, query, hits))
400 recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
404 /* find min for each sequence to get proper base offset */
405 for (i = 0; i<hits; i++)
407 if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
408 min_val_recs = recs[i].sysno;
409 if (min_val_exp == 0 || exp[i] < min_val_exp)
410 min_val_exp = exp[i];
413 /* compare sequences using base offset */
414 for (i = 0; i<hits; i++)
415 if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
419 printf("Sequence not in right order for query\n%s\ngot exp\n",
421 for (i = 0; i<hits; i++)
422 printf(" " ZINT_FORMAT " " ZINT_FORMAT "\n",
423 recs[i].sysno, exp[i]);
425 zebra_meta_records_destroy (zh, recs, 4);
438 static void filter_cb(void *cd, const char *name)
440 struct finfo *f = (struct finfo*) cd;
441 if (!strcmp(f->name, name))
445 void tl_check_filter(ZebraService zs, const char *name)
451 zebra_filter_info(zs, &f, filter_cb);
454 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);