Skip to content

Commit

Permalink
add unit test & tidy diff
Browse files Browse the repository at this point in the history
  • Loading branch information
mlin committed May 31, 2021
1 parent 49a568f commit 78946be
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
7 changes: 5 additions & 2 deletions include/SQLiteCpp/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
17 changes: 5 additions & 12 deletions src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/Database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
{
Expand Down

0 comments on commit 78946be

Please sign in to comment.