diff 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
line wrap: on
line diff
--- a/SQLite/UnitTests/UnitTestsMain.cpp	Wed Jul 11 14:39:59 2018 +0200
+++ b/SQLite/UnitTests/UnitTestsMain.cpp	Thu Jul 12 10:44:17 2018 +0200
@@ -19,8 +19,8 @@
  **/
 
 
+#include "../../Framework/SQLite/SQLiteDatabase.h"
 #include "../Plugins/SQLiteIndex.h"
-#include "../../Framework/Plugins/IndexUnitTests.h"
 
 #include <Core/Logging.h>
 #include <Core/SystemToolbox.h>
@@ -28,6 +28,8 @@
 #include <gtest/gtest.h>
 
 
+#include "../../Framework/Plugins/IndexUnitTests.h"
+
 
 TEST(SQLiteIndex, Lock)
 {
@@ -57,6 +59,51 @@
 }
 
 
+TEST(SQLite, ImplicitTransaction)
+{
+  OrthancDatabases::SQLiteDatabase db;
+  db.OpenInMemory();
+
+  ASSERT_FALSE(db.DoesTableExist("test"));
+  ASSERT_FALSE(db.DoesTableExist("test2"));
+
+  {
+    std::auto_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(false));
+    ASSERT_FALSE(t->IsImplicit());
+  }
+
+  {
+    OrthancDatabases::Query query("CREATE TABLE test(id INT)", false);
+    std::auto_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
+    
+    std::auto_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
+    ASSERT_TRUE(t->IsImplicit());
+    ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
+    ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
+
+    OrthancDatabases::Dictionary args;
+    t->ExecuteWithoutResult(*s, args);
+    ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
+    t->Commit();
+
+    ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
+  }
+
+  {
+    // An implicit transaction does not need to be explicitely committed
+    OrthancDatabases::Query query("CREATE TABLE test2(id INT)", false);
+    std::auto_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
+    
+    std::auto_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
+
+    OrthancDatabases::Dictionary args;
+    t->ExecuteWithoutResult(*s, args);
+  }
+
+  ASSERT_TRUE(db.DoesTableExist("test"));
+  ASSERT_TRUE(db.DoesTableExist("test2"));
+}
+
 
 int main(int argc, char **argv)
 {