00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <config.h>
00023
00024 #include "api_qpbackend.h"
00025
00026 #include <xapian.h>
00027
00028 #include "backendmanager.h"
00029 #include "testsuite.h"
00030 #include "testutils.h"
00031
00032 #include "apitest.h"
00033
00034 using namespace std;
00035
00036 struct test {
00037 const char *query;
00038 const char *expect;
00039 };
00040
00042 DEFINE_TESTCASE(qpsynonympartial1, synonyms) {
00043 static const test test_queries[] = {
00044 { "hello", "hello:(pos=1)" },
00045 { "~hello", "(hello:(pos=1) SYNONYM hi:(pos=1) SYNONYM howdy:(pos=1))" },
00046 { "hello world", "(hello:(pos=1) OR world:(pos=2))" },
00047 { "~hello world", "((hello:(pos=1) SYNONYM hi:(pos=1) SYNONYM howdy:(pos=1)) OR world:(pos=2))" },
00048 { "world ~hello", "(world:(pos=1) OR (hello:(pos=2) SYNONYM hi:(pos=2) SYNONYM howdy:(pos=2)))" },
00049 { NULL, NULL }
00050 };
00051 static const test test_queries_auto[] = {
00052 { "hello", "(hello:(pos=1) SYNONYM hi:(pos=1) SYNONYM howdy:(pos=1))" },
00053 { "~hello", "(hello:(pos=1) SYNONYM hi:(pos=1) SYNONYM howdy:(pos=1))" },
00054 { "hello world", "((hello:(pos=1) SYNONYM hi:(pos=1) SYNONYM howdy:(pos=1)) OR world:(pos=2))" },
00055 { "~hello world", "((hello:(pos=1) SYNONYM hi:(pos=1) SYNONYM howdy:(pos=1)) OR world:(pos=2))" },
00056 { "world ~hello", "(world:(pos=1) OR (hello:(pos=2) SYNONYM hi:(pos=2) SYNONYM howdy:(pos=2)))" },
00057 { NULL, NULL }
00058 };
00059 static const test test_queries_partial_auto[] = {
00060 { "hello", "hello:(pos=1)" },
00061 { "~hello", "hello:(pos=1)" },
00062 { "hello world", "((hello:(pos=1) SYNONYM hi:(pos=1) SYNONYM howdy:(pos=1)) OR world:(pos=2))" },
00063 { "~hello world", "((hello:(pos=1) SYNONYM hi:(pos=1) SYNONYM howdy:(pos=1)) OR world:(pos=2))" },
00064 { "world ~hello", "(world:(pos=1) OR hello:(pos=2))" },
00065 { NULL, NULL }
00066 };
00067
00068 Xapian::WritableDatabase db(get_writable_database());
00069 db.add_synonym("hello", "hi");
00070 db.add_synonym("hello", "howdy");
00071 db.commit();
00072
00073 Xapian::Enquire enquire(db);
00074 Xapian::QueryParser qp;
00075 Xapian::Stem stemmmer("english");
00076 qp.set_database(db);
00077 qp.add_prefix("foo", "XFOO");
00078
00079 const test *p;
00080 for (p = test_queries; p->query; ++p) {
00081 string expect, parsed;
00082 if (p->expect)
00083 expect = p->expect;
00084 else
00085 expect = "parse error";
00086 try {
00087 unsigned f = qp.FLAG_SYNONYM | qp.FLAG_PARTIAL | qp.FLAG_DEFAULT;
00088 Xapian::Query qobj = qp.parse_query(p->query, f);
00089 parsed = qobj.get_description();
00090 expect = string("Xapian::Query(") + expect + ')';
00091 } catch (const Xapian::QueryParserError &e) {
00092 parsed = e.get_msg();
00093 } catch (const Xapian::Error &e) {
00094 parsed = e.get_description();
00095 } catch (...) {
00096 parsed = "Unknown exception!";
00097 }
00098 tout << "Query: " << p->query << '\n';
00099 TEST_STRINGS_EQUAL(parsed, expect);
00100 }
00101
00102 for (p = test_queries_auto; p->query; ++p) {
00103 string expect, parsed;
00104 if (p->expect)
00105 expect = p->expect;
00106 else
00107 expect = "parse error";
00108 try {
00109 unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_DEFAULT;
00110 Xapian::Query qobj = qp.parse_query(p->query, f);
00111 parsed = qobj.get_description();
00112 expect = string("Xapian::Query(") + expect + ')';
00113 } catch (const Xapian::QueryParserError &e) {
00114 parsed = e.get_msg();
00115 } catch (const Xapian::Error &e) {
00116 parsed = e.get_description();
00117 } catch (...) {
00118 parsed = "Unknown exception!";
00119 }
00120 tout << "Query: " << p->query << '\n';
00121 TEST_STRINGS_EQUAL(parsed, expect);
00122 }
00123
00124 for (p = test_queries_partial_auto; p->query; ++p) {
00125 string expect, parsed;
00126 if (p->expect)
00127 expect = p->expect;
00128 else
00129 expect = "parse error";
00130 try {
00131 unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_PARTIAL;
00132 f |= qp.FLAG_DEFAULT;
00133 Xapian::Query qobj = qp.parse_query(p->query, f);
00134 parsed = qobj.get_description();
00135 expect = string("Xapian::Query(") + expect + ')';
00136 } catch (const Xapian::QueryParserError &e) {
00137 parsed = e.get_msg();
00138 } catch (const Xapian::Error &e) {
00139 parsed = e.get_description();
00140 } catch (...) {
00141 parsed = "Unknown exception!";
00142 }
00143 tout << "Query: " << p->query << '\n';
00144 TEST_STRINGS_EQUAL(parsed, expect);
00145 }
00146
00147 return true;
00148 }