1 /* $Id: testlib.c,v 1.14 2005-04-20 10:18:19 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 */
27 #include <yaz/pquery.h>
28 #include <idzebra/api.h>
31 /** start_log: open a log file */
32 /* FIXME - parse command line arguments to set log levels etc */
33 int log_level=0; /* not static, t*.c may use it */
35 void start_log(int argc, char **argv)
42 sprintf(logname, "%s.log", argv[0]);
43 yaz_log_init_file(logname);
44 log_level = yaz_log_mask_str_x(argv[0],0);
45 yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level);
46 yaz_log(log_level,"starting %s",argv[0]);
50 * start_up : do common start things, and a zebra_start
52 * - build the name of logfile from argv[0], and open it
53 * if no argv passed, do not open a log
54 * - read zebra.cfg from env var srcdir if it exists; otherwise current dir
55 * default to zebra.cfg, if no name is given
57 ZebraService start_up(char *cfgname, int argc, char **argv)
60 start_log(argc, argv);
61 return start_service(cfgname);
65 * get_srcdir: return env srcdir or . (if does does not exist)
67 const char *get_srcdir()
69 const char *srcdir = getenv("srcdir");
70 if (!srcdir || ! *srcdir)
75 /** start_service - do a zebra_start with a decent config name */
76 ZebraService start_service(char *cfgname)
79 const char *srcdir = get_srcdir();
81 if (!cfgname || ! *cfgname )
84 sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
88 printf("zebra_start failed, probably because missing config file \n"
96 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
97 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
105 yaz_log(log_level,"========= Exiting with return code %d", retcode);
107 yaz_log(log_level,"========= All tests OK");
113 /** inits the database and inserts test data */
115 void init_data(ZebraHandle zh, const char **recs)
120 zebra_select_database(zh, "Default");
121 yaz_log(log_level, "going to call init");
123 yaz_log(log_level, "init returned %d", i);
126 printf("init failed with %d\n",i);
127 zebra_result(zh, &i, &addinfo);
128 printf(" Error %d %s\n", i, addinfo);
133 zebra_begin_trans (zh, 1);
134 for (i = 0; recs[i]; i++)
135 zebra_add_record(zh, recs[i], strlen(recs[i]));
142 int do_query_x(int lineno, ZebraHandle zh, char *query, int exphits,
146 YAZ_PQF_Parser parser;
148 const char *setname="rsetname";
152 yaz_log(log_level, "======================================");
153 yaz_log(log_level, "qry[%d]: %s", lineno, query);
154 odr = odr_createmem (ODR_DECODE);
156 parser = yaz_pqf_create();
157 rpn = yaz_pqf_parse(parser, odr, query);
158 yaz_pqf_destroy(parser);
160 printf("Error: Parse failed \n%s\n", query);
163 rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
166 if (rc != ZEBRA_FAIL)
168 printf("Error: search returned %d (OK), but error was expected\n"
172 int code = zebra_errCode(zh);
173 if (code != experror)
175 printf("Error: search returned error code %d, but error %d was "
177 code, experror, query);
183 if (rc == ZEBRA_FAIL) {
184 printf("Error: search returned %d\n%s\n", rc, query);
187 if (exphits != -1 && hits != exphits) {
188 printf("Error: search returned " ZINT_FORMAT
189 " hits instead of %d\n%s\n",
190 hits, exphits, query);
199 int do_query(int lineno, ZebraHandle zh, char *query, int exphits)
201 return do_query_x(lineno, zh, query, exphits, 0);
206 * makes a query, checks number of hits, and for the first hit, that
207 * it contains the given string, and that it gets the right score
209 void ranking_query(int lineno, ZebraHandle zh, char *query,
210 int exphits, char *firstrec, int firstscore)
212 ZebraRetrievalRecord retrievalRecord[10];
213 ODR odr_output = odr_createmem (ODR_ENCODE);
214 const char *setname="rsetname";
219 hits = do_query(lineno, zh, query, exphits);
221 for (i = 0; i<10; i++)
222 retrievalRecord[i].position = i+1;
224 rc = zebra_records_retrieve (zh, odr_output, setname, 0,
225 VAL_TEXT_XML, hits, retrievalRecord);
229 printf("Error: retrieve returned %d \n%s\n",rc,query);
233 if (!strstr(retrievalRecord[0].buf, firstrec))
235 printf("Error: Got the wrong record first\n");
236 printf("Expected '%s' but got \n", firstrec);
237 printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
241 if (retrievalRecord[0].score != firstscore)
243 printf("Error: first rec got score %d instead of %d\n",
244 retrievalRecord[0].score, firstscore);
247 odr_destroy (odr_output);
250 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
253 ZebraMetaRecord *meta;
254 ODR odr_output = odr_createmem (ODR_ENCODE);
255 const char *setname="rsetname";
256 zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
260 hits = do_query(lineno, zh, query, exphits);
262 for (i = 0; i<exphits; i++)
265 meta = zebra_meta_records_create (zh, setname, exphits, positions);
269 printf("Error: retrieve returned error\n%s\n", query);
273 for (i = 0; i<exphits; i++)
275 if (meta[i].sysno != ids[i])
277 printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT,
278 ids[i], meta[i].sysno);
282 zebra_meta_records_destroy(zh, meta, exphits);
283 odr_destroy (odr_output);
292 static void filter_cb(void *cd, const char *name)
294 struct finfo *f = (struct finfo*) cd;
295 if (!strcmp(f->name, name))
299 void check_filter(ZebraService zs, const char *name)
305 zebra_filter_info(zs, &f, filter_cb);
308 yaz_log(YLOG_WARN, "Filter %s does not exist.", name);