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