File: api_qpbackend.cc

package info (click to toggle)
xapian-core 1.4.3-2~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 21,224 kB
  • sloc: cpp: 113,806; ansic: 8,723; sh: 4,433; perl: 836; makefile: 567; tcl: 317; python: 40
file content (148 lines) | stat: -rw-r--r-- 4,850 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/** @file api_qpbackend.cc
 * @brief QueryParser tests which need a backend
 */
/* Copyright (c) 2009,2015 Olly Betts
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <config.h>

#include "api_qpbackend.h"

#include <xapian.h>

#include "backendmanager.h"
#include "testsuite.h"
#include "testutils.h"

#include "apitest.h"

using namespace std;

struct test {
    const char *query;
    const char *expect;
};

/// Regression test for bug#407 fixed in 1.0.17 and 1.1.3.
DEFINE_TESTCASE(qpsynonympartial1, synonyms) {
    static const test test_queries[] = {
	{ "hello", "((SYNONYM WILDCARD OR hello) OR hello@1)" },
	{ "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
	{ "hello world", "(hello@1 OR ((SYNONYM WILDCARD OR world) OR world@2))" },
	{ "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR ((SYNONYM WILDCARD OR world) OR world@2))" },
	{ "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
	{ NULL, NULL }
    };
    static const test test_queries_auto[] = {
	{ "hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
	{ "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
	{ "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
	{ "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
	{ "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
	{ NULL, NULL }
    };
    static const test test_queries_partial_auto[] = {
	{ "hello", "((SYNONYM WILDCARD OR hello) OR hello@1)" },
	{ "~hello", "((SYNONYM WILDCARD OR hello) OR hello@1)" },
	{ "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR ((SYNONYM WILDCARD OR world) OR world@2))" },
	{ "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR ((SYNONYM WILDCARD OR world) OR world@2))" },
	{ "world ~hello", "(world@1 OR ((SYNONYM WILDCARD OR hello) OR hello@2))" },
	{ NULL, NULL }
    };

    Xapian::WritableDatabase db(get_writable_database());
    db.add_synonym("hello", "hi");
    db.add_synonym("hello", "howdy");
    db.commit();

    Xapian::Enquire enquire(db);
    Xapian::QueryParser qp;
    Xapian::Stem stemmmer("english");
    qp.set_database(db);
    qp.add_prefix("foo", "XFOO");

    const test *p;
    for (p = test_queries; p->query; ++p) {
	string expect, parsed;
	if (p->expect)
	    expect = p->expect;
	else
	    expect = "parse error";
	try {
	    unsigned f = qp.FLAG_SYNONYM | qp.FLAG_PARTIAL | qp.FLAG_DEFAULT;
	    Xapian::Query qobj = qp.parse_query(p->query, f);
	    parsed = qobj.get_description();
	    expect = string("Query(") + expect + ')';
	} catch (const Xapian::QueryParserError &e) {
	    parsed = e.get_msg();
	} catch (const Xapian::Error &e) {
	    parsed = e.get_description();
	} catch (...) {
	    parsed = "Unknown exception!";
	}
	tout << "Query: " << p->query << '\n';
	TEST_STRINGS_EQUAL(parsed, expect);
    }

    for (p = test_queries_auto; p->query; ++p) {
	string expect, parsed;
	if (p->expect)
	    expect = p->expect;
	else
	    expect = "parse error";
	try {
	    unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_DEFAULT;
	    Xapian::Query qobj = qp.parse_query(p->query, f);
	    parsed = qobj.get_description();
	    expect = string("Query(") + expect + ')';
	} catch (const Xapian::QueryParserError &e) {
	    parsed = e.get_msg();
	} catch (const Xapian::Error &e) {
	    parsed = e.get_description();
	} catch (...) {
	    parsed = "Unknown exception!";
	}
	tout << "Query: " << p->query << '\n';
	TEST_STRINGS_EQUAL(parsed, expect);
    }

    for (p = test_queries_partial_auto; p->query; ++p) {
	string expect, parsed;
	if (p->expect)
	    expect = p->expect;
	else
	    expect = "parse error";
	try {
	    unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_PARTIAL;
	    f |= qp.FLAG_DEFAULT;
	    Xapian::Query qobj = qp.parse_query(p->query, f);
	    parsed = qobj.get_description();
	    expect = string("Query(") + expect + ')';
	} catch (const Xapian::QueryParserError &e) {
	    parsed = e.get_msg();
	} catch (const Xapian::Error &e) {
	    parsed = e.get_description();
	} catch (...) {
	    parsed = "Unknown exception!";
	}
	tout << "Query: " << p->query << '\n';
	TEST_STRINGS_EQUAL(parsed, expect);
    }

    return true;
}