Update test_soap to new query struct
[yaz-moved-to-github.git] / test / test_cql2ccl.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <string.h>
10 #include <yaz/wrbuf.h>
11 #include <yaz/cql.h>
12 #include <yaz/log.h>
13 #include <yaz/test.h>
14
15 static int tst_query_s(const char *cql, const char *expected_ccl,
16                        const char *expected_keys)
17 {
18     int ret = 1;
19     CQL_parser cp = cql_parser_create();
20     int r = cql_parser_string(cp, cql);
21
22     if (r)
23     {
24         yaz_log(YLOG_WARN, "cql: parse error: %s", cql);
25         ret = 0;
26     }
27     else
28     { /* cql parse OK */
29         WRBUF w = wrbuf_alloc();
30         r = cql_to_ccl(cql_parser_result(cp), wrbuf_vp_puts, w);
31
32         if (expected_ccl && r == 0 && strcmp(wrbuf_cstr(w), expected_ccl) == 0)
33             ;
34         else if (!expected_ccl)
35         {
36             if (!r)
37             {
38                 ret = 0;
39                 yaz_log(YLOG_WARN, "cql: diff: %s", cql);
40                 yaz_log(YLOG_WARN, " exp error");
41                 yaz_log(YLOG_WARN, " got ccl %s", wrbuf_cstr(w));
42             }
43         }
44         else
45         {
46             ret = 0;
47             yaz_log(YLOG_WARN, "cql: diff: %s", cql);
48             yaz_log(YLOG_WARN, " exp ccl %s", expected_ccl);
49             if (!r)
50                 yaz_log(YLOG_WARN, " got ccl %s", wrbuf_cstr(w));
51             else
52                 yaz_log(YLOG_WARN, " got error");
53         }
54         wrbuf_rewind(w);
55         r = cql_sortby_to_sortkeys(cql_parser_result(cp),
56                                    wrbuf_vp_puts, w);
57         if (expected_keys && !r && !strcmp(wrbuf_cstr(w), expected_keys))
58             ;
59         else if (!expected_keys)
60         {
61             if (!r)
62             {
63                 ret = 0;
64                 yaz_log(YLOG_WARN, "cql: diff: %s", cql);
65                 yaz_log(YLOG_WARN, " exp error");
66                 yaz_log(YLOG_WARN, " got sortkeys %s", wrbuf_cstr(w));
67             }
68         }
69         else
70         {
71             ret = 0;
72             yaz_log(YLOG_WARN, "cql: diff: %s", cql);
73             yaz_log(YLOG_WARN, " exp sortkeys %s", expected_keys);
74             if (!r)
75                 yaz_log(YLOG_WARN, " got sortkeys %s", wrbuf_cstr(w));
76             else
77                 yaz_log(YLOG_WARN, " got error");
78         }
79         wrbuf_destroy(w);
80     }
81     cql_parser_destroy(cp);
82     return ret;
83 }
84
85 static int tst_query(const char *cql, const char *expected_ccl)
86 {
87     return tst_query_s(cql, expected_ccl, "");
88 }
89
90 static void tst(void)
91 {
92     YAZ_CHECK(tst_query("\"\"", "\"\""));
93     YAZ_CHECK(tst_query("x", "\"x\""));
94     YAZ_CHECK(tst_query("\"x\"", "\"x\""));
95     YAZ_CHECK(tst_query("\"xy\"", "\"xy\""));
96
97     YAZ_CHECK(tst_query("?", "#"));
98     YAZ_CHECK(tst_query("?a?", "#\"a\"#"));
99     YAZ_CHECK(tst_query("?a", "#\"a\""));
100     YAZ_CHECK(tst_query("a?", "\"a\"#"));
101     YAZ_CHECK(tst_query("\"?\"", "#"));
102     YAZ_CHECK(tst_query("\\?", "\"?\""));
103     YAZ_CHECK(tst_query("\"\\?\"", "\"?\""));
104
105     YAZ_CHECK(tst_query("*", "?"));
106     YAZ_CHECK(tst_query("*a*", "?\"a\"?"));
107     YAZ_CHECK(tst_query("*a", "?\"a\""));
108     YAZ_CHECK(tst_query("a*", "\"a\"?"));
109     YAZ_CHECK(tst_query("\"*\"", "?"));
110     YAZ_CHECK(tst_query("\\*", "\"*\""));
111     YAZ_CHECK(tst_query("\"\\*\"", "\"*\""));
112
113     YAZ_CHECK(tst_query("a b", "\"a\" \"b\""));
114     YAZ_CHECK(tst_query("ab bc", "\"ab\" \"bc\""));
115
116     YAZ_CHECK(tst_query("\\\\", "\"\\\\\""));
117     YAZ_CHECK(tst_query("\\\"", "\"\\\"\""));
118     YAZ_CHECK(tst_query("\\x" , "\"x\""));
119
120     YAZ_CHECK(tst_query("\\*", "\"*\""));
121     YAZ_CHECK(tst_query("\"\\*\"", "\"*\""));
122     YAZ_CHECK(tst_query("\\#", "\"#\""));
123     YAZ_CHECK(tst_query("\"\\#\"", "\"#\""));
124
125     YAZ_CHECK(tst_query("title=x", "title=\"x\""));
126     YAZ_CHECK(tst_query("title=x or author=y",
127                         "(title=\"x\") or (author=\"y\")"));
128     YAZ_CHECK(tst_query("title=x or author=y and date=z",
129                         "((title=\"x\") or (author=\"y\")) and (date=\"z\")"));
130
131     YAZ_CHECK(tst_query("title all \"\"", "title=\"\""));
132
133     YAZ_CHECK(tst_query("title all x", "title=\"x\""));
134     YAZ_CHECK(tst_query("title all x y", "title=\"x\" and title=\"y\""));
135     YAZ_CHECK(tst_query("title all \"x y\"", "title=\"x\" and title=\"y\""));
136
137     YAZ_CHECK(tst_query("title any x", "title=\"x\""));
138     YAZ_CHECK(tst_query("title any x y", "title=\"x\" or title=\"y\""));
139     YAZ_CHECK(tst_query("title any \"x y\"", "title=\"x\" or title=\"y\""));
140
141     YAZ_CHECK(tst_query("title = \"x y\"", "title=\"x y\""));
142     YAZ_CHECK(tst_query("title = x y", "title=\"x\" \"y\""));
143
144     YAZ_CHECK(tst_query("title = x y z",  "title=\"x\" \"y\" \"z\""));
145
146     YAZ_CHECK(tst_query("dc.title=encyclopedia prox dinosaurs",
147                         "(dc.title=\"encyclopedia\") % (\"dinosaurs\")"));
148     YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3 dinosaurs",
149                         "(dc.title=\"encyclopedia\") %3 (\"dinosaurs\")"));
150     YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/unit=word "
151                         "dinosaurs",
152                         "(dc.title=\"encyclopedia\") %3 (\"dinosaurs\")"));
153     YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/unit=phrase "
154                         "dinosaurs", 0));
155     YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/a=b "
156                         "dinosaurs", 0));
157     YAZ_CHECK(tst_query("dc.title=encyclopedia prox/a=b dinosaurs", 0));
158     YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<3 dinosaurs",
159                         "(dc.title=\"encyclopedia\") %2 (\"dinosaurs\")"));
160     YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance=3 dinosaurs", 0));
161     YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance>3 dinosaurs", 0));
162     YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance>=3 dinosaurs", 0));
163     YAZ_CHECK(tst_query_s("a sortby title", "\"a\"",
164                           "title,,1,0,highValue"));
165     YAZ_CHECK(tst_query_s("a sortby dc.title", "\"a\"",
166                           "title,dc,1,0,highValue"));
167     YAZ_CHECK(tst_query_s("a sortby title/ascending", "\"a\"",
168                           "title,,1,0,highValue"));
169     YAZ_CHECK(tst_query_s("a sortby title/descending", "\"a\"",
170                           "title,,0,0,highValue"));
171     YAZ_CHECK(tst_query_s("a sortby title/ignoreCase", "\"a\"",
172                           "title,,1,0,highValue"));
173     YAZ_CHECK(tst_query_s("a sortby title/respectCase", "\"a\"",
174                           "title,,1,1,highValue"));
175     YAZ_CHECK(tst_query_s("a sortby title/missingOmit", "\"a\"",
176                           "title,,1,0,omit"));
177     YAZ_CHECK(tst_query_s("a sortby title/missingFail", "\"a\"",
178                           "title,,1,0,abort"));
179     YAZ_CHECK(tst_query_s("a sortby title/missingLow", "\"a\"",
180                           "title,,1,0,lowValue"));
181     YAZ_CHECK(tst_query_s("a sortby title/missingHigh", "\"a\"",
182                           "title,,1,0,highValue"));
183     YAZ_CHECK(tst_query_s("a sortby title/sort.missingHigh", "\"a\"",
184                           "title,,1,0,highValue"));
185     YAZ_CHECK(tst_query_s("a sortby title/bogus", "\"a\"", 0));
186
187     YAZ_CHECK(tst_query_s("a sortby dc.year dc.author", "\"a\"",
188                           "year,dc,1,0,highValue author,dc,1,0,highValue"));
189
190 }
191
192 int main(int argc, char **argv)
193 {
194     YAZ_CHECK_INIT(argc, argv);
195     YAZ_CHECK_LOG();
196     tst();
197     YAZ_CHECK_TERM;
198 }
199 /*
200  * Local variables:
201  * c-basic-offset: 4
202  * c-file-style: "Stroustrup"
203  * indent-tabs-mode: nil
204  * End:
205  * vim: shiftwidth=4 tabstop=8 expandtab
206  */
207