Add unit tests for binding parameters by name

This commit is contained in:
Sébastien Rombauts 2016-06-30 13:19:46 +02:00
parent 0c43747065
commit 249639ca70

View File

@ -219,6 +219,63 @@ TEST(Statement, bindings) {
EXPECT_EQ(0.123f, query.getColumn(3).getDouble());
}
TEST(Statement, bindByName) {
// 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)"));
EXPECT_EQ(SQLITE_OK, db.getErrorCode());
// Insertion with binded values
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, @msg, @int, @double)");
// First row with text/int/double
insert.bind("@msg", "first");
insert.bind("@int", 123);
insert.bind("@double", 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(4, query.getColumnCount());
// Check the first row
query.executeStep();
EXPECT_TRUE (query.isOk());
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());
// reset() with clearbindings() and new bindings
insert.reset();
insert.clearBindings();
// Second row with string/int64/float
const std::string second("second");
const sqlite_int64 int64 = 12345678900000LL;
const float fl32 = 0.123f;
insert.bind("@msg", second);
insert.bind("@int", int64);
insert.bind("@double", fl32);
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
// Check the second row
query.executeStep();
EXPECT_TRUE (query.isOk());
EXPECT_FALSE(query.isDone());
EXPECT_EQ(2, query.getColumn(0).getInt64());
EXPECT_EQ(second, query.getColumn(1).getText());
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
EXPECT_EQ(0.123f, query.getColumn(3).getDouble());
}
TEST(Statement, isColumnNull) {
// Create a new database
SQLite::Database db(":memory:", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);