1 /** @file api_qpbackend.cc
2 * @brief QueryParser tests which need a backend
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
24 #include "api_qpbackend.h"
28 #include "backendmanager.h"
29 #include "testsuite.h"
30 #include "testutils.h"
41 /// Regression test for bug#407 fixed in 1.0.17 and 1.1.3.
42 DEFINE_TESTCASE(qpsynonympartial1
, synonyms
) {
43 static const test test_queries
[] = {
44 { "hello", "((SYNONYM WILDCARD OR hello) OR hello@1)" },
45 { "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
46 { "hello world", "(hello@1 OR ((SYNONYM WILDCARD OR world) OR world@2))" },
47 { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR ((SYNONYM WILDCARD OR world) OR world@2))" },
48 { "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
51 static const test test_queries_auto
[] = {
52 { "hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
53 { "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
54 { "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
55 { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
56 { "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
59 static const test test_queries_partial_auto
[] = {
60 { "hello", "((SYNONYM WILDCARD OR hello) OR hello@1)" },
61 { "~hello", "((SYNONYM WILDCARD OR hello) OR hello@1)" },
62 { "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR ((SYNONYM WILDCARD OR world) OR world@2))" },
63 { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR ((SYNONYM WILDCARD OR world) OR world@2))" },
64 { "world ~hello", "(world@1 OR ((SYNONYM WILDCARD OR hello) OR hello@2))" },
68 Xapian::WritableDatabase
db(get_writable_database());
69 db
.add_synonym("hello", "hi");
70 db
.add_synonym("hello", "howdy");
73 Xapian::Enquire
enquire(db
);
74 Xapian::QueryParser qp
;
75 Xapian::Stem
stemmmer("english");
77 qp
.add_prefix("foo", "XFOO");
80 for (p
= test_queries
; p
->query
; ++p
) {
81 string expect
, parsed
;
85 expect
= "parse error";
87 unsigned f
= qp
.FLAG_SYNONYM
| qp
.FLAG_PARTIAL
| qp
.FLAG_DEFAULT
;
88 Xapian::Query qobj
= qp
.parse_query(p
->query
, f
);
89 parsed
= qobj
.get_description();
90 expect
= string("Query(") + expect
+ ')';
91 } catch (const Xapian::QueryParserError
&e
) {
93 } catch (const Xapian::Error
&e
) {
94 parsed
= e
.get_description();
96 parsed
= "Unknown exception!";
98 tout
<< "Query: " << p
->query
<< '\n';
99 TEST_STRINGS_EQUAL(parsed
, expect
);
102 for (p
= test_queries_auto
; p
->query
; ++p
) {
103 string expect
, parsed
;
107 expect
= "parse error";
109 unsigned f
= qp
.FLAG_AUTO_SYNONYMS
| qp
.FLAG_DEFAULT
;
110 Xapian::Query qobj
= qp
.parse_query(p
->query
, f
);
111 parsed
= qobj
.get_description();
112 expect
= string("Query(") + expect
+ ')';
113 } catch (const Xapian::QueryParserError
&e
) {
114 parsed
= e
.get_msg();
115 } catch (const Xapian::Error
&e
) {
116 parsed
= e
.get_description();
118 parsed
= "Unknown exception!";
120 tout
<< "Query: " << p
->query
<< '\n';
121 TEST_STRINGS_EQUAL(parsed
, expect
);
124 for (p
= test_queries_partial_auto
; p
->query
; ++p
) {
125 string expect
, parsed
;
129 expect
= "parse error";
131 unsigned f
= qp
.FLAG_AUTO_SYNONYMS
| qp
.FLAG_PARTIAL
;
132 f
|= qp
.FLAG_DEFAULT
;
133 Xapian::Query qobj
= qp
.parse_query(p
->query
, f
);
134 parsed
= qobj
.get_description();
135 expect
= string("Query(") + expect
+ ')';
136 } catch (const Xapian::QueryParserError
&e
) {
137 parsed
= e
.get_msg();
138 } catch (const Xapian::Error
&e
) {
139 parsed
= e
.get_description();
141 parsed
= "Unknown exception!";
143 tout
<< "Query: " << p
->query
<< '\n';
144 TEST_STRINGS_EQUAL(parsed
, expect
);