mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 17:56:13 -04:00
Add unit test for bind() of an uint32_t value
This commit is contained in:
parent
8275c7fb29
commit
5056c29f9e
@ -147,108 +147,139 @@ TEST(Statement, bindings) {
|
|||||||
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL)"));
|
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL)"));
|
||||||
EXPECT_EQ(SQLITE_OK, db.getErrorCode());
|
EXPECT_EQ(SQLITE_OK, db.getErrorCode());
|
||||||
|
|
||||||
// Insertion with binded values
|
// Insertion with bindable parameters
|
||||||
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, ?, ?, ?)");
|
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, ?, ?, ?)");
|
||||||
|
|
||||||
// First row with text/int/double
|
// Compile a SQL query to check the results
|
||||||
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
|
|
||||||
SQLite::Statement query(db, "SELECT * FROM test");
|
SQLite::Statement query(db, "SELECT * FROM test");
|
||||||
EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str());
|
EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str());
|
||||||
EXPECT_EQ(4, query.getColumnCount());
|
EXPECT_EQ(4, query.getColumnCount());
|
||||||
|
|
||||||
// Check the first row
|
// First row with text/int/double
|
||||||
query.executeStep();
|
{
|
||||||
EXPECT_TRUE (query.isOk());
|
const char* text = "first";
|
||||||
EXPECT_FALSE(query.isDone());
|
const int integer = -123;
|
||||||
EXPECT_EQ (1, query.getColumn(0).getInt64());
|
const double dbl = 0.123;
|
||||||
EXPECT_STREQ("first", query.getColumn(1).getText());
|
insert.bind(1, text);
|
||||||
EXPECT_EQ (123, query.getColumn(2).getInt());
|
insert.bind(2, integer);
|
||||||
EXPECT_EQ (0.123, query.getColumn(3).getDouble());
|
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 (0.123, query.getColumn(3).getDouble());
|
||||||
|
}
|
||||||
|
|
||||||
// reset() without clearbindings()
|
// reset() without clearbindings()
|
||||||
insert.reset();
|
insert.reset();
|
||||||
|
|
||||||
// Second row with the same exact values because clearbindings() was not called
|
// Second row with the same exact values because clearbindings() was not called
|
||||||
EXPECT_EQ(1, insert.exec());
|
{
|
||||||
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
|
EXPECT_EQ(1, insert.exec());
|
||||||
|
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
|
||||||
|
|
||||||
// Check the second row
|
// Check the second row
|
||||||
query.executeStep();
|
query.executeStep();
|
||||||
EXPECT_TRUE (query.isOk());
|
EXPECT_TRUE (query.isOk());
|
||||||
EXPECT_FALSE(query.isDone());
|
EXPECT_FALSE(query.isDone());
|
||||||
EXPECT_EQ (2, query.getColumn(0).getInt64());
|
EXPECT_EQ (2, query.getColumn(0).getInt64());
|
||||||
EXPECT_STREQ("first", query.getColumn(1).getText());
|
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());
|
EXPECT_EQ (0.123, query.getColumn(3).getDouble());
|
||||||
|
}
|
||||||
|
|
||||||
// reset() with clearbindings() and no more bindings
|
// reset() with clearbindings() and no more bindings
|
||||||
insert.reset();
|
insert.reset();
|
||||||
insert.clearBindings();
|
insert.clearBindings();
|
||||||
|
|
||||||
// Third row with the all null values because clearbindings() was called
|
// Third row with the all null values because clearbindings() was called
|
||||||
EXPECT_EQ(1, insert.exec());
|
{
|
||||||
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
|
EXPECT_EQ(1, insert.exec());
|
||||||
|
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
|
||||||
|
|
||||||
// Check the third row
|
// Check the third row
|
||||||
query.executeStep();
|
query.executeStep();
|
||||||
EXPECT_TRUE (query.isOk());
|
EXPECT_TRUE (query.isOk());
|
||||||
EXPECT_FALSE(query.isDone());
|
EXPECT_FALSE(query.isDone());
|
||||||
EXPECT_EQ (3, query.getColumn(0).getInt64());
|
EXPECT_EQ (3, query.getColumn(0).getInt64());
|
||||||
EXPECT_TRUE (query.isColumnNull(1));
|
EXPECT_TRUE (query.isColumnNull(1));
|
||||||
EXPECT_STREQ("", query.getColumn(1).getText());
|
EXPECT_STREQ("", query.getColumn(1).getText());
|
||||||
EXPECT_TRUE (query.isColumnNull(2));
|
EXPECT_TRUE (query.isColumnNull(2));
|
||||||
EXPECT_EQ (0, query.getColumn(2).getInt());
|
EXPECT_EQ (0, query.getColumn(2).getInt());
|
||||||
EXPECT_TRUE (query.isColumnNull(3));
|
EXPECT_TRUE (query.isColumnNull(3));
|
||||||
EXPECT_EQ (0.0, query.getColumn(3).getDouble());
|
EXPECT_EQ (0.0, query.getColumn(3).getDouble());
|
||||||
|
}
|
||||||
|
|
||||||
// reset() with clearbindings() and new bindings
|
// reset() with clearbindings() and new bindings
|
||||||
insert.reset();
|
insert.reset();
|
||||||
insert.clearBindings();
|
insert.clearBindings();
|
||||||
|
|
||||||
// Fourth row with string/int64/float
|
// Fourth row with string/int64/float
|
||||||
const std::string fourth("fourth");
|
{
|
||||||
const int64_t int64 = 12345678900000LL;
|
const std::string fourth("fourth");
|
||||||
const float float32 = 0.234f;
|
const int64_t int64 = 12345678900000LL;
|
||||||
insert.bind(1, fourth);
|
const float float32 = 0.234f;
|
||||||
insert.bind(2, int64);
|
insert.bind(1, fourth);
|
||||||
insert.bind(3, float32);
|
insert.bind(2, int64);
|
||||||
EXPECT_EQ(1, insert.exec());
|
insert.bind(3, float32);
|
||||||
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
|
EXPECT_EQ(1, insert.exec());
|
||||||
|
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
|
||||||
|
|
||||||
// Check the fourth row
|
// Check the fourth row
|
||||||
query.executeStep();
|
query.executeStep();
|
||||||
EXPECT_TRUE (query.isOk());
|
EXPECT_TRUE (query.isOk());
|
||||||
EXPECT_FALSE(query.isDone());
|
EXPECT_FALSE(query.isDone());
|
||||||
EXPECT_EQ(4, query.getColumn(0).getInt64());
|
EXPECT_EQ(4, query.getColumn(0).getInt64());
|
||||||
EXPECT_EQ(fourth, query.getColumn(1).getText());
|
EXPECT_EQ(fourth, query.getColumn(1).getText());
|
||||||
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
|
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
|
||||||
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
|
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
|
||||||
|
}
|
||||||
|
|
||||||
// reset() without clearbindings()
|
// reset() without clearbindings()
|
||||||
insert.reset();
|
insert.reset();
|
||||||
|
|
||||||
// Fifth row with binary buffer and a null parameter
|
// Fifth row with binary buffer and a null parameter
|
||||||
const char buffer[] = "binary";
|
{
|
||||||
insert.bind(1, buffer, sizeof(buffer));
|
const char buffer[] = "binary";
|
||||||
insert.bind(2);
|
insert.bind(1, buffer, sizeof(buffer));
|
||||||
EXPECT_EQ(1, insert.exec());
|
insert.bind(2);
|
||||||
|
EXPECT_EQ(1, insert.exec());
|
||||||
|
|
||||||
// Check the fifth row
|
// Check the fifth row
|
||||||
query.executeStep();
|
query.executeStep();
|
||||||
EXPECT_TRUE (query.isOk());
|
EXPECT_TRUE (query.isOk());
|
||||||
EXPECT_FALSE(query.isDone());
|
EXPECT_FALSE(query.isDone());
|
||||||
EXPECT_EQ(5, query.getColumn(0).getInt64());
|
EXPECT_EQ(5, query.getColumn(0).getInt64());
|
||||||
EXPECT_STREQ(buffer, query.getColumn(1).getText());
|
EXPECT_STREQ(buffer, query.getColumn(1).getText());
|
||||||
EXPECT_TRUE (query.isColumnNull(2));
|
EXPECT_TRUE (query.isColumnNull(2));
|
||||||
EXPECT_EQ(0, query.getColumn(2).getInt());
|
EXPECT_EQ(0, query.getColumn(2).getInt());
|
||||||
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
|
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) {
|
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(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL)"));
|
||||||
EXPECT_EQ(SQLITE_OK, db.getErrorCode());
|
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)");
|
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, @msg, @int, @double)");
|
||||||
|
|
||||||
// First row with text/int/double
|
// First row with text/int/double
|
||||||
|
Loading…
x
Reference in New Issue
Block a user