Skip to content

Commit a99d48d

Browse files
authored
Merge pull request #257 from SRombauts/statement-unit-tests
Improve Statement unit tests coverage (bind by name with a std::string)
2 parents cb421a8 + f703742 commit a99d48d

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

tests/Statement_test.cpp

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,119 @@ TEST(Statement, bindByName)
546546
}
547547
}
548548

549+
550+
TEST(Statement, bindByNameString)
551+
{
552+
// Create a new database
553+
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
554+
EXPECT_EQ(SQLite::OK, db.getErrorCode());
555+
556+
// Create a new table
557+
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT, int INTEGER, double REAL, long INTEGER)"));
558+
EXPECT_EQ(SQLite::OK, db.getErrorCode());
559+
560+
// Insertion with bindable parameters
561+
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, @msg, @int, @double, @long)");
562+
563+
const std::string amsg = "@msg";
564+
const std::string aint = "@int";
565+
const std::string along = "@long";
566+
const std::string adouble = "@double";
567+
568+
// First row with text/int/double
569+
insert.bind(amsg, "first");
570+
insert.bind(aint, 123);
571+
insert.bind(along, -123);
572+
insert.bind(adouble, 0.123);
573+
EXPECT_EQ(1, insert.exec());
574+
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
575+
576+
// Compile a SQL query to check the result
577+
SQLite::Statement query(db, "SELECT * FROM test");
578+
EXPECT_STREQ("SELECT * FROM test", query.getQuery().c_str());
579+
EXPECT_EQ(5, query.getColumnCount());
580+
581+
// Check the result
582+
query.executeStep();
583+
EXPECT_TRUE(query.hasRow());
584+
EXPECT_FALSE(query.isDone());
585+
EXPECT_EQ(1, query.getColumn(0).getInt64());
586+
EXPECT_STREQ("first", query.getColumn(1).getText());
587+
EXPECT_EQ(123, query.getColumn(2).getInt());
588+
EXPECT_EQ(0.123, query.getColumn(3).getDouble());
589+
EXPECT_EQ(-123, query.getColumn(4).getInt());
590+
591+
// reset() with clearbindings() and new bindings
592+
insert.reset();
593+
insert.clearBindings();
594+
595+
// Second row with string/int64/float
596+
{
597+
const std::string second("second");
598+
const long long int64 = 12345678900000LL;
599+
const long integer = -123;
600+
const float float32 = 0.234f;
601+
insert.bind(amsg, second);
602+
insert.bind(aint, int64);
603+
insert.bind(adouble, float32);
604+
insert.bind(along, integer);
605+
EXPECT_EQ(1, insert.exec());
606+
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
607+
608+
// Check the result
609+
query.executeStep();
610+
EXPECT_TRUE(query.hasRow());
611+
EXPECT_FALSE(query.isDone());
612+
EXPECT_EQ(2, query.getColumn(0).getInt64());
613+
EXPECT_EQ(second, query.getColumn(1).getText());
614+
EXPECT_EQ(12345678900000LL, query.getColumn(2).getInt64());
615+
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
616+
EXPECT_EQ(-123, query.getColumn(4).getInt());
617+
}
618+
619+
// reset() without clearbindings()
620+
insert.reset();
621+
622+
// Third row with binary buffer and a null parameter
623+
{
624+
const char buffer[] = "binary";
625+
insert.bind(amsg, buffer, sizeof(buffer));
626+
insert.bind(aint);
627+
EXPECT_EQ(1, insert.exec());
628+
629+
// Check the result
630+
query.executeStep();
631+
EXPECT_TRUE(query.hasRow());
632+
EXPECT_FALSE(query.isDone());
633+
EXPECT_EQ(3, query.getColumn(0).getInt64());
634+
EXPECT_STREQ(buffer, query.getColumn(1).getText());
635+
EXPECT_TRUE(query.isColumnNull(2));
636+
EXPECT_EQ(0, query.getColumn(2).getInt());
637+
EXPECT_EQ(0.234f, query.getColumn(3).getDouble());
638+
}
639+
640+
// reset() without clearbindings()
641+
insert.reset();
642+
643+
// Fourth row with uint32_t unsigned value and int64_t 64bits value
644+
{
645+
const uint32_t uint32 = 4294967295U;
646+
const int64_t int64 = 12345678900000LL;
647+
insert.bind(aint, uint32);
648+
insert.bind(along, int64);
649+
EXPECT_EQ(1, insert.exec());
650+
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
651+
652+
// Check the result
653+
query.executeStep();
654+
EXPECT_TRUE(query.hasRow());
655+
EXPECT_FALSE(query.isDone());
656+
EXPECT_EQ(4, query.getColumn(0).getInt64());
657+
EXPECT_EQ(4294967295U, query.getColumn(2).getUInt());
658+
EXPECT_EQ(12345678900000LL, query.getColumn(4).getInt64());
659+
}
660+
}
661+
549662
TEST(Statement, bindNoCopyByName)
550663
{
551664
// Create a new database
@@ -573,6 +686,7 @@ TEST(Statement, bindNoCopyByName)
573686
insert.bindNoCopy("@txt2", txt2);
574687
insert.bindNoCopy("@blob", blob, sizeof(blob));
575688
EXPECT_EQ(1, insert.exec());
689+
EXPECT_EQ(1, db.getLastInsertRowid());
576690
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
577691

578692
// Check the result
@@ -584,6 +698,35 @@ TEST(Statement, bindNoCopyByName)
584698
EXPECT_EQ(0, memcmp(&txt2[0], &query.getColumn(2).getString()[0], txt2.size()));
585699
EXPECT_EQ(0, memcmp(blob, &query.getColumn(3).getString()[0], sizeof(blob)));
586700
}
701+
702+
insert.reset();
703+
query.reset();
704+
705+
// Insert a second row with all variants of bindNoCopy() using std::string names
706+
{
707+
const std::string atxt1 = "@txt1";
708+
const std::string atxt2 = "@txt2";
709+
const std::string ablob = "@blob";
710+
const char* txt1 = "first2";
711+
const std::string txt2 = "sec\0nd2";
712+
const char blob[] = { 'b','l','\0','b','2' };
713+
insert.bindNoCopy(atxt1, txt1);
714+
insert.bindNoCopy(atxt2, txt2);
715+
insert.bindNoCopy(ablob, blob, sizeof(blob));
716+
EXPECT_EQ(1, insert.exec());
717+
EXPECT_EQ(2, db.getLastInsertRowid());
718+
EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
719+
720+
// Check the result
721+
query.executeStep(); // pass on the first row
722+
query.executeStep();
723+
EXPECT_TRUE(query.hasRow());
724+
EXPECT_FALSE(query.isDone());
725+
EXPECT_EQ(2, query.getColumn(0).getInt64());
726+
EXPECT_STREQ(txt1, query.getColumn(1).getText());
727+
EXPECT_EQ(0, memcmp(&txt2[0], &query.getColumn(2).getString()[0], txt2.size()));
728+
EXPECT_EQ(0, memcmp(blob, &query.getColumn(3).getString()[0], sizeof(blob)));
729+
}
587730
}
588731

589732
TEST(Statement, isColumnNull)

0 commit comments

Comments
 (0)