comparison SQLite/UnitTests/UnitTestsMain.cpp @ 23:b2ff1cd2907a

handling of implicit transactions in DatabaseManager
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 12 Jul 2018 10:44:17 +0200
parents 7cea966b6829
children 714c5d2bee76
comparison
equal deleted inserted replaced
22:1e9bad493475 23:b2ff1cd2907a
17 * You should have received a copy of the GNU Affero General Public License 17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/ 19 **/
20 20
21 21
22 #include "../../Framework/SQLite/SQLiteDatabase.h"
22 #include "../Plugins/SQLiteIndex.h" 23 #include "../Plugins/SQLiteIndex.h"
23 #include "../../Framework/Plugins/IndexUnitTests.h"
24 24
25 #include <Core/Logging.h> 25 #include <Core/Logging.h>
26 #include <Core/SystemToolbox.h> 26 #include <Core/SystemToolbox.h>
27 27
28 #include <gtest/gtest.h> 28 #include <gtest/gtest.h>
29 29
30
31 #include "../../Framework/Plugins/IndexUnitTests.h"
30 32
31 33
32 TEST(SQLiteIndex, Lock) 34 TEST(SQLiteIndex, Lock)
33 { 35 {
34 { 36 {
55 db3.Open(); 57 db3.Open();
56 } 58 }
57 } 59 }
58 60
59 61
62 TEST(SQLite, ImplicitTransaction)
63 {
64 OrthancDatabases::SQLiteDatabase db;
65 db.OpenInMemory();
66
67 ASSERT_FALSE(db.DoesTableExist("test"));
68 ASSERT_FALSE(db.DoesTableExist("test2"));
69
70 {
71 std::auto_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(false));
72 ASSERT_FALSE(t->IsImplicit());
73 }
74
75 {
76 OrthancDatabases::Query query("CREATE TABLE test(id INT)", false);
77 std::auto_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
78
79 std::auto_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
80 ASSERT_TRUE(t->IsImplicit());
81 ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
82 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
83
84 OrthancDatabases::Dictionary args;
85 t->ExecuteWithoutResult(*s, args);
86 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
87 t->Commit();
88
89 ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
90 }
91
92 {
93 // An implicit transaction does not need to be explicitely committed
94 OrthancDatabases::Query query("CREATE TABLE test2(id INT)", false);
95 std::auto_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
96
97 std::auto_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
98
99 OrthancDatabases::Dictionary args;
100 t->ExecuteWithoutResult(*s, args);
101 }
102
103 ASSERT_TRUE(db.DoesTableExist("test"));
104 ASSERT_TRUE(db.DoesTableExist("test2"));
105 }
106
60 107
61 int main(int argc, char **argv) 108 int main(int argc, char **argv)
62 { 109 {
63 ::testing::InitGoogleTest(&argc, argv); 110 ::testing::InitGoogleTest(&argc, argv);
64 Orthanc::Logging::Initialize(); 111 Orthanc::Logging::Initialize();