1 /* $Id: testlib.c,v 1.43 2007-04-16 08:44:32 adam Exp $
2 Copyright (C) 1995-2007
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 <yaz/oid_db.h>
39 #include <idzebra/api.h>
42 int log_level = YLOG_LOG;
45 * tl_start_up : do common start things, and a zebra_start
47 * - build the name of logfile from argv[0], and open it
48 * if no argv passed, do not open a log
49 * - read zebra.cfg from env var srcdir if it exists; otherwise current dir
50 * default to zebra.cfg, if no name is given
52 ZebraService tl_start_up(char *cfgname, int argc, char **argv)
54 #if HAVE_SYS_RESOURCE_H
59 setrlimit(RLIMIT_CPU, &rlim);
63 return tl_zebra_start(cfgname);
67 * get_srcdir: return env srcdir or . (if does does not exist)
69 const char *tl_get_srcdir(void)
71 const char *srcdir = getenv("srcdir");
72 if (!srcdir || ! *srcdir)
77 /** tl_zebra_start - do a zebra_start with a decent config name */
78 ZebraService tl_zebra_start(const char *cfgname)
81 const char *srcdir = tl_get_srcdir();
82 if (!cfgname || ! *cfgname )
85 sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
86 return zebra_start(cfg);
89 /** tl_close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
90 int tl_close_down(ZebraHandle zh, ZebraService zs)
102 /** inits the database and inserts test data */
104 int tl_init_data(ZebraHandle zh, const char **recs)
111 if (zebra_select_database(zh, "Default") != ZEBRA_OK)
114 yaz_log(log_level, "going to call init");
115 res = zebra_init(zh);
116 if (res == ZEBRA_FAIL)
118 yaz_log(log_level, "init_data: zebra_init failed with %d", res);
119 printf("init_data failed with %d\n", res);
125 if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
127 for (i = 0; recs[i]; i++)
129 if (zebra_add_record(zh, recs[i], strlen(recs[i])) != ZEBRA_OK)
131 if (zebra_end_trans(zh) != ZEBRA_OK)
136 if (zebra_end_trans(zh) != ZEBRA_OK)
143 int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
146 YAZ_PQF_Parser parser;
148 const char *setname="rsetname";
152 yaz_log(log_level, "======================================");
153 yaz_log(log_level, "query: %s", query);
154 odr = odr_createmem (ODR_DECODE);
158 parser = yaz_pqf_create();
159 rpn = yaz_pqf_parse(parser, odr, query);
160 yaz_pqf_destroy(parser);
163 yaz_log(log_level, "could not parse pqf query %s\n", query);
164 printf("could not parse pqf query %s\n", query);
169 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
174 if (rc != ZEBRA_FAIL)
176 yaz_log(log_level, "search returned %d (OK), but error was "
178 printf("Error: search returned %d (OK), but error was expected\n"
182 code = zebra_errCode(zh);
183 if (code != experror)
185 yaz_log(log_level, "search returned error code %d, but error %d "
186 "was expected", code, experror);
187 printf("Error: search returned error code %d, but error %d was "
189 code, experror, query);
195 if (rc == ZEBRA_FAIL) {
196 int code = zebra_errCode(zh);
197 yaz_log(log_level, "search returned %d. Code %d", rc, code);
199 printf("Error: search returned %d. Code %d\n%s\n", rc,
203 if (exphits != -1 && hits != exphits)
205 yaz_log(log_level, "search returned " ZINT_FORMAT
206 " hits instead of " ZINT_FORMAT, hits, exphits);
207 printf("Error: search returned " ZINT_FORMAT
208 " hits instead of " ZINT_FORMAT "\n%s\n",
209 hits, exphits, query);
217 int tl_query(ZebraHandle zh, const char *query, zint exphits)
219 return tl_query_x(zh, query, exphits, 0);
222 int tl_scan(ZebraHandle zh, const char *query,
224 int exp_pos, int exp_num, int exp_partial,
225 const char **exp_entries)
228 ODR odr = odr_createmem(ODR_ENCODE);
229 ZebraScanEntry *entries = 0;
233 yaz_log(log_level, "======================================");
234 yaz_log(log_level, "scan: pos=%d num=%d %s", pos, num, query);
236 res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
241 printf("Error: scan returned OK, but partial was not set\n"
245 if (partial != exp_partial)
247 printf("Error: scan OK, with partial/expected %d/%d\n",
248 partial, exp_partial);
251 if (res != ZEBRA_OK) /* failure */
255 printf("Error: scan failed, but no error was expected\n");
263 printf("Error: scan OK, but error was expected\n");
271 printf("Error: scan OK, with num/expected %d/%d\n",
277 printf("Error: scan OK, with pos/expected %d/%d\n",
286 for (i = 0; i<num; i++)
288 if (strcmp(exp_entries[i], entries[i].term))
290 printf("Error: scan OK of %s, no %d got=%s exp=%s\n",
291 query, i, entries[i].term, exp_entries[i]);
306 * makes a query, checks number of hits, and for the first hit, that
307 * it contains the given string, and that it gets the right score
309 int tl_ranking_query(ZebraHandle zh, char *query,
310 int exphits, char *firstrec, int firstscore)
312 ZebraRetrievalRecord retrievalRecord[10];
314 const char *setname = "rsetname";
319 if (!tl_query(zh, query, exphits))
322 for (i = 0; i<10; i++)
323 retrievalRecord[i].position = i+1;
325 odr_output = odr_createmem(ODR_ENCODE);
326 rc = zebra_records_retrieve(zh, odr_output, setname, 0,
327 yaz_oid_xml(), exphits, retrievalRecord);
330 else 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);
337 else if (retrievalRecord[0].score != firstscore)
339 printf("Error: first rec got score %d instead of %d\n",
340 retrievalRecord[0].score, firstscore);
343 odr_destroy (odr_output);
347 int tl_meta_query(ZebraHandle zh, char *query, int exphits,
350 ZebraMetaRecord *meta;
351 const char *setname= "rsetname";
355 if (!tl_query(zh, query, exphits))
358 positions = (zint *) xmalloc(1 + (exphits * sizeof(zint)));
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);
371 for (i = 0; i<exphits; i++)
373 if (meta[i].sysno != ids[i])
375 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
376 ids[i], meta[i].sysno);
380 zebra_meta_records_destroy(zh, meta, exphits);
385 int tl_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
387 ZebraMetaRecord *recs;
390 zint min_val_recs = 0;
391 zint min_val_exp = 0;
394 if (!tl_query(zh, query, hits))
397 recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
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);
435 static void filter_cb(void *cd, const char *name)
437 struct finfo *f = (struct finfo*) cd;
438 if (!strcmp(f->name, name))
442 void tl_check_filter(ZebraService zs, const char *name)
448 zebra_filter_info(zs, &f, filter_cb);
451 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
462 * indent-tabs-mode: nil
464 * vim: shiftwidth=4 tabstop=8 expandtab