root / tags / 1.0.8 / xapian-core / tests / api_transdb.cc

Revision 9734, 4.2 kB (checked in by olly, 14 months ago)

tests/: Collate the transaction tests.

  • Property svn:eol-style set to native
Line 
1/** @file api_transdb.cc
2 * @brief tests requiring a database backend supporting transactions
3 */
4/* Copyright (C) 2006 Olly Betts
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <config.h>
22
23#include <xapian.h>
24
25#include "apitest.h"
26#include "testutils.h"
27
28using namespace std;
29
30/// Test incorrect uses of the transaction API lead to errors.
31DEFINE_TESTCASE(badtransaction1, transactions) {
32    Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
33
34    TEST_EXCEPTION(Xapian::InvalidOperationError, db.commit_transaction());
35    TEST_EXCEPTION(Xapian::InvalidOperationError, db.cancel_transaction());
36
37    db.begin_transaction();
38    TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
39    db.commit_transaction();
40
41    TEST_EXCEPTION(Xapian::InvalidOperationError, db.commit_transaction());
42    TEST_EXCEPTION(Xapian::InvalidOperationError, db.cancel_transaction());
43
44    db.begin_transaction();
45    TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
46    db.cancel_transaction();
47
48    TEST_EXCEPTION(Xapian::InvalidOperationError, db.commit_transaction());
49    TEST_EXCEPTION(Xapian::InvalidOperationError, db.cancel_transaction());
50
51    db.begin_transaction();
52    db.commit_transaction();
53
54    db.begin_transaction();
55    db.cancel_transaction();
56
57    return true;
58}
59
60/// Test committing a simple transaction.
61DEFINE_TESTCASE(committransaction1, transactions) {
62    Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
63
64    Xapian::doccount docs = db.get_doccount();
65    db.begin_transaction();
66    Xapian::Document doc;
67    doc.set_data("testing");
68    doc.add_term("befuddlement");
69    db.add_document(doc);
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);
76
77    return true;
78}
79
80/// Test cancelling a simple transaction.
81DEFINE_TESTCASE(canceltransaction1, transactions) {
82    Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
83
84    Xapian::doccount docs = db.get_doccount();
85    db.begin_transaction();
86    Xapian::Document doc;
87    doc.set_data("testing");
88    doc.add_term("befuddlement");
89    db.add_document(doc);
90    TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
91    TEST_EQUAL(db.get_doccount(), docs + 1);
92    TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
93    db.cancel_transaction();
94    TEST_EQUAL(db.get_doccount(), docs);
95    TEST_EQUAL(db.get_termfreq("befuddlement"), 0);
96
97    return true;
98}
99
100/// Test that begin_transaction() flushes any changes pending before the
101//  transaction.
102DEFINE_TESTCASE(canceltransaction2, transactions) {
103    Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
104
105    Xapian::doccount docs = db.get_doccount();
106    Xapian::Document doc0;
107    doc0.set_data("pending");
108    doc0.add_term("pending_update");
109    Xapian::docid docid = db.add_document(doc0);
110
111    db.begin_transaction();
112    TEST_EQUAL(db.get_doccount(), docs + 1);
113    Xapian::Document doc;
114    doc.set_data("testing");
115    doc.add_term("befuddlement");
116    db.add_document(doc);
117    TEST_EQUAL(db.get_doccount(), docs + 2);
118    db.cancel_transaction();
119
120    TEST_EQUAL(db.get_doccount(), docs + 1);
121    TEST(db.term_exists("pending_update"));
122    Xapian::Document doc_out = db.get_document(docid);
123    TEST_EQUAL(doc_out.get_data(), "pending");
124
125    return true;
126}
Note: See TracBrowser for help on using the browser.