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