Skip to content

Commit 56899b6

Browse files
committed
Add Iterators tests
1 parent 9699499 commit 56899b6

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ set(SQLITECPP_TESTS
144144
tests/VariadicBind_test.cpp
145145
tests/Exception_test.cpp
146146
tests/ExecuteMany_test.cpp
147+
tests/Iterators.cpp
147148
)
148149
source_group(tests FILES ${SQLITECPP_TESTS})
149150

include/SQLiteCpp/Row.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Row
5555
* @throws SQLite::Exception when there is no column with given name
5656
*/
5757
Column at(const char* aName) const;
58-
58+
5959
/**
6060
* @return Column with given index
6161
*
@@ -78,7 +78,7 @@ class Row
7878
{
7979
return at(aName);
8080
}
81-
81+
8282
/**
8383
* @return Column with given index
8484
*

src/Row.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ namespace SQLite
2020
Row::Row(TStatementWeakPtr apStatement, std::size_t aID) :
2121
mpStatement(apStatement), mID(aID)
2222
{
23-
checkStatement();
2423
auto statement = mpStatement.lock();
25-
mColumnCount = statement->mColumnCount;
24+
if (statement)
25+
mColumnCount = statement->mColumnCount;
2626
}
2727

2828
Column Row::at(int_fast16_t aIndex) const

tests/Iterators.cpp

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* @file Iterators.cpp
3+
* @ingroup tests
4+
* @brief Test of Statement iterators
5+
*
6+
* Copyright (c) 2022 Kacper Zielinski ([email protected])
7+
*
8+
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
9+
* or copy at http://opensource.org/licenses/MIT)
10+
*/
11+
12+
#include <SQLiteCpp/Database.h>
13+
#include <SQLiteCpp/Statement.h>
14+
15+
#include <gtest/gtest.h>
16+
17+
#include <vector>
18+
#include <string>
19+
#include <algorithm>
20+
21+
SQLite::Database createDatabase()
22+
{
23+
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
24+
EXPECT_EQ(SQLite::OK, db.exec("CREATE TABLE test (number INTEGER, number_str TEXT)"));
25+
26+
SQLite::Statement inserter(db, "INSERT INTO test VALUES(?,?)");
27+
for (int i = 10; i > 0; --i)
28+
{
29+
inserter.bind(1, i);
30+
inserter.bind(2, std::to_string(i));
31+
EXPECT_EQ(1, inserter.exec());
32+
EXPECT_NO_THROW(inserter.reset());
33+
}
34+
return db;
35+
}
36+
37+
TEST(Iterators, RowIterator)
38+
{
39+
auto db = createDatabase();
40+
41+
SQLite::Statement query(db, "SELECT * FROM test");
42+
std::vector<int> numbers;
43+
44+
for (const auto& row : query)
45+
{
46+
numbers.push_back(row[0]);
47+
}
48+
49+
//TODO: EXPECT_TRUE(query.isDone());
50+
51+
const auto v_size = static_cast<int>(numbers.size());
52+
for (int i = v_size; i > 0; --i)
53+
{
54+
EXPECT_EQ(i, numbers[v_size - i]);
55+
}
56+
57+
auto it = query.begin();
58+
++it;
59+
EXPECT_STREQ("number_str", it->getColumn(1).getName());
60+
EXPECT_EQ(1, it->getColumnIndex("number_str"));
61+
62+
// getColumn aliases
63+
EXPECT_EQ(9, it->at(0).getInt());
64+
EXPECT_EQ(9, it->getColumn(0).getInt());
65+
EXPECT_EQ(9, it->operator[](0).getInt());
66+
67+
auto it2 = query.begin();
68+
++it2;
69+
EXPECT_EQ(it, it2);
70+
71+
// RowInterator is advancing common statement object
72+
++it;
73+
EXPECT_EQ(it->at(0).getInt(), it2->at(0).getInt());
74+
// But iterators internal state is diffrent.
75+
EXPECT_NE(it, it2);
76+
}
77+
78+
TEST(Iterators, RowIterator_STL_Algorithms)
79+
{
80+
auto db = createDatabase();
81+
82+
SQLite::Statement query(db, "SELECT * FROM test");
83+
84+
for (auto it = query.begin(); it != query.end(); std::advance(it, 3))
85+
{
86+
EXPECT_TRUE(it->getRowNumber() % 3 == 0);
87+
}
88+
89+
EXPECT_TRUE(std::all_of(query.begin(), query.end(), [](const SQLite::Row& row)
90+
{
91+
return row[0].getInt() > 0;
92+
}));
93+
}
94+
95+
TEST(Iterators, ColumnIterator)
96+
{
97+
auto db = createDatabase();
98+
99+
SQLite::Statement query_only1(db, "SELECT * FROM test LIMIT 1");
100+
std::vector<std::string> numbers_str;
101+
102+
for(const auto& row : query_only1)
103+
for (const auto& column : row)
104+
{
105+
numbers_str.emplace_back(column.getText());
106+
}
107+
108+
for (const auto& column : numbers_str)
109+
{
110+
EXPECT_EQ("10", column);
111+
}
112+
}
113+
114+
TEST(Iterators, ColumnIterator_STL_Algorithms)
115+
{
116+
auto db = createDatabase();
117+
118+
SQLite::Statement query_only1(db, "SELECT * FROM test LIMIT 1");
119+
std::vector<int> numbers;
120+
121+
for (const auto& row : query_only1)
122+
{
123+
EXPECT_EQ(2, std::count_if(row.begin(), row.end(), [i = 10](const SQLite::Column& column)
124+
{
125+
return column.getText() == std::to_string(i);
126+
}));
127+
}
128+
}

0 commit comments

Comments
 (0)