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
5 This file is part of the Zebra server.
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
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
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
23 /** testlib - utilities for the api tests */
27 #include <yaz/pquery.h>
28 #include <idzebra/api.h>
31 /* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
32 ZebraService start_service(char *cfgname)
35 char *srcdir = getenv("srcdir");
37 if (!srcdir || ! *srcdir)
39 if (!cfgname || ! *cfgname )
41 /*sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : ""); */
43 sprintf(cfg, "%.200s/%s",srcdir, cfgname);
47 printf("zebra_start failed, probably because missing config file \n"
54 /** inits the database and inserts test data */
56 void init_data( ZebraHandle zh, const char **recs)
61 zebra_select_database(zh, "Default");
62 logf(LOG_LOG,"going to call init");
64 logf(LOG_LOG,"init returned %d",i);
67 printf("init failed with %d\n",i);
68 zebra_result(zh, &i, &addinfo);
69 printf(" Error %d %s\n",i,addinfo);
72 zebra_begin_trans (zh, 1);
73 for (i = 0; recs[i]; i++)
74 zebra_add_record (zh, recs[i], strlen(recs[i]));
82 int Query(int lineno, ZebraHandle zh, char *query, int exphits)
85 YAZ_PQF_Parser parser;
87 const char *setname="rsetname";
92 logf(LOG_LOG,"======================================");
93 logf(LOG_LOG,"qry[%d]: %s", lineno, query);
94 odr=odr_createmem (ODR_DECODE);
96 parser = yaz_pqf_create();
97 rpn = yaz_pqf_parse(parser, odr, query);
99 printf("Error: Parse failed \n%s\n",query);
102 rc=zebra_search_RPN (zh, odr, rpn, setname, &hits);
104 printf("Error: search returned %d \n%s\n",rc,query);
108 if (hits != exphits) {
109 printf("Error: search returned %d hits instead of %d\n",
113 yaz_pqf_destroy(parser);
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
123 void RankingQuery(int lineno, ZebraHandle zh, char *query,
124 int exphits, char *firstrec, int firstscore )
126 ZebraRetrievalRecord retrievalRecord[10];
127 ODR odr_output = odr_createmem (ODR_ENCODE);
128 const char *setname="rsetname";
133 hits=Query(lineno, zh, query, exphits);
135 for (i = 0; i<10; i++)
136 retrievalRecord[i].position = i+1;
138 rc=zebra_records_retrieve (zh, odr_output, setname, 0,
139 VAL_TEXT_XML, hits, retrievalRecord);
142 printf("Error: retrieve returned %d \n%s\n",rc,query);
146 if (!strstr(retrievalRecord[0].buf, firstrec))
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);
154 if (retrievalRecord[0].score != firstscore)
156 printf("Error: first rec got score %d instead of %d\n",
157 retrievalRecord[0].score, firstscore);
160 odr_destroy (odr_output);