Test that would have caught the truncation flags switch
[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 t=r water", "water*"));
77
78     YAZ_CHECK(compare(ct, "@attr 5=2 water", "*water"));
79     YAZ_CHECK(compare(ct, "@attr t=l water", "*water"));
80
81     YAZ_CHECK(compare(ct, "@attr 5=3 water", "*water*"));
82     YAZ_CHECK(compare(ct, "@attr t=b water", "*water*"));
83
84     /*
85     YAZ_CHECK(compare(ct, "@or @attr 1=1016 water @attr 7=1 @attr 1=4 0", "any:water rank:??");
86      */
87
88     solr_transform_close(ct);
89 }
90
91 static void tst2(void)
92 {
93     WRBUF w = wrbuf_alloc();
94     solr_transform_t ct = 0;
95     const char *srcdir = getenv("srcdir");
96     if (srcdir)
97     {
98         wrbuf_puts(w, srcdir);
99         wrbuf_puts(w, "/");
100     }
101     wrbuf_puts(w, "../etc/pqf.properties");
102     
103     ct = solr_transform_open_fname(wrbuf_cstr(w));
104     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
105 #if 0
106     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
107 #endif
108
109     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
110     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title:abc"));
111 #if 0
112     YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
113     YAZ_CHECK(compare(ct, "@attr 2=1 @attr 1=30 1980", "dc.date:[* to 1980]"));
114     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date:1980"));
115     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date:[* to 1980]"));
116     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date:[* to 1980]"));
117     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date:[1980 to *]"));
118
119     YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "solr.allRecords=1"));
120 #endif
121     YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
122     solr_transform_close(ct);
123     wrbuf_destroy(w);
124 }
125
126 int main (int argc, char **argv)
127 {
128     YAZ_CHECK_INIT(argc, argv);
129     YAZ_CHECK_LOG();
130     tst1();
131     tst2();
132     YAZ_CHECK_TERM;
133 }
134
135 /*
136  * Local variables:
137  * c-basic-offset: 4
138  * c-file-style: "Stroustrup"
139  * indent-tabs-mode: nil
140  * End:
141  * vim: shiftwidth=4 tabstop=8 expandtab
142  */
143