comparison PostgreSQL/UnitTests/PostgreSQLTests.cpp @ 230:675f8322eb7c

refactored StorageBackend by introducing an accessor class
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 06 Apr 2021 11:17:00 +0200
parents a4918d57435c
children 35598014f140
comparison
equal deleted inserted replaced
229:5744f09b0b1b 230:675f8322eb7c
63 } 63 }
64 64
65 65
66 static int64_t CountLargeObjects(PostgreSQLDatabase& db) 66 static int64_t CountLargeObjects(PostgreSQLDatabase& db)
67 { 67 {
68 // Count the number of large objects in the DB 68 PostgreSQLTransaction transaction(db, TransactionType_ReadOnly);
69 PostgreSQLStatement s(db, "SELECT COUNT(*) FROM pg_catalog.pg_largeobject"); 69
70 PostgreSQLResult r(s); 70 int64_t count;
71 return r.GetInteger64(0); 71
72 {
73 // Count the number of large objects in the DB
74 PostgreSQLStatement s(db, "SELECT COUNT(*) FROM pg_catalog.pg_largeobject");
75 PostgreSQLResult r(s);
76 count = r.GetInteger64(0);
77 }
78
79 transaction.Commit();
80 return count;
72 } 81 }
73 82
74 83
75 TEST(PostgreSQL, Basic) 84 TEST(PostgreSQL, Basic)
76 { 85 {
347 } 356 }
348 357
349 358
350 TEST(PostgreSQL, StorageArea) 359 TEST(PostgreSQL, StorageArea)
351 { 360 {
361 std::unique_ptr<PostgreSQLDatabase> database(PostgreSQLDatabase::OpenDatabaseConnection(globalParameters_));
362
352 PostgreSQLStorageArea storageArea(globalParameters_, true /* clear database */); 363 PostgreSQLStorageArea storageArea(globalParameters_, true /* clear database */);
353 364
354 { 365 {
355 DatabaseManager::Transaction transaction(storageArea.GetManager(), TransactionType_ReadWrite); 366 PostgreSQLStorageArea::Accessor accessor(storageArea);
356 PostgreSQLDatabase& db = 367
357 dynamic_cast<PostgreSQLDatabase&>(transaction.GetDatabase()); 368 ASSERT_EQ(0, CountLargeObjects(*database));
358
359 ASSERT_EQ(0, CountLargeObjects(db));
360 369
361 for (int i = 0; i < 10; i++) 370 for (int i = 0; i < 10; i++)
362 { 371 {
363 std::string uuid = boost::lexical_cast<std::string>(i); 372 std::string uuid = boost::lexical_cast<std::string>(i);
364 std::string value = "Value " + boost::lexical_cast<std::string>(i * 2); 373 std::string value = "Value " + boost::lexical_cast<std::string>(i * 2);
365 storageArea.Create(transaction, uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown); 374 accessor.Create(uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown);
366 } 375 }
367 376
368 std::string buffer; 377 std::string buffer;
369 ASSERT_THROW(storageArea.ReadToString(buffer, transaction, "nope", OrthancPluginContentType_Unknown), 378 ASSERT_THROW(accessor.ReadToString(buffer, "nope", OrthancPluginContentType_Unknown),
370 Orthanc::OrthancException); 379 Orthanc::OrthancException);
371 380
372 ASSERT_EQ(10, CountLargeObjects(db)); 381 ASSERT_EQ(10, CountLargeObjects(*database));
373 storageArea.Remove(transaction, "5", OrthancPluginContentType_Unknown); 382 accessor.Remove("5", OrthancPluginContentType_Unknown);
374 383
375 ASSERT_EQ(9, CountLargeObjects(db)); 384 ASSERT_EQ(9, CountLargeObjects(*database));
376 385
377 for (int i = 0; i < 10; i++) 386 for (int i = 0; i < 10; i++)
378 { 387 {
379 std::string uuid = boost::lexical_cast<std::string>(i); 388 std::string uuid = boost::lexical_cast<std::string>(i);
380 std::string expected = "Value " + boost::lexical_cast<std::string>(i * 2); 389 std::string expected = "Value " + boost::lexical_cast<std::string>(i * 2);
381 390
382 if (i == 5) 391 if (i == 5)
383 { 392 {
384 ASSERT_THROW(storageArea.ReadToString(buffer, transaction, uuid, OrthancPluginContentType_Unknown), 393 ASSERT_THROW(accessor.ReadToString(buffer, uuid, OrthancPluginContentType_Unknown),
385 Orthanc::OrthancException); 394 Orthanc::OrthancException);
386 } 395 }
387 else 396 else
388 { 397 {
389 storageArea.ReadToString(buffer, transaction, uuid, OrthancPluginContentType_Unknown); 398 accessor.ReadToString(buffer, uuid, OrthancPluginContentType_Unknown);
390 ASSERT_EQ(expected, buffer); 399 ASSERT_EQ(expected, buffer);
391 } 400 }
392 } 401 }
393 402
394 for (int i = 0; i < 10; i++) 403 for (int i = 0; i < 10; i++)
395 { 404 {
396 storageArea.Remove(transaction, boost::lexical_cast<std::string>(i), 405 accessor.Remove(boost::lexical_cast<std::string>(i), OrthancPluginContentType_Unknown);
397 OrthancPluginContentType_Unknown); 406 }
398 } 407
399 408 ASSERT_EQ(0, CountLargeObjects(*database));
400 ASSERT_EQ(0, CountLargeObjects(db));
401
402 transaction.Commit();
403 } 409 }
404 } 410 }
405 411
406 412
407 TEST(PostgreSQL, ImplicitTransaction) 413 TEST(PostgreSQL, ImplicitTransaction)