forked from fnc12/sqlite_orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests2.cpp
More file actions
405 lines (333 loc) · 12.8 KB
/
tests2.cpp
File metadata and controls
405 lines (333 loc) · 12.8 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <sqlite_orm/sqlite_orm.h>
#include <catch2/catch.hpp>
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
#include <optional> // std::optional
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
using namespace sqlite_orm;
TEST_CASE("Empty storage") {
auto storage = make_storage("empty.sqlite");
storage.table_exists("table");
}
TEST_CASE("Remove") {
struct Object {
int id;
std::string name;
};
{
auto storage = make_storage(
"test_remove.sqlite",
make_table("objects", make_column("id", &Object::id, primary_key()), make_column("name", &Object::name)));
storage.sync_schema();
storage.remove_all<Object>();
auto id1 = storage.insert(Object{0, "Skillet"});
REQUIRE(storage.count<Object>() == 1);
storage.remove<Object>(id1);
REQUIRE(storage.count<Object>() == 0);
}
{
auto storage = make_storage("test_remove.sqlite",
make_table("objects",
make_column("id", &Object::id),
make_column("name", &Object::name),
primary_key(&Object::id)));
storage.sync_schema();
storage.remove_all<Object>();
auto id1 = storage.insert(Object{0, "Skillet"});
REQUIRE(storage.count<Object>() == 1);
storage.remove<Object>(id1);
REQUIRE(storage.count<Object>() == 0);
}
{
auto storage = make_storage("",
make_table("objects",
make_column("id", &Object::id),
make_column("name", &Object::name),
primary_key(&Object::id, &Object::name)));
storage.sync_schema();
storage.replace(Object{1, "Skillet"});
assert(storage.count<Object>() == 1);
storage.remove<Object>(1, "Skillet");
REQUIRE(storage.count<Object>() == 0);
storage.replace(Object{1, "Skillet"});
storage.replace(Object{2, "Paul Cless"});
REQUIRE(storage.count<Object>() == 2);
storage.remove<Object>(1, "Skillet");
REQUIRE(storage.count<Object>() == 1);
}
}
TEST_CASE("Select") {
sqlite3 *db;
auto dbFileName = "test.db";
auto rc = sqlite3_open(dbFileName, &db);
assert(rc == SQLITE_OK);
auto sql = "CREATE TABLE IF NOT EXISTS WORDS("
"ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"CURRENT_WORD TEXT NOT NULL,"
"BEFORE_WORD TEXT NOT NULL,"
"AFTER_WORD TEXT NOT NULL,"
"OCCURANCES INT NOT NULL);";
char *errMsg = nullptr;
rc = sqlite3_exec(db, sql, nullptr, nullptr, &errMsg);
REQUIRE(rc == SQLITE_OK);
sqlite3_stmt *stmt;
// delete previous words. This command is excess in travis or other docker based CI tools
// but it is required on local machine
sql = "DELETE FROM WORDS";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
REQUIRE(rc == SQLITE_OK);
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE) {
// cout << sqlite3_errmsg(db) << endl;
throw std::runtime_error(sqlite3_errmsg(db));
}
sqlite3_finalize(stmt);
sql = "INSERT INTO WORDS (CURRENT_WORD, BEFORE_WORD, AFTER_WORD, OCCURANCES) VALUES(?, ?, ?, ?)";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
REQUIRE(rc == SQLITE_OK);
// INSERT [ ID, 'best', 'behaviour', 'hey', 5 ]
sqlite3_bind_text(stmt, 1, "best", -1, nullptr);
sqlite3_bind_text(stmt, 2, "behaviour", -1, nullptr);
sqlite3_bind_text(stmt, 3, "hey", -1, nullptr);
sqlite3_bind_int(stmt, 4, 5);
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE) {
// cout << sqlite3_errmsg(db) << endl;
throw std::runtime_error(sqlite3_errmsg(db));
}
sqlite3_finalize(stmt);
auto firstId = sqlite3_last_insert_rowid(db);
// INSERT [ ID, 'corruption', 'blood', 'brothers', 15 ]
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
REQUIRE(rc == SQLITE_OK);
sqlite3_bind_text(stmt, 1, "corruption", -1, nullptr);
sqlite3_bind_text(stmt, 2, "blood", -1, nullptr);
sqlite3_bind_text(stmt, 3, "brothers", -1, nullptr);
sqlite3_bind_int(stmt, 4, 15);
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE) {
// cout << sqlite3_errmsg(db) << endl;
throw std::runtime_error(sqlite3_errmsg(db));
}
sqlite3_finalize(stmt);
auto secondId = sqlite3_last_insert_rowid(db);
{
// SELECT ID, CURRENT_WORD, BEFORE_WORD, AFTER_WORD, OCCURANCES
// FROM WORDS
// WHERE ID = firstId
sql = "SELECT ID, CURRENT_WORD, BEFORE_WORD, AFTER_WORD, OCCURANCES FROM WORDS WHERE ID = ?";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
REQUIRE(rc == SQLITE_OK);
sqlite3_bind_int64(stmt, 1, firstId);
rc = sqlite3_step(stmt);
if(rc != SQLITE_ROW) {
// cout << sqlite3_errmsg(db) << endl;
throw std::runtime_error(sqlite3_errmsg(db));
}
REQUIRE(sqlite3_column_int(stmt, 0) == firstId);
REQUIRE(::strcmp((const char *)sqlite3_column_text(stmt, 1), "best") == 0);
REQUIRE(::strcmp((const char *)sqlite3_column_text(stmt, 2), "behaviour") == 0);
REQUIRE(::strcmp((const char *)sqlite3_column_text(stmt, 3), "hey") == 0);
REQUIRE(sqlite3_column_int(stmt, 4) == 5);
sqlite3_finalize(stmt);
}
sqlite3_close(db);
struct Word {
int id;
std::string currentWord;
std::string beforeWord;
std::string afterWord;
int occurances;
};
auto storage = make_storage(dbFileName,
make_table("WORDS",
make_column("ID", &Word::id, primary_key(), autoincrement()),
make_column("CURRENT_WORD", &Word::currentWord),
make_column("BEFORE_WORD", &Word::beforeWord),
make_column("AFTER_WORD", &Word::afterWord),
make_column("OCCURANCES", &Word::occurances)));
storage.sync_schema(); // sync schema must not alter any data cause schemas are the same
REQUIRE(storage.count<Word>() == 2);
auto firstRow = storage.get_no_throw<Word>(firstId);
REQUIRE(firstRow);
REQUIRE(firstRow->currentWord == "best");
REQUIRE(firstRow->beforeWord == "behaviour");
REQUIRE(firstRow->afterWord == "hey");
REQUIRE(firstRow->occurances == 5);
auto secondRow = storage.get_pointer<Word>(secondId);
REQUIRE(secondRow);
REQUIRE(secondRow->currentWord == "corruption");
REQUIRE(secondRow->beforeWord == "blood");
REQUIRE(secondRow->afterWord == "brothers");
REQUIRE(secondRow->occurances == 15);
auto cols = columns(&Word::id, &Word::currentWord, &Word::beforeWord, &Word::afterWord, &Word::occurances);
auto rawTuples = storage.select(cols, where(eq(&Word::id, firstId)));
REQUIRE(rawTuples.size() == 1);
{
auto &firstTuple = rawTuples.front();
REQUIRE(std::get<0>(firstTuple) == firstId);
REQUIRE(std::get<1>(firstTuple) == "best");
REQUIRE(std::get<2>(firstTuple) == "behaviour");
REQUIRE(std::get<3>(firstTuple) == "hey");
REQUIRE(std::get<4>(firstTuple) == 5);
}
rawTuples = storage.select(cols, where(eq(&Word::id, secondId)));
REQUIRE(rawTuples.size() == 1);
{
auto &secondTuple = rawTuples.front();
REQUIRE(std::get<0>(secondTuple) == secondId);
REQUIRE(std::get<1>(secondTuple) == "corruption");
REQUIRE(std::get<2>(secondTuple) == "blood");
REQUIRE(std::get<3>(secondTuple) == "brothers");
REQUIRE(std::get<4>(secondTuple) == 15);
}
auto ordr = order_by(&Word::id);
auto idsOnly = storage.select(&Word::id, ordr);
REQUIRE(idsOnly.size() == 2);
REQUIRE(idsOnly[0] == firstId);
REQUIRE(idsOnly[1] == secondId);
auto currentWordsOnly = storage.select(&Word::currentWord, ordr);
REQUIRE(currentWordsOnly.size() == 2);
REQUIRE(currentWordsOnly[0] == "best");
REQUIRE(currentWordsOnly[1] == "corruption");
auto beforeWordsOnly = storage.select(&Word::beforeWord, ordr);
REQUIRE(beforeWordsOnly.size() == 2);
REQUIRE(beforeWordsOnly[0] == "behaviour");
REQUIRE(beforeWordsOnly[1] == "blood");
auto afterWordsOnly = storage.select(&Word::afterWord, ordr);
REQUIRE(afterWordsOnly.size() == 2);
REQUIRE(afterWordsOnly[0] == "hey");
REQUIRE(afterWordsOnly[1] == "brothers");
auto occurencesOnly = storage.select(&Word::occurances, ordr);
REQUIRE(occurencesOnly.size() == 2);
REQUIRE(occurencesOnly[0] == 5);
REQUIRE(occurencesOnly[1] == 15);
// test update_all with the same storage
storage.update_all(set(assign(&Word::currentWord, "ototo")), where(is_equal(&Word::id, firstId)));
REQUIRE(storage.get<Word>(firstId).currentWord == "ototo");
}
TEST_CASE("Replace query") {
struct Object {
int id;
std::string name;
};
struct User {
User(int id_, std::string name_) : id(id_), name(move(name_)) {}
int getId() const {
return this->id;
}
void setId(int id_) {
this->id = id_;
}
std::string getName() const {
return this->name;
}
void setName(std::string name_) {
this->name = move(name_);
}
private:
int id = 0;
std::string name;
};
auto storage = make_storage(
"test_replace.sqlite",
make_table("objects", make_column("id", &Object::id, primary_key()), make_column("name", &Object::name)),
make_table("users",
make_column("id", &User::getId, &User::setId, primary_key()),
make_column("name", &User::setName, &User::getName)));
storage.sync_schema();
storage.remove_all<Object>();
storage.remove_all<User>();
storage.replace(Object{
100,
"Baby",
});
REQUIRE(storage.count<Object>() == 1);
auto baby = storage.get<Object>(100);
REQUIRE(baby.id == 100);
REQUIRE(baby.name == "Baby");
storage.replace(Object{
200,
"Time",
});
REQUIRE(storage.count<Object>() == 2);
auto time = storage.get<Object>(200);
REQUIRE(time.id == 200);
REQUIRE(time.name == "Time");
storage.replace(Object{
100,
"Ototo",
});
REQUIRE(storage.count<Object>() == 2);
auto ototo = storage.get<Object>(100);
REQUIRE(ototo.id == 100);
REQUIRE(ototo.name == "Ototo");
auto initList = {
Object{
300,
"Iggy",
},
Object{
400,
"Azalea",
},
};
storage.replace_range(initList.begin(), initList.end());
REQUIRE(storage.count<Object>() == 4);
// test empty container
std::vector<Object> emptyVector;
storage.replace_range(emptyVector.begin(), emptyVector.end());
REQUIRE(storage.count<User>() == 0);
storage.replace(User{10, "Daddy Yankee"});
}
TEST_CASE("Insert") {
struct Object {
int id;
std::string name;
};
struct ObjectWithoutRowid {
int id;
std::string name;
};
auto storage = make_storage(
"test_insert.sqlite",
make_table("objects", make_column("id", &Object::id, primary_key()), make_column("name", &Object::name)),
make_table("objects_without_rowid",
make_column("id", &ObjectWithoutRowid::id, primary_key()),
make_column("name", &ObjectWithoutRowid::name))
.without_rowid());
storage.sync_schema();
storage.remove_all<Object>();
storage.remove_all<ObjectWithoutRowid>();
for(auto i = 0; i < 100; ++i) {
storage.insert(Object{
0,
"Skillet",
});
REQUIRE(storage.count<Object>() == i + 1);
}
auto initList = {
Object{
0,
"Insane",
},
Object{
0,
"Super",
},
Object{
0,
"Sun",
},
};
auto countBefore = storage.count<Object>();
storage.insert_range(initList.begin(), initList.end());
REQUIRE(storage.count<Object>() == countBefore + static_cast<int>(initList.size()));
// test empty container
std::vector<Object> emptyVector;
storage.insert_range(emptyVector.begin(), emptyVector.end());
// test insert without rowid
storage.insert(ObjectWithoutRowid{10, "Life"});
REQUIRE(storage.get<ObjectWithoutRowid>(10).name == "Life");
storage.insert(ObjectWithoutRowid{20, "Death"});
REQUIRE(storage.get<ObjectWithoutRowid>(20).name == "Death");
}