2 * @brief tests of document replacing.
4 /* Copyright 2009 Richard Boulton
5 * Copyright 2015,2016 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 #include "api_replacedoc.h"
32 #include "testsuite.h"
33 #include "testutils.h"
38 // Test that positionlists are updated correctly.
39 DEFINE_TESTCASE(poslistupdate1
, positional
&& writable
) {
40 Xapian::WritableDatabase db
= get_writable_database();
43 doc
.add_posting("pos", 2);
44 doc
.add_posting("pos", 3);
48 TEST_EQUAL(docterms_to_string(db
, 1), "Term(pos, wdf=2, pos=[2, 3])");
50 doc
= db
.get_document(1);
52 db
.replace_document(1, doc
);
54 TEST_EQUAL(docterms_to_string(db
, 1),
55 "Term(pos, wdf=2, pos=[2, 3]), "
58 doc
= db
.get_document(1);
59 doc
.add_posting("pos3", 1);
60 doc
.add_posting("pos3", 5);
61 db
.replace_document(1, doc
);
63 TEST_EQUAL(docterms_to_string(db
, 1),
64 "Term(pos, wdf=2, pos=[2, 3]), "
66 "Term(pos3, wdf=2, pos=[1, 5])");
68 doc
= db
.get_document(1);
69 doc
.remove_term("pos");
70 db
.replace_document(1, doc
);
72 TEST_EQUAL(docterms_to_string(db
, 1),
74 "Term(pos3, wdf=2, pos=[1, 5])");
76 // Regression test: the old positionlist fragment used to be left lying
78 Xapian::PositionIterator
posit(db
.positionlist_begin(1, "pos"));
79 TEST(posit
== db
.positionlist_end(1, "pos"));
81 doc
= db
.get_document(1);
82 doc
.remove_term("pos3");
83 db
.replace_document(1, doc
);
85 TEST_EQUAL(docterms_to_string(db
, 1),
88 // Regression test: the old positionlist fragment used to be left lying
90 Xapian::PositionIterator
posit2(db
.positionlist_begin(1, "pos3"));
91 TEST(posit2
== db
.positionlist_end(1, "pos3"));
93 doc
= db
.get_document(1);
95 db
.replace_document(1, doc
);
97 TEST_EQUAL(docterms_to_string(db
, 1),
102 static Xapian::Document
104 Xapian::Document doc
;
105 doc
.add_term("z0", 0);
106 doc
.add_term("z1", 1);
112 return ", Term(z0, wdf=0), Term(z1, wdf=1)";
115 /** Check that changing the wdf of a term in a document works.
117 DEFINE_TESTCASE(modtermwdf1
, writable
) {
118 Xapian::WritableDatabase
db(get_writable_database());
120 string
bdt(basic_docterms());
122 // Add a simple document.
123 Xapian::Document
doc1(basic_doc());
124 doc1
.add_term("takeaway", 1);
125 db
.add_document(doc1
);
128 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=1)" + bdt
);
130 // Modify the wdf of an existing document, checking stats before commit.
131 Xapian::Document
doc2(basic_doc());
132 doc2
.add_term("takeaway", 2);
133 db
.replace_document(1, doc2
);
135 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=2)" + bdt
);
137 // Remove a term, and then put it back again.
138 Xapian::Document
doc0(basic_doc());
139 db
.replace_document(1, doc0
);
141 TEST_EQUAL(docterms_to_string(db
, 1), bdt
.substr(2));
142 db
.replace_document(1, doc1
);
144 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=1)" + bdt
);
146 // Remove a term, commit, then put it back, remove it, and put it back.
147 // This is to test the handling of items in the change cache.
148 db
.replace_document(1, doc0
);
150 db
.replace_document(1, doc2
);
151 db
.replace_document(1, doc0
);
152 db
.replace_document(1, doc2
);
155 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=2)" + bdt
);
157 // Remove a term, and then put it back again without checking stats.
158 db
.replace_document(1, doc0
);
159 db
.replace_document(1, doc2
);
161 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=2)" + bdt
);
163 // Modify a term, and then put it back again without checking stats.
164 db
.replace_document(1, doc1
);
165 db
.replace_document(1, doc2
);
167 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=2)" + bdt
);
169 // Modify the wdf of an existing document, checking stats after commit.
170 Xapian::Document
doc3(basic_doc());
171 doc3
.add_term("takeaway", 3);
172 db
.replace_document(1, doc3
);
175 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=3)" + bdt
);
177 // Change a document, without changing its length.
178 Xapian::Document
doc3_diff(basic_doc());
179 doc3_diff
.add_term("takeaways", 3);
180 db
.replace_document(1, doc3_diff
);
183 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaways, wdf=3)" + bdt
);
186 db
.replace_document(1, doc3
);
188 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=3)" + bdt
);
190 // Modify a document taken from the database.
191 Xapian::Document
doc4(db
.get_document(1));
192 Xapian::Document
doc3a(db
.get_document(1)); // need this one later
193 doc3a
.add_boolean_term("takeaway"); // Pull the document termlist into memory.
194 doc4
.add_term("takeaway", 1);
195 db
.replace_document(1, doc4
);
197 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=4)" + bdt
);
199 // Add a document which was previously added and then modified.
200 doc1
.add_term("takeaway", 1);
201 db
.replace_document(1, doc1
);
203 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=2)" + bdt
);
205 // Add back a document which was taken from the database, but never modified.
206 db
.replace_document(1, doc3a
);
208 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=3)" + bdt
);
210 // Add a position to the document.
211 Xapian::Document
doc5(db
.get_document(1));
212 doc5
.add_posting("takeaway", 1, 2);
213 db
.replace_document(1, doc5
);
215 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=5, pos=[1])" + bdt
);
217 // Add a position without changing the wdf.
218 Xapian::Document
doc5b(db
.get_document(1));
219 doc5b
.add_posting("takeaway", 2, 0);
220 db
.replace_document(1, doc5b
);
222 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=5, pos=[1, 2])" + bdt
);
224 // Delete a position without changing the wdf.
225 Xapian::Document
doc5c(basic_doc());
226 doc5c
.add_posting("takeaway", 2, 5);
227 db
.replace_document(1, doc5c
);
229 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=5, pos=[2])" + bdt
);
231 // Delete the other position without changing the wdf.
232 Xapian::Document
doc5d(basic_doc());
233 doc5d
.add_term("takeaway", 5);
234 db
.replace_document(1, doc5d
);
236 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=5)" + bdt
);
238 // Reduce the wdf to 0, but don't delete the term.
239 Xapian::Document
doc0freq(basic_doc());
240 doc0freq
.add_term("takeaway", 0);
241 db
.replace_document(1, doc0freq
);
243 TEST_EQUAL(docterms_to_string(db
, 1), "Term(takeaway, wdf=0)" + bdt
);
245 // Reduce the wdf to 0, and delete the term.
246 db
.replace_document(1, doc0
);
248 TEST_EQUAL(docterms_to_string(db
, 1), bdt
.substr(2));
250 // Delete the document.
251 db
.delete_document(1);
253 TEST_EXCEPTION(Xapian::DocNotFoundError
, docterms_to_string(db
, 1));
254 TEST_EQUAL(postlist_to_string(db
, "takeaway"), "");
255 TEST_EXCEPTION(Xapian::DocNotFoundError
, docstats_to_string(db
, 1));
256 TEST_EQUAL(termstats_to_string(db
, "takeaway"), "tf=0,cf=0");
257 TEST_EQUAL(db
.get_doccount(), 0);
258 TEST_EQUAL(db
.get_avlength(), 0);
259 TEST_EQUAL(db
.get_lastdocid(), 1);