-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_Database.cpp
291 lines (243 loc) · 7.19 KB
/
test_Database.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//***************************************************************************
// Copyright 2007-2023 Universidade do Porto - Faculdade de Engenharia *
// Laboratório de Sistemas e Tecnologia Subaquática (LSTS) *
//***************************************************************************
// This file is part of DUNE: Unified Navigation Environment. *
// *
// Commercial Licence Usage *
// Licencees holding valid commercial DUNE licences may use this file in *
// accordance with the commercial licence agreement provided with the *
// Software or, alternatively, in accordance with the terms contained in a *
// written agreement between you and Faculdade de Engenharia da *
// Universidade do Porto. For licensing terms, conditions, and further *
// information contact [email protected]. *
// *
// Modified European Union Public Licence - EUPL v.1.1 Usage *
// Alternatively, this file may be used under the terms of the Modified *
// EUPL, Version 1.1 only (the "Licence"), appearing in the file LICENCE.md *
// included in the packaging of this file. You may not use this work *
// except in compliance with the Licence. Unless required by applicable *
// law or agreed to in writing, software distributed under the Licence is *
// distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF *
// ANY KIND, either express or implied. See the Licence for the specific *
// language governing permissions and limitations at *
// https://github.com/LSTS/dune/blob/master/LICENCE.md and *
// http://ec.europa.eu/idabc/eupl.html. *
//***************************************************************************
// Author: Eduardo Marques *
//***************************************************************************
// Test program for DUNE::Database package. *
//***************************************************************************
// ISO C++ headers
#include <iostream>
#include <sstream>
#include "Test.hpp"
// DUNE headers
#include <DUNE/Database.hpp>
#include <DUNE/Math.hpp>
using namespace DUNE::Database;
using namespace DUNE;
static bool
valuesAreOk(int id, double value, const std::string& name, std::pair<bool, Blob> data)
{
std::stringstream expected_name;
expected_name << "NAME" << id;
if (expected_name.str() != name)
return false;
if (Math::round(10 * value) != id)
return false;
if (!data.first && (id % 2) == 0)
return true;
if ((int)data.second.size() != id)
return false;
int j = 0;
for (j = 0; j < id; ++j)
if (data.second[j] != j)
return false;
return true;
}
int
main(int argc, char** argv)
{
Test test("DUNE::Database");
Connection db(Connection::CF_CREATE);
bool passed = false;
// Begin transaction test.
try
{
db.beginTransaction();
passed = true;
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
test.boolean("Connection::beginTransaction()", passed);
// Table drop test.
passed = false;
try
{
db.execute("DROP TABLE IF EXISTS TEST");
passed = true;
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
test.boolean("Connection::execute() -- DROP TABLE", passed);
// Table creation test.
passed = false;
try
{
db.execute("CREATE TABLE TEST ( ID INTEGER PRIMARY KEY NOT NULL, VALUE REAL NOT NULL, NAME VARCHAR2 NOT NULL, DATA BLOB)");
passed = true;
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
test.boolean("Connection::execute() -- CREATE TABLE", passed);
const int N = 10;
// Insertion statement test.
passed = false;
try
{
Statement insertion("INSERT INTO TEST VALUES(?,?,?,?)", db);
for (int i = 0; i < N; i++)
{
int id = i;
double value = 0.1 * (double)i;
std::stringstream name;
Blob blob(i);
name << "NAME" << i;
for (int j = 0; j < i; j++)
blob[j] = j;
insertion << id
<< value
<< name.str();
if ((i % 2) == 0)
insertion << Null(); // NULL column
else
insertion << blob;
insertion.execute();
}
passed = true;
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
test.boolean("Statement -- INSERT", passed);
// COMMIT test
passed = false;
try
{
db.commit();
passed = true;
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
test.boolean("Connection::commit()", passed);
// SELECT test (1)
passed = false;
try
{
Statement query("SELECT ID, VALUE, NAME, DATA FROM TEST ORDER BY ID", db);
int expected_id = 0;
int c = 0;
while (query.execute())
{
int id;
double value;
std::string name;
std::pair<bool, Blob> blob;
query >> id >> value >> name >> blob;
if (!valuesAreOk(id, value, name, blob))
break;
++c;
}
if (c == N)
passed = true;
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
test.boolean("Statement -- SELECT (1)", passed);
// SELECT test (2)
passed = false;
try
{
Statement query("SELECT VALUE, NAME, DATA FROM TEST WHERE ID = ?", db);
int c = 0;
for (int id = 0; id < N; ++id)
{
query << id;
if (!query.execute())
break;
double value;
std::string name;
std::pair<bool, Blob> blob;
query >> value >> name >> blob;
if (!valuesAreOk(id, value, name, blob))
break;
if (query.execute())
break;
++c;
}
if (c == N)
passed = true;
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
test.boolean("Statement -- SELECT (2)", passed);
// ROLLBACK test
passed = false;
try
{
double value;
Statement update("UPDATE TEST SET VALUE = -VALUE", db);
Statement query("SELECT SUM(VALUE) FROM TEST", db);
db.beginTransaction();
update.execute();
query.execute();
query >> value;
if (value > 0)
throw std::runtime_error("unexpected result");
query.reset();
db.rollback();
query.execute();
query >> value;
if (value < 0)
throw std::runtime_error("unexpected result");
passed = true;
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
test.boolean("Statement -- ROLLBACK", passed);
// DELETE test
passed = false;
try
{
int count;
db.execute("UPDATE TEST SET VALUE=0", &count);
if (count != N)
throw std::runtime_error("wrong number of rows");
db.execute("DELETE FROM TEST", &count);
if (count != N)
throw std::runtime_error("wrong number of rows");
passed = true;
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
test.boolean("Connection -- Row count", passed);
return 0;
}