Mercurial > hg > orthanc-databases
comparison MySQL/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 | 1e9bad493475 |
children | 5600949bfb12 |
comparison
equal
deleted
inserted
replaced
22:1e9bad493475 | 23:b2ff1cd2907a |
---|---|
26 | 26 |
27 #include "../../Framework/Common/Integer64Value.h" | 27 #include "../../Framework/Common/Integer64Value.h" |
28 #include "../../Framework/MySQL/MySQLDatabase.h" | 28 #include "../../Framework/MySQL/MySQLDatabase.h" |
29 #include "../../Framework/MySQL/MySQLResult.h" | 29 #include "../../Framework/MySQL/MySQLResult.h" |
30 #include "../../Framework/MySQL/MySQLStatement.h" | 30 #include "../../Framework/MySQL/MySQLStatement.h" |
31 #include "../../Framework/MySQL/MySQLTransaction.h" | |
31 #include "../../Framework/Plugins/IndexUnitTests.h" | 32 #include "../../Framework/Plugins/IndexUnitTests.h" |
32 | 33 |
33 #include <Core/Logging.h> | 34 #include <Core/Logging.h> |
34 | 35 |
35 #include <gtest/gtest.h> | 36 #include <gtest/gtest.h> |
128 transaction.Commit(); | 129 transaction.Commit(); |
129 } | 130 } |
130 } | 131 } |
131 | 132 |
132 | 133 |
134 TEST(MySQL, ImplicitTransaction) | |
135 { | |
136 OrthancDatabases::MySQLDatabase::ClearDatabase(globalParameters_); | |
137 OrthancDatabases::MySQLDatabase db(globalParameters_); | |
138 db.Open(); | |
139 | |
140 { | |
141 OrthancDatabases::MySQLTransaction t(db); | |
142 ASSERT_FALSE(db.DoesTableExist(t, "test")); | |
143 ASSERT_FALSE(db.DoesTableExist(t, "test2")); | |
144 } | |
145 | |
146 { | |
147 std::auto_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(false)); | |
148 ASSERT_FALSE(t->IsImplicit()); | |
149 } | |
150 | |
151 { | |
152 OrthancDatabases::Query query("CREATE TABLE test(id INT)", false); | |
153 std::auto_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query)); | |
154 | |
155 std::auto_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true)); | |
156 ASSERT_TRUE(t->IsImplicit()); | |
157 ASSERT_THROW(t->Commit(), Orthanc::OrthancException); | |
158 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException); | |
159 | |
160 OrthancDatabases::Dictionary args; | |
161 t->ExecuteWithoutResult(*s, args); | |
162 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException); | |
163 t->Commit(); | |
164 | |
165 ASSERT_THROW(t->Commit(), Orthanc::OrthancException); | |
166 } | |
167 | |
168 { | |
169 // An implicit transaction does not need to be explicitely committed | |
170 OrthancDatabases::Query query("CREATE TABLE test2(id INT)", false); | |
171 std::auto_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query)); | |
172 | |
173 std::auto_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true)); | |
174 | |
175 OrthancDatabases::Dictionary args; | |
176 t->ExecuteWithoutResult(*s, args); | |
177 } | |
178 | |
179 { | |
180 OrthancDatabases::MySQLTransaction t(db); | |
181 ASSERT_TRUE(db.DoesTableExist(t, "test")); | |
182 ASSERT_TRUE(db.DoesTableExist(t, "test2")); | |
183 } | |
184 } | |
185 | |
186 | |
133 int main(int argc, char **argv) | 187 int main(int argc, char **argv) |
134 { | 188 { |
135 if (argc < 5) | 189 if (argc < 5) |
136 { | 190 { |
137 std::cerr << "Usage (UNIX): " << argv[0] << " <socket> <username> <password> <database>" | 191 std::cerr << "Usage (UNIX): " << argv[0] << " <socket> <username> <password> <database>" |