Mercurial > hg > orthanc
annotate UnitTestsSources/SQLiteChromiumTests.cpp @ 2412:cad393b41bc3
handy shortcuts in DicomMap
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 29 Sep 2017 11:35:45 +0200 |
parents | a3a65de1840f |
children | 878b59270859 |
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 |
2244
a3a65de1840f
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
5 * Copyright (C) 2017 Osimis, 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 | |
54 class SQLConnectionTest : public testing::Test | |
55 { | |
56 public: | |
57 SQLConnectionTest() | |
58 { | |
59 } | |
60 | |
61 virtual ~SQLConnectionTest() | |
62 { | |
63 } | |
64 | |
65 virtual void SetUp() | |
66 { | |
67 db_.OpenInMemory(); | |
68 } | |
69 | |
70 virtual void TearDown() | |
71 { | |
72 db_.Close(); | |
73 } | |
74 | |
138 | 75 Connection& db() |
0 | 76 { |
77 return db_; | |
78 } | |
79 | |
80 private: | |
138 | 81 Connection db_; |
0 | 82 }; |
83 | |
84 | |
85 | |
86 TEST_F(SQLConnectionTest, Execute) | |
87 { | |
88 // Valid statement should return true. | |
89 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
90 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); | |
91 | |
92 // Invalid statement should fail. | |
93 ASSERT_EQ(SQLITE_ERROR, | |
94 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); | |
95 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); | |
96 } | |
97 | |
98 TEST_F(SQLConnectionTest, ExecuteWithErrorCode) { | |
99 ASSERT_EQ(SQLITE_OK, | |
100 db().ExecuteAndReturnErrorCode("CREATE TABLE foo (a, b)")); | |
101 ASSERT_EQ(SQLITE_ERROR, | |
102 db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE")); | |
103 ASSERT_EQ(SQLITE_ERROR, | |
104 db().ExecuteAndReturnErrorCode( | |
138 | 105 "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)")); |
0 | 106 } |
107 | |
108 TEST_F(SQLConnectionTest, CachedStatement) { | |
138 | 109 StatementId id1("foo", 12); |
0 | 110 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
111 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); | |
112 | |
113 // Create a new cached statement. | |
114 { | |
138 | 115 Statement s(db(), id1, "SELECT a FROM foo"); |
0 | 116 ASSERT_TRUE(s.Step()); |
117 EXPECT_EQ(12, s.ColumnInt(0)); | |
118 } | |
119 | |
120 // The statement should be cached still. | |
121 EXPECT_TRUE(db().HasCachedStatement(id1)); | |
122 | |
123 { | |
124 // Get the same statement using different SQL. This should ignore our | |
125 // SQL and use the cached one (so it will be valid). | |
138 | 126 Statement s(db(), id1, "something invalid("); |
0 | 127 ASSERT_TRUE(s.Step()); |
128 EXPECT_EQ(12, s.ColumnInt(0)); | |
129 } | |
130 | |
131 // Make sure other statements aren't marked as cached. | |
132 EXPECT_FALSE(db().HasCachedStatement(SQLITE_FROM_HERE)); | |
133 } | |
134 | |
135 TEST_F(SQLConnectionTest, IsSQLValidTest) { | |
136 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
137 ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo")); | |
138 ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo")); | |
139 } | |
140 | |
141 | |
142 | |
143 TEST_F(SQLConnectionTest, DoesStuffExist) { | |
144 // Test DoesTableExist. | |
145 EXPECT_FALSE(db().DoesTableExist("foo")); | |
146 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
147 EXPECT_TRUE(db().DoesTableExist("foo")); | |
148 | |
149 // Should be case sensitive. | |
150 EXPECT_FALSE(db().DoesTableExist("FOO")); | |
151 | |
152 // Test DoesColumnExist. | |
153 EXPECT_FALSE(db().DoesColumnExist("foo", "bar")); | |
154 EXPECT_TRUE(db().DoesColumnExist("foo", "a")); | |
155 | |
156 // Testing for a column on a nonexistent table. | |
157 EXPECT_FALSE(db().DoesColumnExist("bar", "b")); | |
158 } | |
159 | |
160 TEST_F(SQLConnectionTest, GetLastInsertRowId) { | |
161 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)")); | |
162 | |
163 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); | |
164 | |
165 // Last insert row ID should be valid. | |
166 int64_t row = db().GetLastInsertRowId(); | |
167 EXPECT_LT(0, row); | |
168 | |
169 // It should be the primary key of the row we just inserted. | |
138 | 170 Statement s(db(), "SELECT value FROM foo WHERE id=?"); |
0 | 171 s.BindInt64(0, row); |
172 ASSERT_TRUE(s.Step()); | |
173 EXPECT_EQ(12, s.ColumnInt(0)); | |
174 } | |
175 | |
176 TEST_F(SQLConnectionTest, Rollback) { | |
177 ASSERT_TRUE(db().BeginTransaction()); | |
178 ASSERT_TRUE(db().BeginTransaction()); | |
179 EXPECT_EQ(2, db().GetTransactionNesting()); | |
180 db().RollbackTransaction(); | |
181 EXPECT_FALSE(db().CommitTransaction()); | |
182 EXPECT_TRUE(db().BeginTransaction()); | |
183 } | |
184 | |
185 | |
186 | |
187 | |
188 /******************************************************************** | |
189 ** Tests from | |
190 ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/statement_unittest.cc | |
191 ********************************************************************/ | |
192 | |
138 | 193 namespace Orthanc |
0 | 194 { |
138 | 195 namespace SQLite |
196 { | |
197 class SQLStatementTest : public SQLConnectionTest | |
198 { | |
199 }; | |
0 | 200 |
138 | 201 TEST_F(SQLStatementTest, Run) { |
202 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
203 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); | |
0 | 204 |
138 | 205 Statement s(db(), "SELECT b FROM foo WHERE a=?"); |
206 // Stepping it won't work since we haven't bound the value. | |
207 EXPECT_FALSE(s.Step()); | |
0 | 208 |
138 | 209 // Run should fail since this produces output, and we should use Step(). This |
210 // gets a bit wonky since sqlite says this is OK so succeeded is set. | |
211 s.Reset(true); | |
212 s.BindInt(0, 3); | |
213 EXPECT_FALSE(s.Run()); | |
214 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode()); | |
0 | 215 |
138 | 216 // Resetting it should put it back to the previous state (not runnable). |
217 s.Reset(true); | |
0 | 218 |
138 | 219 // Binding and stepping should produce one row. |
220 s.BindInt(0, 3); | |
221 EXPECT_TRUE(s.Step()); | |
222 EXPECT_EQ(12, s.ColumnInt(0)); | |
223 EXPECT_FALSE(s.Step()); | |
224 } | |
0 | 225 |
138 | 226 TEST_F(SQLStatementTest, BasicErrorCallback) { |
227 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)")); | |
228 // Insert in the foo table the primary key. It is an error to insert | |
229 // something other than an number. This error causes the error callback | |
230 // handler to be called with SQLITE_MISMATCH as error code. | |
231 Statement s(db(), "INSERT INTO foo (a) VALUES (?)"); | |
232 s.BindCString(0, "bad bad"); | |
233 EXPECT_THROW(s.Run(), OrthancException); | |
234 } | |
0 | 235 |
138 | 236 TEST_F(SQLStatementTest, Reset) { |
237 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
238 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)")); | |
239 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)")); | |
0 | 240 |
138 | 241 Statement s(db(), "SELECT b FROM foo WHERE a = ? "); |
242 s.BindInt(0, 3); | |
243 ASSERT_TRUE(s.Step()); | |
244 EXPECT_EQ(12, s.ColumnInt(0)); | |
245 ASSERT_FALSE(s.Step()); | |
0 | 246 |
138 | 247 s.Reset(false); |
248 // Verify that we can get all rows again. | |
249 ASSERT_TRUE(s.Step()); | |
250 EXPECT_EQ(12, s.ColumnInt(0)); | |
251 EXPECT_FALSE(s.Step()); | |
0 | 252 |
138 | 253 s.Reset(true); |
254 ASSERT_FALSE(s.Step()); | |
255 } | |
256 } | |
0 | 257 } |
258 | |
259 | |
260 | |
261 | |
262 | |
263 | |
264 /******************************************************************** | |
265 ** Tests from | |
266 ** http://src.chromium.org/viewvc/chrome/trunk/src/sql/transaction_unittest.cc | |
267 ********************************************************************/ | |
268 | |
269 class SQLTransactionTest : public SQLConnectionTest | |
270 { | |
271 public: | |
272 virtual void SetUp() | |
273 { | |
274 SQLConnectionTest::SetUp(); | |
275 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
276 } | |
277 | |
278 // Returns the number of rows in table "foo". | |
279 int CountFoo() | |
280 { | |
138 | 281 Statement count(db(), "SELECT count(*) FROM foo"); |
0 | 282 count.Step(); |
283 return count.ColumnInt(0); | |
284 } | |
285 }; | |
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 } |