tests/api_qpbackend.cc

Go to the documentation of this file.
00001 
00004 /* Copyright (c) 2009 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00019  * USA
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, flint) {
00043     static test test_queries[] = {
00044         { "hello", "hello:(pos=1)" },
00045         { "~hello", "(hello:(pos=1) OR hi:(pos=1) OR howdy:(pos=1))" },
00046         { "hello world", "(hello:(pos=1) OR world:(pos=2))" },
00047         { "~hello world", "(hello:(pos=1) OR hi:(pos=1) OR howdy:(pos=1) OR world:(pos=2))" },
00048         { "world ~hello", "(world:(pos=1) OR hello:(pos=2) OR hi:(pos=2) OR howdy:(pos=2))" },
00049         { NULL, NULL }
00050     };
00051     static test test_queries_auto[] = {
00052         { "hello", "(hello:(pos=1) OR hi:(pos=1) OR howdy:(pos=1))" },
00053         { "~hello", "(hello:(pos=1) OR hi:(pos=1) OR howdy:(pos=1))" },
00054         { "hello world", "(hello:(pos=1) OR hi:(pos=1) OR howdy:(pos=1) OR world:(pos=2))" },
00055         { "~hello world", "(hello:(pos=1) OR hi:(pos=1) OR howdy:(pos=1) OR world:(pos=2))" },
00056         { "world ~hello", "(world:(pos=1) OR hello:(pos=2) OR hi:(pos=2) OR howdy:(pos=2))" },
00057         { NULL, NULL }
00058     };
00059     static test test_queries_partial_auto[] = {
00060         { "hello", "hello:(pos=1)" },
00061         { "~hello", "hello:(pos=1)" },
00062         { "hello world", "(hello:(pos=1) OR hi:(pos=1) OR howdy:(pos=1) OR world:(pos=2))" },
00063         { "~hello world", "(hello:(pos=1) OR hi:(pos=1) OR 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.flush();
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     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 }

Documentation for Xapian (version 1.0.20).
Generated on 28 Apr 2010 by Doxygen 1.5.2.