diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bad2a261a065f54e4a9de0170d90c34da5c7df24..347d0170ae23c71b1e140cbd1bb4c715d2042c83 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -22,6 +22,7 @@ ### append test cases here (file name without the ".cpp" suffix) ####################################################################### set(test_cases + test_async test_connection test_list_properties test_properties diff --git a/test/test_async.cpp b/test/test_async.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8049865abfb74b662c9f2e63619da80fcc7376b --- /dev/null +++ b/test/test_async.cpp @@ -0,0 +1,73 @@ +/* + * This file is a part of the CaosDB Project. + * + * Copyright (C) 2022 IndiScale GmbH <info@indiscale.com> + * Copyright (C) 2022 Timm Fitschen <t.fitschen@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/entity.h" // for Entity, Property +#include "caosdb/connection.h" // for Connection, Connec... +#include "caosdb/message_code.h" // for ENTITY_DOES_NOT_EXIST, Messag... +#include "caosdb/status_code.h" // for StatusCode, SUCCESS +#include "caosdb/transaction.h" // for Entity, Transaction +#include "caosdb/transaction_status.h" // for TransactionStatus +#include <chrono> // for operator""ms, chrono_literals +#include <gtest/gtest-message.h> // for Message +#include <gtest/gtest-test-part.h> // for TestPartResult +#include <gtest/gtest_pred_impl.h> // for AssertionResult +#include <memory> // for allocator, unique_ptr, __shar... +#include <thread> + +namespace caosdb::transaction { +using caosdb::entity::MessageCode; + +/* + * Test the retrieval of a non-existing entity + * + * The transaction is being executed and we can retrieve the state in the mean + * time. + */ +TEST(test_async, retrieve_non_existing) { + using namespace std::chrono_literals; + const auto &connection = caosdb::connection::ConnectionManager::GetDefaultConnection(); + + auto transaction(connection->CreateTransaction()); + + const auto *id = "non-existing-id"; + transaction->RetrieveById(id); + transaction->ExecuteAsynchronously(); + + auto status = transaction->GetStatus(); + EXPECT_EQ(status.GetCode(), StatusCode::EXECUTING); + + // wait some time + std::this_thread::sleep_for(1000ms); + + // DONT call WaitForIt -> the transaction finishes in the back-ground + status = transaction->GetStatus(); + EXPECT_EQ(status.GetCode(), TransactionStatus::TRANSACTION_ERROR().GetCode()); + ASSERT_EQ(status.GetCode(), StatusCode::GENERIC_TRANSACTION_ERROR); + + const auto &result_set = transaction->GetResultSet(); + + const auto &entity = result_set.at(0); + EXPECT_EQ(id, entity.GetId()); + EXPECT_TRUE(entity.HasErrors()); + ASSERT_EQ(entity.GetErrors().size(), 1); + EXPECT_EQ(entity.GetErrors().at(0).GetCode(), MessageCode::ENTITY_DOES_NOT_EXIST); +} + +} // namespace caosdb::transaction