From e8f956be22d0931b73d4352c953cb2e6587fbd50 Mon Sep 17 00:00:00 2001 From: hongshibao Date: Wed, 28 Oct 2015 03:04:13 +0800 Subject: [PATCH] Add Backup test --- CMakeLists.txt | 1 + include/SQLiteCpp/Backup.h | 2 +- src/Backup.cpp | 7 +++++-- tests/Backup_test.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/Backup_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 90a328f..4d8279c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,6 +112,7 @@ set(SQLITECPP_TESTS tests/Column_test.cpp tests/Database_test.cpp tests/Statement_test.cpp + tests/Backup_test.cpp ) source_group(tests FILES ${SQLITECPP_TESTS}) diff --git a/include/SQLiteCpp/Backup.h b/include/SQLiteCpp/Backup.h index 11ca01f..660ce80 100644 --- a/include/SQLiteCpp/Backup.h +++ b/include/SQLiteCpp/Backup.h @@ -3,7 +3,7 @@ * @ingroup SQLiteCpp * @brief Backup is used to backup a database file in a safe and online way. * - * Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com) + * Copyright (c) 2015-2015 Shibao HONG (shibaohong@outlook.com) * * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt * or copy at http://opensource.org/licenses/MIT) diff --git a/src/Backup.cpp b/src/Backup.cpp index e14898a..504042f 100644 --- a/src/Backup.cpp +++ b/src/Backup.cpp @@ -3,7 +3,7 @@ * @ingroup SQLiteCpp * @brief Backup is used to backup a database file in a safe and online way. * - * Copyright (c) 2015 Shibao HONG (shibaohong@outlook.com) + * Copyright (c) 2015-2015 Shibao HONG (shibaohong@outlook.com) * * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt * or copy at http://opensource.org/licenses/MIT) @@ -85,8 +85,11 @@ int Backup::executeStep(const int aNumPage /* = -1 */) if (SQLITE_OK != res && SQLITE_DONE != res && SQLITE_BUSY != res && SQLITE_LOCKED != res) { - std::string strerr("Backup executeStep error with message "); + std::string strerr("Backup executeStep error"); +#if SQLITE_VERSION_NUMBER >= 3007015 // SQLite v3.7.15 is the first version with sqlite3_errstr() interface + strerr += "with error message "; strerr += sqlite3_errstr(res); +#endif throw SQLite::Exception(strerr); } return res; diff --git a/tests/Backup_test.cpp b/tests/Backup_test.cpp new file mode 100644 index 0000000..b3f3b3e --- /dev/null +++ b/tests/Backup_test.cpp @@ -0,0 +1,38 @@ +/** + * @file Backup_test.cpp + * @ingroup tests + * @brief Test of a SQLite Backup. + * + * Copyright (c) 2015-2015 Shibao HONG (shibaohong@outlook.com) + * + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt + * or copy at http://opensource.org/licenses/MIT) + */ + +#include +#include +#include + +#include + +#include + +TEST(Backup, executeStep) { + remove("backup_test.db3"); + remove("backup_test.db3.backup"); + SQLite::Database srcDB("backup_test.db3", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); + srcDB.exec("CREATE TABLE backup_test (id INTEGER PRIMARY KEY, value TEXT)"); + ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, \"first\")")); + ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, \"second\")")); + SQLite::Database destDB("backup_test.db3.backup", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); + SQLite::Backup backup(destDB, srcDB); + const int res = backup.executeStep(); + ASSERT_EQ(res, SQLITE_DONE); + SQLite::Statement query(destDB, "SELECT * FROM backup_test ORDER BY id ASC"); + ASSERT_TRUE(query.executeStep()); + EXPECT_EQ(1, query.getColumn(0).getInt()); + EXPECT_STREQ("first", query.getColumn(1)); + ASSERT_TRUE(query.executeStep()); + EXPECT_EQ(2, query.getColumn(0).getInt()); + EXPECT_STREQ("second", query.getColumn(1)); +}