From 20f979b494b291f9e3a677ef959fe627015810f1 Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Wed, 7 Jul 2021 12:40:22 +0200 Subject: [PATCH] WIP: retrieve single entity --- test/CMakeLists.txt | 3 +- test/caosdb_test_utility.h | 48 ++++++++++++++++++++++++++++++++ test/test_connection.cpp | 10 +++---- test/test_transaction.cpp | 56 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 test/caosdb_test_utility.h create mode 100644 test/test_transaction.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fb806c3..2e34797 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -76,7 +76,8 @@ foreach (i RANGE "${len_test_cases}") ${CONAN_LIBS_GTEST} ${CONAN_LIBS_GRPC} ${CONAN_LIBS_ABSEIL} ${CONAN_LIBS_OPENSSL} ${CONAN_LIBS_C-ARES} ${CONAN_LIBS_BZIP2} ${CONAN_LIBS_PROTOBUF} ${CONAN_LIBS_ZLIB} ${CONAN_LIBS_RE2}) - target_include_directories(${test_case_name} PUBLIC ${CONAN_INCLUDE_DIRS}) + target_include_directories(${test_case_name} + PUBLIC ${CONAN_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}) set_target_properties(${test_case_name} PROPERTIES CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY};${_CMAKE_CXX_CLANG_TIDY_CHECKS}" diff --git a/test/caosdb_test_utility.h b/test/caosdb_test_utility.h new file mode 100644 index 0000000..da0ddb3 --- /dev/null +++ b/test/caosdb_test_utility.h @@ -0,0 +1,48 @@ +/* + * + * This file is a part of the CaosDB Project. + * + * Copyright (C) 2021 Timm Fitschen <t.fitschen@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/>. + * + */ + +#ifndef CAOSDB_TEST_UTILITY_H +#define CAOSDB_TEST_UTILITY_H +/** + * WARNING: THIS FILE IS MAINTAINED AS PART OF THE caosdb-cpplib. Please commit + * changes there and keep these files in sync! + * + * @file caosdb_test_utility.h + * @brief Utility for the unit tests + * @author Timm Fitschen + * @date 2021-07-07 + */ +#define EXPECT_THROW_MESSAGE(statement, exeption_type, message) EXPECT_THROW( \ + try { \ + statement; \ + } catch (const exeption_type& e) { \ + EXPECT_EQ(std::string(e.what()), message); \ + throw; \ + }, exeption_type) +#define ASSERT_THROW_MESSAGE(statement, exeption_type, message) ASSERT_THROW( \ + try { \ + statement; \ + } catch (const exeption_type& e) { \ + ASSERT_EQ(std::string(e.what()), message); \ + throw; \ + }, exeption_type) +#endif diff --git a/test/test_connection.cpp b/test/test_connection.cpp index 40fd4d4..5609ac4 100644 --- a/test/test_connection.cpp +++ b/test/test_connection.cpp @@ -50,7 +50,7 @@ TEST(test_connection, connect_somehost_42347_fails) { auto config = std::make_shared<InsecureCaosDBConnectionConfig>(pHost, port); CaosDBConnection connection(config); - EXPECT_THROW(connection.getVersionInfo(), ConnectionError); + EXPECT_THROW(connection.GetVersionInfo(), ConnectionError); } TEST(test_connection, connection_insecure_authentication_error_wrong_credentials) { @@ -65,7 +65,7 @@ TEST(test_connection, connection_insecure_authentication_error_wrong_credentials auto config = std::make_shared<InsecureCaosDBConnectionConfig>(host, port, auth); auto connection = CaosDBConnection(config); - EXPECT_THROW(connection.getVersionInfo(), AuthenticationError); + EXPECT_THROW(connection.GetVersionInfo(), AuthenticationError); } @@ -77,7 +77,7 @@ TEST(test_connection, connection_insecure_authentication_error_anonymous) { auto config = std::make_shared<InsecureCaosDBConnectionConfig>(host, port); auto connection = CaosDBConnection(config); - EXPECT_THROW(connection.getVersionInfo(), AuthenticationError); + EXPECT_THROW(connection.GetVersionInfo(), AuthenticationError); } TEST(test_connection, connection_ssl_authentication_error_anonymous) { @@ -90,7 +90,7 @@ TEST(test_connection, connection_ssl_authentication_error_anonymous) { auto config = std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions); auto connection = CaosDBConnection(config); - EXPECT_THROW(connection.getVersionInfo(), AuthenticationError); + EXPECT_THROW(connection.GetVersionInfo(), AuthenticationError); } TEST(test_connection, connection_ssl_authentication_error_wrong_credentials) { @@ -106,7 +106,7 @@ TEST(test_connection, connection_ssl_authentication_error_wrong_credentials) { auto config = std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions, auth); auto connection = CaosDBConnection(config); - EXPECT_THROW(connection.getVersionInfo(), AuthenticationError); + EXPECT_THROW(connection.GetVersionInfo(), AuthenticationError); } TEST(test_connection, connection_ssl_authentication_success) { diff --git a/test/test_transaction.cpp b/test/test_transaction.cpp new file mode 100644 index 0000000..99f5046 --- /dev/null +++ b/test/test_transaction.cpp @@ -0,0 +1,56 @@ +/* + * This file is a part of the CaosDB Project. + * + * Copyright (C) 2021 Timm Fitschen <t.fitschen@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/transaction.h" +#include <gtest/gtest-message.h> // for Message +#include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestPartResult +#include <memory> // for make_shared, allocator, shared_ptr +#include <string> // for string +#include "caosdb/authentication.h" // for PlainPasswordAuthenticator +#include "caosdb/connection.h" // for InsecureCaosDBConnectionConfig +#include "caosdb/exceptions.h" // for AuthenticationError, ConnectionError +#include "caosdb/utils.h" // for get_env_var +#include "gtest/gtest_pred_impl.h" // for Test, TEST, EXPECT_EQ, EXPECT_THROW + +namespace caosdb::transaction { + +TEST(test_transaction, first_test) { + auto port_str = caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTP", "8080"); + auto port = std::stoi(port_str); + const auto host = caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost"); + const auto user = caosdb::utils::get_env_var("CAOSDB_USER", "admin"); + const auto password = caosdb::utils::get_env_var("CAOSDB_PASSWORD", "caosdb"); + + const auto *const description = "This is an entity"; + + auto auth = std::make_shared<PlainPasswordAuthenticator>(user, password); + auto config = std::make_shared<SslCaosDBConnectionConfig>(host, port, auth); + auto connection = CaosDBConnection(config); + + auto transaction(connection.CreateTransaction()); + transaction->Retrieve(caosdb::entity::EntityID("someid")); + transaction->Execute(); + auto result_set(std::static_pointer_cast<caosdb::transaction::UniqueResult>( + transaction->GetResultSet())); + + EXPECT_EQ(description, result_set->GetEntity().GetDescription()); +} + +} //namespace caosdb::connection -- GitLab