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