[ci] Fix clang-santisers job for GHA change
[xapian.git] / xapian-core / tests / api_replacedoc.cc
blob00cf6150282d9e8fdde476b2cd13ab6a24958090
1 /** @file
2 * @brief tests of document replacing.
3 */
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
20 * USA
23 #include <config.h>
25 #include "api_replacedoc.h"
27 #include <string>
29 using namespace std;
31 #include <xapian.h>
32 #include "testsuite.h"
33 #include "testutils.h"
35 #include "apitest.h"
36 #include "dbcheck.h"
38 // Test that positionlists are updated correctly.
39 DEFINE_TESTCASE(poslistupdate1, positional && writable) {
40 Xapian::WritableDatabase db = get_writable_database();
42 Xapian::Document doc;
43 doc.add_posting("pos", 2);
44 doc.add_posting("pos", 3);
45 db.add_document(doc);
46 db.commit();
48 TEST_EQUAL(docterms_to_string(db, 1), "Term(pos, wdf=2, pos=[2, 3])");
50 doc = db.get_document(1);
51 doc.add_term("pos2");
52 db.replace_document(1, doc);
53 db.commit();
54 TEST_EQUAL(docterms_to_string(db, 1),
55 "Term(pos, wdf=2, pos=[2, 3]), "
56 "Term(pos2, wdf=1)");
58 doc = db.get_document(1);
59 doc.add_posting("pos3", 1);
60 doc.add_posting("pos3", 5);
61 db.replace_document(1, doc);
62 db.commit();
63 TEST_EQUAL(docterms_to_string(db, 1),
64 "Term(pos, wdf=2, pos=[2, 3]), "
65 "Term(pos2, wdf=1), "
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);
71 db.commit();
72 TEST_EQUAL(docterms_to_string(db, 1),
73 "Term(pos2, wdf=1), "
74 "Term(pos3, wdf=2, pos=[1, 5])");
76 // Regression test: the old positionlist fragment used to be left lying
77 // around here.
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);
84 db.commit();
85 TEST_EQUAL(docterms_to_string(db, 1),
86 "Term(pos2, wdf=1)");
88 // Regression test: the old positionlist fragment used to be left lying
89 // around here.
90 Xapian::PositionIterator posit2(db.positionlist_begin(1, "pos3"));
91 TEST(posit2 == db.positionlist_end(1, "pos3"));
93 doc = db.get_document(1);
94 doc.add_term("pos");
95 db.replace_document(1, doc);
96 db.commit();
97 TEST_EQUAL(docterms_to_string(db, 1),
98 "Term(pos, wdf=1), "
99 "Term(pos2, wdf=1)");
102 static Xapian::Document
103 basic_doc() {
104 Xapian::Document doc;
105 doc.add_term("z0", 0);
106 doc.add_term("z1", 1);
107 return doc;
110 static string
111 basic_docterms() {
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);
127 dbcheck(db, 1, 1);
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);
134 dbcheck(db, 1, 1);
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);
140 dbcheck(db, 1, 1);
141 TEST_EQUAL(docterms_to_string(db, 1), bdt.substr(2));
142 db.replace_document(1, doc1);
143 dbcheck(db, 1, 1);
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);
149 db.commit();
150 db.replace_document(1, doc2);
151 db.replace_document(1, doc0);
152 db.replace_document(1, doc2);
153 db.commit();
154 dbcheck(db, 1, 1);
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);
160 dbcheck(db, 1, 1);
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);
166 dbcheck(db, 1, 1);
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);
173 db.commit();
174 dbcheck(db, 1, 1);
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);
181 db.commit();
182 dbcheck(db, 1, 1);
183 TEST_EQUAL(docterms_to_string(db, 1), "Term(takeaways, wdf=3)" + bdt);
185 // Put it back.
186 db.replace_document(1, doc3);
187 dbcheck(db, 1, 1);
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);
196 dbcheck(db, 1, 1);
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);
202 dbcheck(db, 1, 1);
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);
207 dbcheck(db, 1, 1);
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);
214 dbcheck(db, 1, 1);
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);
221 dbcheck(db, 1, 1);
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);
228 dbcheck(db, 1, 1);
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);
235 dbcheck(db, 1, 1);
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);
242 dbcheck(db, 1, 1);
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);
247 dbcheck(db, 1, 1);
248 TEST_EQUAL(docterms_to_string(db, 1), bdt.substr(2));
250 // Delete the document.
251 db.delete_document(1);
252 dbcheck(db, 0, 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);