Avoid duplicate level print for test api
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.21 2005-05-31 07:22:20 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     int cmd_level = 0;
38     char logname[2048];
39     if (!argv) 
40         return;
41     if (!argv[0])
42         return;
43     sprintf(logname, "%s.log", argv[0]);
44     yaz_log_init_file(logname);
45     log_level = yaz_log_mask_str_x(argv[0], 0);
46     if (argc >= 2)
47         cmd_level |= yaz_log_mask_str_x(argv[1], 0);
48     yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level | cmd_level);
49     yaz_log(log_level, "starting %s", argv[0]);
50 }
51
52 /** 
53  * start_up : do common start things, and a zebra_start
54  *    - nmem_init
55  *    - build the name of logfile from argv[0], and open it
56  *      if no argv passed, do not open a log
57  *    - read zebra.cfg from env var srcdir if it exists; otherwise current dir 
58  *      default to zebra.cfg, if no name is given
59  */
60 ZebraService start_up(char *cfgname, int argc, char **argv)
61 {
62     nmem_init();
63     start_log(argc, argv);
64     return start_service(cfgname);
65 }
66
67 /**
68  * get_srcdir: return env srcdir or . (if does does not exist)
69  */
70 const char *get_srcdir()
71 {
72     const char *srcdir = getenv("srcdir");
73     if (!srcdir || ! *srcdir)
74         srcdir=".";
75     return srcdir;
76
77 }
78 /** start_service - do a zebra_start with a decent config name */
79 ZebraService start_service(char *cfgname)
80 {
81     char cfg[256];
82     const char *srcdir = get_srcdir();
83     ZebraService zs;
84     if (!cfgname || ! *cfgname )
85         cfgname="zebra.cfg";
86
87     sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
88     zs=zebra_start(cfg);
89     if (!zs)
90     {
91         printf("zebra_start failed, probably because missing config file \n"
92                "check %s\n", cfg);
93         exit(9);
94     }
95     return zs;
96 }
97
98
99 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
100 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
101 {
102     if (zh)
103         zebra_close(zh);
104     if (zs)
105         zebra_stop(zs);
106
107     if (retcode)
108         yaz_log(log_level,"========= Exiting with return code %d", retcode);
109     else
110         yaz_log(log_level,"========= All tests OK");
111     nmem_exit();
112     xmalloc_trav("x");
113     return retcode;
114 }
115
116 /** inits the database and inserts test data */
117
118 void init_data(ZebraHandle zh, const char **recs)
119 {
120     int i;
121     char *addinfo;
122     assert(zh);
123     zebra_select_database(zh, "Default");
124     yaz_log(log_level, "going to call init");
125     i = zebra_init(zh);
126     yaz_log(log_level, "init returned %d", i);
127     if (i) 
128     {
129         printf("init failed with %d\n",i);
130         zebra_result(zh, &i, &addinfo);
131         printf("  Error %d   %s\n", i, addinfo);
132         exit(1);
133     }
134     if (recs)
135     {
136         zebra_begin_trans (zh, 1);
137         for (i = 0; recs[i]; i++)
138             zebra_add_record(zh, recs[i], strlen(recs[i]));
139         zebra_end_trans(zh);
140         zebra_commit(zh);
141     }
142
143 }
144
145 int do_query_x(int lineno, ZebraHandle zh, const char *query, zint exphits,
146                int experror)
147 {
148     ODR odr;
149     YAZ_PQF_Parser parser;
150     Z_RPNQuery *rpn;
151     const char *setname="rsetname";
152     zint hits;
153     ZEBRA_RES rc;
154
155     yaz_log(log_level, "======================================");
156     yaz_log(log_level, "qry[%d]: %s", lineno, query);
157     odr = odr_createmem (ODR_DECODE);    
158
159     parser = yaz_pqf_create();
160     rpn = yaz_pqf_parse(parser, odr, query);
161     yaz_pqf_destroy(parser);
162     if (!rpn) {
163         printf("Error: Parse failed \n%s\n", query);
164         exit(1);
165     }
166     rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
167     if (experror)
168     {
169         int code;
170         if (rc != ZEBRA_FAIL)
171         {
172             printf("Error: search returned %d (OK), but error was expected\n"
173                    "%s\n",  rc, query);
174             exit(1);
175         }
176         code = zebra_errCode(zh);
177         if (code != experror)
178         {
179             printf("Error: search returned error code %d, but error %d was "
180                    "expected\n%s\n",
181                    code, experror, query);
182             exit(1);
183         }
184     }
185     else
186     {
187         if (rc == ZEBRA_FAIL) {
188             int code = zebra_errCode(zh);
189             printf("Error: search returned %d. Code %d\n%s\n", rc, 
190                    code, query);
191             exit (1);
192         }
193         if (exphits != -1 && hits != exphits) {
194             printf("Error: search returned " ZINT_FORMAT 
195                    " hits instead of " ZINT_FORMAT "\n%s\n",
196                    hits, exphits, query);
197             exit (1);
198         }
199     }
200     odr_destroy (odr);
201     return hits;
202 }
203
204
205 int do_query(int lineno, ZebraHandle zh, const char *query, zint exphits)
206 {
207     return do_query_x(lineno, zh, query, exphits, 0);
208 }
209
210 void do_scan(int lineno, ZebraHandle zh, const char *query,
211              int pos, int num,
212              int exp_pos, int exp_num, int exp_partial,
213              const char **exp_entries)
214 {
215     ODR odr = odr_createmem(ODR_ENCODE);
216     ZebraScanEntry *entries = 0;
217     int partial = -123;
218     ZEBRA_RES res;
219
220     yaz_log(log_level, "======================================");
221     yaz_log(log_level, "scan[%d]: pos=%d num=%d %s", lineno, pos, num, query);
222
223     res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial);
224     if (res != ZEBRA_OK)
225     {
226         printf("Error: scan returned %d (FAIL), but no error was expected\n"
227                "%s\n",  res, query);
228         exit(1);
229     }
230     else
231     {
232         int fails = 0;
233         if (partial == -123)
234         {
235             printf("Error: scan returned OK, but partial was not set\n"
236                    "%s\n", query);
237             fails++;
238         }
239         if (partial != exp_partial)
240         {
241             printf("Error: scan returned OK, with partial/expected %d/%d\n"
242                    "%s\n", partial, exp_partial, query);
243             fails++;
244         }
245         if (num != exp_num)
246         {
247             printf("Error: scan returned OK, with num/expected %d/%d\n"
248                    "%s\n", num, exp_num, query);
249             fails++;
250         }
251         if (pos != exp_pos)
252         {
253             printf("Error: scan returned OK, with pos/expected %d/%d\n"
254                    "%s\n", pos, exp_pos, query);
255             fails++;
256         }
257         if (fails)
258             exit(1);
259         fails = 0;
260         if (exp_entries)
261         {
262             int i;
263             for (i = 0; i<num; i++)
264             {
265                 if (strcmp(exp_entries[i], entries[i].term))
266                 {
267                     printf("Error: scan OK, but entry %d term/exp %s/%s\n"
268                            "%s\n",
269                            i, entries[i].term, exp_entries[i], query);
270                     fails++;
271                 }
272             }
273         }
274         if (fails)
275             exit(0);
276     }
277     odr_destroy(odr);
278 }
279
280 /** 
281  * makes a query, checks number of hits, and for the first hit, that 
282  * it contains the given string, and that it gets the right score
283  */
284 void ranking_query(int lineno, ZebraHandle zh, char *query, 
285                    int exphits, char *firstrec, int firstscore)
286 {
287     ZebraRetrievalRecord retrievalRecord[10];
288     ODR odr_output = odr_createmem (ODR_ENCODE);    
289     const char *setname="rsetname";
290     int hits;
291     int rc;
292     int i;
293         
294     hits = do_query(lineno, zh, query, exphits);
295
296     for (i = 0; i<10; i++)
297         retrievalRecord[i].position = i+1;
298
299     rc = zebra_records_retrieve (zh, odr_output, setname, 0,
300                                  VAL_TEXT_XML, hits, retrievalRecord);
301     
302     if (rc)
303     {
304         printf("Error: retrieve returned %d \n%s\n",rc,query);
305         exit (1);
306     }
307
308     if (!strstr(retrievalRecord[0].buf, firstrec))
309     {
310         printf("Error: Got the wrong record first\n");
311         printf("Expected '%s' but got\n", firstrec);
312         printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
313         exit(1);
314     }
315     
316     if (retrievalRecord[0].score != firstscore)
317     {
318         printf("Error: first rec got score %d instead of %d\n",
319                retrievalRecord[0].score, firstscore);
320         exit(1);
321     }
322     odr_destroy (odr_output);
323 }
324
325 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
326                 zint *ids)
327 {
328     ZebraMetaRecord *meta;
329     ODR odr_output = odr_createmem (ODR_ENCODE);    
330     const char *setname="rsetname";
331     zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
332     int hits;
333     int i;
334         
335     hits = do_query(lineno, zh, query, exphits);
336     
337     for (i = 0; i<exphits; i++)
338         positions[i] = i+1;
339
340     meta = zebra_meta_records_create (zh, setname,  exphits, positions);
341     
342     if (!meta)
343     {
344         printf("Error: retrieve returned error\n%s\n", query);
345         exit (1);
346     }
347
348     for (i = 0; i<exphits; i++)
349     {
350         if (meta[i].sysno != ids[i])
351         {
352             printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
353                    ids[i], meta[i].sysno);
354             exit(1);
355         }
356     }
357     zebra_meta_records_destroy(zh, meta, exphits);
358     odr_destroy (odr_output);
359     free(positions);
360 }
361
362 struct finfo {
363     const char *name;
364     int occurred;
365 };
366
367 static void filter_cb(void *cd, const char *name)
368 {
369     struct finfo *f = (struct finfo*) cd;
370     if (!strcmp(f->name, name))
371         f->occurred = 1;
372 }
373
374 void check_filter(ZebraService zs, const char *name)
375 {
376     struct finfo f;
377
378     f.name = name;
379     f.occurred = 0;
380     zebra_filter_info(zs, &f, filter_cb);
381     if (!f.occurred)
382     {
383         yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
384         exit(0);
385     }
386 }
387
388
389
390