Mercurial > hg > orthanc-databases
annotate PostgreSQL/UnitTests/PostgreSQLTests.cpp @ 32:ac2481fb2c7e OrthancPostgreSQL-2.2
OrthancPostgreSQL-2.2
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 16 Jul 2018 17:05:22 +0200 |
parents | b2ff1cd2907a |
children | 714c5d2bee76 |
rev | line source |
---|---|
0 | 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 <gtest/gtest.h> | |
23 | |
24 #if defined(_WIN32) | |
25 // Fix redefinition of symbols on MinGW (these symbols are manually | |
26 // defined both by PostgreSQL and Google Test) | |
27 # undef S_IRGRP | |
28 # undef S_IROTH | |
29 # undef S_IRWXG | |
30 # undef S_IRWXO | |
31 # undef S_IWGRP | |
32 # undef S_IWOTH | |
33 # undef S_IXGRP | |
34 # undef S_IXOTH | |
35 #endif | |
36 | |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
37 #include "../Plugins/PostgreSQLStorageArea.h" |
0 | 38 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h" |
39 #include "../../Framework/PostgreSQL/PostgreSQLResult.h" | |
40 #include "../../Framework/PostgreSQL/PostgreSQLLargeObject.h" | |
41 | |
42 #include <Core/OrthancException.h> | |
43 | |
44 #include <boost/lexical_cast.hpp> | |
45 | |
46 using namespace OrthancDatabases; | |
47 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
48 extern PostgreSQLParameters globalParameters_; |
0 | 49 |
50 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
51 static PostgreSQLDatabase* CreateTestDatabase() |
0 | 52 { |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
53 std::auto_ptr<PostgreSQLDatabase> pg |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
54 (new PostgreSQLDatabase(globalParameters_)); |
0 | 55 |
56 pg->Open(); | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
57 pg->ClearAll(); |
0 | 58 |
59 return pg.release(); | |
60 } | |
61 | |
62 | |
63 static int64_t CountLargeObjects(PostgreSQLDatabase& db) | |
64 { | |
65 // Count the number of large objects in the DB | |
66 PostgreSQLStatement s(db, "SELECT COUNT(*) FROM pg_catalog.pg_largeobject", true); | |
67 PostgreSQLResult r(s); | |
68 return r.GetInteger64(0); | |
69 } | |
70 | |
71 | |
72 TEST(PostgreSQL, Basic) | |
73 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
74 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
0 | 75 |
76 ASSERT_FALSE(pg->DoesTableExist("Test")); | |
77 pg->Execute("CREATE TABLE Test(name INTEGER, value BIGINT)"); | |
78 ASSERT_TRUE(pg->DoesTableExist("Test")); | |
79 | |
80 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | |
81 s.DeclareInputInteger(0); | |
82 s.DeclareInputInteger64(1); | |
83 | |
84 s.BindInteger(0, 43); | |
85 s.BindNull(0); | |
86 s.BindInteger(0, 42); | |
87 s.BindInteger64(1, -4242); | |
88 s.Run(); | |
89 | |
90 s.BindInteger(0, 43); | |
91 s.BindNull(1); | |
92 s.Run(); | |
93 | |
94 s.BindNull(0); | |
95 s.BindInteger64(1, 4444); | |
96 s.Run(); | |
97 | |
98 { | |
99 PostgreSQLStatement t(*pg, "SELECT name, value FROM Test ORDER BY name", true); | |
100 PostgreSQLResult r(t); | |
101 | |
102 ASSERT_FALSE(r.IsDone()); | |
103 ASSERT_FALSE(r.IsNull(0)); ASSERT_EQ(42, r.GetInteger(0)); | |
104 ASSERT_FALSE(r.IsNull(1)); ASSERT_EQ(-4242, r.GetInteger64(1)); | |
105 | |
106 r.Next(); | |
107 ASSERT_FALSE(r.IsDone()); | |
108 ASSERT_FALSE(r.IsNull(0)); ASSERT_EQ(43, r.GetInteger(0)); | |
109 ASSERT_TRUE(r.IsNull(1)); | |
110 | |
111 r.Next(); | |
112 ASSERT_FALSE(r.IsDone()); | |
113 ASSERT_TRUE(r.IsNull(0)); | |
114 ASSERT_FALSE(r.IsNull(1)); ASSERT_EQ(4444, r.GetInteger64(1)); | |
115 | |
116 r.Next(); | |
117 ASSERT_TRUE(r.IsDone()); | |
118 } | |
119 | |
120 { | |
121 PostgreSQLStatement t(*pg, "SELECT name, value FROM Test WHERE name=$1", true); | |
122 t.DeclareInputInteger(0); | |
123 | |
124 { | |
125 t.BindInteger(0, 42); | |
126 PostgreSQLResult r(t); | |
127 ASSERT_FALSE(r.IsDone()); | |
128 ASSERT_FALSE(r.IsNull(0)); ASSERT_EQ(42, r.GetInteger(0)); | |
129 ASSERT_FALSE(r.IsNull(1)); ASSERT_EQ(-4242, r.GetInteger64(1)); | |
130 | |
131 r.Next(); | |
132 ASSERT_TRUE(r.IsDone()); | |
133 } | |
134 | |
135 { | |
136 t.BindInteger(0, 40); | |
137 PostgreSQLResult r(t); | |
138 ASSERT_TRUE(r.IsDone()); | |
139 } | |
140 } | |
141 | |
142 } | |
143 | |
144 | |
145 TEST(PostgreSQL, String) | |
146 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
147 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
0 | 148 |
149 pg->Execute("CREATE TABLE Test(name INTEGER, value VARCHAR(40))"); | |
150 | |
151 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | |
152 s.DeclareInputInteger(0); | |
153 s.DeclareInputString(1); | |
154 | |
155 s.BindInteger(0, 42); | |
156 s.BindString(1, "Hello"); | |
157 s.Run(); | |
158 | |
159 s.BindInteger(0, 43); | |
160 s.BindNull(1); | |
161 s.Run(); | |
162 | |
163 s.BindNull(0); | |
164 s.BindString(1, ""); | |
165 s.Run(); | |
166 | |
167 { | |
168 PostgreSQLStatement t(*pg, "SELECT name, value FROM Test ORDER BY name", true); | |
169 PostgreSQLResult r(t); | |
170 | |
171 ASSERT_FALSE(r.IsDone()); | |
172 ASSERT_FALSE(r.IsNull(0)); ASSERT_EQ(42, r.GetInteger(0)); | |
173 ASSERT_FALSE(r.IsNull(1)); ASSERT_EQ("Hello", r.GetString(1)); | |
174 | |
175 r.Next(); | |
176 ASSERT_FALSE(r.IsDone()); | |
177 ASSERT_FALSE(r.IsNull(0)); ASSERT_EQ(43, r.GetInteger(0)); | |
178 ASSERT_TRUE(r.IsNull(1)); | |
179 | |
180 r.Next(); | |
181 ASSERT_FALSE(r.IsDone()); | |
182 ASSERT_TRUE(r.IsNull(0)); | |
183 ASSERT_FALSE(r.IsNull(1)); ASSERT_EQ("", r.GetString(1)); | |
184 | |
185 r.Next(); | |
186 ASSERT_TRUE(r.IsDone()); | |
187 } | |
188 } | |
189 | |
190 | |
191 TEST(PostgreSQL, Transaction) | |
192 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
193 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
0 | 194 |
195 pg->Execute("CREATE TABLE Test(name INTEGER, value INTEGER)"); | |
196 | |
197 { | |
198 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | |
199 s.DeclareInputInteger(0); | |
200 s.DeclareInputInteger(1); | |
201 s.BindInteger(0, 42); | |
202 s.BindInteger(1, 4242); | |
203 s.Run(); | |
204 | |
205 { | |
206 PostgreSQLTransaction t(*pg); | |
207 s.BindInteger(0, 43); | |
208 s.BindInteger(1, 4343); | |
209 s.Run(); | |
210 s.BindInteger(0, 44); | |
211 s.BindInteger(1, 4444); | |
212 s.Run(); | |
213 | |
214 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test", true); | |
215 PostgreSQLResult r(u); | |
216 ASSERT_EQ(3, r.GetInteger64(0)); | |
217 | |
218 // No commit | |
219 } | |
220 | |
221 { | |
222 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test", true); | |
223 PostgreSQLResult r(u); | |
224 ASSERT_EQ(1, r.GetInteger64(0)); // Just "1" because of implicit rollback | |
225 } | |
226 | |
227 { | |
228 PostgreSQLTransaction t(*pg); | |
229 s.BindInteger(0, 43); | |
230 s.BindInteger(1, 4343); | |
231 s.Run(); | |
232 s.BindInteger(0, 44); | |
233 s.BindInteger(1, 4444); | |
234 s.Run(); | |
235 | |
236 { | |
237 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test", true); | |
238 PostgreSQLResult r(u); | |
239 ASSERT_EQ(3, r.GetInteger64(0)); | |
240 | |
241 t.Commit(); | |
242 ASSERT_THROW(t.Rollback(), Orthanc::OrthancException); | |
243 ASSERT_THROW(t.Commit(), Orthanc::OrthancException); | |
244 } | |
245 } | |
246 | |
247 { | |
248 PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test", true); | |
249 PostgreSQLResult r(u); | |
250 ASSERT_EQ(3, r.GetInteger64(0)); | |
251 } | |
252 } | |
253 } | |
254 | |
255 | |
256 | |
257 | |
258 | |
259 TEST(PostgreSQL, LargeObject) | |
260 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
261 std::auto_ptr<PostgreSQLDatabase> pg(CreateTestDatabase()); |
0 | 262 ASSERT_EQ(0, CountLargeObjects(*pg)); |
263 | |
264 pg->Execute("CREATE TABLE Test(name VARCHAR, value OID)"); | |
265 | |
266 // Automatically remove the large objects associated with the table | |
267 pg->Execute("CREATE RULE TestDelete AS ON DELETE TO Test DO SELECT lo_unlink(old.value);"); | |
268 | |
269 { | |
270 PostgreSQLStatement s(*pg, "INSERT INTO Test VALUES ($1,$2)", false); | |
271 s.DeclareInputString(0); | |
272 s.DeclareInputLargeObject(1); | |
273 | |
274 for (int i = 0; i < 10; i++) | |
275 { | |
276 PostgreSQLTransaction t(*pg); | |
277 | |
278 std::string value = "Value " + boost::lexical_cast<std::string>(i * 2); | |
279 PostgreSQLLargeObject obj(*pg, value); | |
280 | |
281 s.BindString(0, "Index " + boost::lexical_cast<std::string>(i)); | |
282 s.BindLargeObject(1, obj); | |
283 s.Run(); | |
284 | |
285 std::string tmp; | |
286 PostgreSQLLargeObject::Read(tmp, *pg, obj.GetOid()); | |
287 ASSERT_EQ(value, tmp); | |
288 | |
289 t.Commit(); | |
290 } | |
291 } | |
292 | |
293 | |
294 ASSERT_EQ(10, CountLargeObjects(*pg)); | |
295 | |
296 { | |
297 PostgreSQLTransaction t(*pg); | |
298 PostgreSQLStatement s(*pg, "SELECT * FROM Test ORDER BY name DESC", true); | |
299 PostgreSQLResult r(s); | |
300 | |
301 ASSERT_FALSE(r.IsDone()); | |
302 | |
303 ASSERT_FALSE(r.IsNull(0)); | |
304 ASSERT_EQ("Index 9", r.GetString(0)); | |
305 | |
306 std::string data; | |
307 r.GetLargeObject(data, 1); | |
308 ASSERT_EQ("Value 18", data); | |
309 | |
310 r.Next(); | |
311 ASSERT_FALSE(r.IsDone()); | |
312 | |
313 //ASSERT_TRUE(r.IsString(0)); | |
314 } | |
315 | |
316 | |
317 { | |
318 PostgreSQLTransaction t(*pg); | |
319 PostgreSQLStatement s(*pg, "DELETE FROM Test WHERE name='Index 9'", false); | |
320 s.Run(); | |
321 t.Commit(); | |
322 } | |
323 | |
324 | |
325 { | |
326 // Count the number of items in the DB | |
327 PostgreSQLTransaction t(*pg); | |
328 PostgreSQLStatement s(*pg, "SELECT COUNT(*) FROM Test", true); | |
329 PostgreSQLResult r(s); | |
330 ASSERT_EQ(9, r.GetInteger64(0)); | |
331 } | |
332 | |
333 ASSERT_EQ(9, CountLargeObjects(*pg)); | |
334 } | |
335 | |
336 | |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
337 TEST(PostgreSQL, StorageArea) |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
338 { |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
339 PostgreSQLStorageArea storageArea(globalParameters_); |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
340 storageArea.SetClearAll(true); |
0 | 341 |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
342 { |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
343 DatabaseManager::Transaction transaction(storageArea.GetManager()); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
344 PostgreSQLDatabase& db = |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
345 dynamic_cast<PostgreSQLDatabase&>(transaction.GetDatabase()); |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
346 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
347 ASSERT_EQ(0, CountLargeObjects(db)); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
348 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
349 for (int i = 0; i < 10; i++) |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
350 { |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
351 std::string uuid = boost::lexical_cast<std::string>(i); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
352 std::string value = "Value " + boost::lexical_cast<std::string>(i * 2); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
353 storageArea.Create(transaction, uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
354 } |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
355 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
356 std::string tmp; |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
357 ASSERT_THROW(storageArea.ReadToString(tmp, transaction, "nope", OrthancPluginContentType_Unknown), |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
358 Orthanc::OrthancException); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
359 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
360 ASSERT_EQ(10, CountLargeObjects(db)); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
361 storageArea.Remove(transaction, "5", OrthancPluginContentType_Unknown); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
362 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
363 ASSERT_EQ(9, CountLargeObjects(db)); |
0 | 364 |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
365 for (int i = 0; i < 10; i++) |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
366 { |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
367 std::string uuid = boost::lexical_cast<std::string>(i); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
368 std::string expected = "Value " + boost::lexical_cast<std::string>(i * 2); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
369 std::string content; |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
370 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
371 if (i == 5) |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
372 { |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
373 ASSERT_THROW(storageArea.ReadToString(content, transaction, uuid, OrthancPluginContentType_Unknown), |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
374 Orthanc::OrthancException); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
375 } |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
376 else |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
377 { |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
378 storageArea.ReadToString(content, transaction, uuid, OrthancPluginContentType_Unknown); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
379 ASSERT_EQ(expected, content); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
380 } |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
381 } |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
382 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
383 for (int i = 0; i < 10; i++) |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
384 { |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
385 storageArea.Remove(transaction, boost::lexical_cast<std::string>(i), |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
386 OrthancPluginContentType_Unknown); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
387 } |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
388 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
389 ASSERT_EQ(0, CountLargeObjects(db)); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
390 |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
391 transaction.Commit(); |
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
392 } |
0 | 393 } |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
394 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
395 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
396 TEST(PostgreSQL, ImplicitTransaction) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
397 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
398 std::auto_ptr<PostgreSQLDatabase> db(CreateTestDatabase()); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
399 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
400 ASSERT_FALSE(db->DoesTableExist("test")); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
401 ASSERT_FALSE(db->DoesTableExist("test2")); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
402 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
403 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
404 std::auto_ptr<OrthancDatabases::ITransaction> t(db->CreateTransaction(false)); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
405 ASSERT_FALSE(t->IsImplicit()); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
406 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
407 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
408 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
409 Query query("CREATE TABLE test(id INT)", false); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
410 std::auto_ptr<IPrecompiledStatement> s(db->Compile(query)); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
411 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
412 std::auto_ptr<ITransaction> t(db->CreateTransaction(true)); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
413 ASSERT_TRUE(t->IsImplicit()); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
414 ASSERT_THROW(t->Commit(), Orthanc::OrthancException); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
415 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
416 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
417 Dictionary args; |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
418 t->ExecuteWithoutResult(*s, args); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
419 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
420 t->Commit(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
421 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
422 ASSERT_THROW(t->Commit(), Orthanc::OrthancException); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
423 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
424 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
425 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
426 // An implicit transaction does not need to be explicitely committed |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
427 Query query("CREATE TABLE test2(id INT)", false); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
428 std::auto_ptr<IPrecompiledStatement> s(db->Compile(query)); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
429 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
430 std::auto_ptr<ITransaction> t(db->CreateTransaction(true)); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
431 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
432 Dictionary args; |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
433 t->ExecuteWithoutResult(*s, args); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
434 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
435 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
436 ASSERT_TRUE(db->DoesTableExist("test")); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
437 ASSERT_TRUE(db->DoesTableExist("test2")); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
438 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
439 |