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