Enable testcase closedb5 for remote backends
[xapian.git] / xapian-core / tests / api_closedb.cc
blob8fa482980776403d592dda390dd885c6feb834d2
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) {
226 Xapian::WritableDatabase wdb = get_writable_database();
227 wdb.begin_transaction();
228 wdb.add_document(Xapian::Document());
229 TEST_EQUAL(wdb.get_doccount(), 1);
230 wdb.close();
231 Xapian::Database db = get_writable_database_as_database();
232 TEST_EQUAL(db.get_doccount(), 0);
236 // Same test but for an unflushed transaction.
237 Xapian::WritableDatabase wdb = get_writable_database();
238 wdb.begin_transaction(false);
239 wdb.add_document(Xapian::Document());
240 TEST_EQUAL(wdb.get_doccount(), 1);
241 wdb.close();
242 Xapian::Database db = get_writable_database_as_database();
243 TEST_EQUAL(db.get_doccount(), 0);
245 return true;
248 /// Database::keep_alive() should fail after close() for a remote database.
249 DEFINE_TESTCASE(closedb6, remote) {
250 Xapian::Database db(get_database("etext"));
251 db.close();
253 try {
254 db.keep_alive();
255 return false;
256 } catch (const Xapian::DatabaseError &) {
258 return true;
261 // Test WritableDatabase methods.
262 DEFINE_TESTCASE(closedb7, writable) {
263 Xapian::WritableDatabase db(get_writable_database());
264 db.add_document(Xapian::Document());
265 db.close();
267 // Since we can't make any changes which need to be committed, db.commit()
268 // is a no-op, and so doesn't have to fail. Similarly we may be able to
269 // call db.begin_transaction(), but we can't make any changes inside that
270 // transaction.
271 TEST_EXCEPTION(Xapian::DatabaseError,
272 db.add_document(Xapian::Document()));
273 TEST_EXCEPTION(Xapian::DatabaseError,
274 db.delete_document(1));
275 TEST_EXCEPTION(Xapian::DatabaseError,
276 db.replace_document(1, Xapian::Document()));
277 TEST_EXCEPTION(Xapian::DatabaseError,
278 db.replace_document(2, Xapian::Document()));
279 TEST_EXCEPTION(Xapian::DatabaseError,
280 db.replace_document("Qi", Xapian::Document()));
282 return true;
285 // Test spelling related methods.
286 DEFINE_TESTCASE(closedb8, writable && spelling) {
287 Xapian::WritableDatabase db(get_writable_database());
288 db.add_spelling("pneumatic");
289 db.add_spelling("pneumonia");
290 db.close();
292 TEST_EXCEPTION(Xapian::DatabaseError,
293 db.add_spelling("penmanship"));
294 TEST_EXCEPTION(Xapian::DatabaseError,
295 db.remove_spelling("pneumatic"));
296 TEST_EXCEPTION(Xapian::DatabaseError,
297 db.get_spelling_suggestion("newmonia"));
298 TEST_EXCEPTION(Xapian::DatabaseError,
299 db.spellings_begin());
301 return true;
304 // Test synonym related methods.
305 DEFINE_TESTCASE(closedb9, writable && synonyms) {
306 Xapian::WritableDatabase db(get_writable_database());
307 db.add_synonym("color", "colour");
308 db.add_synonym("honor", "honour");
309 db.close();
311 TEST_EXCEPTION(Xapian::DatabaseError,
312 db.add_synonym("behavior", "behaviour"));
313 TEST_EXCEPTION(Xapian::DatabaseError,
314 db.remove_synonym("honor", "honour"));
315 TEST_EXCEPTION(Xapian::DatabaseError,
316 db.clear_synonyms("honor"));
317 TEST_EXCEPTION(Xapian::DatabaseError,
318 db.synonyms_begin("color"));
319 TEST_EXCEPTION(Xapian::DatabaseError,
320 db.synonym_keys_begin());
322 return true;
325 // Test metadata related methods.
326 DEFINE_TESTCASE(closedb10, writable && metadata) {
327 Xapian::WritableDatabase db(get_writable_database());
328 db.set_metadata("foo", "FOO");
329 db.set_metadata("bar", "BAR");
330 db.close();
332 TEST_EXCEPTION(Xapian::DatabaseError,
333 db.set_metadata("test", "TEST"));
334 TEST_EXCEPTION(Xapian::DatabaseError,
335 db.get_metadata("foo"));
336 TEST_EXCEPTION(Xapian::DatabaseError,
337 db.get_metadata("foo"));
338 TEST_EXCEPTION(Xapian::DatabaseError,
339 db.metadata_keys_begin());
341 return true;