From 78946beb55b194c5944d75202c37ff87eb76ba6c Mon Sep 17 00:00:00 2001 From: Mike Lin Date: Sun, 30 May 2021 16:41:49 -1000 Subject: [PATCH] add unit test & tidy diff --- include/SQLiteCpp/Database.h | 7 +++++-- src/Database.cpp | 17 +++++------------ tests/Database_test.cpp | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index 00bb3a4a..5e05164f 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -434,7 +434,7 @@ class Database /// Return UTF-8 encoded English language explanation of the most recent failed API call (if any). const char* getErrorMsg() const noexcept; - /// Return the filename used to open the database; empty if the Database wrapped existing sqlite3* + /// Return the filename used to open the database. const std::string& getFilename() const noexcept { return mFilename; @@ -556,7 +556,10 @@ class Database static Header getHeaderInfo(const std::string& aFilename); // Parse SQLite header data from a database file. - Header getHeaderInfo(); + Header getHeaderInfo() + { + return getHeaderInfo(mFilename); + } /** * @brief BackupType for the backup() method diff --git a/src/Database.cpp b/src/Database.cpp index 3504d45b..74bd819a 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -94,6 +94,11 @@ Database::Database(sqlite3* apSQLite, { SQLITECPP_ASSERT(apSQLite != nullptr, "Database(nullptr)"); mSQLitePtr.reset(apSQLite); + const char *zFilename = sqlite3_db_filename(mSQLitePtr.get(), nullptr); + if (zFilename) + { + mFilename = zFilename; + } if (aBusyTimeoutMs > 0) { setBusyTimeout(aBusyTimeoutMs); @@ -446,18 +451,6 @@ Header Database::getHeaderInfo(const std::string& aFilename) return h; } -Header Database::getHeaderInfo() -{ - if (!mFilename.empty()) - { - return getHeaderInfo(mFilename); - } - const char *zFilename = sqlite3_db_filename(mSQLitePtr.get(), nullptr); - return getHeaderInfo(std::string(zFilename ? zFilename : "")); -} - - - void Database::backup(const char* apFilename, BackupType aType) { // Open the database file identified by apFilename diff --git a/tests/Database_test.cpp b/tests/Database_test.cpp index 2493a0d4..30d46f7c 100644 --- a/tests/Database_test.cpp +++ b/tests/Database_test.cpp @@ -121,6 +121,28 @@ TEST(Database, createCloseReopen) remove("test.db3"); } +TEST(Database, wrapper) +{ + remove("test.db4"); + // Create a new database using SQLite3 directly + sqlite3 *dbconn = nullptr; + int rc = sqlite3_open_v2("test.db4", &dbconn, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr); + EXPECT_EQ(rc, SQLITE_OK); + { + // instantiate SQLite::Database wrapper + SQLite::Database db(dbconn); + EXPECT_FALSE(db.tableExists("test")); + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); + EXPECT_TRUE(db.tableExists("test")); + std::string filename = db.getFilename(); + EXPECT_STREQ(filename.substr(filename.rfind('/')+1).c_str(), "test.db4"); + } + // dbconn remains open after db destruction + rc = sqlite3_close(dbconn); + EXPECT_EQ(rc, SQLITE_OK); + remove("test.db4"); +} + TEST(Database, inMemory) { {