Skip to content
Snippets Groups Projects
Verified Commit d1852b10 authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: retrieve simple entity

parent 60379439
No related branches found
No related tags found
No related merge requests found
Pipeline #9974 passed
Pipeline: caosdb-cppinttest

#9983

    ...@@ -286,7 +286,7 @@ install(FILES ${PROJECT_SOURCE_DIR}/caosdbConfigVersion.cmake ...@@ -286,7 +286,7 @@ install(FILES ${PROJECT_SOURCE_DIR}/caosdbConfigVersion.cmake
    ####################################################### #######################################################
    option(AUTOFORMATTING "call clang-format at configure time" ON) option(AUTOFORMATTING "call clang-format at configure time" ON)
    if(AUTOFORMATTING) if(AUTOFORMATTING)
    file(GLOB format_test_sources test/*.cpp) file(GLOB format_test_sources test/*.cpp test/*.h)
    execute_process(COMMAND clang-format -i --verbose ${libcaosdb_INCL} execute_process(COMMAND clang-format -i --verbose ${libcaosdb_INCL}
    ${libcaosdb_SRC} ${libcaosdb_TEST_SRC} ${libcaosdb_SRC} ${libcaosdb_TEST_SRC}
    ${PROJECT_SOURCE_DIR}/src/caosdbcli.cpp ${PROJECT_SOURCE_DIR}/src/caosdbcli.cpp
    ......
    ...@@ -48,6 +48,10 @@ The coverage report can be viewed in a browser by opening ...@@ -48,6 +48,10 @@ The coverage report can be viewed in a browser by opening
    * install clang-format on your system. * install clang-format on your system.
    * `clang-format -i --verbose $(find test/ src/ include/ -type f -iname "*.cpp" -o -iname "*.h" -o -iname "*.h.in")` * `clang-format -i --verbose $(find test/ src/ include/ -type f -iname "*.cpp" -o -iname "*.h" -o -iname "*.h.in")`
    ## Naming Convention
    Please adhere to [Google's C++ naming conventions](https://google.github.io/styleguide/cppguide.html#Naming).
    ## Documentation ## Documentation
    In the build directory, run In the build directory, run
    ......
    ...@@ -25,20 +25,23 @@ ...@@ -25,20 +25,23 @@
    #ifndef CAOSDB_ENTITY_H #ifndef CAOSDB_ENTITY_H
    #define CAOSDB_ENTITY_H #define CAOSDB_ENTITY_H
    #include <string> #include <memory> // for shared_ptr
    #include <string> // for string
    #include "caosdb/entity/v1alpha1/main.pb.h" // for Entity
    namespace caosdb::entity { namespace caosdb::entity {
    using ProtoEntity = caosdb::entity::v1alpha1::Entity;
    /** /**
    * Entity ID. * Entity ID.
    */ */
    class EntityID { class EntityID {
    private: private:
    std::string id; std::shared_ptr<std::string> id;
    public: public:
    EntityID(std::string id); explicit EntityID(const std::string &id);
    [[nodiscard]] auto ToString() const -> std::string; explicit EntityID(const std::shared_ptr<std::string> &id);
    }; };
    /** /**
    ...@@ -47,11 +50,14 @@ public: ...@@ -47,11 +50,14 @@ public:
    class Entity { class Entity {
    private: private:
    EntityID id; EntityID id;
    std::shared_ptr<ProtoEntity> delegate;
    public: public:
    explicit Entity(ProtoEntity *delegate);
    [[nodiscard]] auto GetId() const -> EntityID; [[nodiscard]] auto GetId() const -> EntityID;
    auto SetId(EntityID id) -> void; auto SetId(EntityID id) -> void;
    [[nodiscard]] auto GetName() const -> std::string; [[nodiscard]] auto GetName() const -> std::string;
    [[nodiscard]] auto GetDescription() const -> std::string;
    }; };
    } // namespace caosdb::entity } // namespace caosdb::entity
    ......
    ...@@ -39,8 +39,8 @@ using caosdb::entity::v1alpha1::EntityTransactionService; ...@@ -39,8 +39,8 @@ using caosdb::entity::v1alpha1::EntityTransactionService;
    using caosdb::entity::v1alpha1::RetrieveRequest; using caosdb::entity::v1alpha1::RetrieveRequest;
    class ResultSet { class ResultSet {
    public: // public:
    [[nodiscard]] auto GetById(EntityID id) const -> Entity; //[[nodiscard]] auto GetEntityById(EntityID id) const -> Entity;
    }; };
    class UniqueResult : public ResultSet { class UniqueResult : public ResultSet {
    ...@@ -48,7 +48,8 @@ private: ...@@ -48,7 +48,8 @@ private:
    Entity entity; Entity entity;
    public: public:
    UniqueResult(ProtoEntity entity); UniqueResult(Entity entity);
    [[nodiscard]] auto GetEntity() const -> Entity;
    }; };
    enum TransactionState { INIT = 10, EXECUTING = 20, SUCCESS = 30, ERROR = 40 }; enum TransactionState { INIT = 10, EXECUTING = 20, SUCCESS = 30, ERROR = 40 };
    ......
    ...@@ -20,17 +20,24 @@ ...@@ -20,17 +20,24 @@
    */ */
    #include "caosdb/entity.h" #include "caosdb/entity.h"
    #include <utility> #include <utility>
    namespace caosdb::entity { namespace caosdb::entity {
    EntityID::EntityID(std::string id) { this->id = std::move(id); } EntityID::EntityID(const std::string &id) {
    this->id = std::make_shared<std::string>(id);
    }
    [[nodiscard]] auto EntityID::ToString() const -> std::string { EntityID::EntityID(const std::shared_ptr<std::string> &id) {
    return this->id; this->id = std::make_shared<std::string>(*id);
    } }
    Entity::Entity(ProtoEntity *delegate)
    : id(EntityID(delegate->eid())), delegate(delegate) {}
    [[nodiscard]] auto Entity::GetDescription() const -> std::string {
    return this->delegate->description();
    }
    [[nodiscard]] auto Entity::GetId() const -> EntityID { return this->id; } [[nodiscard]] auto Entity::GetId() const -> EntityID { return this->id; }
    auto Entity::SetId(EntityID id) -> void { this->id = std::move(id); } auto Entity::SetId(EntityID id) -> void { this->id = std::move(id); }
    ......
    /* /*
    * This file is a part of the CaosDB Project. * This file is a part of the CaosDB Project.
    *
    * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com> * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
    * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com> * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
    * *
    ...@@ -36,18 +35,24 @@ namespace caosdb::transaction { ...@@ -36,18 +35,24 @@ namespace caosdb::transaction {
    using caosdb::entity::v1alpha1::EntityTransactionService; using caosdb::entity::v1alpha1::EntityTransactionService;
    using caosdb::entity::v1alpha1::RetrieveRequest; using caosdb::entity::v1alpha1::RetrieveRequest;
    using caosdb::entity::v1alpha1::RetrieveResponse; using caosdb::entity::v1alpha1::RetrieveResponse;
    using ProtoEntity = caosdb::entity::v1alpha1::Entity;
    using caosdb::exceptions::AuthenticationError; using caosdb::exceptions::AuthenticationError;
    using caosdb::exceptions::ConnectionError; using caosdb::exceptions::ConnectionError;
    UniqueResult::UniqueResult(Entity entity) : entity(std::move(entity)) {}
    [[nodiscard]] auto UniqueResult::GetEntity() const -> Entity {
    return this->entity;
    }
    Transaction::Transaction( Transaction::Transaction(
    std::shared_ptr<EntityTransactionService::Stub> service_stub) { std::shared_ptr<EntityTransactionService::Stub> service_stub) {
    this->service_stub = std::move(service_stub); this->service_stub = std::move(service_stub);
    } }
    auto Transaction::Retrieve(const EntityID &id) -> void { auto Transaction::Retrieve(const EntityID & /*id*/) -> void {
    // TODO(tf)
    RetrieveRequest request; RetrieveRequest request;
    request.mutable_entity()->set_eid(id.ToString());
    this->request = request; this->request = request;
    } }
    ...@@ -72,8 +77,10 @@ auto Transaction::Execute() -> void { ...@@ -72,8 +77,10 @@ auto Transaction::Execute() -> void {
    throw std::runtime_error(status.error_message()); throw std::runtime_error(status.error_message());
    } }
    } }
    // TODO(tf)
    this->result_set = std::make_shared<ResultSet>(); auto entity = Entity(response.release_entity());
    auto result_set = std::make_shared<UniqueResult>(entity);
    this->result_set = result_set;
    } }
    auto Transaction::WaitForIt() const -> void { auto Transaction::WaitForIt() const -> void {
    ......
    ...@@ -46,11 +46,8 @@ auto main() -> int { ...@@ -46,11 +46,8 @@ auto main() -> int {
    const auto port = std::stoi(port_str); const auto port = std::stoi(port_str);
    const auto user = caosdb::utils::get_env_var("CAOSDB_USER", "admin"); const auto user = caosdb::utils::get_env_var("CAOSDB_USER", "admin");
    const auto password = caosdb::utils::get_env_var("CAOSDB_PASSWORD", "caosdb"); const auto password = caosdb::utils::get_env_var("CAOSDB_PASSWORD", "caosdb");
    std::cout << "USER: " << user << "\n";
    std::cout << "PORT: " << port_str << "\n";
    std::cout << "HOST: " << host << "\n";
    std::cout << "CACERT: " << pem_file << "\n";
    // setup the connection
    auto auth = auto auth =
    std::make_shared<caosdb::authentication::PlainPasswordAuthenticator>( std::make_shared<caosdb::authentication::PlainPasswordAuthenticator>(
    user, password); user, password);
    ...@@ -59,10 +56,24 @@ auto main() -> int { ...@@ -59,10 +56,24 @@ auto main() -> int {
    auto config = std::make_shared<caosdb::connection::SslCaosDBConnectionConfig>( auto config = std::make_shared<caosdb::connection::SslCaosDBConnectionConfig>(
    host, port, cacert, auth); host, port, cacert, auth);
    caosdb::connection::CaosDBConnection connection(config); caosdb::connection::CaosDBConnection connection(config);
    std::cout << std::endl << connection << std::endl;
    // get version info of the server
    const auto &v_info = connection.GetVersionInfo(); const auto &v_info = connection.GetVersionInfo();
    std::cout << "VersionInfo(" << v_info->GetMajor() << "." << v_info->GetMinor() std::cout << "Server Version: " << v_info->GetMajor() << "."
    << "." << v_info->GetPatch() << "-" << v_info->GetPreRelease() << v_info->GetMinor() << "." << v_info->GetPatch() << "-"
    << "-" << v_info->GetBuild() << ")" << std::endl; << v_info->GetPreRelease() << "-" << v_info->GetBuild()
    << std::endl;
    // retrieve an entity
    auto transaction(connection.CreateTransaction());
    transaction->Retrieve(caosdb::entity::EntityID("someid"));
    transaction->Execute();
    auto result_set(std::static_pointer_cast<caosdb::transaction::UniqueResult>(
    transaction->GetResultSet()));
    // print description
    std::cout << "Entity Description: "
    << result_set->GetEntity().GetDescription() << std::endl;
    return 0; return 0;
    } }
    ...@@ -26,14 +26,13 @@ set(test_cases ...@@ -26,14 +26,13 @@ set(test_cases
    test_utils test_utils
    ) )
    ################################################### ###################################################
    ### Set up tests using GoogleTest (GTest) ### Set up tests using GoogleTest (GTest)
    ################################################### ###################################################
    # special linting for tests # special linting for tests
    set(_CMAKE_CXX_CLANG_TIDY_TEST_CHECKS set(_CMAKE_CXX_CLANG_TIDY_TEST_CHECKS
    "${_CMAKE_CXX_CLANG_TIDY_CHECKS},-cert-err58-cpp,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-modernize-use-trailing-return-type,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers" "${_CMAKE_CXX_CLANG_TIDY_CHECKS},-cert-err58-cpp,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-modernize-use-trailing-return-type,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-cppcoreguidelines-avoid-goto,-hicpp-avoid-goto"
    ) )
    # add special cmake functions for gtest # add special cmake functions for gtest
    ...@@ -49,6 +48,8 @@ foreach (i RANGE "${len_test_cases}") ...@@ -49,6 +48,8 @@ foreach (i RANGE "${len_test_cases}")
    ${libcaosdb_TEST_SRC}") ${libcaosdb_TEST_SRC}")
    target_link_libraries(${test_case_name} target_link_libraries(${test_case_name}
    PRIVATE ${LIBCAOSDB} ${CONAN_LIBS_GTEST} ${CONAN_LIBS_BOOST}) PRIVATE ${LIBCAOSDB} ${CONAN_LIBS_GTEST} ${CONAN_LIBS_BOOST})
    target_include_directories(${test_case_name}
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
    if(_LINTING) if(_LINTING)
    set_target_properties(${test_case_name} set_target_properties(${test_case_name}
    PROPERTIES PROPERTIES
    ......
    /*
    *
    * 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
    /**
    * @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
    ...@@ -23,12 +23,17 @@ ...@@ -23,12 +23,17 @@
    #include <memory> // for allocator, make_shared, unique_ptr #include <memory> // for allocator, make_shared, unique_ptr
    #include "caosdb/connection.h" // for InsecureCaosDBConnectionConfig #include "caosdb/connection.h" // for InsecureCaosDBConnectionConfig
    #include "caosdb/entity.h" // for EntityID #include "caosdb/entity.h" // for EntityID
    #include "caosdb/exceptions.h" // for ConnectionError
    #include "caosdb/transaction.h" // for Transaction, EntityID #include "caosdb/transaction.h" // for Transaction, EntityID
    #include "gtest/gtest_pred_impl.h" // for Test, SuiteApiResolver, TEST, Tes... #include "gtest/gtest-message.h" // for Message
    #include "gtest/gtest-test-part.h" // for TestPartResult
    #include "gtest/gtest.h" // for Test, SuiteApiResolver, TestInfo ...
    #include "caosdb_test_utility.h" // for EXPECT_THROW_MESSAGE
    namespace caosdb::transaction { namespace caosdb::transaction {
    using caosdb::connection::CaosDBConnection; using caosdb::connection::CaosDBConnection;
    using caosdb::connection::InsecureCaosDBConnectionConfig; using caosdb::connection::InsecureCaosDBConnectionConfig;
    using caosdb::exceptions::ConnectionError;
    TEST(test_transaction, create_transaction) { TEST(test_transaction, create_transaction) {
    const auto *pHost = "localhost"; const auto *pHost = "localhost";
    ...@@ -37,7 +42,8 @@ TEST(test_transaction, create_transaction) { ...@@ -37,7 +42,8 @@ TEST(test_transaction, create_transaction) {
    auto transaction = connection.CreateTransaction(); auto transaction = connection.CreateTransaction();
    transaction->Retrieve(EntityID("someid")); transaction->Retrieve(EntityID("someid"));
    transaction->Execute(); EXPECT_THROW_MESSAGE(transaction->Execute(), ConnectionError,
    "failed to connect to all addresses");
    } }
    } // namespace caosdb::transaction } // namespace caosdb::transaction
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment