Test for solr sort specs
[yaz-moved-to-github.git] / test / test_sortspec.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 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/sortspec.h>
12 #include <yaz/log.h>
13 #include <yaz/test.h>
14
15 static int cql(const char *arg, const char *expected_result)
16 {
17     ODR odr = odr_createmem(ODR_ENCODE);
18     Z_SortKeySpecList *sort_spec = yaz_sort_spec(odr, arg);
19     int ret = 0;
20
21     if (!sort_spec)
22     {
23         yaz_log(YLOG_WARN, "yaz_sort_spec : parse error: %s", arg);
24     }
25     else
26     {
27         WRBUF w = wrbuf_alloc();
28         int r = yaz_sort_spec_to_cql(sort_spec, w);
29
30         if (!expected_result && r)
31             ret = 1;
32         else if (expected_result && r == 0)
33         {
34             if (strcmp(wrbuf_cstr(w), expected_result) == 0)
35                 ret = 1;
36             else
37             {
38                 yaz_log(YLOG_WARN, "sort: diff: %s", arg);
39                 yaz_log(YLOG_WARN, " expected %s", expected_result);
40                 yaz_log(YLOG_WARN, " got      %s", wrbuf_cstr(w));
41             }
42         }
43         else if (r)
44         {
45             yaz_log(YLOG_WARN, "sort: diff %s", arg);
46             yaz_log(YLOG_WARN, " expected %s", expected_result);
47             yaz_log(YLOG_WARN, " got error %d", r);
48         }
49         else if (r == 0)
50         {
51             yaz_log(YLOG_WARN, "sort: diff %s", arg);
52             yaz_log(YLOG_WARN, " expected error");
53             yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
54         }
55         wrbuf_destroy(w);
56     }
57     odr_destroy(odr);
58     return ret;
59 }
60
61 static int type7(const char *arg, const char *expected_result)
62 {
63     ODR odr = odr_createmem(ODR_ENCODE);
64     Z_SortKeySpecList *sort_spec = yaz_sort_spec(odr, arg);
65     int ret = 0;
66
67     if (!sort_spec)
68     {
69         yaz_log(YLOG_WARN, "yaz_sort_spec : parse error: %s", arg);
70     }
71     else
72     {
73         WRBUF w = wrbuf_alloc();
74         int r;
75
76         wrbuf_puts(w, "q");
77         r = yaz_sort_spec_to_type7(sort_spec, w);
78
79         if (!expected_result && r)
80             ret = 1;
81         else if (expected_result && r == 0)
82         {
83             if (strcmp(wrbuf_cstr(w), expected_result) == 0)
84                 ret = 1;
85             else
86             {
87                 yaz_log(YLOG_WARN, "sort: diff: %s", arg);
88                 yaz_log(YLOG_WARN, " expected %s", expected_result);
89                 yaz_log(YLOG_WARN, " got      %s", wrbuf_cstr(w));
90             }
91         }
92         else if (r)
93         {
94             yaz_log(YLOG_WARN, "sort: diff %s", arg);
95             yaz_log(YLOG_WARN, " expected %s", expected_result);
96             yaz_log(YLOG_WARN, " got error %d", r);
97         }
98         else if (r == 0)
99         {
100             yaz_log(YLOG_WARN, "sort: diff %s", arg);
101             yaz_log(YLOG_WARN, " expected error");
102             yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
103         }
104         wrbuf_destroy(w);
105     }
106     odr_destroy(odr);
107     return ret;
108 }
109
110 static int strategy_sortkeys(const char *arg, const char *expected_result, int (*strategy) (Z_SortKeySpecList *, WRBUF))
111 {
112     ODR odr = odr_createmem(ODR_ENCODE);
113     Z_SortKeySpecList *sort_spec = yaz_sort_spec(odr, arg);
114     int ret = 0;
115
116     if (!sort_spec)
117     {
118         yaz_log(YLOG_WARN, "yaz_sort_spec : parse error: %s", arg);
119     }
120     else
121     {
122         WRBUF w = wrbuf_alloc();
123         int r = (strategy)(sort_spec, w);
124
125         if (!expected_result && r)
126             ret = 1;
127         else if (expected_result && r == 0)
128         {
129             if (strcmp(wrbuf_cstr(w), expected_result) == 0)
130                 ret = 1;
131             else
132             {
133                 yaz_log(YLOG_WARN, "sort: diff: %s", arg);
134                 yaz_log(YLOG_WARN, " expected %s", expected_result);
135                 yaz_log(YLOG_WARN, " got      %s", wrbuf_cstr(w));
136             }
137         }
138         else if (r)
139         {
140             yaz_log(YLOG_WARN, "sort: diff %s", arg);
141             yaz_log(YLOG_WARN, " expected %s", expected_result);
142             yaz_log(YLOG_WARN, " got error %d", r);
143         }
144         else if (r == 0)
145         {
146             yaz_log(YLOG_WARN, "sort: diff %s", arg);
147             yaz_log(YLOG_WARN, " expected error");
148             yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
149         }
150         wrbuf_destroy(w);
151     }
152     odr_destroy(odr);
153     return ret;
154 }
155
156 static int srw_sortkeys(const char *arg, const char *expected_result) {
157     return strategy_sortkeys(arg, expected_result, yaz_sort_spec_to_srw_sortkeys);
158 }
159
160 static int solr_sortkeys(const char *arg, const char *expected_result) {
161     return strategy_sortkeys(arg, expected_result, yaz_sort_spec_to_solr_sortkeys);
162 }
163
164
165
166 static int check_srw_sortkeys_to_sort_spec(const char *arg,
167                                            const char *expected_result)
168 {
169     WRBUF w = wrbuf_alloc();
170     int ret = 0;
171     int r = yaz_srw_sortkeys_to_sort_spec(arg, w);
172
173     if (!expected_result && r)
174         ret = 1;
175     else if (expected_result && r == 0)
176     {
177         if (strcmp(wrbuf_cstr(w), expected_result) == 0)
178             ret = 1;
179         else
180         {
181             yaz_log(YLOG_WARN, "sort: diff: %s", arg);
182             yaz_log(YLOG_WARN, " expected %s", expected_result);
183             yaz_log(YLOG_WARN, " got      %s", wrbuf_cstr(w));
184         }
185     }
186     else if (r)
187     {
188         yaz_log(YLOG_WARN, "sort: diff %s", arg);
189         yaz_log(YLOG_WARN, " expected %s", expected_result);
190         yaz_log(YLOG_WARN, " got error %d", r);
191     }
192     else if (r == 0)
193     {
194         yaz_log(YLOG_WARN, "sort: diff %s", arg);
195         yaz_log(YLOG_WARN, " expected error");
196         yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
197     }
198     wrbuf_destroy(w);
199     return ret;
200 }
201
202
203 static void tst(void)
204 {
205     YAZ_CHECK(cql("title a",
206                   " SORTBY title/ascending/ignoreCase"));
207     YAZ_CHECK(cql("title a date ds",
208                   " SORTBY title/ascending/ignoreCase"
209                   " date/descending/respectCase"));
210     YAZ_CHECK(cql("1=4,2=3 a", 0));
211     YAZ_CHECK(cql("date a=1900",
212                   " SORTBY date/ascending/ignoreCase/missingValue=1900"));
213
214     YAZ_CHECK(type7("title a",
215                   "@or q @attr 1=title @attr 7=1 0"));
216     YAZ_CHECK(type7("title a date ds",
217                     "@or @or q @attr 1=title @attr 7=1 0"
218                     " @attr 1=date @attr 7=2 1"));
219     YAZ_CHECK(type7("1=4,2=3 a",
220                   "@or q @attr 1=4 @attr 2=3 @attr 7=1 0"));
221     YAZ_CHECK(type7("date a=1900",
222                   "@or q @attr 1=date @attr 7=1 0"));
223
224     YAZ_CHECK(srw_sortkeys("title a",
225                            "title,,1,0,highValue"));
226     YAZ_CHECK(srw_sortkeys("title a date ds",
227                            "title,,1,0,highValue "
228                            "date,,0,1,highValue"));
229     YAZ_CHECK(srw_sortkeys("1=4,2=3 a", 0));
230     YAZ_CHECK(srw_sortkeys("date a=1900",
231                            "date,,1,0,1900"));
232     YAZ_CHECK(check_srw_sortkeys_to_sort_spec(
233                   "date,,1,0,1900",
234                   "date ai=1900"));
235
236     YAZ_CHECK(solr_sortkeys("title a",
237                            "title asc"));
238     YAZ_CHECK(solr_sortkeys("title a date ds",
239                            "title asc, date desc"));
240     YAZ_CHECK(solr_sortkeys("1=4,2=3 a", 0));
241 }
242
243 int main(int argc, char **argv)
244 {
245     YAZ_CHECK_INIT(argc, argv);
246     YAZ_CHECK_LOG();
247     tst();
248     YAZ_CHECK_TERM;
249 }
250 /*
251  * Local variables:
252  * c-basic-offset: 4
253  * c-file-style: "Stroustrup"
254  * indent-tabs-mode: nil
255  * End:
256  * vim: shiftwidth=4 tabstop=8 expandtab
257  */
258