Test cases for RPN to SOLR conversion
[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=4 abc", 0)); /* should fail */
60
61     solr_transform_define_pattern(ct, "index.title", "1=4");
62     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title:abc"));
63
64     solr_transform_define_pattern(ct, "index.foo", "1=bar");
65     YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo:abc"));
66
67     solr_transform_close(ct);
68 }
69
70 static void tst2(void)
71 {
72     WRBUF w = wrbuf_alloc();
73     solr_transform_t ct = 0;
74     const char *srcdir = getenv("srcdir");
75     if (srcdir)
76     {
77         wrbuf_puts(w, srcdir);
78         wrbuf_puts(w, "/");
79     }
80     wrbuf_puts(w, "../etc/pqf.properties");
81     
82     ct = solr_transform_open_fname(wrbuf_cstr(w));
83     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
84     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
85     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
86     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title:abc"));
87     YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
88     YAZ_CHECK(compare(ct, "@attr 2=1 @attr 1=30 1980", "dc.date:[* to 1980]"));
89     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date:1980"));
90     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date:[* to 1980]"));
91     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date:[* to 1980]"));
92     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date:[1980 to *]"));
93
94     YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "solr.allRecords=1"));
95     YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
96     solr_transform_close(ct);
97     wrbuf_destroy(w);
98 }
99
100 int main (int argc, char **argv)
101 {
102     YAZ_CHECK_INIT(argc, argv);
103     YAZ_CHECK_LOG();
104     tst1();
105     tst2();
106     YAZ_CHECK_TERM;
107 }
108
109 /*
110  * Local variables:
111  * c-basic-offset: 4
112  * c-file-style: "Stroustrup"
113  * indent-tabs-mode: nil
114  * End:
115  * vim: shiftwidth=4 tabstop=8 expandtab
116  */
117