Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Lightweight/DataMapper/BelongsTo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class BelongsTo
}

private:
void RequireLoaded()
void RequireLoaded() const
{
if (_loaded)
return;
Expand All @@ -346,9 +346,9 @@ class BelongsTo

ValueType _referencedFieldValue {};
Loader _loader {};
bool _loaded = false;
bool _modified = false;
std::unique_ptr<ReferencedRecord> _record {};
mutable bool _loaded = false;
mutable std::unique_ptr<ReferencedRecord> _record {};
};

template <auto ReferencedField, auto ColumnNameOverrideString, SqlNullable Nullable>
Expand Down
26 changes: 26 additions & 0 deletions src/tests/DataMapper/RelationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,4 +620,30 @@ TEST_CASE_METHOD(SqlTestFixture, "BelongsTo Optinal records", "[DataMapper]")
REQUIRE(!nullableFKUserNotSet.user.Record().transform(Light::Unwrap).value_or(User{}).id.Value());
}

bool CheckFieldInEntityConstCorrectness(auto const& nullableFKUser)
{
return static_cast<bool>(nullableFKUser.id.Value());
}

bool CheckBelongsToInEntityConstCorrectness(auto const& nullableFKUser)
{
return nullableFKUser.user.Record().transform(Light::Unwrap).has_value();
}

TEST_CASE_METHOD(SqlTestFixture, "Entity const corectness", "[DataMapper]")
{
auto dm = DataMapper();

dm.CreateTables<User, NullableForeignKeyUser>();

auto user = User { .id = SqlGuid::Create(), .name = "John Doe" };
dm.Create(user);

auto nullableFKUser = NullableForeignKeyUser { .user = user };
dm.Create(nullableFKUser);

REQUIRE(CheckFieldInEntityConstCorrectness(nullableFKUser));
REQUIRE(CheckBelongsToInEntityConstCorrectness(nullableFKUser));
}

// NOLINTEND(bugprone-unchecked-optional-access)
Loading