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