rpn2solr: rewrite rel operator handling YAZ-685
[yaz-moved-to-github.git] / test / test_rpn2solr.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 <stdlib.h>
10 #include <stdio.h>
11
12 #include <yaz/test.h>
13 #include <yaz/log.h>
14 #include <yaz/rpn2solr.h>
15 #include <yaz/wrbuf.h>
16 #include <yaz/pquery.h>
17
18 static int compare(solr_transform_t ct, const char *pqf, const char *solr)
19 {
20     int ret = 0;
21     ODR odr = odr_createmem(ODR_ENCODE);
22     WRBUF w = wrbuf_alloc();
23     Z_RPNQuery *q = p_query_rpn(odr, pqf);
24
25     if (q)
26     {
27         int r = solr_transform_rpn2solr_wrbuf(ct, w, q);
28
29         if (r != 0)
30         {
31             /* transform error */
32             yaz_log(YLOG_LOG, "%s -> Error %d", pqf, r);
33             if (!solr) /* also expected error? */
34                 ret = 1;
35         }
36         else if (r == 0)
37         {
38             yaz_log(YLOG_LOG, "%s -> %s", pqf, wrbuf_cstr(w));
39             if (solr && !strcmp(wrbuf_cstr(w), solr))
40                 ret = 1;
41             else
42                 yaz_log(YLOG_LOG, "Exp: %s", solr);
43         }
44     }
45     wrbuf_destroy(w);
46     odr_destroy(odr);
47     return ret;
48 }
49
50 static void tst1(void)
51 {
52     solr_transform_t ct = solr_transform_create();
53
54     YAZ_CHECK(compare(ct, "@or a @and b c", "a OR (b AND c)"));
55     YAZ_CHECK(compare(ct, "abc", "abc"));
56     YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
57     YAZ_CHECK(compare(ct, "@not a b", "a AND NOT b"));
58     YAZ_CHECK(compare(ct, "@and @or a b c", "(a OR b) AND c"));
59     YAZ_CHECK(compare(ct, "@and a b", "a AND b"));
60     YAZ_CHECK(compare(ct, "@or a b", "a OR b"));
61     YAZ_CHECK(compare(ct, "@attr 1=field abc", "field:abc"));
62     YAZ_CHECK(compare(ct, "@attr 1=field \"a b c\"", "field:\"a b c\""));
63     YAZ_CHECK(compare(ct, "@attr 1=4 abc", 0)); /* should fail */
64
65     solr_transform_define_pattern(ct, "index.title", "1=4");
66     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title:abc"));
67
68     solr_transform_define_pattern(ct, "index.foo", "1=bar");
69     YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo:abc"));
70
71     /* Truncation */
72     YAZ_CHECK(compare(ct, "@attr 5=1 water", "water*"));
73     YAZ_CHECK(compare(ct, "@attr 5=2 water", 0));
74     YAZ_CHECK(compare(ct, "@attr 5=3 water", 0));
75     YAZ_CHECK(compare(ct, "@attr 5=100 water", "water"));
76     YAZ_CHECK(compare(ct, "@attr 5=101 water", 0));
77     YAZ_CHECK(compare(ct, "@attr 5=104 w#ter", "w?ter"));
78     YAZ_CHECK(compare(ct, "@attr 5=104 w#t?r", "w?t*r"));
79     YAZ_CHECK(compare(ct, "@attr 5=104 w#te?", "w?te*"));
80     YAZ_CHECK(compare(ct, "@attr 5=104 w\\#te?", "w?te*")); /* PQF eats # */
81     YAZ_CHECK(compare(ct, "@attr 5=104 w\\\\#te?", "w#te*"));
82
83     /* reserved characters */
84     YAZ_CHECK(compare(ct, "@attr 5=104 \\\"\\\\\\\\",
85                       "\\\"" "\\\\"));
86     YAZ_CHECK(compare(ct, "@attr 5=104 \\\"\\\\\\\\\\\"",
87                       "\\\"" "\\\\" "\\\""));
88     YAZ_CHECK(compare(ct, "@attr 5=104 \\\"\\\\", "\\\"\\\\"));
89
90     YAZ_CHECK(compare(ct, "@attr 5=104 \\{:\\}", "\\{\\:\\}"));
91
92     YAZ_CHECK(compare(ct, "@attr 5=104 \\\"\\\\\\\\\\\"",
93                       "\\\"" "\\\\" "\\\""));
94
95     YAZ_CHECK(compare(ct, "@attr 1=date @attr 2=1 1980", "date:[* TO 1980}"));
96     YAZ_CHECK(compare(ct, "@attr 1=date @attr 2=2 1980", "date:[* TO 1980]"));
97     YAZ_CHECK(compare(ct, "@attr 1=date @attr 2=3 1980", "date:1980"));
98     YAZ_CHECK(compare(ct, "@attr 1=date @attr 2=4 1980", "date:[1980 TO *]"));
99     YAZ_CHECK(compare(ct, "@attr 1=date @attr 2=5 1980", "date:{1980 TO *]"));
100     YAZ_CHECK(compare(ct, "@and @attr 1=date @attr 2=2 234 @attr 1=date @attr 2=4 1990", "date:[1990 TO 234]"));
101     YAZ_CHECK(compare(ct, "@and @attr 1=date @attr 2=2 345 @attr 1=date @attr 2=4 234",  "date:[234 TO 345]"));
102     YAZ_CHECK(compare(ct, "@and @attr 1=date @attr 2=5 234 @attr 1=titl @attr 2=2 1990", "date:{234 TO *] AND titl:[* TO 1990]"));
103     YAZ_CHECK(compare(ct, "@and @attr 1=date @attr 2=5 234 @attr 1=date @attr 2=2 1990", "date:{234 TO 1990]"));
104     YAZ_CHECK(compare(ct, "@and @attr 1=date @attr 2=4 234 @attr 1=date @attr 2=2 1990", "date:[234 TO 1990]"));
105     YAZ_CHECK(compare(ct, "@and @attr 1=date @attr 2=5 234 @attr 1=date @attr 2=1 1990", "date:{234 TO 1990}"));
106     YAZ_CHECK(compare(ct, "@and @attr 1=date @attr 2=4 234 @attr 1=date @attr 2=2 1990", "date:[234 TO 1990]"));
107     YAZ_CHECK(compare(ct, "@or  @attr 1=date @attr 2=4 234 @attr 1=date @attr 2=2 1990", "date:[234 TO *] OR date:[* TO 1990]"));
108     YAZ_CHECK(compare(ct, "@or  @attr 1=date @attr 2=2 234 @attr 1=date @attr 2=4 1990", "date:[* TO 234] OR date:[1990 TO *]"));
109     YAZ_CHECK(compare(ct, "@attr 1=date @attr 2=1 1980", "date:[* TO 1980}"));
110     YAZ_CHECK(compare(ct, "@attr 1=date @attr 2=2 1980", "date:[* TO 1980]"));
111     YAZ_CHECK(compare(ct, "@and @attr 1=date @attr 2=2 234 @attr 1=date @attr 2=4 1990", "date:[1990 TO 234]"));
112
113     solr_transform_close(ct);
114 }
115
116 static void tst2(void)
117 {
118     WRBUF w = wrbuf_alloc();
119     solr_transform_t ct = 0;
120     const char *srcdir = getenv("srcdir");
121     if (srcdir)
122     {
123         wrbuf_puts(w, srcdir);
124         wrbuf_puts(w, "/");
125     }
126     wrbuf_puts(w, "../etc/pqf.properties");
127
128     ct = solr_transform_open_fname(wrbuf_cstr(w));
129     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
130 #if 0
131     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
132 #endif
133
134     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
135     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title:abc"));
136
137     YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
138
139     /* Date check */ 
140     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=1 1980", "dc.date:[* TO 1980}"));
141     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date:[* TO 1980]"));
142     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date:1980"));
143     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date:[1980 TO *]"));
144     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date:{1980 TO *]"));
145     YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=2 234 @attr 1=30 @attr 2=4 1990", "dc.date:[1990 TO 234]"));
146     YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=2 345 @attr 1=30 @attr 2=4 234", "dc.date:[234 TO 345]"));
147     YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=5 234 @attr 1=4  @attr 2=2 1990", "dc.date:{234 TO *] AND dc.title:[* TO 1990]"));
148     YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=5 234 @attr 1=30 @attr 2=2 1990", "dc.date:{234 TO 1990]"));
149     YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=4 234 @attr 1=30 @attr 2=2 1990", "dc.date:[234 TO 1990]"));
150     YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=5 234 @attr 1=30 @attr 2=1 1990", "dc.date:{234 TO 1990}"));
151     YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=4 234 @attr 1=30 @attr 2=2 1990", "dc.date:[234 TO 1990]"));
152     YAZ_CHECK(compare(ct, "@or  @attr 1=30 @attr 2=4 234 @attr 1=30 @attr 2=2 1990", "dc.date:[234 TO *] OR dc.date:[* TO 1990]"));
153     YAZ_CHECK(compare(ct, "@or  @attr 1=30 @attr 2=2 234 @attr 1=30 @attr 2=4 1990", "dc.date:[* TO 234] OR dc.date:[1990 TO *]"));
154
155 #if 0
156     YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "solr.allRecords=1"));
157 #endif
158     YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
159     solr_transform_close(ct);
160     wrbuf_destroy(w);
161 }
162
163 int main (int argc, char **argv)
164 {
165     YAZ_CHECK_INIT(argc, argv);
166     YAZ_CHECK_LOG();
167     tst1();
168     tst2();
169     YAZ_CHECK_TERM;
170 }
171
172 /*
173  * Local variables:
174  * c-basic-offset: 4
175  * c-file-style: "Stroustrup"
176  * indent-tabs-mode: nil
177  * End:
178  * vim: shiftwidth=4 tabstop=8 expandtab
179  */
180