Skip to content

Commit e8f956b

Browse files
committed
Add Backup test
1 parent 08716b8 commit e8f956b

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ set(SQLITECPP_TESTS
112112
tests/Column_test.cpp
113113
tests/Database_test.cpp
114114
tests/Statement_test.cpp
115+
tests/Backup_test.cpp
115116
)
116117
source_group(tests FILES ${SQLITECPP_TESTS})
117118

include/SQLiteCpp/Backup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief Backup is used to backup a database file in a safe and online way.
55
*
6-
* Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com)
6+
* Copyright (c) 2015-2015 Shibao HONG (shibaohong@outlook.com)
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)

src/Backup.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief Backup is used to backup a database file in a safe and online way.
55
*
6-
* Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com)
6+
* Copyright (c) 2015-2015 Shibao HONG (shibaohong@outlook.com)
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)
@@ -85,8 +85,11 @@ int Backup::executeStep(const int aNumPage /* = -1 */)
8585
if (SQLITE_OK != res && SQLITE_DONE != res &&
8686
SQLITE_BUSY != res && SQLITE_LOCKED != res)
8787
{
88-
std::string strerr("Backup executeStep error with message ");
88+
std::string strerr("Backup executeStep error");
89+
#if SQLITE_VERSION_NUMBER >= 3007015 // SQLite v3.7.15 is the first version with sqlite3_errstr() interface
90+
strerr += "with error message ";
8991
strerr += sqlite3_errstr(res);
92+
#endif
9093
throw SQLite::Exception(strerr);
9194
}
9295
return res;

tests/Backup_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @file Backup_test.cpp
3+
* @ingroup tests
4+
* @brief Test of a SQLite Backup.
5+
*
6+
* Copyright (c) 2015-2015 Shibao HONG (shibaohong@outlook.com)
7+
*
8+
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
9+
* or copy at http://opensource.org/licenses/MIT)
10+
*/
11+
12+
#include <SQLiteCpp/Backup.h>
13+
#include <SQLiteCpp/Database.h>
14+
#include <SQLiteCpp/Statement.h>
15+
16+
#include <gtest/gtest.h>
17+
18+
#include <cstdio>
19+
20+
TEST(Backup, executeStep) {
21+
remove("backup_test.db3");
22+
remove("backup_test.db3.backup");
23+
SQLite::Database srcDB("backup_test.db3", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
24+
srcDB.exec("CREATE TABLE backup_test (id INTEGER PRIMARY KEY, value TEXT)");
25+
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, \"first\")"));
26+
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, \"second\")"));
27+
SQLite::Database destDB("backup_test.db3.backup", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
28+
SQLite::Backup backup(destDB, srcDB);
29+
const int res = backup.executeStep();
30+
ASSERT_EQ(res, SQLITE_DONE);
31+
SQLite::Statement query(destDB, "SELECT * FROM backup_test ORDER BY id ASC");
32+
ASSERT_TRUE(query.executeStep());
33+
EXPECT_EQ(1, query.getColumn(0).getInt());
34+
EXPECT_STREQ("first", query.getColumn(1));
35+
ASSERT_TRUE(query.executeStep());
36+
EXPECT_EQ(2, query.getColumn(0).getInt());
37+
EXPECT_STREQ("second", query.getColumn(1));
38+
}

0 commit comments

Comments
 (0)