comparison PostgreSQL/UnitTests/PostgreSQLTests.cpp @ 157:275e14f57f1e

replacing deprecated std::auto_ptr by std::unique_ptr
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 06 Jul 2020 12:45:58 +0200
parents 063aa53b5917
children 3236894320d6
comparison
equal deleted inserted replaced
156:710537acb488 157:275e14f57f1e
39 #include "../../Framework/PostgreSQL/PostgreSQLResult.h" 39 #include "../../Framework/PostgreSQL/PostgreSQLResult.h"
40 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h" 40 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h"
41 #include "../Plugins/PostgreSQLIndex.h" 41 #include "../Plugins/PostgreSQLIndex.h"
42 #include "../Plugins/PostgreSQLStorageArea.h" 42 #include "../Plugins/PostgreSQLStorageArea.h"
43 43
44 #include <Compatibility.h> // For std::unique_ptr<>
44 #include <OrthancException.h> 45 #include <OrthancException.h>
45 46
46 #include <boost/lexical_cast.hpp> 47 #include <boost/lexical_cast.hpp>
47 48
48 using namespace OrthancDatabases; 49 using namespace OrthancDatabases;
50 extern PostgreSQLParameters globalParameters_; 51 extern PostgreSQLParameters globalParameters_;
51 52
52 53
53 static PostgreSQLDatabase* CreateTestDatabase() 54 static PostgreSQLDatabase* CreateTestDatabase()
54 { 55 {
55 std::auto_ptr<PostgreSQLDatabase> pg 56 std::unique_ptr<PostgreSQLDatabase> pg
56 (new PostgreSQLDatabase(globalParameters_)); 57 (new PostgreSQLDatabase(globalParameters_));
57 58
58 pg->Open(); 59 pg->Open();
59 pg->ClearAll(); 60 pg->ClearAll();
60 61
71 } 72 }
72 73
73 74
74 TEST(PostgreSQL, Basic) 75 TEST(PostgreSQL, Basic)
75 { 76 {
76 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); 77 std::unique_ptr<PostgreSQLDatabase> pg(CreateTestDatabase());
77 78
78 ASSERT_FALSE(pg->DoesTableExist("Test")); 79 ASSERT_FALSE(pg->DoesTableExist("Test"));
79 pg->Execute("CREATE TABLE Test(name INTEGER, value BIGINT)"); 80 pg->Execute("CREATE TABLE Test(name INTEGER, value BIGINT)");
80 ASSERT_TRUE(pg->DoesTableExist("Test")); 81 ASSERT_TRUE(pg->DoesTableExist("Test"));
81 82
144 } 145 }
145 146
146 147
147 TEST(PostgreSQL, String) 148 TEST(PostgreSQL, String)
148 { 149 {
149 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); 150 std::unique_ptr<PostgreSQLDatabase> pg(CreateTestDatabase());
150 151
151 pg->Execute("CREATE TABLE Test(name INTEGER, value VARCHAR(40))"); 152 pg->Execute("CREATE TABLE Test(name INTEGER, value VARCHAR(40))");
152 153
153 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); 154 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false);
154 s.DeclareInputInteger(0); 155 s.DeclareInputInteger(0);
190 } 191 }
191 192
192 193
193 TEST(PostgreSQL, Transaction) 194 TEST(PostgreSQL, Transaction)
194 { 195 {
195 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); 196 std::unique_ptr<PostgreSQLDatabase> pg(CreateTestDatabase());
196 197
197 pg->Execute("CREATE TABLE Test(name INTEGER, value INTEGER)"); 198 pg->Execute("CREATE TABLE Test(name INTEGER, value INTEGER)");
198 199
199 { 200 {
200 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); 201 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false);
258 259
259 260
260 261
261 TEST(PostgreSQL, LargeObject) 262 TEST(PostgreSQL, LargeObject)
262 { 263 {
263 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); 264 std::unique_ptr<PostgreSQLDatabase> pg(CreateTestDatabase());
264 ASSERT_EQ(0, CountLargeObjects(*pg)); 265 ASSERT_EQ(0, CountLargeObjects(*pg));
265 266
266 pg->Execute("CREATE TABLE Test(name VARCHAR, value OID)"); 267 pg->Execute("CREATE TABLE Test(name VARCHAR, value OID)");
267 268
268 // Automatically remove the large objects associated with the table 269 // Automatically remove the large objects associated with the table
395 } 396 }
396 397
397 398
398 TEST(PostgreSQL, ImplicitTransaction) 399 TEST(PostgreSQL, ImplicitTransaction)
399 { 400 {
400 std::auto_ptr<PostgreSQLDatabase> db(CreateTestDatabase()); 401 std::unique_ptr<PostgreSQLDatabase> db(CreateTestDatabase());
401 402
402 ASSERT_FALSE(db->DoesTableExist("test")); 403 ASSERT_FALSE(db->DoesTableExist("test"));
403 ASSERT_FALSE(db->DoesTableExist("test2")); 404 ASSERT_FALSE(db->DoesTableExist("test2"));
404 405
405 { 406 {
406 std::auto_ptr<OrthancDatabases::ITransaction> t(db->CreateTransaction(false)); 407 std::unique_ptr<OrthancDatabases::ITransaction> t(db->CreateTransaction(false));
407 ASSERT_FALSE(t->IsImplicit()); 408 ASSERT_FALSE(t->IsImplicit());
408 } 409 }
409 410
410 { 411 {
411 Query query("CREATE TABLE test(id INT)", false); 412 Query query("CREATE TABLE test(id INT)", false);
412 std::auto_ptr<IPrecompiledStatement> s(db->Compile(query)); 413 std::unique_ptr<IPrecompiledStatement> s(db->Compile(query));
413 414
414 std::auto_ptr<ITransaction> t(db->CreateTransaction(true)); 415 std::unique_ptr<ITransaction> t(db->CreateTransaction(true));
415 ASSERT_TRUE(t->IsImplicit()); 416 ASSERT_TRUE(t->IsImplicit());
416 ASSERT_THROW(t->Commit(), Orthanc::OrthancException); 417 ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
417 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException); 418 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
418 419
419 Dictionary args; 420 Dictionary args;
425 } 426 }
426 427
427 { 428 {
428 // An implicit transaction does not need to be explicitely committed 429 // An implicit transaction does not need to be explicitely committed
429 Query query("CREATE TABLE test2(id INT)", false); 430 Query query("CREATE TABLE test2(id INT)", false);
430 std::auto_ptr<IPrecompiledStatement> s(db->Compile(query)); 431 std::unique_ptr<IPrecompiledStatement> s(db->Compile(query));
431 432
432 std::auto_ptr<ITransaction> t(db->CreateTransaction(true)); 433 std::unique_ptr<ITransaction> t(db->CreateTransaction(true));
433 434
434 Dictionary args; 435 Dictionary args;
435 t->ExecuteWithoutResult(*s, args); 436 t->ExecuteWithoutResult(*s, args);
436 } 437 }
437 438
516 #endif 517 #endif
517 518
518 519
519 TEST(PostgreSQL, Lock2) 520 TEST(PostgreSQL, Lock2)
520 { 521 {
521 std::auto_ptr<PostgreSQLDatabase> db1(CreateTestDatabase()); 522 std::unique_ptr<PostgreSQLDatabase> db1(CreateTestDatabase());
522 db1->Open(); 523 db1->Open();
523 524
524 ASSERT_FALSE(db1->ReleaseAdvisoryLock(43)); // lock counter = 0 525 ASSERT_FALSE(db1->ReleaseAdvisoryLock(43)); // lock counter = 0
525 ASSERT_TRUE(db1->AcquireAdvisoryLock(43)); // lock counter = 1 526 ASSERT_TRUE(db1->AcquireAdvisoryLock(43)); // lock counter = 1
526 527
532 ASSERT_TRUE(db1->ReleaseAdvisoryLock(43)); // lock counter = 0 533 ASSERT_TRUE(db1->ReleaseAdvisoryLock(43)); // lock counter = 0
533 ASSERT_FALSE(db1->ReleaseAdvisoryLock(43)); // cannot unlock 534 ASSERT_FALSE(db1->ReleaseAdvisoryLock(43)); // cannot unlock
534 ASSERT_TRUE(db1->AcquireAdvisoryLock(43)); // lock counter = 1 535 ASSERT_TRUE(db1->AcquireAdvisoryLock(43)); // lock counter = 1
535 536
536 { 537 {
537 std::auto_ptr<PostgreSQLDatabase> db2(CreateTestDatabase()); 538 std::unique_ptr<PostgreSQLDatabase> db2(CreateTestDatabase());
538 db2->Open(); 539 db2->Open();
539 540
540 // The "db1" is still actively locking 541 // The "db1" is still actively locking
541 ASSERT_FALSE(db2->AcquireAdvisoryLock(43)); 542 ASSERT_FALSE(db2->AcquireAdvisoryLock(43));
542 543