1 /* $Id: testlib.c,v 1.22 2005-06-14 20:02:30 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 */
26 #include <sys/resource.h>
33 #include <yaz/pquery.h>
34 #include <idzebra/api.h>
37 /** start_log: open a log file */
38 /* FIXME - parse command line arguments to set log levels etc */
39 int log_level=0; /* not static, t*.c may use it */
41 void start_log(int argc, char **argv)
49 sprintf(logname, "%s.log", argv[0]);
50 yaz_log_init_file(logname);
51 log_level = yaz_log_mask_str_x(argv[0], 0);
53 cmd_level |= yaz_log_mask_str_x(argv[1], 0);
54 yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level | cmd_level);
55 yaz_log(log_level, "starting %s", argv[0]);
59 * start_up : do common start things, and a zebra_start
61 * - build the name of logfile from argv[0], and open it
62 * if no argv passed, do not open a log
63 * - read zebra.cfg from env var srcdir if it exists; otherwise current dir
64 * default to zebra.cfg, if no name is given
66 ZebraService start_up(char *cfgname, int argc, char **argv)
71 setrlimit(RLIMIT_CPU, &rlim);
73 start_log(argc, argv);
74 return start_service(cfgname);
78 * get_srcdir: return env srcdir or . (if does does not exist)
80 const char *get_srcdir()
82 const char *srcdir = getenv("srcdir");
83 if (!srcdir || ! *srcdir)
88 /** start_service - do a zebra_start with a decent config name */
89 ZebraService start_service(char *cfgname)
92 const char *srcdir = get_srcdir();
94 if (!cfgname || ! *cfgname )
97 sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
101 printf("zebra_start failed, probably because missing config file \n"
109 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
110 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
118 yaz_log(log_level,"========= Exiting with return code %d", retcode);
120 yaz_log(log_level,"========= All tests OK");
126 /** inits the database and inserts test data */
128 void init_data(ZebraHandle zh, const char **recs)
133 zebra_select_database(zh, "Default");
134 yaz_log(log_level, "going to call init");
136 yaz_log(log_level, "init returned %d", i);
139 printf("init failed with %d\n",i);
140 zebra_result(zh, &i, &addinfo);
141 printf(" Error %d %s\n", i, addinfo);
146 zebra_begin_trans (zh, 1);
147 for (i = 0; recs[i]; i++)
148 zebra_add_record(zh, recs[i], strlen(recs[i]));
155 int do_query_x(int lineno, ZebraHandle zh, const char *query, zint exphits,
159 YAZ_PQF_Parser parser;
161 const char *setname="rsetname";
165 yaz_log(log_level, "======================================");
166 yaz_log(log_level, "qry[%d]: %s", lineno, query);
167 odr = odr_createmem (ODR_DECODE);
169 parser = yaz_pqf_create();
170 rpn = yaz_pqf_parse(parser, odr, query);
171 yaz_pqf_destroy(parser);
173 printf("Error: Parse failed \n%s\n", query);
176 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
180 if (rc != ZEBRA_FAIL)
182 printf("Error: search returned %d (OK), but error was expected\n"
186 code = zebra_errCode(zh);
187 if (code != experror)
189 printf("Error: search returned error code %d, but error %d was "
191 code, experror, query);
197 if (rc == ZEBRA_FAIL) {
198 int code = zebra_errCode(zh);
199 printf("Error: search returned %d. Code %d\n%s\n", rc,
203 if (exphits != -1 && hits != exphits) {
204 printf("Error: search returned " ZINT_FORMAT
205 " hits instead of " ZINT_FORMAT "\n%s\n",
206 hits, exphits, query);
215 int do_query(int lineno, ZebraHandle zh, const char *query, zint exphits)
217 return do_query_x(lineno, zh, query, exphits, 0);
220 void do_scan(int lineno, ZebraHandle zh, const char *query,
222 int exp_pos, int exp_num, int exp_partial,
223 const char **exp_entries)
225 ODR odr = odr_createmem(ODR_ENCODE);
226 ZebraScanEntry *entries = 0;
230 yaz_log(log_level, "======================================");
231 yaz_log(log_level, "scan[%d]: pos=%d num=%d %s", lineno, pos, num, query);
233 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial);
236 printf("Error: scan returned %d (FAIL), but no error was expected\n"
245 printf("Error: scan returned OK, but partial was not set\n"
249 if (partial != exp_partial)
251 printf("Error: scan returned OK, with partial/expected %d/%d\n"
252 "%s\n", partial, exp_partial, query);
257 printf("Error: scan returned OK, with num/expected %d/%d\n"
258 "%s\n", num, exp_num, query);
263 printf("Error: scan returned OK, with pos/expected %d/%d\n"
264 "%s\n", pos, exp_pos, query);
273 for (i = 0; i<num; i++)
275 if (strcmp(exp_entries[i], entries[i].term))
277 printf("Error: scan OK, but entry %d term/exp %s/%s\n"
279 i, entries[i].term, exp_entries[i], query);
291 * makes a query, checks number of hits, and for the first hit, that
292 * it contains the given string, and that it gets the right score
294 void ranking_query(int lineno, ZebraHandle zh, char *query,
295 int exphits, char *firstrec, int firstscore)
297 ZebraRetrievalRecord retrievalRecord[10];
298 ODR odr_output = odr_createmem (ODR_ENCODE);
299 const char *setname="rsetname";
304 hits = do_query(lineno, zh, query, exphits);
306 for (i = 0; i<10; i++)
307 retrievalRecord[i].position = i+1;
309 rc = zebra_records_retrieve (zh, odr_output, setname, 0,
310 VAL_TEXT_XML, hits, retrievalRecord);
314 printf("Error: retrieve returned %d \n%s\n",rc,query);
318 if (!strstr(retrievalRecord[0].buf, firstrec))
320 printf("Error: Got the wrong record first\n");
321 printf("Expected '%s' but got\n", firstrec);
322 printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
326 if (retrievalRecord[0].score != firstscore)
328 printf("Error: first rec got score %d instead of %d\n",
329 retrievalRecord[0].score, firstscore);
332 odr_destroy (odr_output);
335 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
338 ZebraMetaRecord *meta;
339 ODR odr_output = odr_createmem (ODR_ENCODE);
340 const char *setname="rsetname";
341 zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
345 hits = do_query(lineno, zh, query, exphits);
347 for (i = 0; i<exphits; i++)
350 meta = zebra_meta_records_create (zh, setname, exphits, positions);
354 printf("Error: retrieve returned error\n%s\n", query);
358 for (i = 0; i<exphits; i++)
360 if (meta[i].sysno != ids[i])
362 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
363 ids[i], meta[i].sysno);
367 zebra_meta_records_destroy(zh, meta, exphits);
368 odr_destroy (odr_output);
377 static void filter_cb(void *cd, const char *name)
379 struct finfo *f = (struct finfo*) cd;
380 if (!strcmp(f->name, name))
384 void check_filter(ZebraService zs, const char *name)
390 zebra_filter_info(zs, &f, filter_cb);
393 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);