annotate UnitTestsSources/SQLite.cpp @ 821:a7d757d5e93d

back to mainline
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 08 May 2014 17:15:54 +0200
parents 4689e400e0fa
children 3d6f9b7d0add
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #include "gtest/gtest.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 #include "../Core/Toolbox.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 #include "../Core/SQLite/Connection.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 #include <sqlite3.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9
63
f1d0470ff334 renaming unittests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
10 using namespace Orthanc;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 TEST(SQLite, Configuration)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 ASSERT_EQ(1, sqlite3_threadsafe());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 TEST(SQLite, Connection)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 {
803
4689e400e0fa directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 724
diff changeset
21 Toolbox::RemoveFile("UnitTestsResults/coucou");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 SQLite::Connection c;
803
4689e400e0fa directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 724
diff changeset
23 c.Open("UnitTestsResults/coucou");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 c.Execute("CREATE TABLE c(k INTEGER PRIMARY KEY AUTOINCREMENT, v INTEGER)");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 c.Execute("INSERT INTO c VALUES(NULL, 42);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 TEST(SQLite, StatementReferenceBasic)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 sqlite3* db;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 sqlite3_open(":memory:", &db);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 SQLite::StatementReference r(db, "SELECT * FROM sqlite_master");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 ASSERT_EQ(0u, r.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 SQLite::StatementReference r1(r);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 ASSERT_EQ(1u, r.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 ASSERT_EQ(0u, r1.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 SQLite::StatementReference r2(r);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 ASSERT_EQ(2u, r.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 ASSERT_EQ(0u, r1.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 ASSERT_EQ(0u, r2.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 SQLite::StatementReference r3(r2);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 ASSERT_EQ(3u, r.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 ASSERT_EQ(0u, r1.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 ASSERT_EQ(0u, r2.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 ASSERT_EQ(0u, r3.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 ASSERT_EQ(1u, r.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 ASSERT_EQ(0u, r1.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 SQLite::StatementReference r2(r);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 ASSERT_EQ(2u, r.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 ASSERT_EQ(0u, r1.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 ASSERT_EQ(0u, r2.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 ASSERT_EQ(1u, r.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 ASSERT_EQ(0u, r1.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 ASSERT_EQ(0u, r.GetReferenceCount());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 sqlite3_close(db);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 TEST(SQLite, StatementBasic)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 SQLite::Connection c;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 c.OpenInMemory();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 SQLite::Statement s(c, "SELECT * from sqlite_master");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 s.Run();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 for (unsigned int i = 0; i < 5; i++)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 SQLite::Statement cs(c, SQLITE_FROM_HERE, "SELECT * from sqlite_master");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 cs.Step();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 namespace
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 static bool destroyed;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 class MyFunc : public SQLite::IScalarFunction
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 public:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 MyFunc()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 destroyed = false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 virtual ~MyFunc()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 destroyed = true;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 virtual const char* GetName() const
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 return "MYFUNC";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 virtual unsigned int GetCardinality() const
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 return 2;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 virtual void Compute(SQLite::FunctionContext& context)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 context.SetIntResult(1000 + context.GetIntValue(0) * context.GetIntValue(1));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 class MyDelete : public SQLite::IScalarFunction
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 public:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 std::set<int> deleted_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 virtual const char* GetName() const
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 return "MYDELETE";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 virtual unsigned int GetCardinality() const
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 return 1;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 virtual void Compute(SQLite::FunctionContext& context)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 deleted_.insert(context.GetIntValue(0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 context.SetNullResult();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 TEST(SQLite, ScalarFunction)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 SQLite::Connection c;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 c.OpenInMemory();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 c.Register(new MyFunc());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 c.Execute("CREATE TABLE t(id INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 c.Execute("INSERT INTO t VALUES(NULL, 2, 3);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 c.Execute("INSERT INTO t VALUES(NULL, 4, 4);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 c.Execute("INSERT INTO t VALUES(NULL, 6, 5);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 SQLite::Statement t(c, "SELECT MYFUNC(v1, v2), v1, v2 FROM t");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 int i = 0;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 while (t.Step())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 ASSERT_EQ(t.ColumnInt(0), 1000 + t.ColumnInt(1) * t.ColumnInt(2));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 i++;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 ASSERT_EQ(3, i);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 ASSERT_FALSE(destroyed);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 ASSERT_TRUE(destroyed);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 TEST(SQLite, CascadedDeleteCallback)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 SQLite::Connection c;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 c.OpenInMemory();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 MyDelete *func = new MyDelete();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 c.Register(func);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 c.Execute("CREATE TABLE parent(id INTEGER PRIMARY KEY, dummy INTEGER);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 c.Execute("CREATE TABLE child("
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 " id INTEGER PRIMARY KEY, "
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 " parent INTEGER REFERENCES parent(id) ON DELETE CASCADE, "
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 " value INTEGER);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 c.Execute("CREATE TRIGGER childRemoved "
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 "AFTER DELETE ON child "
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 "FOR EACH ROW BEGIN "
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 " SELECT MYDELETE(old.value); "
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 "END;");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 c.Execute("INSERT INTO parent VALUES(42, 100);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 c.Execute("INSERT INTO parent VALUES(43, 101);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 c.Execute("INSERT INTO child VALUES(NULL, 42, 4200);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 c.Execute("INSERT INTO child VALUES(NULL, 42, 4201);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 c.Execute("INSERT INTO child VALUES(NULL, 43, 4300);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 c.Execute("INSERT INTO child VALUES(NULL, 43, 4301);");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 // The following command deletes "parent(43, 101)", then in turns
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 // "child(NULL, 43, 4300/4301)", then calls the MyDelete on 4300 and
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 // 4301
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 c.Execute("DELETE FROM parent WHERE dummy=101");
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 ASSERT_EQ(2u, func->deleted_.size());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 ASSERT_TRUE(func->deleted_.find(4300) != func->deleted_.end());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 ASSERT_TRUE(func->deleted_.find(4301) != func->deleted_.end());
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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 }
724
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
239
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
240
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
241 TEST(SQLite, Types)
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
242 {
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
243 SQLite::Connection c;
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
244 c.OpenInMemory();
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
245 c.Execute("CREATE TABLE a(id INTEGER PRIMARY KEY, value)");
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
246
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
247 {
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
248 SQLite::Statement s(c, std::string("SELECT * FROM a"));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
249 ASSERT_EQ(2, s.ColumnCount());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
250 ASSERT_FALSE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
251 }
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
252
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
253 {
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
254 SQLite::Statement s(c, SQLITE_FROM_HERE, std::string("SELECT * FROM a"));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
255 ASSERT_FALSE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
256 ASSERT_EQ("SELECT * FROM a", s.GetOriginalSQLStatement());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
257 }
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
258
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
259 {
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
260 SQLite::Statement s(c, SQLITE_FROM_HERE, "INSERT INTO a VALUES(NULL, ?);");
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
261 s.BindNull(0); ASSERT_TRUE(s.Run()); s.Reset();
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
262 s.BindBool(0, true); ASSERT_TRUE(s.Run()); s.Reset();
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
263 s.BindInt(0, 42); ASSERT_TRUE(s.Run()); s.Reset();
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
264 s.BindInt64(0, 42ll); ASSERT_TRUE(s.Run()); s.Reset();
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
265 s.BindDouble(0, 42.5); ASSERT_TRUE(s.Run()); s.Reset();
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
266 s.BindCString(0, "Hello"); ASSERT_TRUE(s.Run()); s.Reset();
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
267 s.BindBlob(0, "Hello", 5); ASSERT_TRUE(s.Run()); s.Reset();
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
268 }
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
269
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
270 {
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
271 SQLite::Statement s(c, SQLITE_FROM_HERE, std::string("SELECT * FROM a"));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
272 ASSERT_TRUE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
273 ASSERT_EQ(SQLite::COLUMN_TYPE_NULL, s.GetColumnType(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
274 ASSERT_TRUE(s.ColumnIsNull(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
275 ASSERT_TRUE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
276 ASSERT_EQ(SQLite::COLUMN_TYPE_INTEGER, s.GetColumnType(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
277 ASSERT_TRUE(s.ColumnBool(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
278 ASSERT_TRUE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
279 ASSERT_EQ(SQLite::COLUMN_TYPE_INTEGER, s.GetColumnType(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
280 ASSERT_EQ(42, s.ColumnInt(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
281 ASSERT_TRUE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
282 ASSERT_EQ(SQLite::COLUMN_TYPE_INTEGER, s.GetColumnType(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
283 ASSERT_EQ(42ll, s.ColumnInt64(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
284 ASSERT_TRUE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
285 ASSERT_EQ(SQLite::COLUMN_TYPE_FLOAT, s.GetColumnType(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
286 ASSERT_FLOAT_EQ(42.5, s.ColumnDouble(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
287 ASSERT_TRUE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
288 ASSERT_EQ(SQLite::COLUMN_TYPE_TEXT, s.GetColumnType(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
289 ASSERT_EQ("Hello", s.ColumnString(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
290 ASSERT_TRUE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
291 ASSERT_EQ(SQLite::COLUMN_TYPE_BLOB, s.GetColumnType(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
292 ASSERT_EQ(5, s.ColumnByteLength(1));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
293 ASSERT_TRUE(!memcmp("Hello", s.ColumnBlob(1), 5));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
294
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
295 std::string t;
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
296 ASSERT_TRUE(s.ColumnBlobAsString(1, &t));
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
297 ASSERT_EQ("Hello", t);
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
298
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
299 ASSERT_FALSE(s.Step());
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
300 }
96a2d2da0fee more sqlite tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
301 }