|
| 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