Add two CQL sortby tests
[yaz-moved-to-github.git] / test / test_rpn2solr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 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             {
41                 ret = 1;
42             }
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, "abc", "abc"));
55     YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
56 #if 0
57 /* Invalid PQF, so this will never work */
58     YAZ_CHECK(compare(ct, "a b", "a b"));
59 #endif
60     YAZ_CHECK(compare(ct, "@not a b", "a AND NOT b"));
61     YAZ_CHECK(compare(ct, "@and @or a b c", "(a OR b) AND c"));
62     YAZ_CHECK(compare(ct, "@and a b", "a AND b"));
63     YAZ_CHECK(compare(ct, "@or a b", "a OR b"));
64     YAZ_CHECK(compare(ct, "@attr 1=field abc", "field:abc"));
65     YAZ_CHECK(compare(ct, "@attr 1=field \"a b c\"", "field:\"a b c\""));
66     YAZ_CHECK(compare(ct, "@attr 1=4 abc", 0)); /* should fail */
67
68     solr_transform_define_pattern(ct, "index.title", "1=4");
69     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title:abc"));
70
71     solr_transform_define_pattern(ct, "index.foo", "1=bar");
72     YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo:abc"));
73
74     /* Truncation */
75     YAZ_CHECK(compare(ct, "@attr 5=1 water", "water*"));
76     YAZ_CHECK(compare(ct, "@attr 5=2 water", "*water"));
77     YAZ_CHECK(compare(ct, "@attr 5=3 water", "*water*"));
78
79     /*
80     YAZ_CHECK(compare(ct, "@or @attr 1=1016 water @attr 7=1 @attr 1=4 0", "any:water rank:??");
81      */
82
83     solr_transform_close(ct);
84 }
85
86 static void tst2(void)
87 {
88     WRBUF w = wrbuf_alloc();
89     solr_transform_t ct = 0;
90     const char *srcdir = getenv("srcdir");
91     if (srcdir)
92     {
93         wrbuf_puts(w, srcdir);
94         wrbuf_puts(w, "/");
95     }
96     wrbuf_puts(w, "../etc/pqf.properties");
97     
98     ct = solr_transform_open_fname(wrbuf_cstr(w));
99     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
100 #if 0
101     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
102 #endif
103
104     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
105     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title:abc"));
106 #if 0
107     YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
108     YAZ_CHECK(compare(ct, "@attr 2=1 @attr 1=30 1980", "dc.date:[* to 1980]"));
109     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date:1980"));
110     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date:[* to 1980]"));
111     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date:[* to 1980]"));
112     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date:[1980 to *]"));
113
114     YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "solr.allRecords=1"));
115 #endif
116     YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
117     solr_transform_close(ct);
118     wrbuf_destroy(w);
119 }
120
121 int main (int argc, char **argv)
122 {
123     YAZ_CHECK_INIT(argc, argv);
124     YAZ_CHECK_LOG();
125     tst1();
126     tst2();
127     YAZ_CHECK_TERM;
128 }
129
130 /*
131  * Local variables:
132  * c-basic-offset: 4
133  * c-file-style: "Stroustrup"
134  * indent-tabs-mode: nil
135  * End:
136  * vim: shiftwidth=4 tabstop=8 expandtab
137  */
138