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