X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=test%2Fapi%2Ftestlib.c;h=c32fb1643ce46836cddef5df01df8502d5931591;hb=6c9fcd3b5d3108702fa1ffc92dab4ab6060f9a19;hp=6513bbfb0a66772c844e813f5dd6b4555195925f;hpb=2fefff35eeb40ba802bb3ee11674a6037b84659c;p=idzebra-moved-to-github.git diff --git a/test/api/testlib.c b/test/api/testlib.c index 6513bbf..c32fb16 100644 --- a/test/api/testlib.c +++ b/test/api/testlib.c @@ -1,6 +1,6 @@ -/* $Id: testlib.c,v 1.1 2004-10-28 10:37:15 heikki Exp $ - Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004 - Index Data Aps +/* $Id: testlib.c,v 1.11 2005-01-15 19:38:35 adam Exp $ + Copyright (C) 1995-2005 + Index Data ApS This file is part of the Zebra server. @@ -22,51 +22,144 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA /** testlib - utilities for the api tests */ +#include #include #include #include +#include "testlib.h" +/** start_log: open a log file */ +/* FIXME - parse command line arguments to set log levels etc */ +int log_level=0; /* not static, t*.c may use it */ -/* read zebra.cfg from env var srcdir if it exists; otherwise current dir */ -ZebraService start_service(char *cfgname) +void start_log(int argc, char **argv) { - char cfg[256]; - char *srcdir = getenv("srcdir"); + char logname[2048]; + if (!argv) + return; + if (!argv[0]) + return; + sprintf(logname, "%s.log", argv[0]); + yaz_log_init_file(logname); + log_level = yaz_log_mask_str_x(argv[0],0); + yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level); + yaz_log(log_level,"starting %s",argv[0]); +} + +/** + * start_up : do common start things, and a zebra_start + * - nmem_init + * - build the name of logfile from argv[0], and open it + * if no argv passed, do not open a log + * - read zebra.cfg from env var srcdir if it exists; otherwise current dir + * default to zebra.cfg, if no name is given + */ +ZebraService start_up(char *cfgname, int argc, char **argv) +{ + nmem_init(); + start_log(argc, argv); + return start_service(cfgname); +} + +/** + * get_srcdir: return env srcdir or . (if does does not exist) + */ +const char *get_srcdir() +{ + const char *srcdir = getenv("srcdir"); if (!srcdir || ! *srcdir) srcdir="."; + return srcdir; + +} +/** start_service - do a zebra_start with a decent config name */ +ZebraService start_service(char *cfgname) +{ + char cfg[256]; + const char *srcdir = get_srcdir(); + ZebraService zs; if (!cfgname || ! *cfgname ) cfgname="zebra.cfg"; - /*sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : ""); */ - sprintf(cfg, "%.200s/%s",srcdir, cfgname); - return zebra_start(cfg); + sprintf(cfg, "%.200s/%.50s", srcdir, cfgname); + zs=zebra_start(cfg); + if (!zs) + { + printf("zebra_start failed, probably because missing config file \n" + "check %s\n", cfg); + exit(9); + } + return zs; } -/** - * makes a query, checks number of hits, and for the first hit, that - * it contains the given string, and that it gets the right score - */ -void RankingQuery(int lineno, ZebraHandle zh, char *query, - int exphits, char *firstrec, int firstscore ) + +/** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */ +int close_down(ZebraHandle zh, ZebraService zs, int retcode) { - ZebraRetrievalRecord retrievalRecord[10]; - ODR odr_output = odr_createmem (ODR_DECODE); - ODR odr_input = odr_createmem (ODR_DECODE); - YAZ_PQF_Parser parser = yaz_pqf_create(); - Z_RPNQuery *rpn = yaz_pqf_parse(parser, odr_input, query); + if (zh) + zebra_close(zh); + if (zs) + zebra_stop(zs); + + if (retcode) + yaz_log(log_level,"========= Exiting with return code %d", retcode); + else + yaz_log(log_level,"========= All tests OK"); + nmem_exit(); + xmalloc_trav("x"); + return retcode; +} + +/** inits the database and inserts test data */ + +void init_data(ZebraHandle zh, const char **recs) +{ + int i; + char *addinfo; + assert(zh); + zebra_select_database(zh, "Default"); + yaz_log(log_level, "going to call init"); + i = zebra_init(zh); + yaz_log(log_level, "init returned %d",i); + if (i) + { + printf("init failed with %d\n",i); + zebra_result(zh, &i, &addinfo); + printf(" Error %d %s\n",i,addinfo); + exit(1); + } + if (recs) + { + zebra_begin_trans (zh, 1); + for (i = 0; recs[i]; i++) + zebra_add_record (zh, recs[i], strlen(recs[i])); + zebra_end_trans (zh); + zebra_commit (zh); + } + +} + +int do_query(int lineno, ZebraHandle zh, char *query, int exphits) +{ + ODR odr; + YAZ_PQF_Parser parser; + Z_RPNQuery *rpn; const char *setname="rsetname"; int hits; int rc; - int i; - logf(LOG_LOG,"======================================"); - logf(LOG_LOG,"qry[%d]: %s", lineno, query); + yaz_log(log_level,"======================================"); + yaz_log(log_level,"qry[%d]: %s", lineno, query); + odr=odr_createmem (ODR_DECODE); + + parser = yaz_pqf_create(); + rpn = yaz_pqf_parse(parser, odr, query); if (!rpn) { printf("Error: Parse failed \n%s\n",query); exit(1); } - rc=zebra_search_RPN (zh, odr_input, rpn, setname, &hits); + rc = zebra_search_RPN (zh, odr, rpn, setname, &hits); if (rc) { printf("Error: search returned %d \n%s\n",rc,query); exit (1); @@ -78,17 +171,35 @@ void RankingQuery(int lineno, ZebraHandle zh, char *query, exit (1); } yaz_pqf_destroy(parser); + odr_destroy (odr); + return hits; +} + + +/** + * makes a query, checks number of hits, and for the first hit, that + * it contains the given string, and that it gets the right score + */ +void ranking_query(int lineno, ZebraHandle zh, char *query, + int exphits, char *firstrec, int firstscore ) +{ + ZebraRetrievalRecord retrievalRecord[10]; + ODR odr_output = odr_createmem (ODR_ENCODE); + const char *setname="rsetname"; + int hits; + int rc; + int i; + + hits = do_query(lineno, zh, query, exphits); for (i = 0; i<10; i++) - { retrievalRecord[i].position = i+1; - retrievalRecord[i].score = i+20000; - } - rc=zebra_records_retrieve (zh, odr_output, setname, 0, - VAL_TEXT_XML, hits, retrievalRecord); - - if (rc) { + rc = zebra_records_retrieve (zh, odr_output, setname, 0, + VAL_TEXT_XML, hits, retrievalRecord); + + if (rc) + { printf("Error: retrieve returned %d \n%s\n",rc,query); exit (1); } @@ -104,10 +215,75 @@ void RankingQuery(int lineno, ZebraHandle zh, char *query, if (retrievalRecord[0].score != firstscore) { printf("Error: first rec got score %d instead of %d\n", - retrievalRecord[0].score, firstscore); + retrievalRecord[0].score, firstscore); exit(1); } odr_destroy (odr_output); - odr_destroy (odr_input); } +void meta_query(int lineno, ZebraHandle zh, char *query, int exphits, + zint *ids) +{ + ZebraMetaRecord *meta; + ODR odr_output = odr_createmem (ODR_ENCODE); + const char *setname="rsetname"; + zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint))); + int hits; + int i; + + hits = do_query(lineno, zh, query, exphits); + + for (i = 0; i