Mercurial > hg > orthanc-databases
comparison PostgreSQL/UnitTests/PostgreSQLTests.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 | 9774802fd05f |
children | 714c5d2bee76 |
comparison
equal
deleted
inserted
replaced
22:1e9bad493475 | 23:b2ff1cd2907a |
---|---|
43 | 43 |
44 #include <boost/lexical_cast.hpp> | 44 #include <boost/lexical_cast.hpp> |
45 | 45 |
46 using namespace OrthancDatabases; | 46 using namespace OrthancDatabases; |
47 | 47 |
48 extern OrthancDatabases::PostgreSQLParameters globalParameters_; | 48 extern PostgreSQLParameters globalParameters_; |
49 | 49 |
50 | 50 |
51 static OrthancDatabases::PostgreSQLDatabase* CreateTestDatabase(bool clearAll) | 51 static PostgreSQLDatabase* CreateTestDatabase() |
52 { | 52 { |
53 std::auto_ptr<OrthancDatabases::PostgreSQLDatabase> pg | 53 std::auto_ptr<PostgreSQLDatabase> pg |
54 (new OrthancDatabases::PostgreSQLDatabase(globalParameters_)); | 54 (new PostgreSQLDatabase(globalParameters_)); |
55 | 55 |
56 pg->Open(); | 56 pg->Open(); |
57 | 57 pg->ClearAll(); |
58 if (clearAll) | |
59 { | |
60 pg->ClearAll(); | |
61 } | |
62 | 58 |
63 return pg.release(); | 59 return pg.release(); |
64 } | 60 } |
65 | 61 |
66 | 62 |
73 } | 69 } |
74 | 70 |
75 | 71 |
76 TEST(PostgreSQL, Basic) | 72 TEST(PostgreSQL, Basic) |
77 { | 73 { |
78 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase(true)); | 74 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
79 | 75 |
80 ASSERT_FALSE(pg->DoesTableExist("Test")); | 76 ASSERT_FALSE(pg->DoesTableExist("Test")); |
81 pg->Execute("CREATE TABLE Test(name INTEGER, value BIGINT)"); | 77 pg->Execute("CREATE TABLE Test(name INTEGER, value BIGINT)"); |
82 ASSERT_TRUE(pg->DoesTableExist("Test")); | 78 ASSERT_TRUE(pg->DoesTableExist("Test")); |
83 | 79 |
146 } | 142 } |
147 | 143 |
148 | 144 |
149 TEST(PostgreSQL, String) | 145 TEST(PostgreSQL, String) |
150 { | 146 { |
151 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase(true)); | 147 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
152 | 148 |
153 pg->Execute("CREATE TABLE Test(name INTEGER, value VARCHAR(40))"); | 149 pg->Execute("CREATE TABLE Test(name INTEGER, value VARCHAR(40))"); |
154 | 150 |
155 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | 151 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); |
156 s.DeclareInputInteger(0); | 152 s.DeclareInputInteger(0); |
192 } | 188 } |
193 | 189 |
194 | 190 |
195 TEST(PostgreSQL, Transaction) | 191 TEST(PostgreSQL, Transaction) |
196 { | 192 { |
197 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase(true)); | 193 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
198 | 194 |
199 pg->Execute("CREATE TABLE Test(name INTEGER, value INTEGER)"); | 195 pg->Execute("CREATE TABLE Test(name INTEGER, value INTEGER)"); |
200 | 196 |
201 { | 197 { |
202 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | 198 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); |
260 | 256 |
261 | 257 |
262 | 258 |
263 TEST(PostgreSQL, LargeObject) | 259 TEST(PostgreSQL, LargeObject) |
264 { | 260 { |
265 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase(true)); | 261 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
266 ASSERT_EQ(0, CountLargeObjects(*pg)); | 262 ASSERT_EQ(0, CountLargeObjects(*pg)); |
267 | 263 |
268 pg->Execute("CREATE TABLE Test(name VARCHAR, value OID)"); | 264 pg->Execute("CREATE TABLE Test(name VARCHAR, value OID)"); |
269 | 265 |
270 // Automatically remove the large objects associated with the table | 266 // Automatically remove the large objects associated with the table |
338 } | 334 } |
339 | 335 |
340 | 336 |
341 TEST(PostgreSQL, StorageArea) | 337 TEST(PostgreSQL, StorageArea) |
342 { | 338 { |
343 OrthancDatabases::PostgreSQLStorageArea storageArea(globalParameters_); | 339 PostgreSQLStorageArea storageArea(globalParameters_); |
344 storageArea.SetClearAll(true); | 340 storageArea.SetClearAll(true); |
345 | 341 |
346 { | 342 { |
347 OrthancDatabases::DatabaseManager::Transaction transaction(storageArea.GetManager()); | 343 DatabaseManager::Transaction transaction(storageArea.GetManager()); |
348 OrthancDatabases::PostgreSQLDatabase& db = | 344 PostgreSQLDatabase& db = |
349 dynamic_cast<OrthancDatabases::PostgreSQLDatabase&>(transaction.GetDatabase()); | 345 dynamic_cast<PostgreSQLDatabase&>(transaction.GetDatabase()); |
350 | 346 |
351 ASSERT_EQ(0, CountLargeObjects(db)); | 347 ASSERT_EQ(0, CountLargeObjects(db)); |
352 | 348 |
353 for (int i = 0; i < 10; i++) | 349 for (int i = 0; i < 10; i++) |
354 { | 350 { |
393 ASSERT_EQ(0, CountLargeObjects(db)); | 389 ASSERT_EQ(0, CountLargeObjects(db)); |
394 | 390 |
395 transaction.Commit(); | 391 transaction.Commit(); |
396 } | 392 } |
397 } | 393 } |
394 | |
395 | |
396 TEST(PostgreSQL, ImplicitTransaction) | |
397 { | |
398 std::auto_ptr<PostgreSQLDatabase> db(CreateTestDatabase()); | |
399 | |
400 ASSERT_FALSE(db->DoesTableExist("test")); | |
401 ASSERT_FALSE(db->DoesTableExist("test2")); | |
402 | |
403 { | |
404 std::auto_ptr<OrthancDatabases::ITransaction> t(db->CreateTransaction(false)); | |
405 ASSERT_FALSE(t->IsImplicit()); | |
406 } | |
407 | |
408 { | |
409 Query query("CREATE TABLE test(id INT)", false); | |
410 std::auto_ptr<IPrecompiledStatement> s(db->Compile(query)); | |
411 | |
412 std::auto_ptr<ITransaction> t(db->CreateTransaction(true)); | |
413 ASSERT_TRUE(t->IsImplicit()); | |
414 ASSERT_THROW(t->Commit(), Orthanc::OrthancException); | |
415 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException); | |
416 | |
417 Dictionary args; | |
418 t->ExecuteWithoutResult(*s, args); | |
419 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException); | |
420 t->Commit(); | |
421 | |
422 ASSERT_THROW(t->Commit(), Orthanc::OrthancException); | |
423 } | |
424 | |
425 { | |
426 // An implicit transaction does not need to be explicitely committed | |
427 Query query("CREATE TABLE test2(id INT)", false); | |
428 std::auto_ptr<IPrecompiledStatement> s(db->Compile(query)); | |
429 | |
430 std::auto_ptr<ITransaction> t(db->CreateTransaction(true)); | |
431 | |
432 Dictionary args; | |
433 t->ExecuteWithoutResult(*s, args); | |
434 } | |
435 | |
436 ASSERT_TRUE(db->DoesTableExist("test")); | |
437 ASSERT_TRUE(db->DoesTableExist("test2")); | |
438 } | |
439 |