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