From 9c9cac36898adfd9ed2b349b28d39e89e025fa5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Thu, 9 Jan 2020 09:43:01 +0100 Subject: [PATCH] Improve Statement unit tests coverage (bind by name with a std::string) --- tests/Statement_test.cpp | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/Statement_test.cpp b/tests/Statement_test.cpp index 6c9c8005..c40ccf15 100644 --- a/tests/Statement_test.cpp +++ b/tests/Statement_test.cpp @@ -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 @@ -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 @@ -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)