LCOV - code coverage report
Current view: top level - tests - api_transdb.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core c2b6f1024d3a Lines: 81 81 100.0 %
Date: 2019-05-16 09:13:18 Functions: 5 5 100.0 %
Branches: 147 842 17.5 %

           Branch data     Line data    Source code
       1                 :            : /** @file api_transdb.cc
       2                 :            :  * @brief tests requiring a database backend supporting transactions
       3                 :            :  */
       4                 :            : /* Copyright (C) 2006,2009,2018 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 "api_transdb.h"
      24                 :            : 
      25                 :            : #include <xapian.h>
      26                 :            : 
      27                 :            : #include "apitest.h"
      28                 :            : #include "testutils.h"
      29                 :            : 
      30                 :            : using namespace std;
      31                 :            : 
      32                 :            : /// Test incorrect uses of the transaction API lead to errors.
      33                 :          3 : DEFINE_TESTCASE(badtransaction1, transactions) {
      34 [ +  - ][ +  - ]:          3 :     Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
      35                 :            : 
      36 [ +  - ][ -  + ]:          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.commit_transaction());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ -  + ][ -  +  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
                 [ +  - ]
      37 [ +  - ][ -  +  :          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.cancel_transaction());
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  + ]
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  - ]
      38                 :            : 
      39         [ +  - ]:          3 :     db.begin_transaction();
      40 [ +  - ][ -  +  :          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  + ]
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  - ]
      41         [ +  - ]:          3 :     db.commit_transaction();
      42                 :            : 
      43 [ +  - ][ -  +  :          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.commit_transaction());
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  + ]
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  - ]
      44 [ +  - ][ -  +  :          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.cancel_transaction());
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  + ]
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  - ]
      45                 :            : 
      46         [ +  - ]:          3 :     db.begin_transaction();
      47 [ +  - ][ -  +  :          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  + ]
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  - ]
      48         [ +  - ]:          3 :     db.cancel_transaction();
      49                 :            : 
      50 [ +  - ][ -  +  :          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.commit_transaction());
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  + ]
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  - ]
      51 [ +  - ][ -  +  :          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.cancel_transaction());
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  + ]
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ +  - ]
      52                 :            : 
      53         [ +  - ]:          3 :     db.begin_transaction();
      54         [ +  - ]:          3 :     db.commit_transaction();
      55                 :            : 
      56         [ +  - ]:          3 :     db.begin_transaction();
      57         [ +  - ]:          3 :     db.cancel_transaction();
      58                 :            : 
      59                 :          3 :     return true;
      60                 :            : }
      61                 :            : 
      62                 :            : /// Test committing a simple transaction.
      63                 :          3 : DEFINE_TESTCASE(committransaction1, transactions) {
      64 [ +  - ][ +  - ]:          3 :     Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
      65                 :            : 
      66         [ +  - ]:          3 :     Xapian::doccount docs = db.get_doccount();
      67         [ +  - ]:          3 :     db.begin_transaction();
      68         [ +  - ]:          6 :     Xapian::Document doc;
      69 [ +  - ][ +  - ]:          3 :     doc.set_data("testing");
      70 [ +  - ][ +  - ]:          3 :     doc.add_term("befuddlement");
      71         [ +  - ]:          3 :     db.add_document(doc);
      72 [ +  - ][ -  + ]:          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ -  + ][ -  +  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
                 [ +  - ]
      73 [ +  - ][ -  +  :          3 :     TEST_EQUAL(db.get_doccount(), docs + 1);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      74 [ +  - ][ +  - ]:          3 :     TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      75         [ +  - ]:          3 :     db.commit_transaction();
      76 [ +  - ][ -  +  :          3 :     TEST_EQUAL(db.get_doccount(), docs + 1);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      77 [ +  - ][ +  - ]:          3 :     TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      78                 :            : 
      79                 :          3 :     return true;
      80                 :            : }
      81                 :            : 
      82                 :            : /// Test cancelling a simple transaction.
      83                 :          3 : DEFINE_TESTCASE(canceltransaction1, transactions) {
      84 [ +  - ][ +  - ]:          3 :     Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
      85                 :            : 
      86         [ +  - ]:          3 :     Xapian::doccount docs = db.get_doccount();
      87         [ +  - ]:          3 :     db.begin_transaction();
      88         [ +  - ]:          6 :     Xapian::Document doc;
      89 [ +  - ][ +  - ]:          3 :     doc.set_data("testing");
      90 [ +  - ][ +  - ]:          3 :     doc.add_term("befuddlement");
      91         [ +  - ]:          3 :     db.add_document(doc);
      92 [ +  - ][ -  + ]:          3 :     TEST_EXCEPTION(Xapian::InvalidOperationError, db.begin_transaction());
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ -  + ][ -  +  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
                 [ +  - ]
      93 [ +  - ][ -  +  :          3 :     TEST_EQUAL(db.get_doccount(), docs + 1);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      94 [ +  - ][ +  - ]:          3 :     TEST_EQUAL(db.get_termfreq("befuddlement"), 1);
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      95         [ +  - ]:          3 :     db.cancel_transaction();
      96 [ +  - ][ -  +  :          3 :     TEST_EQUAL(db.get_doccount(), docs);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      97 [ +  - ][ +  - ]:          3 :     TEST_EQUAL(db.get_termfreq("befuddlement"), 0);
           [ -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      98                 :            : 
      99                 :          3 :     return true;
     100                 :            : }
     101                 :            : 
     102                 :            : /// Test that begin_transaction() commits any changes pending before the
     103                 :            : //  transaction.
     104                 :          3 : DEFINE_TESTCASE(canceltransaction2, transactions) {
     105 [ +  - ][ +  - ]:          3 :     Xapian::WritableDatabase db(get_writable_database("apitest_simpledata"));
     106                 :            : 
     107         [ +  - ]:          3 :     Xapian::doccount docs = db.get_doccount();
     108         [ +  - ]:          6 :     Xapian::Document doc0;
     109 [ +  - ][ +  - ]:          3 :     doc0.set_data("pending");
     110 [ +  - ][ +  - ]:          3 :     doc0.add_term("pending_update");
     111         [ +  - ]:          3 :     Xapian::docid docid = db.add_document(doc0);
     112                 :            : 
     113         [ +  - ]:          3 :     db.begin_transaction();
     114 [ +  - ][ -  + ]:          3 :     TEST_EQUAL(db.get_doccount(), docs + 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     115         [ +  - ]:          6 :     Xapian::Document doc;
     116 [ +  - ][ +  - ]:          3 :     doc.set_data("testing");
     117 [ +  - ][ +  - ]:          3 :     doc.add_term("befuddlement");
     118         [ +  - ]:          3 :     db.add_document(doc);
     119 [ +  - ][ -  + ]:          3 :     TEST_EQUAL(db.get_doccount(), docs + 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     120         [ +  - ]:          3 :     db.cancel_transaction();
     121                 :            : 
     122 [ +  - ][ -  + ]:          3 :     TEST_EQUAL(db.get_doccount(), docs + 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     123 [ +  - ][ +  - ]:          3 :     TEST(db.term_exists("pending_update"));
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     124         [ +  - ]:          6 :     Xapian::Document doc_out = db.get_document(docid);
     125 [ +  - ][ +  - ]:          3 :     TEST_EQUAL(doc_out.get_data(), "pending");
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     126                 :            : 
     127                 :          3 :     return true;
     128                 :            : }
     129                 :            : 
     130                 :            : /// Regression test for glass bug fixed in 1.4.6 and 1.5.0.
     131                 :          1 : DEFINE_TESTCASE(canceltransaction3, transactions && path) {
     132                 :            :     {
     133 [ +  - ][ +  - ]:          1 :         Xapian::WritableDatabase db = get_named_writable_database("canceltransaction3");
                 [ +  - ]
     134         [ +  - ]:          1 :         db.begin_transaction();
     135         [ +  - ]:          2 :         Xapian::Document doc;
     136 [ +  - ][ +  - ]:          1 :         doc.add_term("baz");
     137         [ +  - ]:          1 :         db.add_document(doc);
     138         [ +  - ]:          1 :         db.cancel_transaction();
     139         [ +  - ]:          1 :         db.add_document(doc);
     140         [ +  - ]:          2 :         db.commit();
     141                 :            :     }
     142                 :            : 
     143                 :            :     size_t check_errors =
     144                 :            :         Xapian::Database::check(get_named_writable_database_path("canceltransaction3"),
     145 [ +  - ][ +  - ]:          1 :                                 Xapian::DBCHECK_SHOW_STATS, &tout);
                 [ +  - ]
     146 [ -  + ][ #  # ]:          1 :     TEST_EQUAL(check_errors, 0);
     147                 :            : 
     148                 :          1 :     return true;
     149                 :            : }

Generated by: LCOV version 1.11