tests/api_replacedoc.cc

Go to the documentation of this file.
00001 /* api_replacedoc.cc: tests of document replacing.
00002  *
00003  * Copyright 2009 Richard Boulton
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00018  * USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include "api_replacedoc.h"
00024 
00025 #include <string>
00026 #include <map>
00027 
00028 using namespace std;
00029 
00030 #include <xapian.h>
00031 #include "testsuite.h"
00032 #include "testutils.h"
00033 
00034 #include "apitest.h"
00035 #include "dbcheck.h"
00036 
00037 // Test that positionlists are updated correctly.
00038 DEFINE_TESTCASE(poslistupdate1, positional && writable) {
00039     Xapian::WritableDatabase db = get_writable_database();
00040 
00041     Xapian::Document doc;
00042     doc.add_posting("pos", 2);
00043     doc.add_posting("pos", 3);
00044     db.add_document(doc);
00045     db.flush();
00046 
00047     TEST_EQUAL(docterms_to_string(db, 1), "Term(pos, wdf=2, pos=[2, 3])");
00048 
00049     doc = db.get_document(1);
00050     doc.add_term("pos2");
00051     db.replace_document(1, doc);
00052     db.flush();
00053     TEST_EQUAL(docterms_to_string(db, 1),
00054                "Term(pos, wdf=2, pos=[2, 3]), "
00055                "Term(pos2, wdf=1)");
00056 
00057     doc = db.get_document(1);
00058     doc.add_posting("pos3", 1);
00059     doc.add_posting("pos3", 5);
00060     db.replace_document(1, doc);
00061     db.flush();
00062     TEST_EQUAL(docterms_to_string(db, 1),
00063                "Term(pos, wdf=2, pos=[2, 3]), "
00064                "Term(pos2, wdf=1), "
00065                "Term(pos3, wdf=2, pos=[1, 5])");
00066 
00067     doc = db.get_document(1);
00068     doc.remove_term("pos");
00069     db.replace_document(1, doc);
00070     db.flush();
00071     TEST_EQUAL(docterms_to_string(db, 1),
00072                "Term(pos2, wdf=1), "
00073                "Term(pos3, wdf=2, pos=[1, 5])");
00074 
00075     // Regression test: the old positionlist fragment used to be left lying
00076     // around here.
00077     TEST_EXCEPTION(Xapian::RangeError,
00078         Xapian::PositionIterator posit(db.positionlist_begin(1, "pos")));
00079 
00080     doc = db.get_document(1);
00081     doc.remove_term("pos3");
00082     db.replace_document(1, doc);
00083     db.flush();
00084     TEST_EQUAL(docterms_to_string(db, 1),
00085                "Term(pos2, wdf=1)");
00086 
00087     // Regression test: the old positionlist fragment used to be left lying
00088     // around here.
00089     TEST_EXCEPTION(Xapian::RangeError,
00090         Xapian::PositionIterator posit2(db.positionlist_begin(1, "pos3")));
00091 
00092     doc = db.get_document(1);
00093     doc.add_term("pos");
00094     db.replace_document(1, doc);
00095     db.flush();
00096     TEST_EQUAL(docterms_to_string(db, 1),
00097                "Term(pos, wdf=1), "
00098                "Term(pos2, wdf=1)");
00099 
00100     return true;
00101 }
00102 
00103 static Xapian::Document
00104 basic_doc() {
00105     Xapian::Document doc;
00106     doc.add_term("z0", 0);
00107     doc.add_term("z1", 1);
00108     return doc;
00109 }
00110 
00111 static string
00112 basic_docterms() {
00113     return ", Term(z0, wdf=0), Term(z1, wdf=1)";
00114 }
00115 
00118 DEFINE_TESTCASE(modtermwdf1, writable) {
00119     Xapian::WritableDatabase db(get_writable_database());
00120 
00121     string bdt(basic_docterms());
00122 
00123     // Add a simple document.
00124     Xapian::Document doc1(basic_doc());
00125     doc1.add_term("takeaway", 1);
00126     db.add_document(doc1);
00127 
00128     dbcheck(db, 1, 1);
00129     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=1)" + bdt);
00130 
00131     // Modify the wdf of an existing document, checking stats before flush.
00132     Xapian::Document doc2(basic_doc());
00133     doc2.add_term("takeaway", 2);
00134     db.replace_document(1, doc2);
00135     dbcheck(db, 1, 1);
00136     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);
00137 
00138     // Remove a term, and then put it back again.
00139     Xapian::Document doc0(basic_doc());
00140     db.replace_document(1, doc0);
00141     dbcheck(db, 1, 1);
00142     TEST_EQUAL(docterms_to_string(db, 1), bdt.substr(2));
00143     db.replace_document(1, doc1);
00144     dbcheck(db, 1, 1);
00145     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=1)" + bdt);
00146 
00147     // Remove a term, flush, then put it back, remove it, and put it back.
00148     // This is to test the handling of items in the change cache.
00149     db.replace_document(1, doc0);
00150     db.flush();
00151     db.replace_document(1, doc2);
00152     db.replace_document(1, doc0);
00153     db.replace_document(1, doc2);
00154     db.flush();
00155     dbcheck(db, 1, 1);
00156     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);
00157 
00158     // Remove a term, and then put it back again without checking stats.
00159     db.replace_document(1, doc0);
00160     db.replace_document(1, doc2);
00161     dbcheck(db, 1, 1);
00162     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);
00163 
00164     // Modify a term, and then put it back again without checking stats.
00165     db.replace_document(1, doc1);
00166     db.replace_document(1, doc2);
00167     dbcheck(db, 1, 1);
00168     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);
00169 
00170     // Modify the wdf of an existing document, checking stats after flush.
00171     Xapian::Document doc3(basic_doc());
00172     doc3.add_term("takeaway", 3);
00173     db.replace_document(1, doc3);
00174     db.flush();
00175     dbcheck(db, 1, 1);
00176     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=3)" + bdt);
00177 
00178     // Change a document, without changing its length.
00179     Xapian::Document doc3_diff(basic_doc());
00180     doc3_diff.add_term("takeaways", 3);
00181     db.replace_document(1, doc3_diff);
00182     db.flush();
00183     dbcheck(db, 1, 1);
00184     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaways, wdf=3)" + bdt);
00185 
00186     // Put it back.
00187     db.replace_document(1, doc3);
00188     dbcheck(db, 1, 1);
00189     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=3)" + bdt);
00190 
00191     // Modify a document taken from the database.
00192     Xapian::Document doc4(db.get_document(1));
00193     Xapian::Document doc3a(db.get_document(1)); // need this one later
00194     doc3a.termlist_count(); // Pull the document termlist into memory.
00195     doc4.add_term("takeaway", 1);
00196     db.replace_document(1, doc4);
00197     dbcheck(db, 1, 1);
00198     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=4)" + bdt);
00199 
00200     // Add a document which was previously added and then modified.
00201     doc1.add_term("takeaway", 1);
00202     db.replace_document(1, doc1);
00203     dbcheck(db, 1, 1);
00204     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=2)" + bdt);
00205 
00206     // Add back a document which was taken from the database, but never modified.
00207     db.replace_document(1, doc3a);
00208     dbcheck(db, 1, 1);
00209     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=3)" + bdt);
00210 
00211     // Add a position to the document.
00212     Xapian::Document doc5(db.get_document(1));
00213     doc5.add_posting("takeaway", 1, 2);
00214     db.replace_document(1, doc5);
00215     dbcheck(db, 1, 1);
00216     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=5, pos=[1])" + bdt);
00217 
00218     // Add a position without changing the wdf.
00219     Xapian::Document doc5b(db.get_document(1));
00220     doc5b.add_posting("takeaway", 2, 0);
00221     db.replace_document(1, doc5b);
00222     dbcheck(db, 1, 1);
00223     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=5, pos=[1, 2])" + bdt);
00224 
00225     // Delete a position without changing the wdf.
00226     Xapian::Document doc5c(basic_doc());
00227     doc5c.add_posting("takeaway", 2, 5);
00228     db.replace_document(1, doc5c);
00229     dbcheck(db, 1, 1);
00230     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=5, pos=[2])" + bdt);
00231 
00232     // Delete the other position without changing the wdf.
00233     Xapian::Document doc5d(basic_doc());
00234     doc5d.add_term("takeaway", 5);
00235     db.replace_document(1, doc5d);
00236     dbcheck(db, 1, 1);
00237     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=5)" + bdt);
00238 
00239     // Reduce the wdf to 0, but don't delete the term.
00240     Xapian::Document doc0freq(basic_doc());
00241     doc0freq.add_term("takeaway", 0);
00242     db.replace_document(1, doc0freq);
00243     dbcheck(db, 1, 1);
00244     TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaway, wdf=0)" + bdt);
00245 
00246     // Reduce the wdf to 0, and delete the term.
00247     db.replace_document(1, doc0);
00248     dbcheck(db, 1, 1);
00249     TEST_EQUAL(docterms_to_string(db, 1), bdt.substr(2));
00250 
00251     // Delete the document.
00252     db.delete_document(1);
00253     dbcheck(db, 0, 1);
00254     TEST_EXCEPTION(Xapian::DocNotFoundError, docterms_to_string(db, 1));
00255     TEST_EQUAL(postlist_to_string(db, "takeaway"), "");
00256     TEST_EXCEPTION(Xapian::DocNotFoundError, docstats_to_string(db, 1));
00257     TEST_EQUAL(termstats_to_string(db, "takeaway"), "tf=0,cf=0");
00258     TEST_EQUAL(dbstats_to_string(db), "dc=0,al=0,ld=1");
00259 
00260     return true;
00261 }

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