2 * @brief tests requiring a database backend supporting transactions
4 /* Copyright (C) 2006,2009,2018 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");
88 TEST_EXCEPTION(Xapian::InvalidOperationError
, db
.begin_transaction());
89 TEST_EQUAL(db
.get_doccount(), docs
+ 1);
90 TEST_EQUAL(db
.get_termfreq("befuddlement"), 1);
91 db
.cancel_transaction();
92 TEST_EQUAL(db
.get_doccount(), docs
);
93 TEST_EQUAL(db
.get_termfreq("befuddlement"), 0);
96 /// Test that begin_transaction() commits any changes pending before the
98 DEFINE_TESTCASE(canceltransaction2
, transactions
) {
99 Xapian::WritableDatabase
db(get_writable_database("apitest_simpledata"));
101 Xapian::doccount docs
= db
.get_doccount();
102 Xapian::Document doc0
;
103 doc0
.set_data("pending");
104 doc0
.add_term("pending_update");
105 Xapian::docid docid
= db
.add_document(doc0
);
107 db
.begin_transaction();
108 TEST_EQUAL(db
.get_doccount(), docs
+ 1);
109 Xapian::Document doc
;
110 doc
.set_data("testing");
111 doc
.add_term("befuddlement");
112 db
.add_document(doc
);
113 TEST_EQUAL(db
.get_doccount(), docs
+ 2);
114 db
.cancel_transaction();
116 TEST_EQUAL(db
.get_doccount(), docs
+ 1);
117 TEST(db
.term_exists("pending_update"));
118 Xapian::Document doc_out
= db
.get_document(docid
);
119 TEST_EQUAL(doc_out
.get_data(), "pending");
122 /// Regression test for glass bug fixed in 1.4.6 and 1.5.0.
123 DEFINE_TESTCASE(canceltransaction3
, transactions
&& path
) {
125 Xapian::WritableDatabase db
= get_named_writable_database("canceltransaction3");
126 db
.begin_transaction();
127 Xapian::Document doc
;
129 db
.add_document(doc
);
130 db
.cancel_transaction();
131 db
.add_document(doc
);
135 size_t check_errors
=
136 Xapian::Database::check(get_named_writable_database_path("canceltransaction3"),
137 Xapian::DBCHECK_SHOW_STATS
, &tout
);
138 TEST_EQUAL(check_errors
, 0);