2 * @brief tests requiring a database backend supporting transactions
4 /* Copyright (C) 2006,2009,2018,2023 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "api_transdb.h"
28 #include "testutils.h"
32 /// Test incorrect uses of the transaction API lead to errors.
33 DEFINE_TESTCASE(badtransaction1
, transactions
) {
34 Xapian::WritableDatabase
db(get_writable_database("apitest_simpledata"));
36 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.commit_transaction());
37 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.cancel_transaction());
39 db
.begin_transaction();
40 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.begin_transaction());
41 db
.commit_transaction();
43 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.commit_transaction());
44 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.cancel_transaction());
46 db
.begin_transaction();
47 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.begin_transaction());
48 db
.cancel_transaction();
50 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.commit_transaction());
51 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.cancel_transaction());
53 db
.begin_transaction();
54 db
.commit_transaction();
56 db
.begin_transaction();
57 db
.cancel_transaction();
60 /// Test committing a simple transaction.
61 DEFINE_TESTCASE(committransaction1
, transactions
) {
62 Xapian::WritableDatabase
db(get_writable_database("apitest_simpledata"));
64 Xapian::doccount docs
= db
.get_doccount();
65 db
.begin_transaction();
67 doc
.set_data("testing");
68 doc
.add_term("befuddlement");
70 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.begin_transaction());
71 TEST_EQUAL(db
.get_doccount(), docs
+ 1);
72 TEST_EQUAL(db
.get_termfreq("befuddlement"), 1);
73 db
.commit_transaction();
74 TEST_EQUAL(db
.get_doccount(), docs
+ 1);
75 TEST_EQUAL(db
.get_termfreq("befuddlement"), 1);
78 /// Test cancelling a simple transaction.
79 DEFINE_TESTCASE(canceltransaction1
, transactions
) {
80 Xapian::WritableDatabase
db(get_writable_database("apitest_simpledata"));
82 Xapian::doccount docs
= db
.get_doccount();
83 db
.begin_transaction();
85 doc
.set_data("testing");
86 doc
.add_term("befuddlement");
87 doc
.add_value(42, "answer");
89 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.begin_transaction());
90 TEST_EQUAL(db
.get_doccount(), docs
+ 1);
91 TEST_EQUAL(db
.get_termfreq("befuddlement"), 1);
92 TEST_EQUAL(db
.get_value_freq(42), 1);
93 TEST_EQUAL(db
.get_value_lower_bound(42), "answer");
94 TEST_EQUAL(db
.get_value_upper_bound(42), "answer");
95 db
.cancel_transaction();
96 TEST_EQUAL(db
.get_doccount(), docs
);
97 TEST_EQUAL(db
.get_termfreq("befuddlement"), 0);
98 TEST_EQUAL(db
.get_value_freq(42), 0);
99 TEST_EQUAL(db
.get_value_lower_bound(42), "");
100 TEST_EQUAL(db
.get_value_upper_bound(42), "");
103 /// Test that begin_transaction() commits any changes pending before the
105 DEFINE_TESTCASE(canceltransaction2
, transactions
) {
106 Xapian::WritableDatabase
db(get_writable_database("apitest_simpledata"));
108 Xapian::doccount docs
= db
.get_doccount();
109 Xapian::Document doc0
;
110 doc0
.set_data("pending");
111 doc0
.add_term("pending_update");
112 Xapian::docid docid
= db
.add_document(doc0
);
114 db
.begin_transaction();
115 TEST_EQUAL(db
.get_doccount(), docs
+ 1);
116 Xapian::Document doc
;
117 doc
.set_data("testing");
118 doc
.add_term("befuddlement");
119 db
.add_document(doc
);
120 TEST_EQUAL(db
.get_doccount(), docs
+ 2);
121 db
.cancel_transaction();
123 TEST_EQUAL(db
.get_doccount(), docs
+ 1);
124 TEST(db
.term_exists("pending_update"));
125 Xapian::Document doc_out
= db
.get_document(docid
);
126 TEST_EQUAL(doc_out
.get_data(), "pending");
129 /// Regression test for glass bug fixed in 1.4.6 and 1.5.0.
130 DEFINE_TESTCASE(canceltransaction3
, transactions
&& path
) {
132 Xapian::WritableDatabase db
= get_named_writable_database("canceltransaction3");
133 db
.begin_transaction();
134 Xapian::Document doc
;
136 db
.add_document(doc
);
137 db
.cancel_transaction();
138 db
.add_document(doc
);
142 size_t check_errors
=
143 Xapian::Database::check(get_named_writable_database_path("canceltransaction3"),
144 Xapian::DBCHECK_SHOW_STATS
, &tout
);
145 TEST_EQUAL(check_errors
, 0);