Mercurial > hg > orthanc
annotate UnitTests/SQLite.cpp @ 2744:d9a179f23b47 Orthanc-0.6.0
close old branch
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jul 2018 09:39:17 +0200 |
parents | 0e97abc7b950 |
children |
rev | line source |
---|---|
0 | 1 #include "gtest/gtest.h" |
2 | |
3 #include "../Core/Toolbox.h" | |
4 #include "../Core/SQLite/Connection.h" | |
5 #include "../Core/SQLite/Statement.h" | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
6 #include "../Core/SQLite/Transaction.h" |
0 | 7 |
8 #include <sqlite3.h> | |
9 | |
63 | 10 using namespace Orthanc; |
0 | 11 |
12 | |
13 TEST(SQLite, Configuration) | |
14 { | |
15 ASSERT_EQ(1, sqlite3_threadsafe()); | |
16 } | |
17 | |
18 | |
19 TEST(SQLite, Connection) | |
20 { | |
21 Toolbox::RemoveFile("coucou"); | |
22 SQLite::Connection c; | |
23 c.Open("coucou"); | |
24 c.Execute("CREATE TABLE c(k INTEGER PRIMARY KEY AUTOINCREMENT, v INTEGER)"); | |
25 c.Execute("INSERT INTO c VALUES(NULL, 42);"); | |
26 } | |
27 | |
28 | |
29 TEST(SQLite, StatementReferenceBasic) | |
30 { | |
31 sqlite3* db; | |
32 sqlite3_open(":memory:", &db); | |
33 | |
34 { | |
35 SQLite::StatementReference r(db, "SELECT * FROM sqlite_master"); | |
36 ASSERT_EQ(0u, r.GetReferenceCount()); | |
37 | |
38 { | |
39 SQLite::StatementReference r1(r); | |
40 ASSERT_EQ(1u, r.GetReferenceCount()); | |
41 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
42 | |
43 { | |
44 SQLite::StatementReference r2(r); | |
45 ASSERT_EQ(2u, r.GetReferenceCount()); | |
46 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
47 ASSERT_EQ(0u, r2.GetReferenceCount()); | |
48 | |
49 SQLite::StatementReference r3(r2); | |
50 ASSERT_EQ(3u, r.GetReferenceCount()); | |
51 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
52 ASSERT_EQ(0u, r2.GetReferenceCount()); | |
53 ASSERT_EQ(0u, r3.GetReferenceCount()); | |
54 } | |
55 | |
56 ASSERT_EQ(1u, r.GetReferenceCount()); | |
57 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
58 | |
59 { | |
60 SQLite::StatementReference r2(r); | |
61 ASSERT_EQ(2u, r.GetReferenceCount()); | |
62 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
63 ASSERT_EQ(0u, r2.GetReferenceCount()); | |
64 } | |
65 | |
66 ASSERT_EQ(1u, r.GetReferenceCount()); | |
67 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
68 } | |
69 | |
70 ASSERT_EQ(0u, r.GetReferenceCount()); | |
71 } | |
72 | |
73 sqlite3_close(db); | |
74 } | |
75 | |
76 TEST(SQLite, StatementBasic) | |
77 { | |
78 SQLite::Connection c; | |
79 c.OpenInMemory(); | |
80 | |
81 SQLite::Statement s(c, "SELECT * from sqlite_master"); | |
82 s.Run(); | |
83 | |
84 for (unsigned int i = 0; i < 5; i++) | |
85 { | |
86 SQLite::Statement cs(c, SQLITE_FROM_HERE, "SELECT * from sqlite_master"); | |
87 cs.Step(); | |
88 } | |
89 } | |
90 | |
91 | |
92 namespace | |
93 { | |
94 static bool destroyed; | |
95 | |
96 class MyFunc : public SQLite::IScalarFunction | |
97 { | |
98 public: | |
99 MyFunc() | |
100 { | |
101 destroyed = false; | |
102 } | |
103 | |
104 virtual ~MyFunc() | |
105 { | |
106 destroyed = true; | |
107 } | |
108 | |
109 virtual const char* GetName() const | |
110 { | |
111 return "MYFUNC"; | |
112 } | |
113 | |
114 virtual unsigned int GetCardinality() const | |
115 { | |
116 return 2; | |
117 } | |
118 | |
119 virtual void Compute(SQLite::FunctionContext& context) | |
120 { | |
121 context.SetIntResult(1000 + context.GetIntValue(0) * context.GetIntValue(1)); | |
122 } | |
123 }; | |
124 | |
125 class MyDelete : public SQLite::IScalarFunction | |
126 { | |
127 public: | |
128 std::set<int> deleted_; | |
129 | |
130 virtual const char* GetName() const | |
131 { | |
132 return "MYDELETE"; | |
133 } | |
134 | |
135 virtual unsigned int GetCardinality() const | |
136 { | |
137 return 1; | |
138 } | |
139 | |
140 virtual void Compute(SQLite::FunctionContext& context) | |
141 { | |
142 deleted_.insert(context.GetIntValue(0)); | |
143 context.SetNullResult(); | |
144 } | |
145 }; | |
146 } | |
147 | |
148 TEST(SQLite, ScalarFunction) | |
149 { | |
150 { | |
151 SQLite::Connection c; | |
152 c.OpenInMemory(); | |
153 c.Register(new MyFunc()); | |
154 c.Execute("CREATE TABLE t(id INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER);"); | |
155 c.Execute("INSERT INTO t VALUES(NULL, 2, 3);"); | |
156 c.Execute("INSERT INTO t VALUES(NULL, 4, 4);"); | |
157 c.Execute("INSERT INTO t VALUES(NULL, 6, 5);"); | |
158 SQLite::Statement t(c, "SELECT MYFUNC(v1, v2), v1, v2 FROM t"); | |
159 int i = 0; | |
160 while (t.Step()) | |
161 { | |
162 ASSERT_EQ(t.ColumnInt(0), 1000 + t.ColumnInt(1) * t.ColumnInt(2)); | |
163 i++; | |
164 } | |
165 ASSERT_EQ(3, i); | |
166 ASSERT_FALSE(destroyed); | |
167 } | |
168 ASSERT_TRUE(destroyed); | |
169 } | |
170 | |
171 TEST(SQLite, CascadedDeleteCallback) | |
172 { | |
173 SQLite::Connection c; | |
174 c.OpenInMemory(); | |
175 MyDelete *func = new MyDelete(); | |
176 c.Register(func); | |
177 c.Execute("CREATE TABLE parent(id INTEGER PRIMARY KEY, dummy INTEGER);"); | |
178 c.Execute("CREATE TABLE child(" | |
179 " id INTEGER PRIMARY KEY, " | |
180 " parent INTEGER REFERENCES parent(id) ON DELETE CASCADE, " | |
181 " value INTEGER);"); | |
182 c.Execute("CREATE TRIGGER childRemoved " | |
183 "AFTER DELETE ON child " | |
184 "FOR EACH ROW BEGIN " | |
185 " SELECT MYDELETE(old.value); " | |
186 "END;"); | |
187 | |
188 c.Execute("INSERT INTO parent VALUES(42, 100);"); | |
189 c.Execute("INSERT INTO parent VALUES(43, 101);"); | |
190 | |
191 c.Execute("INSERT INTO child VALUES(NULL, 42, 4200);"); | |
192 c.Execute("INSERT INTO child VALUES(NULL, 42, 4201);"); | |
193 | |
194 c.Execute("INSERT INTO child VALUES(NULL, 43, 4300);"); | |
195 c.Execute("INSERT INTO child VALUES(NULL, 43, 4301);"); | |
196 | |
197 // The following command deletes "parent(43, 101)", then in turns | |
198 // "child(NULL, 43, 4300/4301)", then calls the MyDelete on 4300 and | |
199 // 4301 | |
200 c.Execute("DELETE FROM parent WHERE dummy=101"); | |
201 | |
202 ASSERT_EQ(2u, func->deleted_.size()); | |
203 ASSERT_TRUE(func->deleted_.find(4300) != func->deleted_.end()); | |
204 ASSERT_TRUE(func->deleted_.find(4301) != func->deleted_.end()); | |
205 } | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
206 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
207 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
208 TEST(SQLite, EmptyTransactions) |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
209 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
210 try |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
211 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
212 SQLite::Connection c; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
213 c.OpenInMemory(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
214 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
215 c.Execute("CREATE TABLE a(id INTEGER PRIMARY KEY);"); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
216 c.Execute("INSERT INTO a VALUES(NULL)"); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
217 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
218 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
219 SQLite::Transaction t(c); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
220 t.Begin(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
221 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
222 SQLite::Statement s(c, SQLITE_FROM_HERE, "SELECT * FROM a"); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
223 s.Step(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
224 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
225 //t.Commit(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
226 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
227 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
228 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
229 SQLite::Statement s(c, SQLITE_FROM_HERE, "SELECT * FROM a"); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
230 s.Step(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
231 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
232 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
233 catch (OrthancException& e) |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
234 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
235 fprintf(stderr, "Exception: [%s]\n", e.What()); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
236 throw e; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
237 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
238 } |