Update configure to generate config.h
[yaz-moved-to-github.git] / test / test_rpn2solr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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     YAZ_CHECK(compare(ct, "@or @attr 1=1016 water @attr 7=1 @attr 1=4 0", "any:water rank:??");
75      */
76
77     solr_transform_close(ct);
78 }
79
80 static void tst2(void)
81 {
82     WRBUF w = wrbuf_alloc();
83     solr_transform_t ct = 0;
84     const char *srcdir = getenv("srcdir");
85     if (srcdir)
86     {
87         wrbuf_puts(w, srcdir);
88         wrbuf_puts(w, "/");
89     }
90     wrbuf_puts(w, "../etc/pqf.properties");
91     
92     ct = solr_transform_open_fname(wrbuf_cstr(w));
93     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
94 #if 0
95     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
96 #endif
97
98     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
99     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title:abc"));
100 #if 0
101     YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
102     YAZ_CHECK(compare(ct, "@attr 2=1 @attr 1=30 1980", "dc.date:[* to 1980]"));
103     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date:1980"));
104     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date:[* to 1980]"));
105     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date:[* to 1980]"));
106     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date:[1980 to *]"));
107
108     YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "solr.allRecords=1"));
109 #endif
110     YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
111     solr_transform_close(ct);
112     wrbuf_destroy(w);
113 }
114
115 int main (int argc, char **argv)
116 {
117     YAZ_CHECK_INIT(argc, argv);
118     YAZ_CHECK_LOG();
119     tst1();
120     tst2();
121     YAZ_CHECK_TERM;
122 }
123
124 /*
125  * Local variables:
126  * c-basic-offset: 4
127  * c-file-style: "Stroustrup"
128  * indent-tabs-mode: nil
129  * End:
130  * vim: shiftwidth=4 tabstop=8 expandtab
131  */
132