Skip to content

Commit

Permalink
Improve Statement unit tests coverage (bind by name with a std::string)
Browse files Browse the repository at this point in the history
  • Loading branch information
SRombauts committed Jan 9, 2020
1 parent 97b2a07 commit 9c9cac3
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/Statement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,49 @@ TEST(Statement, bindByName)
}
}


TEST(Statement, bindByNameString)
{
// Create a new database
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
EXPECT_EQ(SQLite::OK, db.getErrorCode());

// Create a new table
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL, long INTEGER)"));
EXPECT_EQ(SQLite::OK, db.getErrorCode());

// Insertion with bindable parameters
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, @msg, @int, @double, @long)");

const std::string amsg = "@msg";
const std::string aint = "@int";
const std::string along = "@long";
const std::string adouble = "@double";

// First row with text/int/double
insert.bind(amsg, "first");
insert.bind(aint, 123);
insert.bind(along, -123);
insert.bind(adouble, 0.123);
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());

// Compile a SQL query to check the result
SQLite::Statement query(db, "SELECT * FROM test");
EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str());
EXPECT_EQ(5, query.getColumnCount());

// Check the result
query.executeStep();
EXPECT_TRUE(query.hasRow());
EXPECT_FALSE(query.isDone());
EXPECT_EQ(1, query.getColumn(0).getInt64());
EXPECT_STREQ("first", query.getColumn(1).getText());
EXPECT_EQ(123, query.getColumn(2).getInt());
EXPECT_EQ(0.123, query.getColumn(3).getDouble());
EXPECT_EQ(-123, query.getColumn(4).getInt());
}

TEST(Statement, bindNoCopyByName)
{
// Create a new database
Expand Down Expand Up @@ -573,6 +616,7 @@ TEST(Statement, bindNoCopyByName)
insert.bindNoCopy("@txt2", txt2);
insert.bindNoCopy("@blob", blob, sizeof(blob));
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(1, db.getLastInsertRowid());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());

// Check the result
Expand All @@ -584,6 +628,35 @@ TEST(Statement, bindNoCopyByName)
EXPECT_EQ(0, memcmp(&txt2[0], &query.getColumn(2).getString()[0], txt2.size()));
EXPECT_EQ(0, memcmp(blob, &query.getColumn(3).getString()[0], sizeof(blob)));
}

insert.reset();
query.reset();

// Insert a second row with all variants of bindNoCopy() using std::string names
{
const std::string atxt1 = "@txt1";
const std::string atxt2 = "@txt2";
const std::string ablob = "@blob";
const char* txt1 = "first2";
const std::string txt2 = "sec\0nd2";
const char blob[] = { 'b','l','\0','b','2' };
insert.bindNoCopy(atxt1, txt1);
insert.bindNoCopy(atxt2, txt2);
insert.bindNoCopy(ablob, blob, sizeof(blob));
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(2, db.getLastInsertRowid());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());

// Check the result
query.executeStep(); // pass on the first row
query.executeStep();
EXPECT_TRUE(query.hasRow());
EXPECT_FALSE(query.isDone());
EXPECT_EQ(2, query.getColumn(0).getInt64());
EXPECT_STREQ(txt1, query.getColumn(1).getText());
EXPECT_EQ(0, memcmp(&txt2[0], &query.getColumn(2).getString()[0], txt2.size()));
EXPECT_EQ(0, memcmp(blob, &query.getColumn(3).getString()[0], sizeof(blob)));
}
}

TEST(Statement, isColumnNull)
Expand Down

0 comments on commit 9c9cac3

Please sign in to comment.