Cleaned the test programs a bit, used routines in testlib
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.2 2004-10-28 15:24:36 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
30
31 /* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
32 ZebraService start_service(char *cfgname)
33 {
34     char cfg[256];
35     char *srcdir = getenv("srcdir");
36     ZebraService zs;
37     if (!srcdir || ! *srcdir)
38         srcdir=".";
39     if (!cfgname || ! *cfgname )
40         cfgname="zebra.cfg";
41     /*sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");     */
42
43     sprintf(cfg, "%.200s/%s",srcdir, cfgname);
44     zs=zebra_start(cfg);
45     if (!zs)
46     {
47         printf("zebra_start failed, probably because missing config file \n"
48                "check %s\n", cfg);
49         exit(9);
50     }
51     return zs;
52 }
53
54 /** inits the database and inserts test data */
55
56 void init_data( ZebraHandle zh, const char **recs)
57 {
58     int i;
59     char *addinfo;
60     assert(zh);
61     zebra_select_database(zh, "Default");
62     logf(LOG_LOG,"going to call init");
63     i=zebra_init(zh);
64     logf(LOG_LOG,"init returned %d",i);
65     if (i) 
66     {
67         printf("init failed with %d\n",i);
68         zebra_result(zh, &i, &addinfo);
69         printf("  Error %d   %s\n",i,addinfo);
70         exit(1);
71     }
72     zebra_begin_trans (zh, 1);
73     for (i = 0; recs[i]; i++)
74         zebra_add_record (zh, recs[i], strlen(recs[i]));
75     zebra_end_trans (zh);
76     zebra_commit (zh);
77
78 }
79
80
81
82 int Query(int lineno, ZebraHandle zh, char *query, int exphits)
83 {
84     ODR odr;
85     YAZ_PQF_Parser parser;
86     Z_RPNQuery *rpn;
87     const char *setname="rsetname";
88     int hits;
89     int rc;
90         
91
92     logf(LOG_LOG,"======================================");
93     logf(LOG_LOG,"qry[%d]: %s", lineno, query);
94     odr=odr_createmem (ODR_DECODE);    
95
96     parser = yaz_pqf_create();
97     rpn = yaz_pqf_parse(parser, odr, query);
98     if (!rpn) {
99         printf("Error: Parse failed \n%s\n",query);
100         exit(1);
101     }
102     rc=zebra_search_RPN (zh, odr, rpn, setname, &hits);
103     if (rc) {
104         printf("Error: search returned %d \n%s\n",rc,query);
105         exit (1);
106     }
107
108     if (hits != exphits) {
109         printf("Error: search returned %d hits instead of %d\n",
110                 hits, exphits);
111         exit (1);
112     }
113     yaz_pqf_destroy(parser);
114     odr_destroy (odr);
115     return hits;
116 }
117
118
119 /** 
120  * makes a query, checks number of hits, and for the first hit, that 
121  * it contains the given string, and that it gets the right score
122  */
123 void RankingQuery(int lineno, ZebraHandle zh, char *query, 
124           int exphits, char *firstrec, int firstscore )
125 {
126     ZebraRetrievalRecord retrievalRecord[10];
127     ODR odr_output = odr_createmem (ODR_ENCODE);    
128     const char *setname="rsetname";
129     int hits;
130     int rc;
131     int i;
132         
133     hits=Query(lineno, zh, query, exphits);
134
135     for (i = 0; i<10; i++)
136         retrievalRecord[i].position = i+1;
137
138     rc=zebra_records_retrieve (zh, odr_output, setname, 0,
139                      VAL_TEXT_XML, hits, retrievalRecord);
140
141     if (rc) {
142         printf("Error: retrieve returned %d \n%s\n",rc,query);
143         exit (1);
144     }
145
146     if (!strstr(retrievalRecord[0].buf, firstrec))
147     {
148         printf("Error: Got the wrong record first\n");
149         printf("Expected '%s' but got \n",firstrec);
150         printf("%.*s\n",retrievalRecord[0].len,retrievalRecord[0].buf);
151         exit(1);
152     }
153     
154     if (retrievalRecord[0].score != firstscore)
155     {
156         printf("Error: first rec got score %d instead of %d\n",
157                 retrievalRecord[0].score, firstscore);
158         exit(1);
159     }
160     odr_destroy (odr_output);
161 }
162