Add support for section/chapter indexing. Add safari filter.
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.6 2004-11-29 21:55:28 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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/pquery.h>
27 #include <idzebra/api.h>
28 #include "testlib.h"
29
30 /** start_log: open a log file */
31 /*    FIXME - parse command line arguments to set log levels etc */
32 int log_level=0; /* not static, t*.c may use it */
33
34 void start_log(int argc, char **argv)
35 {
36     char logname[2048];
37     if (!argv) 
38         return;
39     if (!argv[0])
40         return;
41     sprintf(logname, "%s.log", argv[0]);
42     yaz_log_init_file(logname);
43     log_level = yaz_log_mask_str_x(argv[0],0);
44     yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level);
45     yaz_log(log_level,"starting %s",argv[0]);
46 }
47
48 /** 
49  * start_up : do common start things, and a zebra_start
50  *    - nmem_init
51  *    - build the name of logfile from argv[0], and open it
52  *      if no argv passed, do not open a log
53  *    - read zebra.cfg from env var srcdir if it exists; otherwise current dir 
54  *      default to zebra.cfg, if no name is given
55  */
56 ZebraService start_up(char *cfgname, int argc, char **argv)
57 {
58     nmem_init();
59     start_log(argc, argv);
60     return start_service(cfgname);
61 }
62
63 /** start_service - do a zebra_start with a decent config name */
64 ZebraService start_service(char *cfgname)
65 {
66     char cfg[256];
67     char *srcdir = getenv("srcdir");
68     ZebraService zs;
69     if (!srcdir || ! *srcdir)
70         srcdir=".";
71     if (!cfgname || ! *cfgname )
72         cfgname="zebra.cfg";
73
74     sprintf(cfg, "%.200s/%s",srcdir, cfgname);
75     zs=zebra_start(cfg);
76     if (!zs)
77     {
78         printf("zebra_start failed, probably because missing config file \n"
79                "check %s\n", cfg);
80         exit(9);
81     }
82     return zs;
83 }
84
85
86 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
87 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
88 {
89     if (zh)
90         zebra_close(zh);
91     if (zs)
92         zebra_stop(zs);
93
94     if (retcode)
95         yaz_log(log_level,"========= Exiting with return code %d", retcode);
96     else
97         yaz_log(log_level,"========= All tests OK");
98     nmem_exit();
99     xmalloc_trav("x");
100     return retcode;
101 }
102
103 /** inits the database and inserts test data */
104
105 void init_data(ZebraHandle zh, const char **recs)
106 {
107     int i;
108     char *addinfo;
109     assert(zh);
110     zebra_select_database(zh, "Default");
111     yaz_log(log_level, "going to call init");
112     i = zebra_init(zh);
113     yaz_log(log_level, "init returned %d",i);
114     if (i) 
115     {
116         printf("init failed with %d\n",i);
117         zebra_result(zh, &i, &addinfo);
118         printf("  Error %d   %s\n",i,addinfo);
119         exit(1);
120     }
121     if (recs)
122     {
123         zebra_begin_trans (zh, 1);
124         for (i = 0; recs[i]; i++)
125             zebra_add_record (zh, recs[i], strlen(recs[i]));
126         zebra_end_trans (zh);
127         zebra_commit (zh);
128     }
129
130 }
131
132 int do_query(int lineno, ZebraHandle zh, char *query, int exphits)
133 {
134     ODR odr;
135     YAZ_PQF_Parser parser;
136     Z_RPNQuery *rpn;
137     const char *setname="rsetname";
138     int hits;
139     int rc;
140         
141
142     yaz_log(log_level,"======================================");
143     yaz_log(log_level,"qry[%d]: %s", lineno, query);
144     odr=odr_createmem (ODR_DECODE);    
145
146     parser = yaz_pqf_create();
147     rpn = yaz_pqf_parse(parser, odr, query);
148     if (!rpn) {
149         printf("Error: Parse failed \n%s\n",query);
150         exit(1);
151     }
152     rc = zebra_search_RPN (zh, odr, rpn, setname, &hits);
153     if (rc) {
154         printf("Error: search returned %d \n%s\n",rc,query);
155         exit (1);
156     }
157
158     if (hits != exphits) {
159         printf("Error: search returned %d hits instead of %d\n",
160                 hits, exphits);
161         exit (1);
162     }
163     yaz_pqf_destroy(parser);
164     odr_destroy (odr);
165     return hits;
166 }
167
168
169 /** 
170  * makes a query, checks number of hits, and for the first hit, that 
171  * it contains the given string, and that it gets the right score
172  */
173 void ranking_query(int lineno, ZebraHandle zh, char *query, 
174           int exphits, char *firstrec, int firstscore )
175 {
176     ZebraRetrievalRecord retrievalRecord[10];
177     ODR odr_output = odr_createmem (ODR_ENCODE);    
178     const char *setname="rsetname";
179     int hits;
180     int rc;
181     int i;
182         
183     hits = do_query(lineno, zh, query, exphits);
184
185     for (i = 0; i<10; i++)
186         retrievalRecord[i].position = i+1;
187
188     rc = zebra_records_retrieve (zh, odr_output, setname, 0,
189                                  VAL_TEXT_XML, hits, retrievalRecord);
190     
191     if (rc)
192     {
193         printf("Error: retrieve returned %d \n%s\n",rc,query);
194         exit (1);
195     }
196
197     if (!strstr(retrievalRecord[0].buf, firstrec))
198     {
199         printf("Error: Got the wrong record first\n");
200         printf("Expected '%s' but got \n",firstrec);
201         printf("%.*s\n",retrievalRecord[0].len,retrievalRecord[0].buf);
202         exit(1);
203     }
204     
205     if (retrievalRecord[0].score != firstscore)
206     {
207         printf("Error: first rec got score %d instead of %d\n",
208                retrievalRecord[0].score, firstscore);
209         exit(1);
210     }
211     odr_destroy (odr_output);
212 }
213
214 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
215                 zint *ids)
216 {
217     ZebraMetaRecord *meta;
218     ODR odr_output = odr_createmem (ODR_ENCODE);    
219     const char *setname="rsetname";
220     zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
221     int hits;
222     int rc;
223     int i;
224         
225     hits = do_query(lineno, zh, query, exphits);
226     
227     for (i = 0; i<exphits; i++)
228         positions[i] = i+1;
229
230     meta = zebra_meta_records_create (zh, setname,  exphits, positions);
231     
232     if (!meta)
233     {
234         printf("Error: retrieve returned %d \n%s\n",rc,query);
235         exit (1);
236     }
237
238     for (i = 0; i<exphits; i++)
239     {
240         if (meta[i].sysno != ids[i])
241         {
242             printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT,
243                    ids[i], meta[i].sysno);
244             exit(1);
245         }
246     }
247     zebra_meta_records_destroy(zh, meta, exphits);
248     odr_destroy (odr_output);
249     free(positions);
250 }
251