comparison UnitTestsSources/SQLiteTests.cpp @ 1023:226cfef3822e templating

integration mainline->templating
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 10 Jul 2014 11:42:32 +0200
parents dfc076546821
children 6e7e5ed91c2d
comparison
equal deleted inserted replaced
945:427a1f996b7b 1023:226cfef3822e
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "PrecompiledHeadersUnitTests.h"
34 #include "gtest/gtest.h"
35
36 #include "../Core/Toolbox.h"
37 #include "../Core/SQLite/Connection.h"
38 #include "../Core/SQLite/Statement.h"
39 #include "../Core/SQLite/Transaction.h"
40
41 #include <sqlite3.h>
42
43 using namespace Orthanc;
44
45
46 TEST(SQLite, Configuration)
47 {
48 ASSERT_EQ(1, sqlite3_threadsafe());
49 }
50
51
52 TEST(SQLite, Connection)
53 {
54 Toolbox::RemoveFile("UnitTestsResults/coucou");
55 SQLite::Connection c;
56 c.Open("UnitTestsResults/coucou");
57 c.Execute("CREATE TABLE c(k INTEGER PRIMARY KEY AUTOINCREMENT, v INTEGER)");
58 c.Execute("INSERT INTO c VALUES(NULL, 42);");
59 }
60
61
62 TEST(SQLite, StatementReferenceBasic)
63 {
64 sqlite3* db;
65 sqlite3_open(":memory:", &db);
66
67 {
68 SQLite::StatementReference r(db, "SELECT * FROM sqlite_master");
69 ASSERT_EQ(0u, r.GetReferenceCount());
70
71 {
72 SQLite::StatementReference r1(r);
73 ASSERT_EQ(1u, r.GetReferenceCount());
74 ASSERT_EQ(0u, r1.GetReferenceCount());
75
76 {
77 SQLite::StatementReference r2(r);
78 ASSERT_EQ(2u, r.GetReferenceCount());
79 ASSERT_EQ(0u, r1.GetReferenceCount());
80 ASSERT_EQ(0u, r2.GetReferenceCount());
81
82 SQLite::StatementReference r3(r2);
83 ASSERT_EQ(3u, r.GetReferenceCount());
84 ASSERT_EQ(0u, r1.GetReferenceCount());
85 ASSERT_EQ(0u, r2.GetReferenceCount());
86 ASSERT_EQ(0u, r3.GetReferenceCount());
87 }
88
89 ASSERT_EQ(1u, r.GetReferenceCount());
90 ASSERT_EQ(0u, r1.GetReferenceCount());
91
92 {
93 SQLite::StatementReference r2(r);
94 ASSERT_EQ(2u, r.GetReferenceCount());
95 ASSERT_EQ(0u, r1.GetReferenceCount());
96 ASSERT_EQ(0u, r2.GetReferenceCount());
97 }
98
99 ASSERT_EQ(1u, r.GetReferenceCount());
100 ASSERT_EQ(0u, r1.GetReferenceCount());
101 }
102
103 ASSERT_EQ(0u, r.GetReferenceCount());
104 }
105
106 sqlite3_close(db);
107 }
108
109 TEST(SQLite, StatementBasic)
110 {
111 SQLite::Connection c;
112 c.OpenInMemory();
113
114 SQLite::Statement s(c, "SELECT * from sqlite_master");
115 s.Run();
116
117 for (unsigned int i = 0; i < 5; i++)
118 {
119 SQLite::Statement cs(c, SQLITE_FROM_HERE, "SELECT * from sqlite_master");
120 cs.Step();
121 }
122 }
123
124
125 namespace
126 {
127 static bool destroyed;
128
129 class MyFunc : public SQLite::IScalarFunction
130 {
131 public:
132 MyFunc()
133 {
134 destroyed = false;
135 }
136
137 virtual ~MyFunc()
138 {
139 destroyed = true;
140 }
141
142 virtual const char* GetName() const
143 {
144 return "MYFUNC";
145 }
146
147 virtual unsigned int GetCardinality() const
148 {
149 return 2;
150 }
151
152 virtual void Compute(SQLite::FunctionContext& context)
153 {
154 context.SetIntResult(1000 + context.GetIntValue(0) * context.GetIntValue(1));
155 }
156 };
157
158 class MyDelete : public SQLite::IScalarFunction
159 {
160 public:
161 std::set<int> deleted_;
162
163 virtual const char* GetName() const
164 {
165 return "MYDELETE";
166 }
167
168 virtual unsigned int GetCardinality() const
169 {
170 return 1;
171 }
172
173 virtual void Compute(SQLite::FunctionContext& context)
174 {
175 deleted_.insert(context.GetIntValue(0));
176 context.SetNullResult();
177 }
178 };
179 }
180
181 TEST(SQLite, ScalarFunction)
182 {
183 {
184 SQLite::Connection c;
185 c.OpenInMemory();
186 c.Register(new MyFunc());
187 c.Execute("CREATE TABLE t(id INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER);");
188 c.Execute("INSERT INTO t VALUES(NULL, 2, 3);");
189 c.Execute("INSERT INTO t VALUES(NULL, 4, 4);");
190 c.Execute("INSERT INTO t VALUES(NULL, 6, 5);");
191 SQLite::Statement t(c, "SELECT MYFUNC(v1, v2), v1, v2 FROM t");
192 int i = 0;
193 while (t.Step())
194 {
195 ASSERT_EQ(t.ColumnInt(0), 1000 + t.ColumnInt(1) * t.ColumnInt(2));
196 i++;
197 }
198 ASSERT_EQ(3, i);
199 ASSERT_FALSE(destroyed);
200 }
201 ASSERT_TRUE(destroyed);
202 }
203
204 TEST(SQLite, CascadedDeleteCallback)
205 {
206 SQLite::Connection c;
207 c.OpenInMemory();
208 MyDelete *func = new MyDelete();
209 c.Register(func);
210 c.Execute("CREATE TABLE parent(id INTEGER PRIMARY KEY, dummy INTEGER);");
211 c.Execute("CREATE TABLE child("
212 " id INTEGER PRIMARY KEY, "
213 " parent INTEGER REFERENCES parent(id) ON DELETE CASCADE, "
214 " value INTEGER);");
215 c.Execute("CREATE TRIGGER childRemoved "
216 "AFTER DELETE ON child "
217 "FOR EACH ROW BEGIN "
218 " SELECT MYDELETE(old.value); "
219 "END;");
220
221 c.Execute("INSERT INTO parent VALUES(42, 100);");
222 c.Execute("INSERT INTO parent VALUES(43, 101);");
223
224 c.Execute("INSERT INTO child VALUES(NULL, 42, 4200);");
225 c.Execute("INSERT INTO child VALUES(NULL, 42, 4201);");
226
227 c.Execute("INSERT INTO child VALUES(NULL, 43, 4300);");
228 c.Execute("INSERT INTO child VALUES(NULL, 43, 4301);");
229
230 // The following command deletes "parent(43, 101)", then in turns
231 // "child(NULL, 43, 4300/4301)", then calls the MyDelete on 4300 and
232 // 4301
233 c.Execute("DELETE FROM parent WHERE dummy=101");
234
235 ASSERT_EQ(2u, func->deleted_.size());
236 ASSERT_TRUE(func->deleted_.find(4300) != func->deleted_.end());
237 ASSERT_TRUE(func->deleted_.find(4301) != func->deleted_.end());
238 }
239
240
241 TEST(SQLite, EmptyTransactions)
242 {
243 try
244 {
245 SQLite::Connection c;
246 c.OpenInMemory();
247
248 c.Execute("CREATE TABLE a(id INTEGER PRIMARY KEY);");
249 c.Execute("INSERT INTO a VALUES(NULL)");
250
251 {
252 SQLite::Transaction t(c);
253 t.Begin();
254 {
255 SQLite::Statement s(c, SQLITE_FROM_HERE, "SELECT * FROM a");
256 s.Step();
257 }
258 //t.Commit();
259 }
260
261 {
262 SQLite::Statement s(c, SQLITE_FROM_HERE, "SELECT * FROM a");
263 s.Step();
264 }
265 }
266 catch (OrthancException& e)
267 {
268 fprintf(stderr, "Exception: [%s]\n", e.What());
269 throw e;
270 }
271 }
272
273
274 TEST(SQLite, Types)
275 {
276 SQLite::Connection c;
277 c.OpenInMemory();
278 c.Execute("CREATE TABLE a(id INTEGER PRIMARY KEY, value)");
279
280 {
281 SQLite::Statement s(c, std::string("SELECT * FROM a"));
282 ASSERT_EQ(2, s.ColumnCount());
283 ASSERT_FALSE(s.Step());
284 }
285
286 {
287 SQLite::Statement s(c, SQLITE_FROM_HERE, std::string("SELECT * FROM a"));
288 ASSERT_FALSE(s.Step());
289 ASSERT_EQ("SELECT * FROM a", s.GetOriginalSQLStatement());
290 }
291
292 {
293 SQLite::Statement s(c, SQLITE_FROM_HERE, "INSERT INTO a VALUES(NULL, ?);");
294 s.BindNull(0); ASSERT_TRUE(s.Run()); s.Reset();
295 s.BindBool(0, true); ASSERT_TRUE(s.Run()); s.Reset();
296 s.BindInt(0, 42); ASSERT_TRUE(s.Run()); s.Reset();
297 s.BindInt64(0, 42ll); ASSERT_TRUE(s.Run()); s.Reset();
298 s.BindDouble(0, 42.5); ASSERT_TRUE(s.Run()); s.Reset();
299 s.BindCString(0, "Hello"); ASSERT_TRUE(s.Run()); s.Reset();
300 s.BindBlob(0, "Hello", 5); ASSERT_TRUE(s.Run()); s.Reset();
301 }
302
303 {
304 SQLite::Statement s(c, SQLITE_FROM_HERE, std::string("SELECT * FROM a"));
305 ASSERT_TRUE(s.Step());
306 ASSERT_EQ(SQLite::COLUMN_TYPE_NULL, s.GetColumnType(1));
307 ASSERT_TRUE(s.ColumnIsNull(1));
308 ASSERT_TRUE(s.Step());
309 ASSERT_EQ(SQLite::COLUMN_TYPE_INTEGER, s.GetColumnType(1));
310 ASSERT_TRUE(s.ColumnBool(1));
311 ASSERT_TRUE(s.Step());
312 ASSERT_EQ(SQLite::COLUMN_TYPE_INTEGER, s.GetColumnType(1));
313 ASSERT_EQ(42, s.ColumnInt(1));
314 ASSERT_TRUE(s.Step());
315 ASSERT_EQ(SQLite::COLUMN_TYPE_INTEGER, s.GetColumnType(1));
316 ASSERT_EQ(42ll, s.ColumnInt64(1));
317 ASSERT_TRUE(s.Step());
318 ASSERT_EQ(SQLite::COLUMN_TYPE_FLOAT, s.GetColumnType(1));
319 ASSERT_DOUBLE_EQ(42.5, s.ColumnDouble(1));
320 ASSERT_TRUE(s.Step());
321 ASSERT_EQ(SQLite::COLUMN_TYPE_TEXT, s.GetColumnType(1));
322 ASSERT_EQ("Hello", s.ColumnString(1));
323 ASSERT_TRUE(s.Step());
324 ASSERT_EQ(SQLite::COLUMN_TYPE_BLOB, s.GetColumnType(1));
325 ASSERT_EQ(5, s.ColumnByteLength(1));
326 ASSERT_TRUE(!memcmp("Hello", s.ColumnBlob(1), 5));
327
328 std::string t;
329 ASSERT_TRUE(s.ColumnBlobAsString(1, &t));
330 ASSERT_EQ("Hello", t);
331
332 ASSERT_FALSE(s.Step());
333 }
334 }