Mercurial > hg > orthanc
annotate UnitTestsSources/SQLiteChromiumTests.cpp @ 3702:643b5ee86f92
fix handling of errors of OrthancPluginStorageRemove()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 26 Feb 2020 14:36:42 +0100 |
parents | 94f4a18a79cc |
children | f9863630ec7f |
rev | line source |
---|---|
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
1 /** |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
6 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
8 * modify it under the terms of the GNU General Public License as |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
9 * published by the Free Software Foundation, either version 3 of the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
10 * License, or (at your option) any later version. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
11 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
12 * In addition, as a special exception, the copyright holders of this |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
13 * program give permission to link the code of its release with the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
15 * that use the same license as the "OpenSSL" library), and distribute |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
16 * the linked executables. You must obey the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
17 * in all respects for all of the code used other than "OpenSSL". If you |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
18 * modify file(s) with this exception, you may extend this exception to |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
19 * your version of the file(s), but you are not obligated to do so. If |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
20 * you do not wish to do so, delete this exception statement from your |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
21 * version. If you delete this exception statement from all source files |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
22 * in the program, then also delete it here. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
23 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
24 * This program is distributed in the hope that it will be useful, but |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
25 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
27 * General Public License for more details. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
28 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
29 * You should have received a copy of the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
31 **/ |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
32 |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
33 |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
827
diff
changeset
|
34 #include "PrecompiledHeadersUnitTests.h" |
0 | 35 #include "gtest/gtest.h" |
36 | |
37 #include "../Core/Toolbox.h" | |
38 #include "../Core/SQLite/Connection.h" | |
39 #include "../Core/SQLite/Statement.h" | |
40 #include "../Core/SQLite/Transaction.h" | |
41 | |
42 #include <sqlite3.h> | |
43 | |
138 | 44 |
63 | 45 using namespace Orthanc; |
138 | 46 using namespace Orthanc::SQLite; |
0 | 47 |
48 | |
49 /******************************************************************** | |
50 ** Tests from | |
51 ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/connection_unittest.cc | |
52 ********************************************************************/ | |
53 | |
2655 | 54 namespace |
0 | 55 { |
2655 | 56 class SQLConnectionTest : public testing::Test |
0 | 57 { |
2655 | 58 public: |
59 SQLConnectionTest() | |
60 { | |
61 } | |
0 | 62 |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
63 virtual ~SQLConnectionTest() ORTHANC_OVERRIDE |
2655 | 64 { |
65 } | |
66 | |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
67 virtual void SetUp() ORTHANC_OVERRIDE |
2655 | 68 { |
69 db_.OpenInMemory(); | |
70 } | |
0 | 71 |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
72 virtual void TearDown() ORTHANC_OVERRIDE |
2655 | 73 { |
74 db_.Close(); | |
75 } | |
0 | 76 |
2655 | 77 Connection& db() |
78 { | |
79 return db_; | |
80 } | |
81 | |
82 private: | |
83 Connection db_; | |
84 }; | |
85 } | |
0 | 86 |
87 | |
88 | |
89 TEST_F(SQLConnectionTest, Execute) | |
90 { | |
91 // Valid statement should return true. | |
92 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
93 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); | |
94 | |
95 // Invalid statement should fail. | |
96 ASSERT_EQ(SQLITE_ERROR, | |
97 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); | |
98 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); | |
99 } | |
100 | |
101 TEST_F(SQLConnectionTest, ExecuteWithErrorCode) { | |
102 ASSERT_EQ(SQLITE_OK, | |
103 db().ExecuteAndReturnErrorCode("CREATE TABLE foo (a, b)")); | |
104 ASSERT_EQ(SQLITE_ERROR, | |
105 db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE")); | |
106 ASSERT_EQ(SQLITE_ERROR, | |
107 db().ExecuteAndReturnErrorCode( | |
138 | 108 "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)")); |
0 | 109 } |
110 | |
111 TEST_F(SQLConnectionTest, CachedStatement) { | |
138 | 112 StatementId id1("foo", 12); |
0 | 113 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
114 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); | |
115 | |
116 // Create a new cached statement. | |
117 { | |
138 | 118 Statement s(db(), id1, "SELECT a FROM foo"); |
0 | 119 ASSERT_TRUE(s.Step()); |
120 EXPECT_EQ(12, s.ColumnInt(0)); | |
121 } | |
122 | |
123 // The statement should be cached still. | |
124 EXPECT_TRUE(db().HasCachedStatement(id1)); | |
125 | |
126 { | |
127 // Get the same statement using different SQL. This should ignore our | |
128 // SQL and use the cached one (so it will be valid). | |
138 | 129 Statement s(db(), id1, "something invalid("); |
0 | 130 ASSERT_TRUE(s.Step()); |
131 EXPECT_EQ(12, s.ColumnInt(0)); | |
132 } | |
133 | |
134 // Make sure other statements aren't marked as cached. | |
135 EXPECT_FALSE(db().HasCachedStatement(SQLITE_FROM_HERE)); | |
136 } | |
137 | |
138 TEST_F(SQLConnectionTest, IsSQLValidTest) { | |
139 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
140 ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo")); | |
141 ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo")); | |
142 } | |
143 | |
144 | |
145 | |
146 TEST_F(SQLConnectionTest, DoesStuffExist) { | |
147 // Test DoesTableExist. | |
148 EXPECT_FALSE(db().DoesTableExist("foo")); | |
149 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
150 EXPECT_TRUE(db().DoesTableExist("foo")); | |
151 | |
152 // Should be case sensitive. | |
153 EXPECT_FALSE(db().DoesTableExist("FOO")); | |
154 | |
155 // Test DoesColumnExist. | |
156 EXPECT_FALSE(db().DoesColumnExist("foo", "bar")); | |
157 EXPECT_TRUE(db().DoesColumnExist("foo", "a")); | |
158 | |
159 // Testing for a column on a nonexistent table. | |
160 EXPECT_FALSE(db().DoesColumnExist("bar", "b")); | |
161 } | |
162 | |
163 TEST_F(SQLConnectionTest, GetLastInsertRowId) { | |
164 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)")); | |
165 | |
166 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); | |
167 | |
168 // Last insert row ID should be valid. | |
169 int64_t row = db().GetLastInsertRowId(); | |
170 EXPECT_LT(0, row); | |
171 | |
172 // It should be the primary key of the row we just inserted. | |
138 | 173 Statement s(db(), "SELECT value FROM foo WHERE id=?"); |
0 | 174 s.BindInt64(0, row); |
175 ASSERT_TRUE(s.Step()); | |
176 EXPECT_EQ(12, s.ColumnInt(0)); | |
177 } | |
178 | |
179 TEST_F(SQLConnectionTest, Rollback) { | |
180 ASSERT_TRUE(db().BeginTransaction()); | |
181 ASSERT_TRUE(db().BeginTransaction()); | |
182 EXPECT_EQ(2, db().GetTransactionNesting()); | |
183 db().RollbackTransaction(); | |
184 EXPECT_FALSE(db().CommitTransaction()); | |
185 EXPECT_TRUE(db().BeginTransaction()); | |
186 } | |
187 | |
188 | |
189 | |
190 | |
191 /******************************************************************** | |
192 ** Tests from | |
193 ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/statement_unittest.cc | |
194 ********************************************************************/ | |
195 | |
138 | 196 namespace Orthanc |
0 | 197 { |
138 | 198 namespace SQLite |
199 { | |
200 class SQLStatementTest : public SQLConnectionTest | |
201 { | |
202 }; | |
0 | 203 |
138 | 204 TEST_F(SQLStatementTest, Run) { |
205 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
206 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); | |
0 | 207 |
138 | 208 Statement s(db(), "SELECT b FROM foo WHERE a=?"); |
209 // Stepping it won't work since we haven't bound the value. | |
210 EXPECT_FALSE(s.Step()); | |
0 | 211 |
138 | 212 // Run should fail since this produces output, and we should use Step(). This |
213 // gets a bit wonky since sqlite says this is OK so succeeded is set. | |
214 s.Reset(true); | |
215 s.BindInt(0, 3); | |
216 EXPECT_FALSE(s.Run()); | |
217 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode()); | |
0 | 218 |
138 | 219 // Resetting it should put it back to the previous state (not runnable). |
220 s.Reset(true); | |
0 | 221 |
138 | 222 // Binding and stepping should produce one row. |
223 s.BindInt(0, 3); | |
224 EXPECT_TRUE(s.Step()); | |
225 EXPECT_EQ(12, s.ColumnInt(0)); | |
226 EXPECT_FALSE(s.Step()); | |
227 } | |
0 | 228 |
138 | 229 TEST_F(SQLStatementTest, BasicErrorCallback) { |
230 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); | |
231 // Insert in the foo table the primary key. It is an error to insert | |
232 // something other than an number. This error causes the error callback | |
233 // handler to be called with SQLITE_MISMATCH as error code. | |
234 Statement s(db(), "INSERT INTO foo (a) VALUES (?)"); | |
235 s.BindCString(0, "bad bad"); | |
236 EXPECT_THROW(s.Run(), OrthancException); | |
237 } | |
0 | 238 |
138 | 239 TEST_F(SQLStatementTest, Reset) { |
240 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
241 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); | |
242 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); | |
0 | 243 |
138 | 244 Statement s(db(), "SELECT b FROM foo WHERE a = ? "); |
245 s.BindInt(0, 3); | |
246 ASSERT_TRUE(s.Step()); | |
247 EXPECT_EQ(12, s.ColumnInt(0)); | |
248 ASSERT_FALSE(s.Step()); | |
0 | 249 |
138 | 250 s.Reset(false); |
251 // Verify that we can get all rows again. | |
252 ASSERT_TRUE(s.Step()); | |
253 EXPECT_EQ(12, s.ColumnInt(0)); | |
254 EXPECT_FALSE(s.Step()); | |
0 | 255 |
138 | 256 s.Reset(true); |
257 ASSERT_FALSE(s.Step()); | |
258 } | |
259 } | |
0 | 260 } |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | |
267 /******************************************************************** | |
268 ** Tests from | |
269 ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/transaction_unittest.cc | |
270 ********************************************************************/ | |
271 | |
2655 | 272 namespace |
0 | 273 { |
2655 | 274 class SQLTransactionTest : public SQLConnectionTest |
0 | 275 { |
2655 | 276 public: |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
277 virtual void SetUp() ORTHANC_OVERRIDE |
2655 | 278 { |
279 SQLConnectionTest::SetUp(); | |
280 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
281 } | |
0 | 282 |
2655 | 283 // Returns the number of rows in table "foo". |
284 int CountFoo() | |
285 { | |
286 Statement count(db(), "SELECT count(*) FROM foo"); | |
287 count.Step(); | |
288 return count.ColumnInt(0); | |
289 } | |
290 }; | |
291 } | |
0 | 292 |
293 | |
294 TEST_F(SQLTransactionTest, Commit) { | |
295 { | |
138 | 296 Transaction t(db()); |
0 | 297 EXPECT_FALSE(t.IsOpen()); |
298 t.Begin(); | |
299 EXPECT_TRUE(t.IsOpen()); | |
300 | |
301 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); | |
302 | |
303 t.Commit(); | |
304 EXPECT_FALSE(t.IsOpen()); | |
305 } | |
306 | |
307 EXPECT_EQ(1, CountFoo()); | |
308 } | |
309 | |
310 TEST_F(SQLTransactionTest, Rollback) { | |
311 // Test some basic initialization, and that rollback runs when you exit the | |
312 // scope. | |
313 { | |
138 | 314 Transaction t(db()); |
0 | 315 EXPECT_FALSE(t.IsOpen()); |
316 t.Begin(); | |
317 EXPECT_TRUE(t.IsOpen()); | |
318 | |
319 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); | |
320 } | |
321 | |
322 // Nothing should have been committed since it was implicitly rolled back. | |
323 EXPECT_EQ(0, CountFoo()); | |
324 | |
325 // Test explicit rollback. | |
138 | 326 Transaction t2(db()); |
0 | 327 EXPECT_FALSE(t2.IsOpen()); |
328 t2.Begin(); | |
329 | |
330 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); | |
331 t2.Rollback(); | |
332 EXPECT_FALSE(t2.IsOpen()); | |
333 | |
334 // Nothing should have been committed since it was explicitly rolled back. | |
335 EXPECT_EQ(0, CountFoo()); | |
336 } | |
337 | |
338 // Rolling back any part of a transaction should roll back all of them. | |
339 TEST_F(SQLTransactionTest, NestedRollback) { | |
340 EXPECT_EQ(0, db().GetTransactionNesting()); | |
341 | |
342 // Outermost transaction. | |
343 { | |
138 | 344 Transaction outer(db()); |
0 | 345 outer.Begin(); |
346 EXPECT_EQ(1, db().GetTransactionNesting()); | |
347 | |
348 // The first inner one gets committed. | |
349 { | |
138 | 350 Transaction inner1(db()); |
0 | 351 inner1.Begin(); |
352 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); | |
353 EXPECT_EQ(2, db().GetTransactionNesting()); | |
354 | |
355 inner1.Commit(); | |
356 EXPECT_EQ(1, db().GetTransactionNesting()); | |
357 } | |
358 | |
359 // One row should have gotten inserted. | |
360 EXPECT_EQ(1, CountFoo()); | |
361 | |
362 // The second inner one gets rolled back. | |
363 { | |
138 | 364 Transaction inner2(db()); |
0 | 365 inner2.Begin(); |
366 EXPECT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (1, 2)")); | |
367 EXPECT_EQ(2, db().GetTransactionNesting()); | |
368 | |
369 inner2.Rollback(); | |
370 EXPECT_EQ(1, db().GetTransactionNesting()); | |
371 } | |
372 | |
373 // A third inner one will fail in Begin since one has already been rolled | |
374 // back. | |
375 EXPECT_EQ(1, db().GetTransactionNesting()); | |
376 { | |
138 | 377 Transaction inner3(db()); |
63 | 378 EXPECT_THROW(inner3.Begin(), OrthancException); |
0 | 379 EXPECT_EQ(1, db().GetTransactionNesting()); |
380 } | |
381 } | |
382 EXPECT_EQ(0, db().GetTransactionNesting()); | |
383 EXPECT_EQ(0, CountFoo()); | |
384 } |