5735254660ff6260580aecd743b07834bb5e4581
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.38 2006-09-08 09:56:40 adam Exp $
2    Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 /** testlib - utilities for the api tests */
24
25 #if HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #if HAVE_SYS_RESOURCE_H
29 #include <sys/resource.h>
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include <assert.h>
36 #include <yaz/log.h>
37 #include <yaz/pquery.h>
38 #include <idzebra/api.h>
39 #include "testlib.h"
40
41 /** start_log: open a log file */
42 /*    FIXME - parse command line arguments to set log levels etc */
43 int log_level=0; /* not static, t*.c may use it */
44
45 void tl_start_log(int argc, char **argv)
46 {
47     char logname[2048];
48     if (!argv) 
49         return;
50     if (!argv[0])
51         return;
52     sprintf(logname, "%s.log", argv[0]);
53 #if HAVE_UNISTD_H
54     unlink(logname);
55 #endif
56     yaz_log_init_file(logname);
57     if (argc >= 2)
58         log_level = yaz_log_mask_str_x(argv[1], 0);
59     if (argc >= 3)
60         yaz_log_time_format(argv[2]);
61     if (log_level)
62         yaz_log_init_level(log_level);
63     yaz_log(log_level, "starting %s", argv[0]);
64 }
65
66 /** 
67  * tl_start_up : do common start things, and a zebra_start
68  *    - nmem_init
69  *    - build the name of logfile from argv[0], and open it
70  *      if no argv passed, do not open a log
71  *    - read zebra.cfg from env var srcdir if it exists; otherwise current dir 
72  *      default to zebra.cfg, if no name is given
73  */
74 ZebraService tl_start_up(char *cfgname, int argc, char **argv)
75 {
76 #if HAVE_SYS_RESOURCE_H
77 #if HAVE_SYS_TIME_H
78     struct rlimit rlim;
79     rlim.rlim_cur = 60;
80     rlim.rlim_max = 60;
81     setrlimit(RLIMIT_CPU, &rlim);
82 #endif
83 #endif
84     nmem_init();
85     tl_start_log(argc, argv);
86     return tl_zebra_start(cfgname);
87 }
88
89 /**
90  * get_srcdir: return env srcdir or . (if does does not exist)
91  */
92 const char *tl_get_srcdir()
93 {
94     const char *srcdir = getenv("srcdir");
95     if (!srcdir || ! *srcdir)
96         srcdir = ".";
97     return srcdir;
98
99 }
100 /** tl_zebra_start - do a zebra_start with a decent config name */
101 ZebraService tl_zebra_start(const char *cfgname)
102 {
103     char cfg[256];
104     const char *srcdir = tl_get_srcdir();
105     if (!cfgname || ! *cfgname )
106         cfgname="zebra.cfg";
107
108     sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
109     return  zebra_start(cfg);
110 }
111
112 /** tl_close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
113 int tl_close_down(ZebraHandle zh, ZebraService zs)
114 {
115     if (zh)
116         zebra_close(zh);
117     if (zs)
118         zebra_stop(zs);
119
120     nmem_exit();
121     xmalloc_trav("x");
122     return 1;
123 }
124
125 /** inits the database and inserts test data */
126
127 int tl_init_data(ZebraHandle zh, const char **recs)
128 {
129     ZEBRA_RES res;
130
131     if (!zh)
132         return 0;
133
134     if (zebra_select_database(zh, "Default") != ZEBRA_OK)
135         return 0;
136
137     yaz_log(log_level, "going to call init");
138     res = zebra_init(zh);
139     if (res == ZEBRA_FAIL) 
140     {
141         yaz_log(log_level, "init_data: zebra_init failed with %d", res);
142         printf("init_data failed with %d\n", res);
143         return 0;
144     }
145     if (recs)
146     {
147         int i;
148         if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
149             return 0;
150         for (i = 0; recs[i]; i++)
151             zebra_add_record(zh, recs[i], strlen(recs[i]));
152         if (zebra_end_trans(zh) != ZEBRA_OK)
153             return 0;
154         zebra_commit(zh);
155     }
156     return 1;
157 }
158
159 int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
160 {
161     ODR odr;
162     YAZ_PQF_Parser parser;
163     Z_RPNQuery *rpn;
164     const char *setname="rsetname";
165     zint hits;
166     ZEBRA_RES rc;
167
168     yaz_log(log_level, "======================================");
169     yaz_log(log_level, "query: %s", query);
170     odr = odr_createmem (ODR_DECODE);
171     if (!odr)
172         return 0;
173
174     parser = yaz_pqf_create();
175     rpn = yaz_pqf_parse(parser, odr, query);
176     yaz_pqf_destroy(parser);
177     if (!rpn)
178     {
179         yaz_log(log_level, "could not parse pqf query %s\n", query);
180         printf("could not parse pqf query %s\n", query);
181         odr_destroy(odr);
182         return 0;
183     }
184
185     rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
186     odr_destroy(odr);
187     if (experror)
188     {
189         int code;
190         if (rc != ZEBRA_FAIL)
191         {
192             yaz_log(log_level, "search returned %d (OK), but error was "
193                     "expected", rc);
194             printf("Error: search returned %d (OK), but error was expected\n"
195                    "%s\n",  rc, query);
196             return 0;
197         }
198         code = zebra_errCode(zh);
199         if (code != experror)
200         {
201             yaz_log(log_level, "search returned error code %d, but error %d "
202                     "was expected", code, experror);
203             printf("Error: search returned error code %d, but error %d was "
204                    "expected\n%s\n",
205                    code, experror, query);
206             return 0;
207         }
208     }
209     else
210     {
211         if (rc == ZEBRA_FAIL) {
212             int code = zebra_errCode(zh);
213             yaz_log(log_level, "search returned %d. Code %d", rc, code);
214             
215             printf("Error: search returned %d. Code %d\n%s\n", rc, 
216                    code, query);
217             return 0;
218         }
219         if (exphits != -1 && hits != exphits)
220         {
221             yaz_log(log_level, "search returned " ZINT_FORMAT 
222                    " hits instead of " ZINT_FORMAT, hits, exphits);
223             printf("Error: search returned " ZINT_FORMAT 
224                    " hits instead of " ZINT_FORMAT "\n%s\n",
225                    hits, exphits, query);
226             return 0;
227         }
228     }
229     return 1;
230 }
231
232
233 int tl_query(ZebraHandle zh, const char *query, zint exphits)
234 {
235     return tl_query_x(zh, query, exphits, 0);
236 }
237
238 int tl_scan(ZebraHandle zh, const char *query,
239             int pos, int num,
240             int exp_pos, int exp_num, int exp_partial,
241             const char **exp_entries)
242 {
243     int ret = 1;
244     ODR odr = odr_createmem(ODR_ENCODE);
245     ZebraScanEntry *entries = 0;
246     int partial = -123;
247     ZEBRA_RES res;
248
249     yaz_log(log_level, "======================================");
250     yaz_log(log_level, "scan: pos=%d num=%d %s", pos, num, query);
251
252     res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial, 
253                          0 /* setname */);
254
255     if (partial == -123)
256     {
257         printf("Error: scan returned OK, but partial was not set\n"
258                "%s\n", query);
259         ret = 0;
260     }
261     if (partial != exp_partial)
262     {
263         printf("Error: scan OK, with partial/expected %d/%d\n",
264                partial, exp_partial);
265         ret = 0;
266     }
267     if (res != ZEBRA_OK) /* failure */
268     {
269         if (exp_entries)
270         {
271             printf("Error: scan failed, but no error was expected\n");
272             ret = 0;
273         }
274     }
275     else
276     {
277         if (!exp_entries)
278         {
279             printf("Error: scan OK, but error was expected\n");
280             ret = 0;
281         }
282         else
283         {
284             int fails = 0;
285             if (num != exp_num)
286             {
287                 printf("Error: scan OK, with num/expected %d/%d\n",
288                        num, exp_num);
289                 fails++;
290             }
291             if (pos != exp_pos)
292             {
293                 printf("Error: scan OK, with pos/expected %d/%d\n",
294                        pos, exp_pos);
295                 fails++;
296             }
297             if (!fails)
298             {
299                 if (exp_entries)
300                 {
301                     int i;
302                     for (i = 0; i<num; i++)
303                     {
304                         if (strcmp(exp_entries[i], entries[i].term))
305                         {
306                             printf("Error: scan OK of %s, no %d got=%s exp=%s\n",
307                                    query, i, entries[i].term, exp_entries[i]);
308                             fails++;
309                         }
310                     }
311                 }
312             }
313             if (fails)
314                 ret = 0;
315         }
316     }
317     odr_destroy(odr);
318     return ret;
319 }
320
321 /** 
322  * makes a query, checks number of hits, and for the first hit, that 
323  * it contains the given string, and that it gets the right score
324  */
325 int tl_ranking_query(ZebraHandle zh, char *query, 
326                      int exphits, char *firstrec, int firstscore)
327 {
328     ZebraRetrievalRecord retrievalRecord[10];
329     ODR odr_output = 0;
330     const char *setname = "rsetname";
331     int rc;
332     int i;
333     int ret = 1;
334         
335     if (!tl_query(zh, query, exphits))
336         return 0;
337
338     for (i = 0; i<10; i++)
339         retrievalRecord[i].position = i+1;
340
341     odr_output = odr_createmem(ODR_ENCODE);    
342     rc = zebra_records_retrieve(zh, odr_output, setname, 0,
343                                 VAL_TEXT_XML, exphits, retrievalRecord);
344     if (rc != ZEBRA_OK)
345         ret = 0;
346     else if (!strstr(retrievalRecord[0].buf, firstrec))
347     {
348         printf("Error: Got the wrong record first\n");
349         printf("Expected '%s' but got\n", firstrec);
350         printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
351         ret = 0;
352     }
353     else if (retrievalRecord[0].score != firstscore)
354     {
355         printf("Error: first rec got score %d instead of %d\n",
356                retrievalRecord[0].score, firstscore);
357         ret = 0;
358     }
359     odr_destroy (odr_output);
360     return ret;
361 }
362
363 int tl_meta_query(ZebraHandle zh, char *query, int exphits,
364                   zint *ids)
365 {
366     ZebraMetaRecord *meta;
367     const char *setname= "rsetname";
368     zint *positions = 0;
369     int i, ret = 1;
370         
371     if (!tl_query(zh, query, exphits))
372         return 0;
373     
374     positions = (zint *) xmalloc(1 + (exphits * sizeof(zint)));
375     for (i = 0; i<exphits; i++)
376         positions[i] = i+1;
377
378     meta = zebra_meta_records_create(zh, setname,  exphits, positions);
379     
380     if (!meta)
381     {
382         printf("Error: retrieve returned error\n%s\n", query);
383         xfree(positions);
384         return 0;
385     }
386
387     for (i = 0; i<exphits; i++)
388     {
389         if (meta[i].sysno != ids[i])
390         {
391             printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
392                    ids[i], meta[i].sysno);
393             ret = 0;
394         }
395     }
396     zebra_meta_records_destroy(zh, meta, exphits);
397     xfree(positions);
398     return ret;
399 }
400
401 int tl_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
402 {
403     ZebraMetaRecord *recs;
404     zint i;
405     int errs = 0;
406     zint min_val_recs = 0;
407     zint min_val_exp = 0;
408
409     assert(query);
410     if (!tl_query(zh, query, hits))
411         return 0;
412
413     recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
414     if (!recs)
415         return 0;
416
417     /* find min for each sequence to get proper base offset */
418     for (i = 0; i<hits; i++)
419     {
420         if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
421             min_val_recs = recs[i].sysno;
422         if (min_val_exp == 0 || exp[i] < min_val_exp)
423             min_val_exp = exp[i];
424     }
425             
426     /* compare sequences using base offset */
427     for (i = 0; i<hits; i++)
428         if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
429             errs++;
430     if (errs)
431     {
432         printf("Sequence not in right order for query\n%s\ngot exp\n",
433                query);
434         for (i = 0; i<hits; i++)
435             printf(" " ZINT_FORMAT "   " ZINT_FORMAT "\n",
436                    recs[i].sysno, exp[i]);
437     }
438     zebra_meta_records_destroy (zh, recs, 4);
439
440     if (errs)
441         return 0;
442     return 1;
443 }
444
445
446 struct finfo {
447     const char *name;
448     int occurred;
449 };
450
451 static void filter_cb(void *cd, const char *name)
452 {
453     struct finfo *f = (struct finfo*) cd;
454     if (!strcmp(f->name, name))
455         f->occurred = 1;
456 }
457
458 void tl_check_filter(ZebraService zs, const char *name)
459 {
460     struct finfo f;
461
462     f.name = name;
463     f.occurred = 0;
464     zebra_filter_info(zs, &f, filter_cb);
465     if (!f.occurred)
466     {
467         yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
468         exit(0);
469     }
470 }
471
472
473
474
475 /*
476  * Local variables:
477  * c-basic-offset: 4
478  * indent-tabs-mode: nil
479  * End:
480  * vim: shiftwidth=4 tabstop=8 expandtab
481  */
482