1 /* $Id: testlib.c,v 1.40 2006-11-10 12:57:49 adam Exp $
2 Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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)
52 sprintf(logname, "%s.log", argv[0]);
56 yaz_log_init_file(logname);
58 log_level = yaz_log_mask_str_x(argv[1], 0);
60 yaz_log_time_format(argv[2]);
62 yaz_log_init_level(log_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(void)
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++)
152 if (zebra_add_record(zh, recs[i], strlen(recs[i])) != ZEBRA_OK)
154 if (zebra_end_trans(zh) != ZEBRA_OK)
159 if (zebra_end_trans(zh) != ZEBRA_OK)
166 int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
169 YAZ_PQF_Parser parser;
171 const char *setname="rsetname";
175 yaz_log(log_level, "======================================");
176 yaz_log(log_level, "query: %s", query);
177 odr = odr_createmem (ODR_DECODE);
181 parser = yaz_pqf_create();
182 rpn = yaz_pqf_parse(parser, odr, query);
183 yaz_pqf_destroy(parser);
186 yaz_log(log_level, "could not parse pqf query %s\n", query);
187 printf("could not parse pqf query %s\n", query);
192 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
197 if (rc != ZEBRA_FAIL)
199 yaz_log(log_level, "search returned %d (OK), but error was "
201 printf("Error: search returned %d (OK), but error was expected\n"
205 code = zebra_errCode(zh);
206 if (code != experror)
208 yaz_log(log_level, "search returned error code %d, but error %d "
209 "was expected", code, experror);
210 printf("Error: search returned error code %d, but error %d was "
212 code, experror, query);
218 if (rc == ZEBRA_FAIL) {
219 int code = zebra_errCode(zh);
220 yaz_log(log_level, "search returned %d. Code %d", rc, code);
222 printf("Error: search returned %d. Code %d\n%s\n", rc,
226 if (exphits != -1 && hits != exphits)
228 yaz_log(log_level, "search returned " ZINT_FORMAT
229 " hits instead of " ZINT_FORMAT, hits, exphits);
230 printf("Error: search returned " ZINT_FORMAT
231 " hits instead of " ZINT_FORMAT "\n%s\n",
232 hits, exphits, query);
240 int tl_query(ZebraHandle zh, const char *query, zint exphits)
242 return tl_query_x(zh, query, exphits, 0);
245 int tl_scan(ZebraHandle zh, const char *query,
247 int exp_pos, int exp_num, int exp_partial,
248 const char **exp_entries)
251 ODR odr = odr_createmem(ODR_ENCODE);
252 ZebraScanEntry *entries = 0;
256 yaz_log(log_level, "======================================");
257 yaz_log(log_level, "scan: pos=%d num=%d %s", pos, num, query);
259 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
264 printf("Error: scan returned OK, but partial was not set\n"
268 if (partial != exp_partial)
270 printf("Error: scan OK, with partial/expected %d/%d\n",
271 partial, exp_partial);
274 if (res != ZEBRA_OK) /* failure */
278 printf("Error: scan failed, but no error was expected\n");
286 printf("Error: scan OK, but error was expected\n");
294 printf("Error: scan OK, with num/expected %d/%d\n",
300 printf("Error: scan OK, with pos/expected %d/%d\n",
309 for (i = 0; i<num; i++)
311 if (strcmp(exp_entries[i], entries[i].term))
313 printf("Error: scan OK of %s, no %d got=%s exp=%s\n",
314 query, i, entries[i].term, exp_entries[i]);
329 * makes a query, checks number of hits, and for the first hit, that
330 * it contains the given string, and that it gets the right score
332 int tl_ranking_query(ZebraHandle zh, char *query,
333 int exphits, char *firstrec, int firstscore)
335 ZebraRetrievalRecord retrievalRecord[10];
337 const char *setname = "rsetname";
342 if (!tl_query(zh, query, exphits))
345 for (i = 0; i<10; i++)
346 retrievalRecord[i].position = i+1;
348 odr_output = odr_createmem(ODR_ENCODE);
349 rc = zebra_records_retrieve(zh, odr_output, setname, 0,
350 VAL_TEXT_XML, exphits, retrievalRecord);
353 else if (!strstr(retrievalRecord[0].buf, firstrec))
355 printf("Error: Got the wrong record first\n");
356 printf("Expected '%s' but got\n", firstrec);
357 printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
360 else if (retrievalRecord[0].score != firstscore)
362 printf("Error: first rec got score %d instead of %d\n",
363 retrievalRecord[0].score, firstscore);
366 odr_destroy (odr_output);
370 int tl_meta_query(ZebraHandle zh, char *query, int exphits,
373 ZebraMetaRecord *meta;
374 const char *setname= "rsetname";
378 if (!tl_query(zh, query, exphits))
381 positions = (zint *) xmalloc(1 + (exphits * sizeof(zint)));
382 for (i = 0; i<exphits; i++)
385 meta = zebra_meta_records_create(zh, setname, exphits, positions);
389 printf("Error: retrieve returned error\n%s\n", query);
394 for (i = 0; i<exphits; i++)
396 if (meta[i].sysno != ids[i])
398 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
399 ids[i], meta[i].sysno);
403 zebra_meta_records_destroy(zh, meta, exphits);
408 int tl_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
410 ZebraMetaRecord *recs;
413 zint min_val_recs = 0;
414 zint min_val_exp = 0;
417 if (!tl_query(zh, query, hits))
420 recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
424 /* find min for each sequence to get proper base offset */
425 for (i = 0; i<hits; i++)
427 if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
428 min_val_recs = recs[i].sysno;
429 if (min_val_exp == 0 || exp[i] < min_val_exp)
430 min_val_exp = exp[i];
433 /* compare sequences using base offset */
434 for (i = 0; i<hits; i++)
435 if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
439 printf("Sequence not in right order for query\n%s\ngot exp\n",
441 for (i = 0; i<hits; i++)
442 printf(" " ZINT_FORMAT " " ZINT_FORMAT "\n",
443 recs[i].sysno, exp[i]);
445 zebra_meta_records_destroy (zh, recs, 4);
458 static void filter_cb(void *cd, const char *name)
460 struct finfo *f = (struct finfo*) cd;
461 if (!strcmp(f->name, name))
465 void tl_check_filter(ZebraService zs, const char *name)
471 zebra_filter_info(zs, &f, filter_cb);
474 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
485 * indent-tabs-mode: nil
487 * vim: shiftwidth=4 tabstop=8 expandtab