X-Git-Url: http://git.indexdata.com/?p=idzebra-moved-to-github.git;a=blobdiff_plain;f=test%2Fapi%2Ftestlib.c;h=94554a2788d7e3691419a9e8afbc902db2e5c9db;hp=52173d3ad8e69e94f06cee7f33df8c1ff6122895;hb=49d0ee122a9f86ec2967b577dcc297c501785edd;hpb=a47789869bfde93b798a773df1762fb7c76fe665 diff --git a/test/api/testlib.c b/test/api/testlib.c index 52173d3..94554a2 100644 --- a/test/api/testlib.c +++ b/test/api/testlib.c @@ -1,6 +1,6 @@ -/* $Id: testlib.c,v 1.4 2004-11-04 13:10:07 heikki Exp $ - Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004 - Index Data Aps +/* $Id: testlib.c,v 1.13 2005-04-15 10:47:49 adam Exp $ + Copyright (C) 1995-2005 + Index Data ApS This file is part of the Zebra server. @@ -30,7 +30,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA /** start_log: open a log file */ /* FIXME - parse command line arguments to set log levels etc */ -static int loglevel=0; +int log_level=0; /* not static, t*.c may use it */ + void start_log(int argc, char **argv) { char logname[2048]; @@ -40,9 +41,9 @@ void start_log(int argc, char **argv) return; sprintf(logname, "%s.log", argv[0]); yaz_log_init_file(logname); - loglevel = yaz_log_mask_str_x(argv[0],0); - yaz_log_init_level(LOG_DEFAULT_LEVEL | loglevel); - logf(loglevel,"starting %s",argv[0]); + 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]); } /** @@ -60,18 +61,27 @@ ZebraService start_up(char *cfgname, int argc, char **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]; - char *srcdir = getenv("srcdir"); + const char *srcdir = get_srcdir(); ZebraService zs; - if (!srcdir || ! *srcdir) - srcdir="."; if (!cfgname || ! *cfgname ) cfgname="zebra.cfg"; - sprintf(cfg, "%.200s/%s",srcdir, cfgname); + sprintf(cfg, "%.200s/%.50s", srcdir, cfgname); zs=zebra_start(cfg); if (!zs) { @@ -92,9 +102,9 @@ int close_down(ZebraHandle zh, ZebraService zs, int retcode) zebra_stop(zs); if (retcode) - logf(loglevel,"========= Exiting with return code %d", retcode); + yaz_log(log_level,"========= Exiting with return code %d", retcode); else - logf(loglevel,"========= All tests OK"); + yaz_log(log_level,"========= All tests OK"); nmem_exit(); xmalloc_trav("x"); return retcode; @@ -102,48 +112,46 @@ int close_down(ZebraHandle zh, ZebraService zs, int retcode) /** inits the database and inserts test data */ -void init_data( ZebraHandle zh, const char **recs) +void init_data(ZebraHandle zh, const char **recs) { int i; char *addinfo; assert(zh); zebra_select_database(zh, "Default"); - logf(LOG_DEBUG,"going to call init"); - i=zebra_init(zh); - logf(LOG_DEBUG,"init returned %d",i); + 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); + 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); + 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) +int do_query_x(int lineno, ZebraHandle zh, char *query, int exphits, + int experror) { ODR odr; YAZ_PQF_Parser parser; Z_RPNQuery *rpn; const char *setname="rsetname"; - int hits; - int rc; - + zint hits; + ZEBRA_RES rc; - logf(loglevel,"======================================"); - logf(loglevel,"qry[%d]: %s", lineno, query); - odr=odr_createmem (ODR_DECODE); + 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); @@ -151,16 +159,36 @@ int do_query(int lineno, ZebraHandle zh, char *query, int exphits) printf("Error: Parse failed \n%s\n",query); exit(1); } - rc=zebra_search_RPN (zh, odr, rpn, setname, &hits); - if (rc) { - printf("Error: search returned %d \n%s\n",rc,query); - exit (1); + rc = zebra_search_RPN(zh, odr, rpn, setname, &hits); + if (experror) + { + if (rc != ZEBRA_FAIL) + { + printf("Error: search returned %d (OK), but error was expected\n" + "%s\n", rc, query); + exit(1); + } + int code = zebra_errCode(zh); + if (code != experror) + { + printf("Error: search returned error code %d, but error %d was " + "expected\n%s\n", + code, experror, query); + exit(1); + } } - - if (hits != exphits) { - printf("Error: search returned %d hits instead of %d\n", - hits, exphits); - exit (1); + else + { + if (rc == ZEBRA_FAIL) { + printf("Error: search returned %d\n%s\n", rc, query); + exit (1); + } + if (hits != exphits) { + printf("Error: search returned " ZINT_FORMAT + " hits instead of %d\n%s\n", + hits, exphits, query); + exit (1); + } } yaz_pqf_destroy(parser); odr_destroy (odr); @@ -168,12 +196,18 @@ int do_query(int lineno, ZebraHandle zh, char *query, int exphits) } +int do_query(int lineno, ZebraHandle zh, char *query, int exphits) +{ + return do_query_x(lineno, zh, query, exphits, 0); +} + + /** * 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 ) + int exphits, char *firstrec, int firstscore) { ZebraRetrievalRecord retrievalRecord[10]; ODR odr_output = odr_createmem (ODR_ENCODE); @@ -182,15 +216,16 @@ void ranking_query(int lineno, ZebraHandle zh, char *query, int rc; int i; - hits=do_query(lineno, zh, query, exphits); + hits = do_query(lineno, zh, query, exphits); for (i = 0; i<10; i++) retrievalRecord[i].position = i+1; - 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); } @@ -198,17 +233,83 @@ void ranking_query(int lineno, ZebraHandle zh, char *query, if (!strstr(retrievalRecord[0].buf, firstrec)) { printf("Error: Got the wrong record first\n"); - printf("Expected '%s' but got \n",firstrec); - printf("%.*s\n",retrievalRecord[0].len,retrievalRecord[0].buf); + printf("Expected '%s' but got \n", firstrec); + printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf); exit(1); } 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); } +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