1 /* $Id: testlib.c,v 1.30 2006-03-31 15:58:05 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]);
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 * tl_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 tl_start_up(char *cfgname, int argc, char **argv)
72 #if HAVE_SYS_RESOURCE_H
77 setrlimit(RLIMIT_CPU, &rlim);
81 tl_start_log(argc, argv);
82 return tl_zebra_start(cfgname);
86 * get_srcdir: return env srcdir or . (if does does not exist)
88 const char *tl_get_srcdir()
90 const char *srcdir = getenv("srcdir");
91 if (!srcdir || ! *srcdir)
96 /** tl_zebra_start - do a zebra_start with a decent config name */
97 ZebraService tl_zebra_start(const char *cfgname)
100 const char *srcdir = tl_get_srcdir();
101 if (!cfgname || ! *cfgname )
104 sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
105 return zebra_start(cfg);
108 /** tl_close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
109 int tl_close_down(ZebraHandle zh, ZebraService zs)
121 /** inits the database and inserts test data */
123 int tl_init_data(ZebraHandle zh, const char **recs)
130 if (zebra_select_database(zh, "Default") != ZEBRA_OK)
133 yaz_log(log_level, "going to call init");
134 res = zebra_init(zh);
135 if (res == ZEBRA_FAIL)
137 yaz_log(log_level, "init_data: zebra_init failed with %d", res);
138 printf("init_data failed with %d\n", res);
144 if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
146 for (i = 0; recs[i]; i++)
147 zebra_add_record(zh, recs[i], strlen(recs[i]));
148 if (zebra_end_trans(zh) != ZEBRA_OK)
155 int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
158 YAZ_PQF_Parser parser;
160 const char *setname="rsetname";
164 yaz_log(log_level, "======================================");
165 yaz_log(log_level, "query: %s", query);
166 odr = odr_createmem (ODR_DECODE);
170 parser = yaz_pqf_create();
171 rpn = yaz_pqf_parse(parser, odr, query);
172 yaz_pqf_destroy(parser);
179 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
183 if (rc != ZEBRA_FAIL)
185 yaz_log(log_level, "search returned %d (OK), but error was "
187 printf("Error: search returned %d (OK), but error was expected\n"
191 code = zebra_errCode(zh);
192 if (code != experror)
194 yaz_log(log_level, "search returned error code %d, but error %d "
195 "was expected", code, experror);
196 printf("Error: search returned error code %d, but error %d was "
198 code, experror, query);
204 if (rc == ZEBRA_FAIL) {
205 int code = zebra_errCode(zh);
206 yaz_log(log_level, "search returned %d. Code %d", rc, code);
208 printf("Error: search returned %d. Code %d\n%s\n", rc,
212 if (exphits != -1 && hits != exphits)
214 yaz_log(log_level, "search returned " ZINT_FORMAT
215 " hits instead of " ZINT_FORMAT, hits, exphits);
216 printf("Error: search returned " ZINT_FORMAT
217 " hits instead of " ZINT_FORMAT "\n%s\n",
218 hits, exphits, query);
227 int tl_query(ZebraHandle zh, const char *query, zint exphits)
229 return tl_query_x(zh, query, exphits, 0);
232 int tl_scan(ZebraHandle zh, const char *query,
234 int exp_pos, int exp_num, int exp_partial,
235 const char **exp_entries)
237 ODR odr = odr_createmem(ODR_ENCODE);
238 ZebraScanEntry *entries = 0;
242 yaz_log(log_level, "======================================");
243 yaz_log(log_level, "scan: pos=%d num=%d %s", pos, num, query);
245 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
249 printf("Error: scan returned %d (FAIL), but no error was expected\n"
258 printf("Error: scan returned OK, but partial was not set\n"
262 if (partial != exp_partial)
264 printf("Error: scan returned OK, with partial/expected %d/%d\n"
265 "%s\n", partial, exp_partial, query);
270 printf("Error: scan returned OK, with num/expected %d/%d\n"
271 "%s\n", num, exp_num, query);
276 printf("Error: scan returned OK, with pos/expected %d/%d\n"
277 "%s\n", pos, exp_pos, query);
286 for (i = 0; i<num; i++)
288 if (strcmp(exp_entries[i], entries[i].term))
290 printf("Error: scan OK, but entry %d term/exp %s/%s\n"
292 i, entries[i].term, exp_entries[i], query);
305 * makes a query, checks number of hits, and for the first hit, that
306 * it contains the given string, and that it gets the right score
308 int tl_ranking_query(ZebraHandle zh, char *query,
309 int exphits, char *firstrec, int firstscore)
311 ZebraRetrievalRecord retrievalRecord[10];
312 ODR odr_output = odr_createmem (ODR_ENCODE);
313 const char *setname="rsetname";
317 if (!tl_query(zh, query, exphits))
320 for (i = 0; i<10; i++)
321 retrievalRecord[i].position = i+1;
323 rc = zebra_records_retrieve (zh, odr_output, setname, 0,
324 VAL_TEXT_XML, exphits, retrievalRecord);
328 if (!strstr(retrievalRecord[0].buf, firstrec))
330 printf("Error: Got the wrong record first\n");
331 printf("Expected '%s' but got\n", firstrec);
332 printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
336 if (retrievalRecord[0].score != firstscore)
338 printf("Error: first rec got score %d instead of %d\n",
339 retrievalRecord[0].score, firstscore);
342 odr_destroy (odr_output);
346 int tl_meta_query(ZebraHandle zh, char *query, int exphits,
349 ZebraMetaRecord *meta;
350 ODR odr_output = odr_createmem (ODR_ENCODE);
351 const char *setname="rsetname";
352 zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
355 if (!tl_query(zh, query, exphits))
358 for (i = 0; i<exphits; i++)
361 meta = zebra_meta_records_create (zh, setname, exphits, positions);
365 printf("Error: retrieve returned error\n%s\n", query);
369 for (i = 0; i<exphits; i++)
371 if (meta[i].sysno != ids[i])
373 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
374 ids[i], meta[i].sysno);
378 zebra_meta_records_destroy(zh, meta, exphits);
379 odr_destroy (odr_output);
384 int tl_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 if (!tl_query(zh, query, hits))
396 recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
400 /* find min for each sequence to get proper base offset */
401 for (i = 0; i<hits; i++)
403 if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
404 min_val_recs = recs[i].sysno;
405 if (min_val_exp == 0 || exp[i] < min_val_exp)
406 min_val_exp = exp[i];
409 /* compare sequences using base offset */
410 for (i = 0; i<hits; i++)
411 if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
415 printf("Sequence not in right order for query\n%s\ngot exp\n",
417 for (i = 0; i<hits; i++)
418 printf(" " ZINT_FORMAT " " ZINT_FORMAT "\n",
419 recs[i].sysno, exp[i]);
421 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 tl_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);