comparison PostgreSQL/UnitTests/PostgreSQLTests.cpp @ 14:9774802fd05f

PostgreSQLStorageArea working
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Jul 2018 20:28:27 +0200
parents 9e5e7a8314e0
children b2ff1cd2907a
comparison
equal deleted inserted replaced
13:927264a0c137 14:9774802fd05f
32 # undef S_IWOTH 32 # undef S_IWOTH
33 # undef S_IXGRP 33 # undef S_IXGRP
34 # undef S_IXOTH 34 # undef S_IXOTH
35 #endif 35 #endif
36 36
37 #include "../Plugins/PostgreSQLStorageArea.h"
37 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h" 38 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h"
38 #include "../../Framework/PostgreSQL/PostgreSQLResult.h" 39 #include "../../Framework/PostgreSQL/PostgreSQLResult.h"
39 #include "../../Framework/PostgreSQL/PostgreSQLLargeObject.h" 40 #include "../../Framework/PostgreSQL/PostgreSQLLargeObject.h"
40 41
41 #include <Core/OrthancException.h> 42 #include <Core/OrthancException.h>
64 65
65 66
66 static int64_t CountLargeObjects(PostgreSQLDatabase& db) 67 static int64_t CountLargeObjects(PostgreSQLDatabase& db)
67 { 68 {
68 // Count the number of large objects in the DB 69 // Count the number of large objects in the DB
69 PostgreSQLTransaction t(db);
70 PostgreSQLStatement s(db, "SELECT COUNT(*) FROM pg_catalog.pg_largeobject", true); 70 PostgreSQLStatement s(db, "SELECT COUNT(*) FROM pg_catalog.pg_largeobject", true);
71 PostgreSQLResult r(s); 71 PostgreSQLResult r(s);
72 return r.GetInteger64(0); 72 return r.GetInteger64(0);
73 } 73 }
74 74
336 336
337 ASSERT_EQ(9, CountLargeObjects(*pg)); 337 ASSERT_EQ(9, CountLargeObjects(*pg));
338 } 338 }
339 339
340 340
341 341 TEST(PostgreSQL, StorageArea)
342 #if ORTHANC_POSTGRESQL_STATIC == 1 342 {
343 # include <c.h> // PostgreSQL includes 343 OrthancDatabases::PostgreSQLStorageArea storageArea(globalParameters_);
344 344 storageArea.SetClearAll(true);
345 TEST(PostgreSQL, Version) 345
346 { 346 {
347 ASSERT_STREQ("9.6.1", PG_VERSION); 347 OrthancDatabases::DatabaseManager::Transaction transaction(storageArea.GetManager());
348 } 348 OrthancDatabases::PostgreSQLDatabase& db =
349 #endif 349 dynamic_cast<OrthancDatabases::PostgreSQLDatabase&>(transaction.GetDatabase());
350
351 ASSERT_EQ(0, CountLargeObjects(db));
352
353 for (int i = 0; i < 10; i++)
354 {
355 std::string uuid = boost::lexical_cast<std::string>(i);
356 std::string value = "Value " + boost::lexical_cast<std::string>(i * 2);
357 storageArea.Create(transaction, uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown);
358 }
359
360 std::string tmp;
361 ASSERT_THROW(storageArea.ReadToString(tmp, transaction, "nope", OrthancPluginContentType_Unknown),
362 Orthanc::OrthancException);
363
364 ASSERT_EQ(10, CountLargeObjects(db));
365 storageArea.Remove(transaction, "5", OrthancPluginContentType_Unknown);
366
367 ASSERT_EQ(9, CountLargeObjects(db));
368
369 for (int i = 0; i < 10; i++)
370 {
371 std::string uuid = boost::lexical_cast<std::string>(i);
372 std::string expected = "Value " + boost::lexical_cast<std::string>(i * 2);
373 std::string content;
374
375 if (i == 5)
376 {
377 ASSERT_THROW(storageArea.ReadToString(content, transaction, uuid, OrthancPluginContentType_Unknown),
378 Orthanc::OrthancException);
379 }
380 else
381 {
382 storageArea.ReadToString(content, transaction, uuid, OrthancPluginContentType_Unknown);
383 ASSERT_EQ(expected, content);
384 }
385 }
386
387 for (int i = 0; i < 10; i++)
388 {
389 storageArea.Remove(transaction, boost::lexical_cast<std::string>(i),
390 OrthancPluginContentType_Unknown);
391 }
392
393 ASSERT_EQ(0, CountLargeObjects(db));
394
395 transaction.Commit();
396 }
397 }