Add unit test for bind() of an uint32_t value

This commit is contained in:
Sébastien Rombauts 2016-07-07 08:49:08 +02:00
parent 8275c7fb29
commit 5056c29f9e

View File

@ -147,34 +147,40 @@ TEST(Statement, bindings) {
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
// Insertion with bindable parameters
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, ?, ?, ?)");
// First row with text/int/double
insert.bind(1, "first");
insert.bind(2, 123);
insert.bind(3, 0.123);
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
// Compile a SQL query to check the result
// Compile a SQL query to check the results
SQLite::Statement query(db, "SELECT * FROM test");
EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str());
EXPECT_EQ(4, query.getColumnCount());
// First row with text/int/double
{
const char* text = "first";
const int integer = -123;
const double dbl = 0.123;
insert.bind(1, text);
insert.bind(2, integer);
insert.bind(3, dbl);
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
// 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 (-123, query.getColumn(2).getInt());
EXPECT_EQ (0.123, query.getColumn(3).getDouble());
}
// reset() without clearbindings()
insert.reset();
// Second row with the same exact values because clearbindings() was not called
{
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
@ -184,14 +190,16 @@ TEST(Statement, bindings) {
EXPECT_FALSE(query.isDone());
EXPECT_EQ (2, query.getColumn(0).getInt64());
EXPECT_STREQ("first", query.getColumn(1).getText());
EXPECT_EQ (123, query.getColumn(2).getInt());
EXPECT_EQ (-123, query.getColumn(2).getInt());
EXPECT_EQ (0.123, query.getColumn(3).getDouble());
}
// reset() with clearbindings() and no more bindings
insert.reset();
insert.clearBindings();
// Third row with the all null values because clearbindings() was called
{
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
@ -206,12 +214,14 @@ TEST(Statement, bindings) {
EXPECT_EQ (0, query.getColumn(2).getInt());
EXPECT_TRUE (query.isColumnNull(3));
EXPECT_EQ (0.0, query.getColumn(3).getDouble());
}
// reset() with clearbindings() and new bindings
insert.reset();
insert.clearBindings();
// Fourth row with string/int64/float
{
const std::string fourth("fourth");
const int64_t int64 = 12345678900000LL;
const float float32 = 0.234f;
@ -229,11 +239,13 @@ TEST(Statement, bindings) {
EXPECT_EQ(fourth, query.getColumn(1).getText());
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
}
// reset() without clearbindings()
insert.reset();
// Fifth row with binary buffer and a null parameter
{
const char buffer[] = "binary";
insert.bind(1, buffer, sizeof(buffer));
insert.bind(2);
@ -248,7 +260,26 @@ TEST(Statement, bindings) {
EXPECT_TRUE (query.isColumnNull(2));
EXPECT_EQ(0, query.getColumn(2).getInt());
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
}
// reset() without clearbindings()
insert.reset();
// Sixth row with uint32_t unsigned value
{
const uint32_t uint32 = 4294967295U;
insert.bind(2, uint32);
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
// Check the sixth row
query.executeStep();
EXPECT_TRUE(query.isOk());
EXPECT_FALSE(query.isDone());
EXPECT_EQ(6, query.getColumn(0).getInt64());
EXPECT_EQ(4294967295U, query.getColumn(2).getUInt());
}
}
TEST(Statement, bindByName) {
@ -260,7 +291,7 @@ TEST(Statement, bindByName) {
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
// Insertion with bindable parameters
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, @msg, @int, @double)");
// First row with text/int/double