tests/api_transdb.cc

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2006 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <xapian.h>
00024 
00025 #include "apitest.h"
00026 #include "testutils.h"
00027 
00028 using namespace std;
00029 
00031 DEFINE_TESTCASE(badtransaction1, transactions) {
00032     Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
00033 
00034     TEST_EXCEPTION(Xapian::InvalidOperationError, db.commit_transaction());
00035     TEST_EXCEPTION(Xapian::InvalidOperationError, db.cancel_transaction());
00036 
00037     db.begin_transaction();
00038     TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
00039     db.commit_transaction();
00040 
00041     TEST_EXCEPTION(Xapian::InvalidOperationError, db.commit_transaction());
00042     TEST_EXCEPTION(Xapian::InvalidOperationError, db.cancel_transaction());
00043 
00044     db.begin_transaction();
00045     TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
00046     db.cancel_transaction();
00047 
00048     TEST_EXCEPTION(Xapian::InvalidOperationError, db.commit_transaction());
00049     TEST_EXCEPTION(Xapian::InvalidOperationError, db.cancel_transaction());
00050 
00051     db.begin_transaction();
00052     db.commit_transaction();
00053 
00054     db.begin_transaction();
00055     db.cancel_transaction();
00056 
00057     return true;
00058 }
00059 
00061 DEFINE_TESTCASE(committransaction1, transactions) {
00062     Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
00063 
00064     Xapian::doccount docs = db.get_doccount();
00065     db.begin_transaction();
00066     Xapian::Document doc;
00067     doc.set_data("testing");
00068     doc.add_term("befuddlement");
00069     db.add_document(doc);
00070     TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
00071     TEST_EQUAL(db.get_doccount(), docs + 1);
00072     TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
00073     db.commit_transaction();
00074     TEST_EQUAL(db.get_doccount(), docs + 1);
00075     TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
00076 
00077     return true;
00078 }
00079 
00081 DEFINE_TESTCASE(canceltransaction1, transactions) {
00082     Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
00083 
00084     Xapian::doccount docs = db.get_doccount();
00085     db.begin_transaction();
00086     Xapian::Document doc;
00087     doc.set_data("testing");
00088     doc.add_term("befuddlement");
00089     db.add_document(doc);
00090     TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
00091     TEST_EQUAL(db.get_doccount(), docs + 1);
00092     TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
00093     db.cancel_transaction();
00094     TEST_EQUAL(db.get_doccount(), docs);
00095     TEST_EQUAL(db.get_termfreq("befuddlement"), 0);
00096 
00097     return true;
00098 }
00099 
00101 //  transaction.
00102 DEFINE_TESTCASE(canceltransaction2, transactions) {
00103     Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
00104 
00105     Xapian::doccount docs = db.get_doccount();
00106     Xapian::Document doc0;
00107     doc0.set_data("pending");
00108     doc0.add_term("pending_update");
00109     Xapian::docid docid = db.add_document(doc0);
00110 
00111     db.begin_transaction();
00112     TEST_EQUAL(db.get_doccount(), docs + 1);
00113     Xapian::Document doc;
00114     doc.set_data("testing");
00115     doc.add_term("befuddlement");
00116     db.add_document(doc);
00117     TEST_EQUAL(db.get_doccount(), docs + 2);
00118     db.cancel_transaction();
00119 
00120     TEST_EQUAL(db.get_doccount(), docs + 1);
00121     TEST(db.term_exists("pending_update"));
00122     Xapian::Document doc_out = db.get_document(docid);
00123     TEST_EQUAL(doc_out.get_data(), "pending");
00124 
00125     return true;
00126 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.