Merge pull request #257 from SRombauts/statement-unit-tests · SRombauts/SQLiteCpp@a99d48d (original) (raw)
`@@ -546,6 +546,119 @@ TEST(Statement, bindByName)
`
546
546
` }
`
547
547
`}
`
548
548
``
``
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
+
549
662
`TEST(Statement, bindNoCopyByName)
`
550
663
`{
`
551
664
`// Create a new database
`
`@@ -573,6 +686,7 @@ TEST(Statement, bindNoCopyByName)
`
573
686
` insert.bindNoCopy("@txt2", txt2);
`
574
687
` insert.bindNoCopy("@blob", blob, sizeof(blob));
`
575
688
`EXPECT_EQ(1, insert.exec());
`
``
689
`+
EXPECT_EQ(1, db.getLastInsertRowid());
`
576
690
`EXPECT_EQ(SQLITE_DONE, db.getErrorCode());
`
577
691
``
578
692
`// Check the result
`
`@@ -584,6 +698,35 @@ TEST(Statement, bindNoCopyByName)
`
584
698
`EXPECT_EQ(0, memcmp(&txt2[0], &query.getColumn(2).getString()[0], txt2.size()));
`
585
699
`EXPECT_EQ(0, memcmp(blob, &query.getColumn(3).getString()[0], sizeof(blob)));
`
586
700
` }
`
``
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
`+
}
`
587
730
`}
`
588
731
``
589
732
`TEST(Statement, isColumnNull)
`