From a305c1362f1fb96c5cc80e2185802bd143f5b579 Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Wed, 7 Jul 2021 15:51:01 +0200 Subject: [PATCH] WIP: retrieve simple entity --- CMakeLists.txt | 11 +++++ README.md | 8 ++++ test/CMakeLists.txt | 1 + test/caosdb_test_utility.h | 28 ++++++------ test/test_connection.cpp | 89 +++++++++++++++++++------------------- test/test_transaction.cpp | 34 +++++++++------ 6 files changed, 99 insertions(+), 72 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 064babc..681fb86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,3 +49,14 @@ add_compile_options(-Wno-unused-parameter -Wno-unused-result -g) set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) enable_testing() add_subdirectory(test) + + +####################################################### +### code formatting with clang-format +####################################################### +option(AUTOFORMATTING "call clang-format at configure time" ON) +if(AUTOFORMATTING) + file(GLOB format_test_sources test/*.cpp test/*.h) + execute_process(COMMAND clang-format -i --verbose ${format_test_sources} + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) +endif() diff --git a/README.md b/README.md index e7c3670..551117a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,14 @@ Integration tests for caosdb-cpplib and the caosdb-server +# Dependencies + +* clang-tidy +* clang-format +* include-what-you-use +* cmake +* conan + # Run tests 1. `mkdir build && cd build/` diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2e34797..82c8411 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -23,6 +23,7 @@ ####################################################################### set(test_cases test_connection + test_transaction ) ####################################################### diff --git a/test/caosdb_test_utility.h b/test/caosdb_test_utility.h index da0ddb3..4c97c20 100644 --- a/test/caosdb_test_utility.h +++ b/test/caosdb_test_utility.h @@ -31,18 +31,18 @@ * @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) +#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 5609ac4..7540a90 100644 --- a/test/test_connection.cpp +++ b/test/test_connection.cpp @@ -19,21 +19,22 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. * */ -#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/info.h" // for VersionInfo -#include "caosdb/utils.h" // for get_env_var -#include "gtest/gtest_pred_impl.h" // for Test, TEST, EXPECT_EQ, EXPECT_THROW +#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/info.h" // for VersionInfo +#include "caosdb/utils.h" // for get_env_var +#include "gtest/gtest_pred_impl.h" // for Test, TEST, EXPECT_EQ, EXPECT_THROW +#include "caosdb_test_utility.h" namespace caosdb::connection { +using caosdb::authentication::PlainPasswordAuthenticator; using caosdb::exceptions::AuthenticationError; using caosdb::exceptions::ConnectionError; -using caosdb::authentication::PlainPasswordAuthenticator; TEST(test_connection, config_somehost_25323) { auto port = 25323; @@ -53,26 +54,12 @@ TEST(test_connection, connect_somehost_42347_fails) { EXPECT_THROW(connection.GetVersionInfo(), ConnectionError); } -TEST(test_connection, connection_insecure_authentication_error_wrong_credentials) { - 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 *const user = "not-a-user-23461237"; - const auto *const password = "very-c-cred"; - - auto auth = std::make_shared<PlainPasswordAuthenticator>(user, password); - auto config = std::make_shared<InsecureCaosDBConnectionConfig>(host, port, auth); - auto connection = CaosDBConnection(config); - - EXPECT_THROW(connection.GetVersionInfo(), AuthenticationError); -} - - TEST(test_connection, connection_insecure_authentication_error_anonymous) { - auto port_str = caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTP", "8080"); + 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 host = + caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost"); auto config = std::make_shared<InsecureCaosDBConnectionConfig>(host, port); auto connection = CaosDBConnection(config); @@ -81,39 +68,52 @@ TEST(test_connection, connection_insecure_authentication_error_anonymous) { } TEST(test_connection, connection_ssl_authentication_error_anonymous) { - auto port_str = caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443"); + auto port_str = + caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443"); auto port = std::stoi(port_str); - const auto host = caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost"); - const auto path = caosdb::utils::get_env_var("CAOSDB_SERVER_CA_PEM", std::string()); + const auto host = + caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost"); + const auto path = + caosdb::utils::get_env_var("CAOSDB_SERVER_CA_PEM", std::string()); auto ssloptions = std::make_shared<PemFileCACertProvider>(path); - auto config = std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions); + auto config = + std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions); auto connection = CaosDBConnection(config); - EXPECT_THROW(connection.GetVersionInfo(), AuthenticationError); + EXPECT_THROW_MESSAGE(connection.GetVersionInfo(), AuthenticationError, + "Please Login."); } TEST(test_connection, connection_ssl_authentication_error_wrong_credentials) { - auto port_str = caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443"); + auto port_str = + caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443"); auto port = std::stoi(port_str); - const auto host = caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost"); - const auto path = caosdb::utils::get_env_var("CAOSDB_SERVER_CA_PEM", std::string()); + const auto host = + caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost"); + const auto path = + caosdb::utils::get_env_var("CAOSDB_SERVER_CA_PEM", std::string()); const auto *const user = "not-a-user-23461237"; const auto *const password = "very-c-cred"; auto auth = std::make_shared<PlainPasswordAuthenticator>(user, password); auto ssloptions = std::make_shared<PemFileCACertProvider>(path); - auto config = std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions, auth); + auto config = + std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions, auth); auto connection = CaosDBConnection(config); - EXPECT_THROW(connection.GetVersionInfo(), AuthenticationError); + EXPECT_THROW_MESSAGE(connection.GetVersionInfo(), AuthenticationError, + "Authentication failed. Username or password wrong."); } TEST(test_connection, connection_ssl_authentication_success) { - auto port_str = caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443"); + auto port_str = + caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443"); auto port = std::stoi(port_str); - const auto host = caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost"); - const auto path = caosdb::utils::get_env_var("CAOSDB_SERVER_CA_PEM", std::string()); + const auto host = + caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost"); + const auto path = + caosdb::utils::get_env_var("CAOSDB_SERVER_CA_PEM", std::string()); const auto user = caosdb::utils::get_env_var("CAOSDB_USER", "admin"); const auto password = caosdb::utils::get_env_var("CAOSDB_PASSWORD", "caosdb"); @@ -123,7 +123,8 @@ TEST(test_connection, connection_ssl_authentication_success) { auto auth = std::make_shared<PlainPasswordAuthenticator>(user, password); auto ssloptions = std::make_shared<PemFileCACertProvider>(path); - auto config = std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions, auth); + auto config = + std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions, auth); auto connection = CaosDBConnection(config); auto v_info = connection.GetVersionInfo(); @@ -132,4 +133,4 @@ TEST(test_connection, connection_ssl_authentication_success) { EXPECT_EQ(pre_release, v_info->GetPreRelease()); } -} //namespace caosdb::connection +} // namespace caosdb::connection diff --git a/test/test_transaction.cpp b/test/test_transaction.cpp index 99f5046..e02dd7f 100644 --- a/test/test_transaction.cpp +++ b/test/test_transaction.cpp @@ -18,30 +18,36 @@ * 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 +#include <gtest/gtest-message.h> // for Message +#include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestFactoryImpl +#include <memory> // for allocator, make_shared, static_po... +#include <string> // for stoi +#include "caosdb/authentication.h" // for PlainPasswordAuthenticator +#include "caosdb/connection.h" // for InsecureCaosDBConnectionConfig +#include "caosdb/entity.h" // for Entity, EntityID +#include "caosdb/transaction.h" // for Transaction, UniqueResult, Entity +#include "caosdb/utils.h" // for get_env_var +#include "gtest/gtest_pred_impl.h" // for Test, TestInfo, EXPECT_EQ, TEST namespace caosdb::transaction { +using caosdb::authentication::PlainPasswordAuthenticator; +using caosdb::connection::CaosDBConnection; +using caosdb::connection::InsecureCaosDBConnectionConfig; TEST(test_transaction, first_test) { - auto port_str = caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTP", "8080"); + 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 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 config = + std::make_shared<InsecureCaosDBConnectionConfig>(host, port, auth); auto connection = CaosDBConnection(config); auto transaction(connection.CreateTransaction()); @@ -53,4 +59,4 @@ TEST(test_transaction, first_test) { EXPECT_EQ(description, result_set->GetEntity().GetDescription()); } -} //namespace caosdb::connection +} // namespace caosdb::transaction -- GitLab