Skip to content

Commit 64c34bc

Browse files
committed
Added unit tests for new getChanges() and fix comment being too long
1 parent 74c8627 commit 64c34bc

File tree

7 files changed

+31
-9
lines changed

7 files changed

+31
-9
lines changed

examples/example1/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Demonstrates how-to use the SQLite++ wrapper
66
*
7-
* Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com)
7+
* Copyright (c) 2012-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
88
*
99
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
1010
* or copy at http://opensource.org/licenses/MIT)
@@ -308,6 +308,9 @@ int main ()
308308
nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'");
309309
std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl;
310310

311+
nb = db.getTotalChanges();
312+
std::cout << "Nb of total changes since connection: " << nb << std::endl;
313+
311314
// Check the results : expect two row of result
312315
SQLite::Statement query(db, "SELECT * FROM test");
313316
std::cout << "SELECT * FROM test :\n";

include/SQLiteCpp/Database.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief Management of a SQLite Database Connection.
55
*
6-
* Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6+
* Copyright (c) 2012-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)

include/SQLiteCpp/Statement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief A prepared SQLite Statement is a compiled SQL query ready to be executed, pointing to a row of result.
55
*
6-
* Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6+
* Copyright (c) 2012-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)

src/Database.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief Management of a SQLite Database Connection.
55
*
6-
* Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6+
* Copyright (c) 2012-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)
@@ -112,7 +112,8 @@ void Database::setBusyTimeout(const int aBusyTimeoutMs)
112112
check(ret);
113113
}
114114

115-
// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...). Return the number of changes.
115+
// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...).
116+
// Return the number of changes.
116117
int Database::exec(const char* apQueries)
117118
{
118119
const int ret = tryExec(apQueries);

src/Statement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief A prepared SQLite Statement is a compiled SQL query ready to be executed, pointing to a row of result.
55
*
6-
* Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6+
* Copyright (c) 2012-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)

tests/Column_test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup tests
44
* @brief Test of a SQLiteCpp Column.
55
*
6-
* Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6+
* Copyright (c) 2012-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)
@@ -44,6 +44,7 @@ TEST(Column, basis)
4444
// Execute the one-step query to insert the row
4545
EXPECT_EQ(1, insert.exec());
4646
EXPECT_EQ(1, db.getLastInsertRowid());
47+
EXPECT_EQ(1, db.getChanges());
4748
EXPECT_EQ(1, db.getTotalChanges());
4849

4950
EXPECT_THROW(insert.exec(), SQLite::Exception); // exec() shall throw as it needs to be reseted
@@ -230,6 +231,7 @@ TEST(Column, stream)
230231
insert.bind(1, str);
231232
// Execute the one-step query to insert the row
232233
EXPECT_EQ(1, insert.exec());
234+
EXPECT_EQ(1, db.getChanges());
233235
EXPECT_EQ(1, db.getTotalChanges());
234236

235237
SQLite::Statement query(db, "SELECT * FROM test");

tests/Database_test.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup tests
44
* @brief Test of a SQLiteCpp Database.
55
*
6-
* Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6+
* Copyright (c) 2012-2021 Sebastien Rombauts (sebastien.rombauts@gmail.com)
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)
@@ -211,32 +211,38 @@ TEST(Database, exec)
211211
// NOTE: here exec() returns 0 only because it is the first statements since database connexion,
212212
// but its return is an undefined value for "CREATE TABLE" statements.
213213
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");
214+
EXPECT_EQ(0, db.getChanges());
214215
EXPECT_EQ(0, db.getLastInsertRowid());
215216
EXPECT_EQ(0, db.getTotalChanges());
216217

217218
// first row : insert the "first" text value into new row of id 1
218219
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\")"));
220+
EXPECT_EQ(1, db.getChanges());
219221
EXPECT_EQ(1, db.getLastInsertRowid());
220222
EXPECT_EQ(1, db.getTotalChanges());
221223

222224
// second row : insert the "second" text value into new row of id 2
223225
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"second\")"));
226+
EXPECT_EQ(1, db.getChanges());
224227
EXPECT_EQ(2, db.getLastInsertRowid());
225228
EXPECT_EQ(2, db.getTotalChanges());
226229

227230
// third row : insert the "third" text value into new row of id 3
228231
const std::string insert("INSERT INTO test VALUES (NULL, \"third\")");
229232
EXPECT_EQ(1, db.exec(insert));
233+
EXPECT_EQ(1, db.getChanges());
230234
EXPECT_EQ(3, db.getLastInsertRowid());
231235
EXPECT_EQ(3, db.getTotalChanges());
232236

233237
// update the second row : update text value to "second_updated"
234238
EXPECT_EQ(1, db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'"));
239+
EXPECT_EQ(1, db.getChanges());
235240
EXPECT_EQ(3, db.getLastInsertRowid()); // last inserted row ID is still 3
236241
EXPECT_EQ(4, db.getTotalChanges());
237242

238243
// delete the third row
239244
EXPECT_EQ(1, db.exec("DELETE FROM test WHERE id='3'"));
245+
EXPECT_EQ(1, db.getChanges());
240246
EXPECT_EQ(3, db.getLastInsertRowid());
241247
EXPECT_EQ(5, db.getTotalChanges());
242248

@@ -253,12 +259,14 @@ TEST(Database, exec)
253259

254260
// insert two rows with two *different* statements => returns only 1, ie. for the second INSERT statement
255261
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\");INSERT INTO test VALUES (NULL, \"second\");"));
262+
EXPECT_EQ(1, db.getChanges());
256263
EXPECT_EQ(2, db.getLastInsertRowid());
257264
EXPECT_EQ(7, db.getTotalChanges());
258265

259266
#if (SQLITE_VERSION_NUMBER >= 3007011)
260267
// insert two rows with only one statement (starting with SQLite 3.7.11) => returns 2
261268
EXPECT_EQ(2, db.exec("INSERT INTO test VALUES (NULL, \"third\"), (NULL, \"fourth\");"));
269+
EXPECT_EQ(2, db.getChanges());
262270
EXPECT_EQ(4, db.getLastInsertRowid());
263271
EXPECT_EQ(9, db.getTotalChanges());
264272
#endif
@@ -271,32 +279,38 @@ TEST(Database, tryExec)
271279

272280
// Create a new table with an explicit "id" column aliasing the underlying rowid
273281
EXPECT_EQ(SQLite::OK, db.tryExec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"));
282+
EXPECT_EQ(0, db.getChanges());
274283
EXPECT_EQ(0, db.getLastInsertRowid());
275284
EXPECT_EQ(0, db.getTotalChanges());
276285

277286
// first row : insert the "first" text value into new row of id 1
278287
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, \"first\")"));
288+
EXPECT_EQ(1, db.getChanges());
279289
EXPECT_EQ(1, db.getLastInsertRowid());
280290
EXPECT_EQ(1, db.getTotalChanges());
281291

282292
// second row : insert the "second" text value into new row of id 2
283293
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, \"second\")"));
294+
EXPECT_EQ(1, db.getChanges());
284295
EXPECT_EQ(2, db.getLastInsertRowid());
285296
EXPECT_EQ(2, db.getTotalChanges());
286297

287298
// third row : insert the "third" text value into new row of id 3
288299
const std::string insert("INSERT INTO test VALUES (NULL, \"third\")");
289300
EXPECT_EQ(SQLite::OK, db.tryExec(insert));
301+
EXPECT_EQ(1, db.getChanges());
290302
EXPECT_EQ(3, db.getLastInsertRowid());
291303
EXPECT_EQ(3, db.getTotalChanges());
292304

293305
// update the second row : update text value to "second_updated"
294306
EXPECT_EQ(SQLite::OK, db.tryExec("UPDATE test SET value=\"second-updated\" WHERE id='2'"));
307+
EXPECT_EQ(1, db.getChanges());
295308
EXPECT_EQ(3, db.getLastInsertRowid()); // last inserted row ID is still 3
296309
EXPECT_EQ(4, db.getTotalChanges());
297310

298311
// delete the third row
299312
EXPECT_EQ(SQLite::OK, db.tryExec("DELETE FROM test WHERE id='3'"));
313+
EXPECT_EQ(1, db.getChanges());
300314
EXPECT_EQ(3, db.getLastInsertRowid());
301315
EXPECT_EQ(5, db.getTotalChanges());
302316

@@ -309,14 +323,16 @@ TEST(Database, tryExec)
309323
EXPECT_EQ(SQLite::OK, db.tryExec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"));
310324
EXPECT_EQ(5, db.getTotalChanges());
311325

312-
// insert two rows with two *different* statements => returns only 1, ie. for the second INSERT statement
326+
// insert two rows with two *different* statements => only 1 change, ie. for the second INSERT statement
313327
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, \"first\");INSERT INTO test VALUES (NULL, \"second\");"));
328+
EXPECT_EQ(1, db.getChanges());
314329
EXPECT_EQ(2, db.getLastInsertRowid());
315330
EXPECT_EQ(7, db.getTotalChanges());
316331

317332
#if (SQLITE_VERSION_NUMBER >= 3007011)
318333
// insert two rows with only one statement (starting with SQLite 3.7.11)
319334
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, \"third\"), (NULL, \"fourth\");"));
335+
EXPECT_EQ(2, db.getChanges());
320336
EXPECT_EQ(4, db.getLastInsertRowid());
321337
EXPECT_EQ(9, db.getTotalChanges());
322338
#endif

0 commit comments

Comments
 (0)