Skip to content
Snippets Groups Projects
Select Git revision
  • 59c374876c16b092c695e64650b61077d8874bf2
  • main default protected
  • f-sss4grpc
  • dev
  • 108-implement-rpc-call-for-server-side-scripting
  • f-windows-conan-create
  • f-to-string
  • f-update-requirements
  • f-related-projects
  • f-role
  • f-remote-path
  • f-rel-path
  • f-consol-message
  • v0.3.0
  • v0.2.2
  • v0.2.1
  • v0.2.0
  • v0.1.2
  • v0.1.1
  • v0.1
  • v0.0.19
  • v0.0.18
  • v0.0.16
  • v0.0.15
  • v0.0.10
  • v0.0.9
  • v0.0.8
  • v0.0.7
  • v0.0.6
  • v0.0.5
  • v0.0.4
  • v0.0.3
  • v0.0.2
33 results

test_transaction.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    test_transaction.cpp 4.40 KiB
    /*
     * 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/configuration.h"           // for InsecureConnectionConfig...
    #include "caosdb/connection.h"              // for Connection
    #include "caosdb/entity.h"                  // for Entity
    #include "caosdb/entity/v1alpha1/main.pb.h" // for Entity
    #include "caosdb/exceptions.h"              // for ConnectionError
    #include "caosdb/status_code.h"
    #include "caosdb/transaction.h"        // for Transaction, UniqueResult
    #include "caosdb/transaction_status.h" // for ConnectionError
    #include "caosdb_test_utility.h"       // for EXPECT_THROW_MESSAGE
    #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
    #include <memory>                      // for allocator, unique_ptr
    
    namespace caosdb::transaction {
    using caosdb::configuration::InsecureConnectionConfiguration;
    using caosdb::connection::Connection;
    using caosdb::exceptions::ConnectionError;
    using caosdb::transaction::UniqueResult;
    using ProtoEntity = caosdb::entity::v1alpha1::Entity;
    
    TEST(test_transaction, create_transaction) {
      const auto *host = "localhost";
      auto configuration = InsecureConnectionConfiguration(host, 8000);
      Connection connection(configuration);
      auto transaction = connection.CreateTransaction();
    
      transaction->RetrieveById("100");
      EXPECT_THROW_MESSAGE(
        transaction->Execute(), ConnectionError,
        "The attempt to execute this transaction was not successful because the "
        "connection to the server could not be established.");
    }
    
    TEST(test_transaction, unique_result) {
      auto *entity = new ProtoEntity();
      entity->set_id("test");
      UniqueResult result(entity);
    
      EXPECT_EQ("test", result.GetEntity().GetId());
    
      // DON'T DELETE! The caosdb::entity::Entity takes care of that
      // Try it yourself:
      // delete entity;
    }
    
    TEST(test_transaction, test_unavailable) {
      const auto *host = "localhost";
      auto configuration = InsecureConnectionConfiguration(host, 8000);
      Connection connection(configuration);
      auto transaction = connection.CreateTransaction();
    
      transaction->RetrieveById("100");
      transaction->ExecuteAsynchronously();
      EXPECT_EQ(transaction->GetRequestCount(), 1);
    
      auto status = transaction->WaitForIt();
      EXPECT_EQ(status.GetCode(), StatusCode::CONNECTION_ERROR);
    }
    
    TEST(test_transaction, test_retrieve_by_ids) {
      const auto *host = "localhost";
      auto configuration = InsecureConnectionConfiguration(host, 8000);
      Connection connection(configuration);
      auto transaction = connection.CreateTransaction();
    
      std::vector<std::string> ids = {"100", "101", "102"};
      transaction->RetrieveById(ids.begin(), ids.end());
    
      EXPECT_EQ(transaction->GetRequestCount(), 3);
    }
    
    TEST(test_transaction, test_multi_result_set_empty) {
      MultiTransactionResponse response;
    
      MultiResultSet rs(&response);
      EXPECT_EQ(rs.Size(), 0);
    }
    
    TEST(test_transaction, test_multi_result_set_one) {
      MultiTransactionResponse response;
      response.add_responses()
        ->mutable_retrieve_response()
        ->mutable_entity()
        ->set_id("100");
    
      MultiResultSet rs(&response);
      EXPECT_EQ(rs.Size(), 1);
    }
    
    TEST(test_transaction, test_multi_result_set_three) {
      MultiTransactionResponse response;
      response.add_responses()
        ->mutable_retrieve_response()
        ->mutable_entity()
        ->set_id("100");
      response.add_responses()
        ->mutable_retrieve_response()
        ->mutable_entity()
        ->set_id("101");
      response.add_responses()
        ->mutable_retrieve_response()
        ->mutable_entity()
        ->set_id("102");
    
      MultiResultSet rs(&response);
      EXPECT_EQ(rs.Size(), 3);
    }
    
    } // namespace caosdb::transaction