1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
14 #include <yaz/rpn2solr.h>
15 #include <yaz/wrbuf.h>
16 #include <yaz/pquery.h>
18 static int compare(solr_transform_t ct, const char *pqf, const char *solr)
21 ODR odr = odr_createmem(ODR_ENCODE);
22 WRBUF w = wrbuf_alloc();
23 Z_RPNQuery *q = p_query_rpn(odr, pqf);
27 int r = solr_transform_rpn2solr_wrbuf(ct, w, q);
32 yaz_log(YLOG_LOG, "%s -> Error %d", pqf, r);
33 if (!solr) /* also expected error? */
38 yaz_log(YLOG_LOG, "%s -> %s", pqf, wrbuf_cstr(w));
39 if (solr && !strcmp(wrbuf_cstr(w), solr))
50 static void tst1(void)
52 solr_transform_t ct = solr_transform_create();
54 YAZ_CHECK(compare(ct, "abc", "abc"));
55 YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
56 YAZ_CHECK(compare(ct, "@not a b", "a AND NOT b"));
57 YAZ_CHECK(compare(ct, "@and @or a b c", "(a OR b) AND c"));
58 YAZ_CHECK(compare(ct, "@and a b", "a AND b"));
59 YAZ_CHECK(compare(ct, "@or a b", "a OR b"));
60 YAZ_CHECK(compare(ct, "@attr 1=field abc", "field:abc"));
61 YAZ_CHECK(compare(ct, "@attr 1=field \"a b c\"", "field:\"a b c\""));
62 YAZ_CHECK(compare(ct, "@attr 1=4 abc", 0)); /* should fail */
64 solr_transform_define_pattern(ct, "index.title", "1=4");
65 YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title:abc"));
67 solr_transform_define_pattern(ct, "index.foo", "1=bar");
68 YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo:abc"));
71 YAZ_CHECK(compare(ct, "@attr 5=1 water", "water*"));
72 YAZ_CHECK(compare(ct, "@attr 5=2 water", 0));
73 YAZ_CHECK(compare(ct, "@attr 5=3 water", 0));
74 YAZ_CHECK(compare(ct, "@attr 5=100 water", "water"));
75 YAZ_CHECK(compare(ct, "@attr 5=101 water", 0));
76 YAZ_CHECK(compare(ct, "@attr 5=104 w#ter", "w?ter"));
77 YAZ_CHECK(compare(ct, "@attr 5=104 w#t?r", "w?t*r"));
78 YAZ_CHECK(compare(ct, "@attr 5=104 w#te?", "w?te*"));
79 YAZ_CHECK(compare(ct, "@attr 5=104 w\\#te?", "w?te*")); /* PQF eats # */
80 YAZ_CHECK(compare(ct, "@attr 5=104 w\\\\#te?", "w#te*"));
82 /* reserved characters */
83 YAZ_CHECK(compare(ct, "@attr 5=104 \\\"\\\\\\\\",
85 YAZ_CHECK(compare(ct, "@attr 5=104 \\\"\\\\\\\\\\\"",
86 "\\\"" "\\\\" "\\\""));
87 YAZ_CHECK(compare(ct, "@attr 5=104 \\\"\\\\", "\\\"\\\\"));
89 YAZ_CHECK(compare(ct, "@attr 5=104 \\{:\\}", "\\{\\:\\}"));
91 YAZ_CHECK(compare(ct, "@attr 5=104 \\\"\\\\\\\\\\\"",
92 "\\\"" "\\\\" "\\\""));
94 solr_transform_close(ct);
97 static void tst2(void)
99 WRBUF w = wrbuf_alloc();
100 solr_transform_t ct = 0;
101 const char *srcdir = getenv("srcdir");
104 wrbuf_puts(w, srcdir);
107 wrbuf_puts(w, "../etc/pqf.properties");
109 ct = solr_transform_open_fname(wrbuf_cstr(w));
110 YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
112 YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
115 YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
116 YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title:abc"));
118 YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
121 // YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=1 1980", "dc.date:[* TO 1980]"));
122 YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date:[* TO 1980]"));
123 YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date:1980"));
124 YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date:[1980 TO *]"));
125 // YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date:[* TO 1980]"));
126 YAZ_CHECK(compare(ct, "@and @attr 1=30 @attr 2=4 234 @attr 1=30 @attr 2=2 1990", "dc.date:[234 TO *] AND dc.date:[* TO 1990]"));
128 YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "solr.allRecords=1"));
130 YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
131 solr_transform_close(ct);
135 int main (int argc, char **argv)
137 YAZ_CHECK_INIT(argc, argv);
147 * c-file-style: "Stroustrup"
148 * indent-tabs-mode: nil
150 * vim: shiftwidth=4 tabstop=8 expandtab