|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2026 Ioannis Kaliakatsos |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +#include <string> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +#include "database.h" |
| 29 | +#include "person.h" |
| 30 | +#include "vector.h" |
| 31 | + |
| 32 | +using namespace sqlite_reflection; |
| 33 | + |
| 34 | +namespace { |
| 35 | +class FunctionalVectorTest : public ::testing::Test { |
| 36 | + void SetUp() override { |
| 37 | + Database::Initialize(""); |
| 38 | + } |
| 39 | + |
| 40 | + void TearDown() override { |
| 41 | + Database::Finalize(); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +// The whole point of the migration: a fetch result is an fcpp::vector, so functional operations |
| 46 | +// can be chained directly on it, without the caller wrapping the std::vector themselves. |
| 47 | +TEST_F(FunctionalVectorTest, FetchResultChainsFilterAndMap) { |
| 48 | + const auto db = Database::Instance(); |
| 49 | + db->Save(std::vector<Person>{ |
| 50 | + {L"ada", L"lovelace", 36, true, 1}, |
| 51 | + {L"grace", L"hopper", 17, true, 2}, |
| 52 | + {L"alan", L"turing", 41, true, 3}, |
| 53 | + }); |
| 54 | + |
| 55 | + // filter() (mutating, returns vector&) then map<U>() (non-mutating) chained on the temporary |
| 56 | + // returned by FetchAll(), exactly as advertised in the README. |
| 57 | + const auto adult_names = db->FetchAll<Person>() |
| 58 | + .filter([](const Person& p) { return p.age >= 18; }) |
| 59 | + .map<std::wstring>([](const Person& p) { return p.GetFullName(); }); |
| 60 | + |
| 61 | + ASSERT_EQ(2u, adult_names.size()); |
| 62 | + EXPECT_EQ(L"ada lovelace", adult_names[0]); |
| 63 | + EXPECT_EQ(L"alan turing", adult_names[1]); |
| 64 | +} |
| 65 | + |
| 66 | +TEST_F(FunctionalVectorTest, FetchResultSupportsAllOfAnyOf) { |
| 67 | + const auto db = Database::Instance(); |
| 68 | + db->Save(std::vector<Person>{ |
| 69 | + {L"ada", L"lovelace", 36, true, 1}, |
| 70 | + {L"grace", L"hopper", 17, true, 2}, |
| 71 | + }); |
| 72 | + |
| 73 | + const auto people = db->FetchAll<Person>(); |
| 74 | + EXPECT_TRUE(people.all_of([](const Person& p) { return p.age > 0; })); |
| 75 | + EXPECT_FALSE(people.all_of([](const Person& p) { return p.age >= 18; })); |
| 76 | + EXPECT_TRUE(people.any_of([](const Person& p) { return p.age < 18; })); |
| 77 | + EXPECT_FALSE(people.any_of([](const Person& p) { return p.age > 100; })); |
| 78 | +} |
| 79 | + |
| 80 | +// range-for and indexing over a fetch result keep working as with a std::vector |
| 81 | +TEST_F(FunctionalVectorTest, FetchResultSupportsRangeForAndIndexing) { |
| 82 | + const auto db = Database::Instance(); |
| 83 | + db->Save(std::vector<Person>{ |
| 84 | + {L"ada", L"lovelace", 36, true, 1}, |
| 85 | + {L"alan", L"turing", 41, true, 2}, |
| 86 | + }); |
| 87 | + |
| 88 | + const auto people = db->FetchAll<Person>(); |
| 89 | + |
| 90 | + int64_t total_age = 0; |
| 91 | + for (const auto& person : people) { |
| 92 | + total_age += person.age; |
| 93 | + } |
| 94 | + EXPECT_EQ(77, total_age); |
| 95 | + EXPECT_EQ(L"ada", people[0].first_name); |
| 96 | + EXPECT_EQ(L"alan", people[1].first_name); |
| 97 | +} |
| 98 | + |
| 99 | +// Batch Save/Update accept an fcpp::vector directly. |
| 100 | +TEST_F(FunctionalVectorTest, BatchSaveAndUpdateAcceptFcppVector) { |
| 101 | + const auto db = Database::Instance(); |
| 102 | + |
| 103 | + fcpp::vector<Person> people; |
| 104 | + people.insert_back({L"ada", L"lovelace", 36, true, 1}).insert_back({L"alan", L"turing", 41, true, 2}); |
| 105 | + |
| 106 | + db->Save(people); |
| 107 | + |
| 108 | + auto fetched = db->FetchAll<Person>(); |
| 109 | + ASSERT_EQ(2u, fetched.size()); |
| 110 | + EXPECT_EQ(36, fetched[0].age); |
| 111 | + |
| 112 | + people[0].age = 37; |
| 113 | + db->Update(people); |
| 114 | + |
| 115 | + fetched = db->FetchAll<Person>(); |
| 116 | + ASSERT_EQ(2u, fetched.size()); |
| 117 | + EXPECT_EQ(37, fetched[0].age); |
| 118 | +} |
| 119 | + |
| 120 | +// SaveAutoIncrement writes the generated ids back into the passed-in fcpp::vector. |
| 121 | +TEST_F(FunctionalVectorTest, SaveAutoIncrementWritesIdsBackIntoFcppVector) { |
| 122 | + const auto db = Database::Instance(); |
| 123 | + |
| 124 | + fcpp::vector<Person> people; |
| 125 | + people.insert_back({L"ada", L"lovelace", 36, true}).insert_back({L"alan", L"turing", 41, true}); |
| 126 | + |
| 127 | + db->SaveAutoIncrement(people); |
| 128 | + |
| 129 | + EXPECT_EQ(1, people[0].id); |
| 130 | + EXPECT_EQ(2, people[1].id); |
| 131 | + EXPECT_EQ(2u, db->FetchAll<Person>().size()); |
| 132 | +} |
| 133 | + |
| 134 | +// The std::vector compatibility overloads keep working for callers that still pass a std::vector. |
| 135 | +TEST_F(FunctionalVectorTest, StdVectorCompatibilityOverloadsStillWork) { |
| 136 | + const auto db = Database::Instance(); |
| 137 | + |
| 138 | + std::vector<Person> people; |
| 139 | + people.push_back({L"ada", L"lovelace", 36, true, 1}); |
| 140 | + people.push_back({L"alan", L"turing", 41, true, 2}); |
| 141 | + |
| 142 | + db->Save(people); |
| 143 | + EXPECT_EQ(2u, db->FetchAll<Person>().size()); |
| 144 | + |
| 145 | + people[0].age = 37; |
| 146 | + db->Update(people); |
| 147 | + EXPECT_EQ(37, db->Fetch<Person>(1).age); |
| 148 | +} |
| 149 | + |
| 150 | +// The SaveAutoIncrement std::vector wrapper writes the generated ids back into the caller's |
| 151 | +// std::vector, just like the fcpp::vector overload. |
| 152 | +TEST_F(FunctionalVectorTest, SaveAutoIncrementStdVectorWrapperWritesIdsBack) { |
| 153 | + const auto db = Database::Instance(); |
| 154 | + |
| 155 | + std::vector<Person> people; |
| 156 | + people.push_back({L"ada", L"lovelace", 36, true}); |
| 157 | + people.push_back({L"alan", L"turing", 41, true}); |
| 158 | + |
| 159 | + db->SaveAutoIncrement(people); |
| 160 | + |
| 161 | + EXPECT_EQ(1, people[0].id); |
| 162 | + EXPECT_EQ(2, people[1].id); |
| 163 | + EXPECT_EQ(2u, db->FetchAll<Person>().size()); |
| 164 | +} |
| 165 | +} // namespace |
0 commit comments