Mercurial > hg > orthanc-databases
annotate MySQL/UnitTests/UnitTestsMain.cpp @ 185:7c46155b5bb4
fix mingw build
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 16 Dec 2020 13:59:17 +0100 |
parents | 275e14f57f1e |
children | 3236894320d6 |
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 | |
140
4cd7e45b671e
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
138
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
0 | 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/MySQLIndex.h" | |
17 | 23 #include "../Plugins/MySQLStorageArea.h" |
0 | 24 |
25 OrthancDatabases::MySQLParameters globalParameters_; | |
26 | |
17 | 27 #include "../../Framework/Common/Integer64Value.h" |
28 #include "../../Framework/MySQL/MySQLDatabase.h" | |
29 #include "../../Framework/MySQL/MySQLResult.h" | |
30 #include "../../Framework/MySQL/MySQLStatement.h" | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
31 #include "../../Framework/MySQL/MySQLTransaction.h" |
0 | 32 #include "../../Framework/Plugins/IndexUnitTests.h" |
33 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
34 #include <Compatibility.h> // For std::unique_ptr<> |
152 | 35 #include <HttpClient.h> |
36 #include <Logging.h> | |
37 #include <Toolbox.h> | |
0 | 38 |
39 #include <gtest/gtest.h> | |
40 | |
41 | |
42 TEST(MySQLIndex, Lock) | |
43 { | |
44 OrthancDatabases::MySQLParameters noLock = globalParameters_; | |
45 noLock.SetLock(false); | |
46 | |
47 OrthancDatabases::MySQLParameters lock = globalParameters_; | |
48 lock.SetLock(true); | |
49 | |
50 OrthancDatabases::MySQLIndex db1(noLock); | |
51 db1.SetClearAll(true); | |
52 db1.Open(); | |
53 | |
54 { | |
55 OrthancDatabases::MySQLIndex db2(lock); | |
56 db2.Open(); | |
57 | |
58 OrthancDatabases::MySQLIndex db3(lock); | |
59 ASSERT_THROW(db3.Open(), Orthanc::OrthancException); | |
60 } | |
61 | |
62 OrthancDatabases::MySQLIndex db4(lock); | |
63 db4.Open(); | |
64 } | |
65 | |
66 | |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
67 TEST(MySQL, Lock2) |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
68 { |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
69 OrthancDatabases::MySQLDatabase::ClearDatabase(globalParameters_); |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
70 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
71 OrthancDatabases::MySQLDatabase db1(globalParameters_); |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
72 db1.Open(); |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
73 |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
74 ASSERT_FALSE(db1.ReleaseAdvisoryLock("mylock")); // lock counter = 0 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
75 ASSERT_TRUE(db1.AcquireAdvisoryLock("mylock")); // lock counter = 1 |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
76 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
77 // OK, as this is the same connection |
138
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
78 ASSERT_TRUE(db1.AcquireAdvisoryLock("mylock")); |
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
79 // lock counter = 2 if MySQL >= 5.7, or 1 if MySQL < 5.7 (because |
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
80 // acquiring a lock releases all the previously-acquired locks) |
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
81 |
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
82 ASSERT_TRUE(db1.ReleaseAdvisoryLock("mylock")); |
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
83 // lock counter = 1 if MySQL >= 5.7, or 0 if MySQL < 5.7 |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
84 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
85 // Try and release twice the lock |
138
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
86 db1.ReleaseAdvisoryLock("mylock"); // Succeeds iff MySQL >= 5.7 |
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
87 |
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
88 ASSERT_TRUE(db1.AcquireAdvisoryLock("mylock2")); // lock counter = 1 |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
89 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
90 { |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
91 OrthancDatabases::MySQLDatabase db2(globalParameters_); |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
92 db2.Open(); |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
93 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
94 // The "db1" is still actively locking |
138
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
95 ASSERT_FALSE(db2.AcquireAdvisoryLock("mylock2")); |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
96 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
97 // Release the "db1" lock |
138
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
98 ASSERT_TRUE(db1.ReleaseAdvisoryLock("mylock2")); |
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
99 ASSERT_FALSE(db1.ReleaseAdvisoryLock("mylock2")); |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
100 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
101 // "db2" can now acquire the lock, but not "db1" |
138
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
102 ASSERT_TRUE(db2.AcquireAdvisoryLock("mylock2")); |
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
103 ASSERT_FALSE(db1.AcquireAdvisoryLock("mylock2")); |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
104 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
105 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
106 // "db2" is closed, "db1" can now acquire the lock |
138
5c5cd59c991f
fix unit tests if MySQL >= 5.7
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
107 ASSERT_TRUE(db1.AcquireAdvisoryLock("mylock2")); |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
108 } |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
109 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
110 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
111 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
112 /** |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
113 * WARNING: The following test only succeeds if MySQL >= 5.7. This is |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
114 * because in MySQL < 5.7, acquiring a lock by calling "SELECT |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
115 * GET_LOCK()" releases all the previously acquired locks! |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
116 **/ |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
117 TEST(MySQL, DISABLED_Lock3) |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
118 { |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
119 OrthancDatabases::MySQLDatabase::ClearDatabase(globalParameters_); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
120 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
121 OrthancDatabases::MySQLDatabase db1(globalParameters_); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
122 db1.Open(); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
123 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
124 ASSERT_TRUE(db1.AcquireAdvisoryLock("mylock1")); // lock counter = 1 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
125 ASSERT_TRUE(db1.AcquireAdvisoryLock("mylock2")); // lock counter = 1 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
126 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
127 { |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
128 OrthancDatabases::MySQLDatabase db2(globalParameters_); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
129 db2.Open(); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
130 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
131 ASSERT_FALSE(db2.AcquireAdvisoryLock("mylock1")); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
132 } |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
133 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
134 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
135 |
17 | 136 static int64_t CountFiles(OrthancDatabases::MySQLDatabase& db) |
137 { | |
138 OrthancDatabases::Query query("SELECT COUNT(*) FROM StorageArea", true); | |
139 OrthancDatabases::MySQLStatement s(db, query); | |
140 OrthancDatabases::MySQLTransaction t(db); | |
141 OrthancDatabases::Dictionary d; | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
142 std::unique_ptr<OrthancDatabases::IResult> result(s.Execute(t, d)); |
17 | 143 return dynamic_cast<const OrthancDatabases::Integer64Value&>(result->GetField(0)).GetValue(); |
144 } | |
145 | |
146 | |
147 TEST(MySQL, StorageArea) | |
148 { | |
149 OrthancDatabases::MySQLStorageArea storageArea(globalParameters_); | |
150 storageArea.SetClearAll(true); | |
151 | |
152 { | |
153 OrthancDatabases::DatabaseManager::Transaction transaction(storageArea.GetManager()); | |
154 OrthancDatabases::MySQLDatabase& db = | |
155 dynamic_cast<OrthancDatabases::MySQLDatabase&>(transaction.GetDatabase()); | |
156 | |
157 ASSERT_EQ(0, CountFiles(db)); | |
158 | |
159 for (int i = 0; i < 10; i++) | |
160 { | |
161 std::string uuid = boost::lexical_cast<std::string>(i); | |
162 std::string value = "Value " + boost::lexical_cast<std::string>(i * 2); | |
163 storageArea.Create(transaction, uuid, value.c_str(), value.size(), OrthancPluginContentType_Unknown); | |
164 } | |
165 | |
166 std::string tmp; | |
167 ASSERT_THROW(storageArea.ReadToString(tmp, transaction, "nope", OrthancPluginContentType_Unknown), | |
168 Orthanc::OrthancException); | |
169 | |
170 ASSERT_EQ(10, CountFiles(db)); | |
171 storageArea.Remove(transaction, "5", OrthancPluginContentType_Unknown); | |
172 | |
173 ASSERT_EQ(9, CountFiles(db)); | |
174 | |
175 for (int i = 0; i < 10; i++) | |
176 { | |
177 std::string uuid = boost::lexical_cast<std::string>(i); | |
178 std::string expected = "Value " + boost::lexical_cast<std::string>(i * 2); | |
179 std::string content; | |
180 | |
181 if (i == 5) | |
182 { | |
183 ASSERT_THROW(storageArea.ReadToString(content, transaction, uuid, OrthancPluginContentType_Unknown), | |
184 Orthanc::OrthancException); | |
185 } | |
186 else | |
187 { | |
188 storageArea.ReadToString(content, transaction, uuid, OrthancPluginContentType_Unknown); | |
189 ASSERT_EQ(expected, content); | |
190 } | |
191 } | |
192 | |
193 for (int i = 0; i < 10; i++) | |
194 { | |
195 storageArea.Remove(transaction, boost::lexical_cast<std::string>(i), | |
196 OrthancPluginContentType_Unknown); | |
197 } | |
198 | |
199 ASSERT_EQ(0, CountFiles(db)); | |
200 | |
201 transaction.Commit(); | |
202 } | |
203 } | |
204 | |
205 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
206 TEST(MySQL, ImplicitTransaction) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
207 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
208 OrthancDatabases::MySQLDatabase::ClearDatabase(globalParameters_); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
209 OrthancDatabases::MySQLDatabase db(globalParameters_); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
210 db.Open(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
211 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
212 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
213 OrthancDatabases::MySQLTransaction t(db); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
214 ASSERT_FALSE(db.DoesTableExist(t, "test")); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
215 ASSERT_FALSE(db.DoesTableExist(t, "test2")); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
216 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
217 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
218 { |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
219 std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(false)); |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
220 ASSERT_FALSE(t->IsImplicit()); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
221 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
222 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
223 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
224 OrthancDatabases::Query query("CREATE TABLE test(id INT)", false); |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
225 std::unique_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query)); |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
226 |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
227 std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true)); |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
228 ASSERT_TRUE(t->IsImplicit()); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
229 ASSERT_THROW(t->Commit(), Orthanc::OrthancException); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
230 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
231 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
232 OrthancDatabases::Dictionary args; |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
233 t->ExecuteWithoutResult(*s, args); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
234 ASSERT_THROW(t->Rollback(), Orthanc::OrthancException); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
235 t->Commit(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
236 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
237 ASSERT_THROW(t->Commit(), Orthanc::OrthancException); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
238 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
239 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
240 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
241 // An implicit transaction does not need to be explicitely committed |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
242 OrthancDatabases::Query query("CREATE TABLE test2(id INT)", false); |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
243 std::unique_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query)); |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
244 |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
245 std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true)); |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
246 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
247 OrthancDatabases::Dictionary args; |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
248 t->ExecuteWithoutResult(*s, args); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
249 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
250 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
251 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
252 OrthancDatabases::MySQLTransaction t(db); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
253 ASSERT_TRUE(db.DoesTableExist(t, "test")); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
254 ASSERT_TRUE(db.DoesTableExist(t, "test2")); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
255 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
256 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
257 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
258 |
0 | 259 int main(int argc, char **argv) |
260 { | |
261 if (argc < 5) | |
262 { | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
263 std::cerr |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
264 #if !defined(_WIN32) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
265 << "Usage (UNIX socket): " << argv[0] << " <socket> <username> <password> <database>" |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
266 << std::endl |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
267 #endif |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
268 << "Usage (TCP connection): " << argv[0] << " <host> <port> <username> <password> <database>" |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
269 << std::endl << std::endl |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
270 #if !defined(_WIN32) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
271 << "Example (UNIX socket): " << argv[0] << " /var/run/mysqld/mysqld.sock root root orthanctest" |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
272 << std::endl |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
273 #endif |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
274 << "Example (TCP connection): " << argv[0] << " localhost 3306 root root orthanctest" |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
275 << std::endl << std::endl; |
0 | 276 return -1; |
277 } | |
278 | |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
279 std::vector<std::string> args; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
280 for (int i = 1; i < argc; i++) |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
281 { |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
282 // Ignore arguments beginning with "-" to allow passing arguments |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
283 // to Google Test such as "--gtest_filter=" |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
284 if (argv[i] != NULL && |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
285 argv[i][0] != '-') |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
286 { |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
287 args.push_back(std::string(argv[i])); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
288 } |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
289 } |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
290 |
0 | 291 ::testing::InitGoogleTest(&argc, argv); |
292 Orthanc::Logging::Initialize(); | |
293 Orthanc::Logging::EnableInfoLevel(true); | |
294 Orthanc::Logging::EnableTraceLevel(true); | |
42 | 295 Orthanc::Toolbox::InitializeOpenSsl(); |
40
5600949bfb12
preparing for release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
296 Orthanc::HttpClient::GlobalInitialize(); |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
297 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
298 if (args.size() == 4) |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
299 { |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
300 // UNIX socket flavor |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
301 globalParameters_.SetHost(""); |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
302 globalParameters_.SetUnixSocket(args[0]); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
303 globalParameters_.SetUsername(args[1]); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
304 globalParameters_.SetPassword(args[2]); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
305 globalParameters_.SetDatabase(args[3]); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
306 } |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
307 else if (args.size() == 5) |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
308 { |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
309 // TCP connection flavor |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
310 globalParameters_.SetHost(args[0]); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
311 globalParameters_.SetPort(boost::lexical_cast<unsigned int>(args[1])); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
312 globalParameters_.SetUsername(args[2]); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
313 globalParameters_.SetPassword(args[3]); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
314 globalParameters_.SetDatabase(args[4]); |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
315 |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
316 // Force the use of TCP on localhost, even if UNIX sockets are available |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
42
diff
changeset
|
317 globalParameters_.SetUnixSocket(""); |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
318 } |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
319 else |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
320 { |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
321 LOG(ERROR) << "Bad number of arguments"; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
322 return -1; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
323 } |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
324 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
325 Json::Value config; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
326 globalParameters_.Format(config); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
327 std::cout << "Parameters of the MySQL connection: " << std::endl |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
17
diff
changeset
|
328 << config.toStyledString() << std::endl; |
0 | 329 |
330 int result = RUN_ALL_TESTS(); | |
331 | |
40
5600949bfb12
preparing for release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
332 Orthanc::HttpClient::GlobalFinalize(); |
42 | 333 Orthanc::Toolbox::FinalizeOpenSsl(); |
334 OrthancDatabases::MySQLDatabase::GlobalFinalization(); | |
0 | 335 Orthanc::Logging::Finalize(); |
336 | |
337 return result; | |
338 } |