c2225f9314f3245f86fc75d540f4987e6c69b34a
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.10 2005-01-04 20:00:20 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23 /** testlib - utilities for the api tests */
24
25 #include <assert.h>
26 #include <yaz/log.h>
27 #include <yaz/pquery.h>
28 #include <idzebra/api.h>
29 #include "testlib.h"
30
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 */
34
35 void start_log(int argc, char **argv)
36 {
37     char logname[2048];
38     if (!argv) 
39         return;
40     if (!argv[0])
41         return;
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]);
47 }
48
49 /** 
50  * start_up : do common start things, and a zebra_start
51  *    - nmem_init
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
56  */
57 ZebraService start_up(char *cfgname, int argc, char **argv)
58 {
59     nmem_init();
60     start_log(argc, argv);
61     return start_service(cfgname);
62 }
63
64 /**
65  * get_srcdir: return env srcdir or . (if does does not exist)
66  */
67 const char *get_srcdir()
68 {
69     const char *srcdir = getenv("srcdir");
70     if (!srcdir || ! *srcdir)
71         srcdir=".";
72     return srcdir;
73
74 }
75 /** start_service - do a zebra_start with a decent config name */
76 ZebraService start_service(char *cfgname)
77 {
78     char cfg[256];
79     const char *srcdir = get_srcdir();
80     ZebraService zs;
81     if (!cfgname || ! *cfgname )
82         cfgname="zebra.cfg";
83
84     sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
85     zs=zebra_start(cfg);
86     if (!zs)
87     {
88         printf("zebra_start failed, probably because missing config file \n"
89                "check %s\n", cfg);
90         exit(9);
91     }
92     return zs;
93 }
94
95
96 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
97 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
98 {
99     if (zh)
100         zebra_close(zh);
101     if (zs)
102         zebra_stop(zs);
103
104     if (retcode)
105         yaz_log(log_level,"========= Exiting with return code %d", retcode);
106     else
107         yaz_log(log_level,"========= All tests OK");
108     nmem_exit();
109     xmalloc_trav("x");
110     return retcode;
111 }
112
113 /** inits the database and inserts test data */
114
115 void init_data(ZebraHandle zh, const char **recs)
116 {
117     int i;
118     char *addinfo;
119     assert(zh);
120     zebra_select_database(zh, "Default");
121     yaz_log(log_level, "going to call init");
122     i = zebra_init(zh);
123     yaz_log(log_level, "init returned %d",i);
124     if (i) 
125     {
126         printf("init failed with %d\n",i);
127         zebra_result(zh, &i, &addinfo);
128         printf("  Error %d   %s\n",i,addinfo);
129         exit(1);
130     }
131     if (recs)
132     {
133         zebra_begin_trans (zh, 1);
134         for (i = 0; recs[i]; i++)
135             zebra_add_record (zh, recs[i], strlen(recs[i]));
136         zebra_end_trans (zh);
137         zebra_commit (zh);
138     }
139
140 }
141
142 int do_query(int lineno, ZebraHandle zh, char *query, int exphits)
143 {
144     ODR odr;
145     YAZ_PQF_Parser parser;
146     Z_RPNQuery *rpn;
147     const char *setname="rsetname";
148     int hits;
149     int rc;
150         
151
152     yaz_log(log_level,"======================================");
153     yaz_log(log_level,"qry[%d]: %s", lineno, query);
154     odr=odr_createmem (ODR_DECODE);    
155
156     parser = yaz_pqf_create();
157     rpn = yaz_pqf_parse(parser, odr, query);
158     if (!rpn) {
159         printf("Error: Parse failed \n%s\n",query);
160         exit(1);
161     }
162     rc = zebra_search_RPN (zh, odr, rpn, setname, &hits);
163     if (rc) {
164         printf("Error: search returned %d \n%s\n",rc,query);
165         exit (1);
166     }
167
168     if (hits != exphits) {
169         printf("Error: search returned %d hits instead of %d\n",
170                 hits, exphits);
171         exit (1);
172     }
173     yaz_pqf_destroy(parser);
174     odr_destroy (odr);
175     return hits;
176 }
177
178
179 /** 
180  * makes a query, checks number of hits, and for the first hit, that 
181  * it contains the given string, and that it gets the right score
182  */
183 void ranking_query(int lineno, ZebraHandle zh, char *query, 
184           int exphits, char *firstrec, int firstscore )
185 {
186     ZebraRetrievalRecord retrievalRecord[10];
187     ODR odr_output = odr_createmem (ODR_ENCODE);    
188     const char *setname="rsetname";
189     int hits;
190     int rc;
191     int i;
192         
193     hits = do_query(lineno, zh, query, exphits);
194
195     for (i = 0; i<10; i++)
196         retrievalRecord[i].position = i+1;
197
198     rc = zebra_records_retrieve (zh, odr_output, setname, 0,
199                                  VAL_TEXT_XML, hits, retrievalRecord);
200     
201     if (rc)
202     {
203         printf("Error: retrieve returned %d \n%s\n",rc,query);
204         exit (1);
205     }
206
207     if (!strstr(retrievalRecord[0].buf, firstrec))
208     {
209         printf("Error: Got the wrong record first\n");
210         printf("Expected '%s' but got \n",firstrec);
211         printf("%.*s\n",retrievalRecord[0].len,retrievalRecord[0].buf);
212         exit(1);
213     }
214     
215     if (retrievalRecord[0].score != firstscore)
216     {
217         printf("Error: first rec got score %d instead of %d\n",
218                retrievalRecord[0].score, firstscore);
219         exit(1);
220     }
221     odr_destroy (odr_output);
222 }
223
224 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
225                 zint *ids)
226 {
227     ZebraMetaRecord *meta;
228     ODR odr_output = odr_createmem (ODR_ENCODE);    
229     const char *setname="rsetname";
230     zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
231     int hits;
232     int i;
233         
234     hits = do_query(lineno, zh, query, exphits);
235     
236     for (i = 0; i<exphits; i++)
237         positions[i] = i+1;
238
239     meta = zebra_meta_records_create (zh, setname,  exphits, positions);
240     
241     if (!meta)
242     {
243         printf("Error: retrieve returned error\n%s\n", query);
244         exit (1);
245     }
246
247     for (i = 0; i<exphits; i++)
248     {
249         if (meta[i].sysno != ids[i])
250         {
251             printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT,
252                    ids[i], meta[i].sysno);
253             exit(1);
254         }
255     }
256     zebra_meta_records_destroy(zh, meta, exphits);
257     odr_destroy (odr_output);
258     free(positions);
259 }
260
261 struct finfo {
262     const char *name;
263     int occurred;
264 };
265
266 static void filter_cb(void *cd, const char *name)
267 {
268     struct finfo *f = (struct finfo*) cd;
269     if (!strcmp(f->name, name))
270         f->occurred = 1;
271 }
272
273 void check_filter(ZebraService zs, const char *name)
274 {
275     struct finfo f;
276
277     f.name = name;
278     f.occurred = 0;
279     zebra_filter_info(zs, &f, filter_cb);
280     if (!f.occurred)
281     {
282         yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
283         exit(0);
284     }
285 }
286
287
288
289