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"
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))" },
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))" },
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))" },
70 Xapian::Database db
= get_database("qpsynonympartial1",
71 [](Xapian::WritableDatabase
& wdb
,
73 wdb
.add_synonym("hello", "hi");
74 wdb
.add_synonym("hello", "howdy");
76 Xapian::Enquire
enquire(db
);
77 Xapian::QueryParser qp
;
78 Xapian::Stem
stemmmer("english");
80 qp
.add_prefix("foo", "XFOO");
83 for (p
= test_queries
; p
->query
; ++p
) {
84 string expect
, parsed
;
88 expect
= "parse error";
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
) {
96 } catch (const Xapian::Error
&e
) {
97 parsed
= e
.get_description();
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
;
110 expect
= "parse error";
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();
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
;
132 expect
= "parse error";
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();
144 parsed
= "Unknown exception!";
146 tout
<< "Query: " << p
->query
<< '\n';
147 TEST_STRINGS_EQUAL(parsed
, expect
);