Refactored more stuff into testlib, cleaned up the tests.
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.3 2004-10-29 13:02:39 heikki 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/log.h>
27 #include <yaz/pquery.h>
28 #include <idzebra/api.h>
29 #include "testlib.h"
30
31 /** start_log: open a log file */
32 /*    FIXME - parse command line arguments to set log levels etc */
33 void start_log(int argc, char **argv)
34 {
35     char logname[2048];
36     if (!argv) 
37         return;
38     if (!argv[0])
39         return;
40     sprintf(logname, "%s.log", argv[0]);
41     yaz_log_init_file(logname);
42 }
43
44 /** 
45  * 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 start_up(char *cfgname, int argc, char **argv)
53 {
54     nmem_init();
55     start_log(argc, argv);
56     return start_service(cfgname);
57 }
58
59 /** start_service - do a zebra_start with a decent config name */
60 ZebraService start_service(char *cfgname)
61 {
62     char cfg[256];
63     char *srcdir = getenv("srcdir");
64     ZebraService zs;
65     if (!srcdir || ! *srcdir)
66         srcdir=".";
67     if (!cfgname || ! *cfgname )
68         cfgname="zebra.cfg";
69
70     sprintf(cfg, "%.200s/%s",srcdir, cfgname);
71     zs=zebra_start(cfg);
72     if (!zs)
73     {
74         printf("zebra_start failed, probably because missing config file \n"
75                "check %s\n", cfg);
76         exit(9);
77     }
78     return zs;
79 }
80
81
82 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
83 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
84 {
85     if (zh)
86         zebra_close(zh);
87     if (zs)
88         zebra_stop(zs);
89
90     if (retcode)
91         logf(LOG_LOG,"========= Exiting with return code %d", retcode);
92     else
93         logf(LOG_LOG,"========= All tests OK");
94     nmem_exit();
95     xmalloc_trav("x");
96     return retcode;
97 }
98
99 /** inits the database and inserts test data */
100
101 void init_data( ZebraHandle zh, const char **recs)
102 {
103     int i;
104     char *addinfo;
105     assert(zh);
106     zebra_select_database(zh, "Default");
107     logf(LOG_LOG,"going to call init");
108     i=zebra_init(zh);
109     logf(LOG_LOG,"init returned %d",i);
110     if (i) 
111     {
112         printf("init failed with %d\n",i);
113         zebra_result(zh, &i, &addinfo);
114         printf("  Error %d   %s\n",i,addinfo);
115         exit(1);
116     }
117     if (recs)
118     {
119         zebra_begin_trans (zh, 1);
120         for (i = 0; recs[i]; i++)
121             zebra_add_record (zh, recs[i], strlen(recs[i]));
122         zebra_end_trans (zh);
123         zebra_commit (zh);
124     }
125
126 }
127
128
129
130 int do_query(int lineno, ZebraHandle zh, char *query, int exphits)
131 {
132     ODR odr;
133     YAZ_PQF_Parser parser;
134     Z_RPNQuery *rpn;
135     const char *setname="rsetname";
136     int hits;
137     int rc;
138         
139
140     logf(LOG_LOG,"======================================");
141     logf(LOG_LOG,"qry[%d]: %s", lineno, query);
142     odr=odr_createmem (ODR_DECODE);    
143
144     parser = yaz_pqf_create();
145     rpn = yaz_pqf_parse(parser, odr, query);
146     if (!rpn) {
147         printf("Error: Parse failed \n%s\n",query);
148         exit(1);
149     }
150     rc=zebra_search_RPN (zh, odr, rpn, setname, &hits);
151     if (rc) {
152         printf("Error: search returned %d \n%s\n",rc,query);
153         exit (1);
154     }
155
156     if (hits != exphits) {
157         printf("Error: search returned %d hits instead of %d\n",
158                 hits, exphits);
159         exit (1);
160     }
161     yaz_pqf_destroy(parser);
162     odr_destroy (odr);
163     return hits;
164 }
165
166
167 /** 
168  * makes a query, checks number of hits, and for the first hit, that 
169  * it contains the given string, and that it gets the right score
170  */
171 void ranking_query(int lineno, ZebraHandle zh, char *query, 
172           int exphits, char *firstrec, int firstscore )
173 {
174     ZebraRetrievalRecord retrievalRecord[10];
175     ODR odr_output = odr_createmem (ODR_ENCODE);    
176     const char *setname="rsetname";
177     int hits;
178     int rc;
179     int i;
180         
181     hits=do_query(lineno, zh, query, exphits);
182
183     for (i = 0; i<10; i++)
184         retrievalRecord[i].position = i+1;
185
186     rc=zebra_records_retrieve (zh, odr_output, setname, 0,
187                      VAL_TEXT_XML, hits, retrievalRecord);
188
189     if (rc) {
190         printf("Error: retrieve returned %d \n%s\n",rc,query);
191         exit (1);
192     }
193
194     if (!strstr(retrievalRecord[0].buf, firstrec))
195     {
196         printf("Error: Got the wrong record first\n");
197         printf("Expected '%s' but got \n",firstrec);
198         printf("%.*s\n",retrievalRecord[0].len,retrievalRecord[0].buf);
199         exit(1);
200     }
201     
202     if (retrievalRecord[0].score != firstscore)
203     {
204         printf("Error: first rec got score %d instead of %d\n",
205                 retrievalRecord[0].score, firstscore);
206         exit(1);
207     }
208     odr_destroy (odr_output);
209 }
210