Removed livcode ranking
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.1 2004-10-28 10:37:15 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 <yaz/log.h>
26 #include <yaz/pquery.h>
27 #include <idzebra/api.h>
28
29
30 /* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
31 ZebraService start_service(char *cfgname)
32 {
33     char cfg[256];
34     char *srcdir = getenv("srcdir");
35     if (!srcdir || ! *srcdir)
36         srcdir=".";
37     if (!cfgname || ! *cfgname )
38         cfgname="zebra.cfg";
39     /*sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");     */
40
41     sprintf(cfg, "%.200s/%s",srcdir, cfgname);
42     return zebra_start(cfg);
43 }
44
45 /** 
46  * makes a query, checks number of hits, and for the first hit, that 
47  * it contains the given string, and that it gets the right score
48  */
49 void RankingQuery(int lineno, ZebraHandle zh, char *query, 
50           int exphits, char *firstrec, int firstscore )
51 {
52     ZebraRetrievalRecord retrievalRecord[10];
53     ODR odr_output = odr_createmem (ODR_DECODE);    
54     ODR odr_input = odr_createmem (ODR_DECODE);    
55     YAZ_PQF_Parser parser = yaz_pqf_create();
56     Z_RPNQuery *rpn = yaz_pqf_parse(parser, odr_input, query);
57     const char *setname="rsetname";
58     int hits;
59     int rc;
60     int i;
61         
62     logf(LOG_LOG,"======================================");
63     logf(LOG_LOG,"qry[%d]: %s", lineno, query);
64
65     if (!rpn) {
66         printf("Error: Parse failed \n%s\n",query);
67         exit(1);
68     }
69     rc=zebra_search_RPN (zh, odr_input, rpn, setname, &hits);
70     if (rc) {
71         printf("Error: search returned %d \n%s\n",rc,query);
72         exit (1);
73     }
74
75     if (hits != exphits) {
76         printf("Error: search returned %d hits instead of %d\n",
77                 hits, exphits);
78         exit (1);
79     }
80     yaz_pqf_destroy(parser);
81
82     for (i = 0; i<10; i++)
83     {
84         retrievalRecord[i].position = i+1;
85         retrievalRecord[i].score = i+20000;
86     }
87
88     rc=zebra_records_retrieve (zh, odr_output, setname, 0,
89                      VAL_TEXT_XML, hits, retrievalRecord);
90
91     if (rc) {
92         printf("Error: retrieve returned %d \n%s\n",rc,query);
93         exit (1);
94     }
95
96     if (!strstr(retrievalRecord[0].buf, firstrec))
97     {
98         printf("Error: Got the wrong record first\n");
99         printf("Expected '%s' but got \n",firstrec);
100         printf("%.*s\n",retrievalRecord[0].len,retrievalRecord[0].buf);
101         exit(1);
102     }
103     
104     if (retrievalRecord[0].score != firstscore)
105     {
106         printf("Error: first rec got score %d instead of %d\n",
107                 retrievalRecord[0].score, firstscore);
108         exit(1);
109     }
110     odr_destroy (odr_output);
111     odr_destroy (odr_input);
112 }
113