Happy new year
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* This file is part of the Zebra server.
2    Copyright (C) Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** testlib - utilities for the api tests */
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
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 <yaz/oid_db.h>
39 #include <idzebra/api.h>
40 #include "testlib.h"
41
42 int log_level = YLOG_LOG;
43
44 /*
45  * tl_start_up : do common start things, and a zebra_start
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     return tl_zebra_start(cfgname);
62 }
63
64 /**
65  * get_srcdir: return env srcdir or . (if does does not exist)
66  */
67 const char *tl_get_srcdir(void)
68 {
69     const char *srcdir = getenv("srcdir");
70     if (!srcdir || ! *srcdir)
71         srcdir = ".";
72     return srcdir;
73
74 }
75 /** tl_zebra_start - do a zebra_start with a decent config name */
76 ZebraService tl_zebra_start(const char *cfgname)
77 {
78     char cfg[256];
79     const char *srcdir = tl_get_srcdir();
80     if (!cfgname || ! *cfgname )
81         cfgname="zebra.cfg";
82
83     sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
84     return  zebra_start(cfg);
85 }
86
87 /** tl_close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
88 int tl_close_down(ZebraHandle zh, ZebraService zs)
89 {
90     if (zh)
91         zebra_close(zh);
92     if (zs)
93         zebra_stop(zs);
94
95     xmalloc_trav("x");
96     return 1;
97 }
98
99 /** inits the database and inserts test data */
100
101 int tl_init_data(ZebraHandle zh, const char **recs)
102 {
103     ZEBRA_RES res;
104
105     if (!zh)
106         return 0;
107
108     if (zebra_select_database(zh, "Default") != ZEBRA_OK)
109         return 0;
110
111     yaz_log(log_level, "going to call init");
112     res = zebra_init(zh);
113     if (res == ZEBRA_FAIL)
114     {
115         yaz_log(log_level, "init_data: zebra_init failed with %d", res);
116         printf("init_data failed with %d\n", res);
117         return 0;
118     }
119     if (recs)
120     {
121         int i;
122         if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
123             return 0;
124         for (i = 0; recs[i]; i++)
125         {
126             if (zebra_add_record(zh, recs[i], strlen(recs[i])) != ZEBRA_OK)
127             {
128                 if (zebra_end_trans(zh) != ZEBRA_OK)
129                     return 0;
130                 return 0;
131             }
132         }
133         if (zebra_end_trans(zh) != ZEBRA_OK)
134             return 0;
135         zebra_commit(zh);
136     }
137     return 1;
138 }
139
140 int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
141 {
142     ODR odr;
143     YAZ_PQF_Parser parser;
144     Z_RPNQuery *rpn;
145     const char *setname="rsetname";
146     zint hits;
147     ZEBRA_RES rc;
148
149     yaz_log(log_level, "======================================");
150     yaz_log(log_level, "query: %s", query);
151     odr = odr_createmem (ODR_DECODE);
152     if (!odr)
153         return 0;
154
155     parser = yaz_pqf_create();
156     rpn = yaz_pqf_parse(parser, odr, query);
157     yaz_pqf_destroy(parser);
158     if (!rpn)
159     {
160         yaz_log(log_level, "could not parse pqf query %s\n", query);
161         printf("could not parse pqf query %s\n", query);
162         odr_destroy(odr);
163         return 0;
164     }
165
166     rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
167     odr_destroy(odr);
168     if (experror)
169     {
170         int code;
171         if (rc != ZEBRA_FAIL)
172         {
173             yaz_log(log_level, "search returned %d (OK), but error was "
174                     "expected", rc);
175             printf("Error: search returned %d (OK), but error was expected\n"
176                    "%s\n",  rc, query);
177             return 0;
178         }
179         code = zebra_errCode(zh);
180         if (code != experror)
181         {
182             yaz_log(log_level, "search returned error code %d, but error %d "
183                     "was expected", code, experror);
184             printf("Error: search returned error code %d, but error %d was "
185                    "expected\n%s\n",
186                    code, experror, query);
187             return 0;
188         }
189     }
190     else
191     {
192         if (rc == ZEBRA_FAIL) {
193             int code = zebra_errCode(zh);
194             yaz_log(log_level, "search returned %d. Code %d", rc, code);
195
196             printf("Error: search returned %d. Code %d\n%s\n", rc,
197                    code, query);
198             return 0;
199         }
200         if (exphits != -1 && hits != exphits)
201         {
202             yaz_log(log_level, "search returned " ZINT_FORMAT
203                    " hits instead of " ZINT_FORMAT, hits, exphits);
204             printf("Error: search returned " ZINT_FORMAT
205                    " hits instead of " ZINT_FORMAT "\n%s\n",
206                    hits, exphits, query);
207             return 0;
208         }
209     }
210     return 1;
211 }
212
213
214 int tl_query(ZebraHandle zh, const char *query, zint exphits)
215 {
216     return tl_query_x(zh, query, exphits, 0);
217 }
218
219 int tl_scan(ZebraHandle zh, const char *query,
220             int pos, int num,
221             int exp_pos, int exp_num, int exp_partial,
222             const char **exp_entries)
223 {
224     int ret = 1;
225     ODR odr = odr_createmem(ODR_ENCODE);
226     ZebraScanEntry *entries = 0;
227     int partial = -123;
228     ZEBRA_RES res;
229
230     yaz_log(log_level, "======================================");
231     yaz_log(log_level, "scan: pos=%d num=%d %s", pos, num, query);
232
233     res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
234                          0 /* setname */);
235
236     if (partial == -123)
237     {
238         printf("Error: scan returned OK, but partial was not set\n"
239                "%s\n", query);
240         ret = 0;
241     }
242     if (partial != exp_partial)
243     {
244         printf("Error: scan OK, with partial/expected %d/%d\n",
245                partial, exp_partial);
246         ret = 0;
247     }
248     if (res != ZEBRA_OK) /* failure */
249     {
250         if (exp_entries)
251         {
252             printf("Error: scan failed, but no error was expected\n");
253             ret = 0;
254         }
255     }
256     else
257     {
258         if (!exp_entries)
259         {
260             printf("Error: scan OK, but error was expected\n");
261             ret = 0;
262         }
263         else
264         {
265             int fails = 0;
266             if (num != exp_num)
267             {
268                 printf("Error: scan OK, with num/expected %d/%d\n",
269                        num, exp_num);
270                 fails++;
271             }
272             if (pos != exp_pos)
273             {
274                 printf("Error: scan OK, with pos/expected %d/%d\n",
275                        pos, exp_pos);
276                 fails++;
277             }
278             if (!fails)
279             {
280                 if (exp_entries)
281                 {
282                     int i;
283                     for (i = 0; i<num; i++)
284                     {
285                         if (strcmp(exp_entries[i], entries[i].term))
286                         {
287                             printf("Error: scan OK of %s, no %d got=%s exp=%s\n",
288                                    query, i, entries[i].term, exp_entries[i]);
289                             fails++;
290                         }
291                     }
292                 }
293             }
294             if (fails)
295                 ret = 0;
296         }
297     }
298     odr_destroy(odr);
299     return ret;
300 }
301
302 /**
303  * makes a query, checks number of hits, and for the first hit, that
304  * it contains the given string, and that it gets the right score
305  */
306 int tl_ranking_query(ZebraHandle zh, char *query,
307                      int exphits, char *firstrec, int firstscore)
308 {
309     ZebraRetrievalRecord retrievalRecord[10];
310     ODR odr_output = 0;
311     const char *setname = "rsetname";
312     int rc;
313     int i;
314     int ret = 1;
315
316     if (!tl_query(zh, query, exphits))
317         return 0;
318
319     for (i = 0; i<10; i++)
320         retrievalRecord[i].position = i+1;
321
322     odr_output = odr_createmem(ODR_ENCODE);
323     rc = zebra_records_retrieve(zh, odr_output, setname, 0,
324                                 yaz_oid_recsyn_xml, exphits, retrievalRecord);
325     if (rc != ZEBRA_OK)
326         ret = 0;
327     else if (!strstr(retrievalRecord[0].buf, firstrec))
328     {
329         printf("Error: Got the wrong record first\n");
330         printf("Expected '%s' but got\n", firstrec);
331         printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
332         ret = 0;
333     }
334     else if (retrievalRecord[0].score != firstscore)
335     {
336         printf("Error: first rec got score %d instead of %d\n",
337                retrievalRecord[0].score, firstscore);
338         ret = 0;
339     }
340     odr_destroy (odr_output);
341     return ret;
342 }
343
344 int tl_meta_query(ZebraHandle zh, char *query, int exphits,
345                   zint *ids)
346 {
347     ZebraMetaRecord *meta;
348     const char *setname= "rsetname";
349     zint *positions = 0;
350     int i, ret = 1;
351
352     if (!tl_query(zh, query, exphits))
353         return 0;
354
355     positions = (zint *) xmalloc(1 + (exphits * sizeof(zint)));
356     for (i = 0; i<exphits; i++)
357         positions[i] = i+1;
358
359     meta = zebra_meta_records_create(zh, setname,  exphits, positions);
360
361     if (!meta)
362     {
363         printf("Error: retrieve returned error\n%s\n", query);
364         xfree(positions);
365         return 0;
366     }
367
368     for (i = 0; i<exphits; i++)
369     {
370         if (meta[i].sysno != ids[i])
371         {
372             printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
373                    ids[i], meta[i].sysno);
374             ret = 0;
375         }
376     }
377     zebra_meta_records_destroy(zh, meta, exphits);
378     xfree(positions);
379     return ret;
380 }
381
382 int tl_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
383 {
384     ZebraMetaRecord *recs;
385     zint i;
386     int errs = 0;
387     zint min_val_recs = 0;
388     zint min_val_exp = 0;
389
390     assert(query);
391     if (!tl_query(zh, query, hits))
392         return 0;
393
394     recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
395     if (!recs)
396         return 0;
397
398     /* find min for each sequence to get proper base offset */
399     for (i = 0; i<hits; i++)
400     {
401         if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
402             min_val_recs = recs[i].sysno;
403         if (min_val_exp == 0 || exp[i] < min_val_exp)
404             min_val_exp = exp[i];
405     }
406
407     /* compare sequences using base offset */
408     for (i = 0; i<hits; i++)
409         if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
410             errs++;
411     if (errs)
412     {
413         printf("Sequence not in right order for query\n%s\ngot exp\n",
414                query);
415         for (i = 0; i<hits; i++)
416             printf(" " ZINT_FORMAT "   " ZINT_FORMAT "\n",
417                    recs[i].sysno, exp[i]);
418     }
419     zebra_meta_records_destroy (zh, recs, 4);
420
421     if (errs)
422         return 0;
423     return 1;
424 }
425
426
427 struct finfo {
428     const char *name;
429     int occurred;
430 };
431
432 static void filter_cb(void *cd, const char *name)
433 {
434     struct finfo *f = (struct finfo*) cd;
435     if (!strcmp(f->name, name))
436         f->occurred = 1;
437 }
438
439 void tl_check_filter(ZebraService zs, const char *name)
440 {
441     struct finfo f;
442
443     f.name = name;
444     f.occurred = 0;
445     zebra_filter_info(zs, &f, filter_cb);
446     if (!f.occurred)
447     {
448         yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
449         exit(0);
450     }
451 }
452
453 ZEBRA_RES tl_fetch(ZebraHandle zh, int position, const char *element_set,
454                    const Odr_oid * format, ODR odr,
455                    const char **rec_buf, size_t *rec_len)
456 {
457     ZebraRetrievalRecord retrievalRecord[1];
458     Z_RecordComposition *comp;
459     ZEBRA_RES res;
460
461     retrievalRecord[0].position = position;
462
463     yaz_set_esn(&comp, element_set, odr->mem);
464
465     res = zebra_records_retrieve(zh, odr, "rsetname", comp, format, 1,
466                                  retrievalRecord);
467     if (res != ZEBRA_OK)
468     {
469         int code = zebra_errCode(zh);
470         yaz_log(YLOG_FATAL, "zebra_records_retrieve returned error %d",
471                 code);
472     }
473     else
474     {
475         *rec_buf = retrievalRecord[0].buf;
476         *rec_len = retrievalRecord[0].len;
477     }
478     return res;
479 }
480
481 ZEBRA_RES tl_fetch_compare(ZebraHandle zh,
482                            int position, const char *element_set,
483                            const Odr_oid *format, const char *cmp_rec)
484 {
485     const char *rec_buf = 0;
486     size_t rec_len = 0;
487     ODR odr = odr_createmem(ODR_ENCODE);
488     ZEBRA_RES res = tl_fetch(zh, position, element_set, format, odr,
489                              &rec_buf, &rec_len);
490     if (res == ZEBRA_OK)
491     {
492         if (strlen(cmp_rec) != rec_len)
493             res = ZEBRA_FAIL;
494         else if (memcmp(cmp_rec, rec_buf, rec_len))
495             res = ZEBRA_FAIL;
496         if (res == ZEBRA_FAIL)
497         {
498             int l = rec_len;
499             yaz_log(YLOG_LOG, "Expected: %s", cmp_rec);
500             yaz_log(YLOG_LOG, "Got: %.*s", l, rec_buf);
501         }
502     }
503     odr_destroy(odr);
504     return res;
505 }
506
507 ZEBRA_RES tl_fetch_first_compare(ZebraHandle zh,
508                                  const char *element_set,
509                                  const Odr_oid *format, const char *cmp_rec)
510 {
511     return tl_fetch_compare(zh, 1, element_set, format, cmp_rec);
512 }
513
514 /*
515  * Local variables:
516  * c-basic-offset: 4
517  * c-file-style: "Stroustrup"
518  * indent-tabs-mode: nil
519  * End:
520  * vim: shiftwidth=4 tabstop=8 expandtab
521  */
522