comparison PostgreSQL/UnitTests/UnitTestsMain.cpp @ 0:7cea966b6829

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 08:16:29 +0200
parents
children 9774802fd05f
comparison
equal deleted inserted replaced
-1:000000000000 0:7cea966b6829
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "../Plugins/PostgreSQLIndex.h"
23
24 #include <Core/Logging.h>
25 #include <gtest/gtest.h>
26
27 OrthancDatabases::PostgreSQLParameters globalParameters_;
28
29 #include "../../Framework/Plugins/IndexUnitTests.h"
30
31
32 TEST(PostgreSQLParameters, Basic)
33 {
34 OrthancDatabases::PostgreSQLParameters p;
35 p.SetDatabase("world");
36
37 ASSERT_EQ("postgresql://localhost:5432/world", p.GetConnectionUri());
38
39 p.ResetDatabase();
40 ASSERT_EQ("postgresql://localhost:5432/", p.GetConnectionUri());
41
42 p.SetDatabase("hello");
43 ASSERT_EQ("postgresql://localhost:5432/hello", p.GetConnectionUri());
44
45 p.SetHost("server");
46 ASSERT_EQ("postgresql://server:5432/hello", p.GetConnectionUri());
47
48 p.SetPortNumber(1234);
49 ASSERT_EQ("postgresql://server:1234/hello", p.GetConnectionUri());
50
51 p.SetPortNumber(5432);
52 ASSERT_EQ("postgresql://server:5432/hello", p.GetConnectionUri());
53
54 p.SetUsername("user");
55 p.SetPassword("pass");
56 ASSERT_EQ("postgresql://user:pass@server:5432/hello", p.GetConnectionUri());
57
58 p.SetPassword("");
59 ASSERT_EQ("postgresql://user@server:5432/hello", p.GetConnectionUri());
60
61 p.SetUsername("");
62 p.SetPassword("pass");
63 ASSERT_EQ("postgresql://server:5432/hello", p.GetConnectionUri());
64
65 p.SetUsername("");
66 p.SetPassword("");
67 ASSERT_EQ("postgresql://server:5432/hello", p.GetConnectionUri());
68
69 p.SetConnectionUri("hello://world");
70 ASSERT_EQ("hello://world", p.GetConnectionUri());
71 }
72
73
74 TEST(PostgreSQLIndex, Lock)
75 {
76 OrthancDatabases::PostgreSQLParameters noLock = globalParameters_;
77 noLock.SetLock(false);
78
79 OrthancDatabases::PostgreSQLParameters lock = globalParameters_;
80 lock.SetLock(true);
81
82 OrthancDatabases::PostgreSQLIndex db1(noLock);
83 db1.SetClearAll(true);
84 db1.Open();
85
86 {
87 OrthancDatabases::PostgreSQLIndex db2(lock);
88 db2.Open();
89
90 OrthancDatabases::PostgreSQLIndex db3(lock);
91 ASSERT_THROW(db3.Open(), Orthanc::OrthancException);
92 }
93
94 OrthancDatabases::PostgreSQLIndex db4(lock);
95 db4.Open();
96 }
97
98
99 #if 0
100 TEST(PostgreSQL, StorageArea)
101 {
102 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase(true));
103 PostgreSQLStorageArea s(pg.release(), true, true);
104
105 ASSERT_EQ(0, CountLargeObjects(s.GetDatabase()));
106
107 for (int i = 0; i < 10; i++)
108 {
109 std::string uuid = boost::lexical_cast<std::string>(i);
110 std::string value = "Value " + boost::lexical_cast<std::string>(i * 2);
111 s.Create(uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown);
112 }
113
114 std::string tmp;
115 ASSERT_THROW(s.Read(tmp, "nope", OrthancPluginContentType_Unknown), Orthanc::OrthancException);
116
117 ASSERT_EQ(10, CountLargeObjects(s.GetDatabase()));
118 s.Remove("5", OrthancPluginContentType_Unknown);
119 ASSERT_EQ(9, CountLargeObjects(s.GetDatabase()));
120
121 for (int i = 0; i < 10; i++)
122 {
123 std::string uuid = boost::lexical_cast<std::string>(i);
124 std::string expected = "Value " + boost::lexical_cast<std::string>(i * 2);
125 std::string content;
126
127 if (i == 5)
128 {
129 ASSERT_THROW(s.Read(content, uuid, OrthancPluginContentType_Unknown), Orthanc::OrthancException);
130 }
131 else
132 {
133 s.Read(content, uuid, OrthancPluginContentType_Unknown);
134 ASSERT_EQ(expected, content);
135 }
136 }
137
138 s.Clear();
139 ASSERT_EQ(0, CountLargeObjects(s.GetDatabase()));
140 }
141 #endif
142
143
144 int main(int argc, char **argv)
145 {
146 if (argc < 6)
147 {
148 std::cerr << "Usage: " << argv[0] << " <host> <port> <username> <password> <database>"
149 << std::endl << std::endl
150 << "Example: " << argv[0] << " localhost 5432 postgres postgres orthanctest"
151 << std::endl << std::endl;
152 return -1;
153 }
154
155 globalParameters_.SetHost(argv[1]);
156 globalParameters_.SetPortNumber(boost::lexical_cast<uint16_t>(argv[2]));
157 globalParameters_.SetUsername(argv[3]);
158 globalParameters_.SetPassword(argv[4]);
159 globalParameters_.SetDatabase(argv[5]);
160
161 ::testing::InitGoogleTest(&argc, argv);
162 Orthanc::Logging::Initialize();
163 Orthanc::Logging::EnableInfoLevel(true);
164 Orthanc::Logging::EnableTraceLevel(true);
165
166 int result = RUN_ALL_TESTS();
167
168 Orthanc::Logging::Finalize();
169
170 return result;
171 }