Add tests for newly added >= and <= support
[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             printf("'%s' -> '%s'\n", pqf, wrbuf_cstr(w));
39             yaz_log(YLOG_LOG, "%s -> %s", pqf, wrbuf_cstr(w));
40             if (solr && !strcmp(wrbuf_cstr(w), solr))
41             {
42                 ret = 1;
43             }
44         }
45     }
46     wrbuf_destroy(w);
47     odr_destroy(odr);
48     return ret;
49 }
50
51 static void tst1(void)
52 {
53     solr_transform_t ct = solr_transform_create();
54
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     solr_transform_close(ct);
96 }
97
98 static void tst2(void)
99 {
100     WRBUF w = wrbuf_alloc();
101     solr_transform_t ct = 0;
102     const char *srcdir = getenv("srcdir");
103     if (srcdir)
104     {
105         wrbuf_puts(w, srcdir);
106         wrbuf_puts(w, "/");
107     }
108     wrbuf_puts(w, "../etc/pqf.properties");
109
110     ct = solr_transform_open_fname(wrbuf_cstr(w));
111     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
112 #if 0
113     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
114 #endif
115
116     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
117     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title:abc"));
118
119     YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
120
121     /* Date check */ 
122 //  YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=1 1980", "dc.date:[* TO 1980]"));
123     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date:[* TO 1980]"));
124     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date:1980"));
125     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date:[1980 TO *]"));
126 //    YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date:[* TO 1980]"));
127     YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=4 234 @attr 1=30 @attr 2=2 1990", "dc.date:[234 TO *] AND dc.date:[* TO 1990]"));
128 #if 0
129     YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "solr.allRecords=1"));
130 #endif
131     YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
132     solr_transform_close(ct);
133     wrbuf_destroy(w);
134 }
135
136 int main (int argc, char **argv)
137 {
138     YAZ_CHECK_INIT(argc, argv);
139     YAZ_CHECK_LOG();
140     tst1();
141     tst2();
142     YAZ_CHECK_TERM;
143 }
144
145 /*
146  * Local variables:
147  * c-basic-offset: 4
148  * c-file-style: "Stroustrup"
149  * indent-tabs-mode: nil
150  * End:
151  * vim: shiftwidth=4 tabstop=8 expandtab
152  */
153