Add query with sort
[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
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #include <yaz/test.h>
10 #include <yaz/log.h>
11 #include <yaz/rpn2solr.h>
12 #include <yaz/wrbuf.h>
13 #include <yaz/pquery.h>
14
15 static int compare(solr_transform_t ct, const char *pqf, const char *solr)
16 {
17     int ret = 0;
18     ODR odr = odr_createmem(ODR_ENCODE);
19     WRBUF w = wrbuf_alloc();
20     Z_RPNQuery *q = p_query_rpn(odr, pqf);
21
22     if (q)
23     {
24         int r = solr_transform_rpn2solr_wrbuf(ct, w, q);
25
26         if (r != 0)
27         {
28             /* transform error */
29             yaz_log(YLOG_LOG, "%s -> Error %d", pqf, r);
30             if (!solr) /* also expected error? */
31                 ret = 1;
32         }
33         else if (r == 0)
34         {
35             yaz_log(YLOG_LOG, "%s -> %s", pqf, wrbuf_cstr(w));
36             if (solr && !strcmp(wrbuf_cstr(w), solr))
37             {
38                 ret = 1;
39             }
40         }
41     }
42     wrbuf_destroy(w);
43     odr_destroy(odr);
44     return ret;
45 }
46
47 static void tst1(void)
48 {
49     solr_transform_t ct = solr_transform_create();
50
51     YAZ_CHECK(compare(ct, "abc", "abc"));
52     YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
53     YAZ_CHECK(compare(ct, "a b", "a b"));
54     YAZ_CHECK(compare(ct, "@not a b", "a AND NOT b"));
55     YAZ_CHECK(compare(ct, "@and @or a b c", "(a OR b) AND c"));
56     YAZ_CHECK(compare(ct, "@and a b", "a AND b"));
57     YAZ_CHECK(compare(ct, "@or a b", "a OR b"));
58     YAZ_CHECK(compare(ct, "@attr 1=field abc", "field:abc"));
59     YAZ_CHECK(compare(ct, "@attr 1=field \"a b c\"", "field:\"a b c\""));
60     YAZ_CHECK(compare(ct, "@attr 1=4 abc", 0)); /* should fail */
61
62     solr_transform_define_pattern(ct, "index.title", "1=4");
63     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title:abc"));
64
65     solr_transform_define_pattern(ct, "index.foo", "1=bar");
66     YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo:abc"));
67     /*
68     YAZ_CHECK(compare(ct, "@or @attr 1=1016 water @attr 7=1 @attr 1=4 0", "any:water rank:??");
69      */
70
71     solr_transform_close(ct);
72 }
73
74 static void tst2(void)
75 {
76     WRBUF w = wrbuf_alloc();
77     solr_transform_t ct = 0;
78     const char *srcdir = getenv("srcdir");
79     if (srcdir)
80     {
81         wrbuf_puts(w, srcdir);
82         wrbuf_puts(w, "/");
83     }
84     wrbuf_puts(w, "../etc/pqf.properties");
85     
86     ct = solr_transform_open_fname(wrbuf_cstr(w));
87     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
88     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
89     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
90     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title:abc"));
91     YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
92     YAZ_CHECK(compare(ct, "@attr 2=1 @attr 1=30 1980", "dc.date:[* to 1980]"));
93     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date:1980"));
94     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date:[* to 1980]"));
95     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date:[* to 1980]"));
96     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date:[1980 to *]"));
97
98     YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "solr.allRecords=1"));
99     YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
100     solr_transform_close(ct);
101     wrbuf_destroy(w);
102 }
103
104 int main (int argc, char **argv)
105 {
106     YAZ_CHECK_INIT(argc, argv);
107     YAZ_CHECK_LOG();
108     tst1();
109     tst2();
110     YAZ_CHECK_TERM;
111 }
112
113 /*
114  * Local variables:
115  * c-basic-offset: 4
116  * c-file-style: "Stroustrup"
117  * indent-tabs-mode: nil
118  * End:
119  * vim: shiftwidth=4 tabstop=8 expandtab
120  */
121