Move X-Path tests to xpath subdirectory.
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.7 2004-12-02 14:05:04 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 /**
64  * get_srcdir: return env srcdir or . (if does does not exist)
65  */
66 const char *get_srcdir()
67 {
68     const char *srcdir = getenv("srcdir");
69     if (!srcdir || ! *srcdir)
70         srcdir=".";
71     return srcdir;
72
73 }
74 /** start_service - do a zebra_start with a decent config name */
75 ZebraService start_service(char *cfgname)
76 {
77     char cfg[256];
78     const char *srcdir = get_srcdir();
79     ZebraService zs;
80     if (!cfgname || ! *cfgname )
81         cfgname="zebra.cfg";
82
83     sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
84     zs=zebra_start(cfg);
85     if (!zs)
86     {
87         printf("zebra_start failed, probably because missing config file \n"
88                "check %s\n", cfg);
89         exit(9);
90     }
91     return zs;
92 }
93
94
95 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
96 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
97 {
98     if (zh)
99         zebra_close(zh);
100     if (zs)
101         zebra_stop(zs);
102
103     if (retcode)
104         yaz_log(log_level,"========= Exiting with return code %d", retcode);
105     else
106         yaz_log(log_level,"========= All tests OK");
107     nmem_exit();
108     xmalloc_trav("x");
109     return retcode;
110 }
111
112 /** inits the database and inserts test data */
113
114 void init_data(ZebraHandle zh, const char **recs)
115 {
116     int i;
117     char *addinfo;
118     assert(zh);
119     zebra_select_database(zh, "Default");
120     yaz_log(log_level, "going to call init");
121     i = zebra_init(zh);
122     yaz_log(log_level, "init returned %d",i);
123     if (i) 
124     {
125         printf("init failed with %d\n",i);
126         zebra_result(zh, &i, &addinfo);
127         printf("  Error %d   %s\n",i,addinfo);
128         exit(1);
129     }
130     if (recs)
131     {
132         zebra_begin_trans (zh, 1);
133         for (i = 0; recs[i]; i++)
134             zebra_add_record (zh, recs[i], strlen(recs[i]));
135         zebra_end_trans (zh);
136         zebra_commit (zh);
137     }
138
139 }
140
141 int do_query(int lineno, ZebraHandle zh, char *query, int exphits)
142 {
143     ODR odr;
144     YAZ_PQF_Parser parser;
145     Z_RPNQuery *rpn;
146     const char *setname="rsetname";
147     int hits;
148     int rc;
149         
150
151     yaz_log(log_level,"======================================");
152     yaz_log(log_level,"qry[%d]: %s", lineno, query);
153     odr=odr_createmem (ODR_DECODE);    
154
155     parser = yaz_pqf_create();
156     rpn = yaz_pqf_parse(parser, odr, query);
157     if (!rpn) {
158         printf("Error: Parse failed \n%s\n",query);
159         exit(1);
160     }
161     rc = zebra_search_RPN (zh, odr, rpn, setname, &hits);
162     if (rc) {
163         printf("Error: search returned %d \n%s\n",rc,query);
164         exit (1);
165     }
166
167     if (hits != exphits) {
168         printf("Error: search returned %d hits instead of %d\n",
169                 hits, exphits);
170         exit (1);
171     }
172     yaz_pqf_destroy(parser);
173     odr_destroy (odr);
174     return hits;
175 }
176
177
178 /** 
179  * makes a query, checks number of hits, and for the first hit, that 
180  * it contains the given string, and that it gets the right score
181  */
182 void ranking_query(int lineno, ZebraHandle zh, char *query, 
183           int exphits, char *firstrec, int firstscore )
184 {
185     ZebraRetrievalRecord retrievalRecord[10];
186     ODR odr_output = odr_createmem (ODR_ENCODE);    
187     const char *setname="rsetname";
188     int hits;
189     int rc;
190     int i;
191         
192     hits = do_query(lineno, zh, query, exphits);
193
194     for (i = 0; i<10; i++)
195         retrievalRecord[i].position = i+1;
196
197     rc = zebra_records_retrieve (zh, odr_output, setname, 0,
198                                  VAL_TEXT_XML, hits, retrievalRecord);
199     
200     if (rc)
201     {
202         printf("Error: retrieve returned %d \n%s\n",rc,query);
203         exit (1);
204     }
205
206     if (!strstr(retrievalRecord[0].buf, firstrec))
207     {
208         printf("Error: Got the wrong record first\n");
209         printf("Expected '%s' but got \n",firstrec);
210         printf("%.*s\n",retrievalRecord[0].len,retrievalRecord[0].buf);
211         exit(1);
212     }
213     
214     if (retrievalRecord[0].score != firstscore)
215     {
216         printf("Error: first rec got score %d instead of %d\n",
217                retrievalRecord[0].score, firstscore);
218         exit(1);
219     }
220     odr_destroy (odr_output);
221 }
222
223 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
224                 zint *ids)
225 {
226     ZebraMetaRecord *meta;
227     ODR odr_output = odr_createmem (ODR_ENCODE);    
228     const char *setname="rsetname";
229     zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
230     int hits;
231     int rc;
232     int i;
233         
234     hits = do_query(lineno, zh, query, exphits);
235     
236     for (i = 0; i<exphits; i++)
237         positions[i] = i+1;
238
239     meta = zebra_meta_records_create (zh, setname,  exphits, positions);
240     
241     if (!meta)
242     {
243         printf("Error: retrieve returned %d \n%s\n",rc,query);
244         exit (1);
245     }
246
247     for (i = 0; i<exphits; i++)
248     {
249         if (meta[i].sysno != ids[i])
250         {
251             printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT,
252                    ids[i], meta[i].sysno);
253             exit(1);
254         }
255     }
256     zebra_meta_records_destroy(zh, meta, exphits);
257     odr_destroy (odr_output);
258     free(positions);
259 }
260