Make TradWeight a simple subclass and deprecate
[xapian.git] / xapian-core / tests / api_qpbackend.cc
blobb2b83ebac9a5799a2956232ac444296f0b80b355
1 /** @file
2 * @brief QueryParser tests which need a backend
3 */
4 /* Copyright (c) 2009,2015 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
22 #include <config.h>
24 #include "api_qpbackend.h"
26 #include <xapian.h>
28 #include "backendmanager.h"
29 #include "testsuite.h"
30 #include "testutils.h"
32 #include "apitest.h"
34 using namespace std;
36 namespace {
37 struct test {
38 const char *query;
39 const char *expect;
43 /// Regression test for bug#407 fixed in 1.0.17 and 1.1.3.
44 DEFINE_TESTCASE(qpsynonympartial1, synonyms) {
45 static const test test_queries[] = {
46 { "hello", "(WILDCARD SYNONYM hello OR hello@1)" },
47 { "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
48 { "hello world", "(hello@1 OR (WILDCARD SYNONYM world OR world@2))" },
49 { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR (WILDCARD SYNONYM world OR world@2))" },
50 { "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
51 { NULL, NULL }
53 static const test test_queries_auto[] = {
54 { "hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
55 { "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
56 { "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
57 { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
58 { "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
59 { NULL, NULL }
61 static const test test_queries_partial_auto[] = {
62 { "hello", "(WILDCARD SYNONYM hello OR hello@1)" },
63 { "~hello", "(WILDCARD SYNONYM hello OR hello@1)" },
64 { "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR (WILDCARD SYNONYM world OR world@2))" },
65 { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR (WILDCARD SYNONYM world OR world@2))" },
66 { "world ~hello", "(world@1 OR (WILDCARD SYNONYM hello OR hello@2))" },
67 { NULL, NULL }
70 Xapian::Database db = get_database("qpsynonympartial1",
71 [](Xapian::WritableDatabase& wdb,
72 const string&) {
73 wdb.add_synonym("hello", "hi");
74 wdb.add_synonym("hello", "howdy");
75 });
76 Xapian::Enquire enquire(db);
77 Xapian::QueryParser qp;
78 Xapian::Stem stemmmer("english");
79 qp.set_database(db);
80 qp.add_prefix("foo", "XFOO");
82 const test *p;
83 for (p = test_queries; p->query; ++p) {
84 string expect, parsed;
85 if (p->expect)
86 expect = p->expect;
87 else
88 expect = "parse error";
89 try {
90 unsigned f = qp.FLAG_SYNONYM | qp.FLAG_PARTIAL | qp.FLAG_DEFAULT;
91 Xapian::Query qobj = qp.parse_query(p->query, f);
92 parsed = qobj.get_description();
93 expect = string("Query(") + expect + ')';
94 } catch (const Xapian::QueryParserError &e) {
95 parsed = e.get_msg();
96 } catch (const Xapian::Error &e) {
97 parsed = e.get_description();
98 } catch (...) {
99 parsed = "Unknown exception!";
101 tout << "Query: " << p->query << '\n';
102 TEST_STRINGS_EQUAL(parsed, expect);
105 for (p = test_queries_auto; p->query; ++p) {
106 string expect, parsed;
107 if (p->expect)
108 expect = p->expect;
109 else
110 expect = "parse error";
111 try {
112 unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_DEFAULT;
113 Xapian::Query qobj = qp.parse_query(p->query, f);
114 parsed = qobj.get_description();
115 expect = string("Query(") + expect + ')';
116 } catch (const Xapian::QueryParserError &e) {
117 parsed = e.get_msg();
118 } catch (const Xapian::Error &e) {
119 parsed = e.get_description();
120 } catch (...) {
121 parsed = "Unknown exception!";
123 tout << "Query: " << p->query << '\n';
124 TEST_STRINGS_EQUAL(parsed, expect);
127 for (p = test_queries_partial_auto; p->query; ++p) {
128 string expect, parsed;
129 if (p->expect)
130 expect = p->expect;
131 else
132 expect = "parse error";
133 try {
134 unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_PARTIAL;
135 f |= qp.FLAG_DEFAULT;
136 Xapian::Query qobj = qp.parse_query(p->query, f);
137 parsed = qobj.get_description();
138 expect = string("Query(") + expect + ')';
139 } catch (const Xapian::QueryParserError &e) {
140 parsed = e.get_msg();
141 } catch (const Xapian::Error &e) {
142 parsed = e.get_description();
143 } catch (...) {
144 parsed = "Unknown exception!";
146 tout << "Query: " << p->query << '\n';
147 TEST_STRINGS_EQUAL(parsed, expect);