1 /* $Id: testlib.c,v 1.4 2004-11-04 13:10:07 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 static int loglevel=0;
34 void start_log(int argc, char **argv)
41 sprintf(logname, "%s.log", argv[0]);
42 yaz_log_init_file(logname);
43 loglevel = yaz_log_mask_str_x(argv[0],0);
44 yaz_log_init_level(LOG_DEFAULT_LEVEL | loglevel);
45 logf(loglevel,"starting %s",argv[0]);
49 * start_up : do common start things, and a zebra_start
51 * - build the name of logfile from argv[0], and open it
52 * if no argv passed, do not open a log
53 * - read zebra.cfg from env var srcdir if it exists; otherwise current dir
54 * default to zebra.cfg, if no name is given
56 ZebraService start_up(char *cfgname, int argc, char **argv)
59 start_log(argc, argv);
60 return start_service(cfgname);
63 /** start_service - do a zebra_start with a decent config name */
64 ZebraService start_service(char *cfgname)
67 char *srcdir = getenv("srcdir");
69 if (!srcdir || ! *srcdir)
71 if (!cfgname || ! *cfgname )
74 sprintf(cfg, "%.200s/%s",srcdir, cfgname);
78 printf("zebra_start failed, probably because missing config file \n"
86 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
87 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
95 logf(loglevel,"========= Exiting with return code %d", retcode);
97 logf(loglevel,"========= All tests OK");
103 /** inits the database and inserts test data */
105 void init_data( ZebraHandle zh, const char **recs)
110 zebra_select_database(zh, "Default");
111 logf(LOG_DEBUG,"going to call init");
113 logf(LOG_DEBUG,"init returned %d",i);
116 printf("init failed with %d\n",i);
117 zebra_result(zh, &i, &addinfo);
118 printf(" Error %d %s\n",i,addinfo);
123 zebra_begin_trans (zh, 1);
124 for (i = 0; recs[i]; i++)
125 zebra_add_record (zh, recs[i], strlen(recs[i]));
126 zebra_end_trans (zh);
134 int do_query(int lineno, ZebraHandle zh, char *query, int exphits)
137 YAZ_PQF_Parser parser;
139 const char *setname="rsetname";
144 logf(loglevel,"======================================");
145 logf(loglevel,"qry[%d]: %s", lineno, query);
146 odr=odr_createmem (ODR_DECODE);
148 parser = yaz_pqf_create();
149 rpn = yaz_pqf_parse(parser, odr, query);
151 printf("Error: Parse failed \n%s\n",query);
154 rc=zebra_search_RPN (zh, odr, rpn, setname, &hits);
156 printf("Error: search returned %d \n%s\n",rc,query);
160 if (hits != exphits) {
161 printf("Error: search returned %d hits instead of %d\n",
165 yaz_pqf_destroy(parser);
172 * makes a query, checks number of hits, and for the first hit, that
173 * it contains the given string, and that it gets the right score
175 void ranking_query(int lineno, ZebraHandle zh, char *query,
176 int exphits, char *firstrec, int firstscore )
178 ZebraRetrievalRecord retrievalRecord[10];
179 ODR odr_output = odr_createmem (ODR_ENCODE);
180 const char *setname="rsetname";
185 hits=do_query(lineno, zh, query, exphits);
187 for (i = 0; i<10; i++)
188 retrievalRecord[i].position = i+1;
190 rc=zebra_records_retrieve (zh, odr_output, setname, 0,
191 VAL_TEXT_XML, hits, retrievalRecord);
194 printf("Error: retrieve returned %d \n%s\n",rc,query);
198 if (!strstr(retrievalRecord[0].buf, firstrec))
200 printf("Error: Got the wrong record first\n");
201 printf("Expected '%s' but got \n",firstrec);
202 printf("%.*s\n",retrievalRecord[0].len,retrievalRecord[0].buf);
206 if (retrievalRecord[0].score != firstscore)
208 printf("Error: first rec got score %d instead of %d\n",
209 retrievalRecord[0].score, firstscore);
212 odr_destroy (odr_output);