Eliminate broken PostListCursor for non-const HoneyTable
[xapian.git] / xapian-core / tests / api_closedb.cc
blob4d4c7f06cc99c0c1c763aa37b9a521b090e2d5fc
1 /** @file api_closedb.cc
2 * @brief Tests of closing databases.
3 */
4 /* Copyright 2008,2009 Lemur Consulting Ltd
5 * Copyright 2009,2012,2015 Olly Betts
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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 USA
22 #include <config.h>
24 #include "api_closedb.h"
26 #include <xapian.h>
28 #include "apitest.h"
29 #include "testutils.h"
31 using namespace std;
33 #define COUNT_CLOSEDEXC(CODE) \
34 try { CODE; } catch (const Xapian::DatabaseError &) { ++closedexc_count; }
36 #define IF_NOT_CLOSEDEXC(CODE) \
37 do { \
38 hadexc = false; \
39 try { \
40 CODE; \
41 } catch (const Xapian::DatabaseError &) { \
42 ++closedexc_count; \
43 hadexc = true; \
44 } \
45 } while (false); if (hadexc)
47 // Iterators used by closedb1.
48 struct closedb1_iterators {
49 Xapian::Database db;
50 Xapian::Document doc1;
51 Xapian::PostingIterator pl1;
52 Xapian::PostingIterator pl2;
53 Xapian::PostingIterator plend;
55 void setup(Xapian::Database db_) {
56 db = db_;
58 // Set up the iterators for the test.
59 pl1 = db.postlist_begin("paragraph");
60 pl2 = db.postlist_begin("paragraph");
61 ++pl2;
62 plend = db.postlist_end("paragraph");
65 int perform() {
66 int closedexc_count = 0;
67 bool hadexc;
69 // Getting a document may throw closed.
70 IF_NOT_CLOSEDEXC(doc1 = db.get_document(1)) {
71 COUNT_CLOSEDEXC(TEST_EQUAL(doc1.get_data().substr(0, 33),
72 "This is a test document used with"));
73 COUNT_CLOSEDEXC(doc1.termlist_begin());
76 // Causing the database to access its files raises the "database
77 // closed" error.
78 COUNT_CLOSEDEXC(db.postlist_begin("paragraph"));
79 COUNT_CLOSEDEXC(db.get_document(1).get_value(1));
80 COUNT_CLOSEDEXC(db.termlist_begin(1));
81 COUNT_CLOSEDEXC(db.positionlist_begin(1, "paragraph"));
82 COUNT_CLOSEDEXC(db.allterms_begin());
83 COUNT_CLOSEDEXC(db.allterms_begin("p"));
84 COUNT_CLOSEDEXC(db.get_termfreq("paragraph"));
85 COUNT_CLOSEDEXC(db.get_collection_freq("paragraph"));
86 COUNT_CLOSEDEXC(db.term_exists("paragraph"));
87 COUNT_CLOSEDEXC(db.get_value_freq(1));
88 COUNT_CLOSEDEXC(db.get_value_lower_bound(1));
89 COUNT_CLOSEDEXC(db.get_value_upper_bound(1));
90 COUNT_CLOSEDEXC(db.valuestream_begin(1));
91 COUNT_CLOSEDEXC(db.get_doclength(1));
92 COUNT_CLOSEDEXC(db.get_unique_terms(1));
94 // Reopen raises the "database closed" error.
95 COUNT_CLOSEDEXC(db.reopen());
97 TEST_NOT_EQUAL(pl1, plend);
99 COUNT_CLOSEDEXC(db.postlist_begin("paragraph"));
101 COUNT_CLOSEDEXC(TEST_EQUAL(*pl1, 1));
102 COUNT_CLOSEDEXC(TEST_EQUAL(pl1.get_doclength(), 28));
103 COUNT_CLOSEDEXC(TEST_EQUAL(pl1.get_unique_terms(), 21));
105 // Advancing the iterator may or may not raise an error, but if it
106 // doesn't it must return the correct answers.
107 bool advanced = false;
108 try {
109 ++pl1;
110 advanced = true;
111 } catch (const Xapian::DatabaseError &) {}
113 if (advanced) {
114 COUNT_CLOSEDEXC(TEST_EQUAL(*pl1, 2));
115 COUNT_CLOSEDEXC(TEST_EQUAL(pl1.get_doclength(), 81));
116 COUNT_CLOSEDEXC(TEST_EQUAL(pl1.get_unique_terms(), 56));
119 return closedexc_count;
123 // Test for closing a database
124 DEFINE_TESTCASE(closedb1, backend) {
125 Xapian::Database db(get_database("apitest_simpledata"));
126 closedb1_iterators iters;
128 // Run the test, checking that we get no "closed" exceptions.
129 iters.setup(db);
130 int closedexc_count = iters.perform();
131 TEST_EQUAL(closedexc_count, 0);
133 // Setup for the next test.
134 iters.setup(db);
136 // Close the database.
137 db.close();
139 // Reopening a closed database should always raise DatabaseError.
140 TEST_EXCEPTION(Xapian::DatabaseError, db.reopen());
142 // Run the test again, checking that we get some "closed" exceptions.
143 closedexc_count = iters.perform();
144 TEST_NOT_EQUAL(closedexc_count, 0);
146 // get_description() shouldn't throw an exception. Actually do something
147 // with the description, in case this method is marked as "pure" in the
148 // future.
149 TEST(!db.get_description().empty());
151 // Calling close repeatedly is okay.
152 db.close();
154 return true;
157 // Test closing a writable database, and that it drops the lock.
158 DEFINE_TESTCASE(closedb2, writable && !inmemory && !remote) {
159 Xapian::WritableDatabase dbw1(get_named_writable_database("apitest_closedb2"));
160 TEST_EXCEPTION(Xapian::DatabaseLockError,
161 Xapian::WritableDatabase db(get_named_writable_database_path("apitest_closedb2"),
162 Xapian::DB_OPEN));
163 dbw1.close();
164 Xapian::WritableDatabase dbw2 = get_named_writable_database("apitest_closedb2");
165 TEST_EXCEPTION(Xapian::DatabaseError, dbw1.postlist_begin("paragraph"));
166 TEST_EQUAL(dbw2.postlist_begin("paragraph"), dbw2.postlist_end("paragraph"));
168 return true;
171 /// Check API methods which might either work or throw an exception.
172 DEFINE_TESTCASE(closedb3, backend) {
173 Xapian::Database db(get_database("etext"));
174 const string & uuid = db.get_uuid();
175 db.close();
176 try {
177 TEST_EQUAL(db.get_uuid(), uuid);
178 } catch (const Xapian::DatabaseError &) {
180 try {
181 TEST(db.has_positions());
182 } catch (const Xapian::DatabaseError &) {
184 try {
185 TEST_EQUAL(db.get_doccount(), 566);
186 } catch (const Xapian::DatabaseError &) {
188 try {
189 TEST_EQUAL(db.get_lastdocid(), 566);
190 } catch (const Xapian::DatabaseError &) {
192 try {
193 TEST_REL(db.get_doclength_lower_bound(), <, db.get_avlength());
194 } catch (const Xapian::DatabaseError &) {
196 try {
197 TEST_REL(db.get_doclength_upper_bound(), >, db.get_avlength());
198 } catch (const Xapian::DatabaseError &) {
200 try {
201 TEST(db.get_wdf_upper_bound("king"));
202 } catch (const Xapian::DatabaseError &) {
204 try {
205 // For non-remote databases, keep_alive() is a no-op anyway.
206 db.keep_alive();
207 } catch (const Xapian::DatabaseError &) {
209 return true;
212 /// Regression test for bug fixed in 1.1.4 - close() should implicitly commit().
213 DEFINE_TESTCASE(closedb4, writable && !inmemory) {
214 Xapian::WritableDatabase wdb(get_writable_database());
215 wdb.add_document(Xapian::Document());
216 TEST_EQUAL(wdb.get_doccount(), 1);
217 wdb.close();
218 Xapian::Database db(get_writable_database_as_database());
219 TEST_EQUAL(db.get_doccount(), 1);
220 return true;
223 /// If a transaction is active, close() shouldn't implicitly commit().
224 DEFINE_TESTCASE(closedb5, transactions && !remote) {
225 // FIXME: Fails with the remote backend, but I suspect it may be a test
226 // harness issue.
228 Xapian::WritableDatabase wdb = get_writable_database();
229 wdb.begin_transaction();
230 wdb.add_document(Xapian::Document());
231 TEST_EQUAL(wdb.get_doccount(), 1);
232 wdb.close();
233 Xapian::Database db = get_writable_database_as_database();
234 TEST_EQUAL(db.get_doccount(), 0);
238 // Same test but for an unflushed transaction.
239 Xapian::WritableDatabase wdb = get_writable_database();
240 wdb.begin_transaction(false);
241 wdb.add_document(Xapian::Document());
242 TEST_EQUAL(wdb.get_doccount(), 1);
243 wdb.close();
244 Xapian::Database db = get_writable_database_as_database();
245 TEST_EQUAL(db.get_doccount(), 0);
247 return true;
250 /// Database::keep_alive() should fail after close() for a remote database.
251 DEFINE_TESTCASE(closedb6, remote) {
252 Xapian::Database db(get_database("etext"));
253 db.close();
255 try {
256 db.keep_alive();
257 return false;
258 } catch (const Xapian::DatabaseError &) {
260 return true;
263 // Test WritableDatabase methods.
264 DEFINE_TESTCASE(closedb7, writable) {
265 Xapian::WritableDatabase db(get_writable_database());
266 db.add_document(Xapian::Document());
267 db.close();
269 // Since we can't make any changes which need to be committed, db.commit()
270 // is a no-op, and so doesn't have to fail. Similarly we may be able to
271 // call db.begin_transaction(), but we can't make any changes inside that
272 // transaction.
273 TEST_EXCEPTION(Xapian::DatabaseError,
274 db.add_document(Xapian::Document()));
275 TEST_EXCEPTION(Xapian::DatabaseError,
276 db.delete_document(1));
277 TEST_EXCEPTION(Xapian::DatabaseError,
278 db.replace_document(1, Xapian::Document()));
279 TEST_EXCEPTION(Xapian::DatabaseError,
280 db.replace_document(2, Xapian::Document()));
281 TEST_EXCEPTION(Xapian::DatabaseError,
282 db.replace_document("Qi", Xapian::Document()));
284 return true;
287 // Test spelling related methods.
288 DEFINE_TESTCASE(closedb8, writable && spelling) {
289 Xapian::WritableDatabase db(get_writable_database());
290 db.add_spelling("pneumatic");
291 db.add_spelling("pneumonia");
292 db.close();
294 TEST_EXCEPTION(Xapian::DatabaseError,
295 db.add_spelling("penmanship"));
296 TEST_EXCEPTION(Xapian::DatabaseError,
297 db.remove_spelling("pneumatic"));
298 TEST_EXCEPTION(Xapian::DatabaseError,
299 db.get_spelling_suggestion("newmonia"));
300 TEST_EXCEPTION(Xapian::DatabaseError,
301 db.spellings_begin());
303 return true;
306 // Test synonym related methods.
307 DEFINE_TESTCASE(closedb9, writable && synonyms) {
308 Xapian::WritableDatabase db(get_writable_database());
309 db.add_synonym("color", "colour");
310 db.add_synonym("honor", "honour");
311 db.close();
313 TEST_EXCEPTION(Xapian::DatabaseError,
314 db.add_synonym("behavior", "behaviour"));
315 TEST_EXCEPTION(Xapian::DatabaseError,
316 db.remove_synonym("honor", "honour"));
317 TEST_EXCEPTION(Xapian::DatabaseError,
318 db.clear_synonyms("honor"));
319 TEST_EXCEPTION(Xapian::DatabaseError,
320 db.synonyms_begin("color"));
321 TEST_EXCEPTION(Xapian::DatabaseError,
322 db.synonym_keys_begin());
324 return true;
327 // Test metadata related methods.
328 DEFINE_TESTCASE(closedb10, writable && metadata) {
329 Xapian::WritableDatabase db(get_writable_database());
330 db.set_metadata("foo", "FOO");
331 db.set_metadata("bar", "BAR");
332 db.close();
334 TEST_EXCEPTION(Xapian::DatabaseError,
335 db.set_metadata("test", "TEST"));
336 TEST_EXCEPTION(Xapian::DatabaseError,
337 db.get_metadata("foo"));
338 TEST_EXCEPTION(Xapian::DatabaseError,
339 db.get_metadata("foo"));
340 TEST_EXCEPTION(Xapian::DatabaseError,
341 db.metadata_keys_begin());
343 return true;