Skip to content

Commit

Permalink
Add Iterators tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperos155 committed Aug 2, 2022
1 parent 9699499 commit 56899b6
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ set(SQLITECPP_TESTS
tests/VariadicBind_test.cpp
tests/Exception_test.cpp
tests/ExecuteMany_test.cpp
tests/Iterators.cpp
)
source_group(tests FILES ${SQLITECPP_TESTS})

Expand Down
4 changes: 2 additions & 2 deletions include/SQLiteCpp/Row.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Row
* @throws SQLite::Exception when there is no column with given name
*/
Column at(const char* aName) const;

/**
* @return Column with given index
*
Expand All @@ -78,7 +78,7 @@ class Row
{
return at(aName);
}

/**
* @return Column with given index
*
Expand Down
4 changes: 2 additions & 2 deletions src/Row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace SQLite
Row::Row(TStatementWeakPtr apStatement, std::size_t aID) :
mpStatement(apStatement), mID(aID)
{
checkStatement();
auto statement = mpStatement.lock();
mColumnCount = statement->mColumnCount;
if (statement)
mColumnCount = statement->mColumnCount;
}

Column Row::at(int_fast16_t aIndex) const
Expand Down
128 changes: 128 additions & 0 deletions tests/Iterators.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* @file Iterators.cpp
* @ingroup tests
* @brief Test of Statement iterators
*
* Copyright (c) 2022 Kacper Zielinski ([email protected])
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/

#include <SQLiteCpp/Database.h>
#include <SQLiteCpp/Statement.h>

#include <gtest/gtest.h>

#include <vector>
#include <string>
#include <algorithm>

SQLite::Database createDatabase()
{
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
EXPECT_EQ(SQLite::OK, db.exec("CREATE TABLE test (number INTEGER, number_str TEXT)"));

SQLite::Statement inserter(db, "INSERT INTO test VALUES(?,?)");
for (int i = 10; i > 0; --i)
{
inserter.bind(1, i);
inserter.bind(2, std::to_string(i));
EXPECT_EQ(1, inserter.exec());
EXPECT_NO_THROW(inserter.reset());
}
return db;
}

TEST(Iterators, RowIterator)
{
auto db = createDatabase();

SQLite::Statement query(db, "SELECT * FROM test");
std::vector<int> numbers;

for (const auto& row : query)
{
numbers.push_back(row[0]);
}

//TODO: EXPECT_TRUE(query.isDone());

const auto v_size = static_cast<int>(numbers.size());
for (int i = v_size; i > 0; --i)
{
EXPECT_EQ(i, numbers[v_size - i]);
}

auto it = query.begin();
++it;
EXPECT_STREQ("number_str", it->getColumn(1).getName());
EXPECT_EQ(1, it->getColumnIndex("number_str"));

// getColumn aliases
EXPECT_EQ(9, it->at(0).getInt());
EXPECT_EQ(9, it->getColumn(0).getInt());
EXPECT_EQ(9, it->operator[](0).getInt());

auto it2 = query.begin();
++it2;
EXPECT_EQ(it, it2);

// RowInterator is advancing common statement object
++it;
EXPECT_EQ(it->at(0).getInt(), it2->at(0).getInt());
// But iterators internal state is diffrent.
EXPECT_NE(it, it2);
}

TEST(Iterators, RowIterator_STL_Algorithms)
{
auto db = createDatabase();

SQLite::Statement query(db, "SELECT * FROM test");

for (auto it = query.begin(); it != query.end(); std::advance(it, 3))
{
EXPECT_TRUE(it->getRowNumber() % 3 == 0);
}

EXPECT_TRUE(std::all_of(query.begin(), query.end(), [](const SQLite::Row& row)
{
return row[0].getInt() > 0;
}));
}

TEST(Iterators, ColumnIterator)
{
auto db = createDatabase();

SQLite::Statement query_only1(db, "SELECT * FROM test LIMIT 1");
std::vector<std::string> numbers_str;

for(const auto& row : query_only1)
for (const auto& column : row)
{
numbers_str.emplace_back(column.getText());
}

for (const auto& column : numbers_str)
{
EXPECT_EQ("10", column);
}
}

TEST(Iterators, ColumnIterator_STL_Algorithms)
{
auto db = createDatabase();

SQLite::Statement query_only1(db, "SELECT * FROM test LIMIT 1");
std::vector<int> numbers;

for (const auto& row : query_only1)
{
EXPECT_EQ(2, std::count_if(row.begin(), row.end(), [i = 10](const SQLite::Column& column)
{
return column.getText() == std::to_string(i);
}));
}
}

0 comments on commit 56899b6

Please sign in to comment.