Many public functions returns ZEBRA_RES rather than int to avoid
[idzebra-moved-to-github.git] / test / api / testlib.c
index 3cc35df..94554a2 100644 (file)
@@ -1,6 +1,6 @@
-/* $Id: testlib.c,v 1.7 2004-12-02 14:05:04 adam 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.
 
@@ -23,6 +23,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 /** testlib - utilities for the api tests */
 
 #include <assert.h>
+#include <yaz/log.h>
 #include <yaz/pquery.h>
 #include <idzebra/api.h>
 #include "testlib.h"
@@ -119,38 +120,38 @@ void init_data(ZebraHandle zh, const char **recs)
     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);
+    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;
 
-    yaz_log(log_level,"======================================");
-    yaz_log(log_level,"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);
@@ -158,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);
@@ -175,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);    
@@ -206,8 +233,8 @@ 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);
     }
     
@@ -228,7 +255,6 @@ void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
     const char *setname="rsetname";
     zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
     int hits;
-    int rc;
     int i;
         
     hits = do_query(lineno, zh, query, exphits);
@@ -240,7 +266,7 @@ void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
     
     if (!meta)
     {
-        printf("Error: retrieve returned %d \n%s\n",rc,query);
+        printf("Error: retrieve returned error\n%s\n", query);
         exit (1);
     }
 
@@ -258,3 +284,32 @@ void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
     free(positions);
 }
 
+struct finfo {
+    const char *name;
+    int occurred;
+};
+
+static void filter_cb(void *cd, const char *name)
+{
+    struct finfo *f = (struct finfo*) cd;
+    if (!strcmp(f->name, name))
+       f->occurred = 1;
+}
+
+void check_filter(ZebraService zs, const char *name)
+{
+    struct finfo f;
+
+    f.name = name;
+    f.occurred = 0;
+    zebra_filter_info(zs, &f, filter_cb);
+    if (!f.occurred)
+    {
+       yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
+       exit(0);
+    }
+}
+
+
+
+