Fixed a few printfs
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.17 2005-05-03 09:07:17 adam Exp $
2    Copyright (C) 1995-2005
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_x(int lineno, ZebraHandle zh, char *query, int exphits,
143               int experror)
144 {
145     ODR odr;
146     YAZ_PQF_Parser parser;
147     Z_RPNQuery *rpn;
148     const char *setname="rsetname";
149     zint hits;
150     ZEBRA_RES rc;
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     yaz_pqf_destroy(parser);
159     if (!rpn) {
160         printf("Error: Parse failed \n%s\n", query);
161         exit(1);
162     }
163     rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
164     if (experror)
165     {
166         if (rc != ZEBRA_FAIL)
167         {
168             printf("Error: search returned %d (OK), but error was expected\n"
169                    "%s\n",  rc, query);
170             exit(1);
171         }
172         int code = zebra_errCode(zh);
173         if (code != experror)
174         {
175             printf("Error: search returned error code %d, but error %d was "
176                    "expected\n%s\n",
177                    code, experror, query);
178             exit(1);
179         }
180     }
181     else
182     {
183         if (rc == ZEBRA_FAIL) {
184             int code = zebra_errCode(zh);
185             printf("Error: search returned %d. Code %d\n%s\n", rc, 
186                    code, query);
187             exit (1);
188         }
189         if (exphits != -1 && hits != exphits) {
190             printf("Error: search returned " ZINT_FORMAT 
191                    " hits instead of %d\n%s\n",
192                    hits, exphits, query);
193             exit (1);
194         }
195     }
196     odr_destroy (odr);
197     return hits;
198 }
199
200
201 int do_query(int lineno, ZebraHandle zh, char *query, int exphits)
202 {
203     return do_query_x(lineno, zh, query, exphits, 0);
204 }
205
206 void do_scan(int lineno, ZebraHandle zh, const char *query,
207              int pos, int num,
208              int exp_pos, int exp_num, int exp_partial,
209              const char **exp_entries)
210 {
211     ODR odr = odr_createmem(ODR_ENCODE);
212     ZebraScanEntry *entries = 0;
213     int partial = -123;
214     ZEBRA_RES res;
215
216     yaz_log(log_level, "======================================");
217     yaz_log(log_level, "scan[%d]: pos=%d num=%d %s", lineno, pos, num, query);
218
219     res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial);
220     if (res != ZEBRA_OK)
221     {
222         printf("Error: scan returned %d (FAIL), but no error was expected\n"
223                "%s\n",  res, query);
224         exit(1);
225     }
226     else
227     {
228         int fails = 0;
229         if (partial == -123)
230         {
231             printf("Error: scan returned OK, but partial was not set\n"
232                    "%s\n", query);
233             fails++;
234         }
235         if (partial != exp_partial)
236         {
237             printf("Error: scan returned OK, with partial/expected %d/%d\n"
238                    "%s\n", partial, exp_partial, query);
239             fails++;
240         }
241         if (num != exp_num)
242         {
243             printf("Error: scan returned OK, with num/expected %d/%d\n"
244                    "%s\n", num, exp_num, query);
245             fails++;
246         }
247         if (pos != exp_pos)
248         {
249             printf("Error: scan returned OK, with pos/expected %d/%d\n"
250                    "%s\n", pos, exp_pos, query);
251             fails++;
252         }
253         if (fails)
254             exit(1);
255         fails = 0;
256         if (exp_entries)
257         {
258             int i;
259             for (i = 0; i<num; i++)
260             {
261                 if (strcmp(exp_entries[i], entries[i].term))
262                 {
263                     printf("Error: scan OK, but entry %d term/exp %s/%s\n"
264                            "%s\n",
265                            i, entries[i].term, exp_entries[i], query);
266                     fails++;
267                 }
268             }
269         }
270         if (fails)
271             exit(0);
272     }
273     odr_destroy(odr);
274 }
275
276 /** 
277  * makes a query, checks number of hits, and for the first hit, that 
278  * it contains the given string, and that it gets the right score
279  */
280 void ranking_query(int lineno, ZebraHandle zh, char *query, 
281                    int exphits, char *firstrec, int firstscore)
282 {
283     ZebraRetrievalRecord retrievalRecord[10];
284     ODR odr_output = odr_createmem (ODR_ENCODE);    
285     const char *setname="rsetname";
286     int hits;
287     int rc;
288     int i;
289         
290     hits = do_query(lineno, zh, query, exphits);
291
292     for (i = 0; i<10; i++)
293         retrievalRecord[i].position = i+1;
294
295     rc = zebra_records_retrieve (zh, odr_output, setname, 0,
296                                  VAL_TEXT_XML, hits, retrievalRecord);
297     
298     if (rc)
299     {
300         printf("Error: retrieve returned %d \n%s\n",rc,query);
301         exit (1);
302     }
303
304     if (!strstr(retrievalRecord[0].buf, firstrec))
305     {
306         printf("Error: Got the wrong record first\n");
307         printf("Expected '%s' but got\n", firstrec);
308         printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
309         exit(1);
310     }
311     
312     if (retrievalRecord[0].score != firstscore)
313     {
314         printf("Error: first rec got score %d instead of %d\n",
315                retrievalRecord[0].score, firstscore);
316         exit(1);
317     }
318     odr_destroy (odr_output);
319 }
320
321 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
322                 zint *ids)
323 {
324     ZebraMetaRecord *meta;
325     ODR odr_output = odr_createmem (ODR_ENCODE);    
326     const char *setname="rsetname";
327     zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
328     int hits;
329     int i;
330         
331     hits = do_query(lineno, zh, query, exphits);
332     
333     for (i = 0; i<exphits; i++)
334         positions[i] = i+1;
335
336     meta = zebra_meta_records_create (zh, setname,  exphits, positions);
337     
338     if (!meta)
339     {
340         printf("Error: retrieve returned error\n%s\n", query);
341         exit (1);
342     }
343
344     for (i = 0; i<exphits; i++)
345     {
346         if (meta[i].sysno != ids[i])
347         {
348             printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
349                    ids[i], meta[i].sysno);
350             exit(1);
351         }
352     }
353     zebra_meta_records_destroy(zh, meta, exphits);
354     odr_destroy (odr_output);
355     free(positions);
356 }
357
358 struct finfo {
359     const char *name;
360     int occurred;
361 };
362
363 static void filter_cb(void *cd, const char *name)
364 {
365     struct finfo *f = (struct finfo*) cd;
366     if (!strcmp(f->name, name))
367         f->occurred = 1;
368 }
369
370 void check_filter(ZebraService zs, const char *name)
371 {
372     struct finfo f;
373
374     f.name = name;
375     f.occurred = 0;
376     zebra_filter_info(zs, &f, filter_cb);
377     if (!f.occurred)
378     {
379         yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
380         exit(0);
381     }
382 }
383
384
385
386