CQL: fix extra terms in wrong order
[yaz-moved-to-github.git] / test / test_cql2ccl.c
index cd1547b..c050f7e 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2011 Index Data
+ * Copyright (C) 1995-2012 Index Data
  * See the file LICENSE for details.
  */
 #if HAVE_CONFIG_H
 #include <yaz/log.h>
 #include <yaz/test.h>
 
-
-static int tst_query(const char *cql, const char *expected_ccl)
+static int tst_query_s(const char *cql, const char *expected_ccl,
+                       const char *expected_keys)
 {
-    int ret = 0;
+    int ret = 1;
     CQL_parser cp = cql_parser_create();
     int r = cql_parser_string(cp, cql);
     
     if (r)
     {
         yaz_log(YLOG_WARN, "cql: parse error: %s", cql);
+        ret = 0;
     }
     else
     { /* cql parse OK */
@@ -29,21 +30,51 @@ static int tst_query(const char *cql, const char *expected_ccl)
         r = cql_to_ccl(cql_parser_result(cp), wrbuf_vp_puts, w);
 
         if (expected_ccl && r == 0 && strcmp(wrbuf_cstr(w), expected_ccl) == 0)
+            ;
+        else if (!expected_ccl)
         {
-            ret = 1;
+            if (!r)
+            {
+                ret = 0;
+                yaz_log(YLOG_WARN, "cql: diff: %s", cql);
+                yaz_log(YLOG_WARN, " exp error");
+                yaz_log(YLOG_WARN, " got ccl %s", wrbuf_cstr(w));
+            }
         }
-        else if (!expected_ccl)
+        else
         {
-            if (r)
-                ret = 1; /* expected conversion error, OK */
+            ret = 0;
+            yaz_log(YLOG_WARN, "cql: diff: %s", cql);
+            yaz_log(YLOG_WARN, " exp ccl %s", expected_ccl);
+            if (!r)
+                yaz_log(YLOG_WARN, " got ccl %s", wrbuf_cstr(w));
             else
-                yaz_log(YLOG_WARN, "cql: expected conversion error: %s", cql);
+                yaz_log(YLOG_WARN, " got error");
+        }
+        wrbuf_rewind(w);
+        r = cql_sortby_to_sortkeys(cql_parser_result(cp), 
+                                   wrbuf_vp_puts, w);
+        if (expected_keys && !r && !strcmp(wrbuf_cstr(w), expected_keys))
+            ;
+        else if (!expected_keys)
+        {
+            if (!r)
+            {
+                ret = 0;
+                yaz_log(YLOG_WARN, "cql: diff: %s", cql);
+                yaz_log(YLOG_WARN, " exp error");
+                yaz_log(YLOG_WARN, " got sortkeys %s", wrbuf_cstr(w));
+            }
         }
         else
         {
+            ret = 0;
             yaz_log(YLOG_WARN, "cql: diff: %s", cql);
-            yaz_log(YLOG_WARN, " expected %s", expected_ccl);
-            yaz_log(YLOG_WARN, " got      %s", wrbuf_cstr(w));
+            yaz_log(YLOG_WARN, " exp sortkeys %s", expected_keys);
+            if (!r)
+                yaz_log(YLOG_WARN, " got sortkeys %s", wrbuf_cstr(w));
+            else
+                yaz_log(YLOG_WARN, " got error");
         }
         wrbuf_destroy(w);
     }
@@ -51,6 +82,11 @@ static int tst_query(const char *cql, const char *expected_ccl)
     return ret;
 }
 
+static int tst_query(const char *cql, const char *expected_ccl)
+{
+    return tst_query_s(cql, expected_ccl, "");
+}
+
 static void tst(void)
 {
     YAZ_CHECK(tst_query("\"\"", "\"\""));
@@ -77,8 +113,9 @@ static void tst(void)
     YAZ_CHECK(tst_query("a b", "\"a\" \"b\""));
     YAZ_CHECK(tst_query("ab bc", "\"ab\" \"bc\""));
 
-    YAZ_CHECK(tst_query("\\\\", "\"\\\"\""));
-    YAZ_CHECK(tst_query("\\\"",   "\"\\\"\""));
+    YAZ_CHECK(tst_query("\\\\", "\"\\\\\""));
+    YAZ_CHECK(tst_query("\\\"", "\"\\\"\""));
+    YAZ_CHECK(tst_query("\\x" , "\"x\""));
 
     YAZ_CHECK(tst_query("\\*", "\"*\""));
     YAZ_CHECK(tst_query("\"\\*\"", "\"*\""));
@@ -87,10 +124,69 @@ static void tst(void)
 
     YAZ_CHECK(tst_query("title=x", "title=\"x\""));
     YAZ_CHECK(tst_query("title=x or author=y",
-                        "(title=\"x\" or author=\"y\")"));
+                        "(title=\"x\") or (author=\"y\")"));
+    YAZ_CHECK(tst_query("title=x or author=y and date=z",
+                        "((title=\"x\") or (author=\"y\")) and (date=\"z\")"));
+
+    YAZ_CHECK(tst_query("title all \"\"", "title=\"\""));
+
+    YAZ_CHECK(tst_query("title all x", "title=\"x\""));
+    YAZ_CHECK(tst_query("title all x y", "title=\"x\" and title=\"y\""));
+    YAZ_CHECK(tst_query("title all \"x y\"", "title=\"x\" and title=\"y\""));
+
+    YAZ_CHECK(tst_query("title any x", "title=\"x\""));
+    YAZ_CHECK(tst_query("title any x y", "title=\"x\" or title=\"y\""));
+    YAZ_CHECK(tst_query("title any \"x y\"", "title=\"x\" or title=\"y\""));
+
+    YAZ_CHECK(tst_query("title = \"x y\"", "title=\"x y\""));
+    YAZ_CHECK(tst_query("title = x y", "title=\"x\" \"y\""));
+
+    YAZ_CHECK(tst_query("title = x y z",  "title=\"x\" \"y\" \"z\""));
+
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox dinosaurs",
+                        "(dc.title=\"encyclopedia\") % (\"dinosaurs\")"));
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3 dinosaurs",
+                        "(dc.title=\"encyclopedia\") %3 (\"dinosaurs\")"));
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/unit=word "
+                        "dinosaurs", 
+                        "(dc.title=\"encyclopedia\") %3 (\"dinosaurs\")"));
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/unit=phrase "
+                        "dinosaurs", 0));
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/a=b "
+                        "dinosaurs", 0));
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox/a=b dinosaurs", 0));
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<3 dinosaurs",
+                        "(dc.title=\"encyclopedia\") %2 (\"dinosaurs\")"));
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance=3 dinosaurs", 0));
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance>3 dinosaurs", 0));
+    YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance>=3 dinosaurs", 0));
+    YAZ_CHECK(tst_query_s("a sortby title", "\"a\"",
+                          "title,,1,0,highValue"));
+    YAZ_CHECK(tst_query_s("a sortby dc.title", "\"a\"",
+                          "title,dc,1,0,highValue"));
+    YAZ_CHECK(tst_query_s("a sortby title/ascending", "\"a\"",
+                          "title,,1,0,highValue"));
+    YAZ_CHECK(tst_query_s("a sortby title/descending", "\"a\"",
+                          "title,,0,0,highValue"));
+    YAZ_CHECK(tst_query_s("a sortby title/ignoreCase", "\"a\"",
+                          "title,,1,0,highValue"));
+    YAZ_CHECK(tst_query_s("a sortby title/respectCase", "\"a\"",
+                          "title,,1,1,highValue"));
+    YAZ_CHECK(tst_query_s("a sortby title/missingOmit", "\"a\"",
+                          "title,,1,0,omit"));
+    YAZ_CHECK(tst_query_s("a sortby title/missingFail", "\"a\"",
+                          "title,,1,0,abort"));
+    YAZ_CHECK(tst_query_s("a sortby title/missingLow", "\"a\"",
+                          "title,,1,0,lowValue"));
+    YAZ_CHECK(tst_query_s("a sortby title/missingHigh", "\"a\"",
+                          "title,,1,0,highValue"));
+    YAZ_CHECK(tst_query_s("a sortby title/sort.missingHigh", "\"a\"",
+                          "title,,1,0,highValue"));
+    YAZ_CHECK(tst_query_s("a sortby title/bogus", "\"a\"", 0));
 
+    YAZ_CHECK(tst_query_s("a sortby dc.year dc.author", "\"a\"",
+                          "year,dc,1,0,highValue author,dc,1,0,highValue"));
 
-    YAZ_CHECK(tst_query("title all x", "title=x"));
 }
 
 int main(int argc, char **argv)