diff --git a/CHANGELOG.md b/CHANGELOG.md index 279c052b6947e2b2e198f5cc8e6d2c3592e0b555..ec80782f4ac28d9e9081599f0ccc3639a2bb6a8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,4 +22,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +* #11 - Transaction::GetResultSet() now always returns a valid reference. + ### Security diff --git a/include/caosdb/transaction.h b/include/caosdb/transaction.h index 621c6cbd80057c0f97d171f2ffac91e65b77498f..f92cf28f8ed670177629f5534cb9777f25e1fa1b 100644 --- a/include/caosdb/transaction.h +++ b/include/caosdb/transaction.h @@ -365,6 +365,10 @@ public: [[nodiscard]] inline auto GetStatus() const noexcept -> TransactionStatus { return this->status; } [[nodiscard]] inline auto GetResultSet() const noexcept -> const ResultSet & { + if (!this->result_set) { + this->result_set = std::make_unique<MultiResultSet>( + std::move(std::vector<std::unique_ptr<Entity>>())); + } return *(this->result_set.get()); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6edff54857aca64b64c99a47ea8344ebf9eee265..6049d0fa369c8e6c2a95bec554c0946e5a2b4aa3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,7 @@ set(test_cases test_entity test_file_transmission test_info + test_issues test_list_properties test_protobuf test_transaction diff --git a/test/test_issues.cpp b/test/test_issues.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4f560dd7e60332e83843a26eb43a7ea2187474b --- /dev/null +++ b/test/test_issues.cpp @@ -0,0 +1,46 @@ +/* + * This file is a part of the CaosDB Project. + * + * Copyright (C) 2021 Daniel Hornung <d.hornung@indiscale.com> + * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +#include "caosdb/configuration.h" // for InsecureConnectionConfig... +#include "caosdb/connection.h" // for Connection +#include "caosdb/exceptions.h" // for ConnectionError +#include "caosdb/status_code.h" +#include "caosdb/transaction.h" // for Transaction +#include <gtest/gtest-message.h> // for Message +#include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestPa... +#include <gtest/gtest_pred_impl.h> // for Test, TestInfo, TEST + +namespace caosdb::transaction { +using caosdb::configuration::InsecureConnectionConfiguration; +using caosdb::connection::Connection; + +TEST(test_issues, test_issue_11) { + const auto *host = "localhost"; + auto configuration = InsecureConnectionConfiguration(host, 8000); + Connection connection(configuration); + auto transaction = connection.CreateTransaction(); + + ASSERT_EQ(transaction->GetResultSet().size(), 0); + transaction->RetrieveById("100"); + ASSERT_EQ(StatusCode::EXECUTING, transaction->ExecuteAsynchronously()); + // Trying to obtain ResultSet while it is still empty. + ASSERT_EQ(transaction->GetResultSet().size(), 0); +} + +} // namespace caosdb::transaction