Skip to content

Commit

Permalink
Add query builders
Browse files Browse the repository at this point in the history
Signed-off-by: Kitsu <[email protected]>
  • Loading branch information
l4l authored and lebdron committed Dec 19, 2017
1 parent 3b2f7c8 commit 38a788c
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 60 deletions.
142 changes: 142 additions & 0 deletions shared_model/builders/protobuf/queries.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_PROTO_QUERY_BUILDER_HPP
#define IROHA_PROTO_QUERY_BUILDER_HPP

// #include "backend/protobuf/queries.hpp"
#include "interfaces/common_objects/types.hpp"
#include "queries.pb.h"

namespace shared_model {
namespace proto {
template <int S = 0>
class TemplateQueryBuilder {
private:
template <int>
friend class TemplateQueryBuilder;

enum RequiredFields {
CreatedTime,
CreatorAccountId,
QueryField,
QueryCounter,
TOTAL
};

template <int s>
using NextBuilder = TemplateQueryBuilder<S | (1 << s)>;

iroha::protocol::Query query_;

template <int Sp>
TemplateQueryBuilder(const TemplateQueryBuilder<Sp> &o)
: query_(o.query_) {}

public:
TemplateQueryBuilder() = default;

NextBuilder<CreatedTime> createdTime(uint64_t created_time) {
query_.mutable_payload()->set_created_time(created_time);
return *this;
}

NextBuilder<CreatorAccountId> creatorAccountId(
const interface::types::AccountIdType &creator_account_id) {
query_.mutable_payload()->set_creator_account_id(creator_account_id);
return *this;
}

NextBuilder<QueryField> setGetAccount(
const interface::types::AccountIdType &accound_id) {
auto query = query_.mutable_payload()->mutable_get_account();
query->set_account_id(accound_id);
return *this;
}

NextBuilder<QueryField> setGetSignatories(
const interface::types::AccountIdType &accound_id) {
auto query =
query_.mutable_payload()->mutable_get_account_signatories();
query->set_account_id(accound_id);
return *this;
}

NextBuilder<QueryField> setGetAccountTransactions(
const interface::types::AccountIdType &accound_id) {
auto query =
query_.mutable_payload()->mutable_get_account_transactions();
query->set_account_id(accound_id);
return *this;
}

NextBuilder<QueryField> setGetAccountAssetTransactions(
const interface::types::AccountIdType &accound_id,
const interface::types::AssetIdType &asset_id) {
auto query =
query_.mutable_payload()->mutable_get_account_asset_transactions();
query->set_account_id(accound_id);
query->set_asset_id(asset_id);
return *this;
}

NextBuilder<QueryField> setGetAccountAssets(
const interface::types::AccountIdType &accound_id,
const interface::types::AssetIdType &asset_id) {
auto query = query_.mutable_payload()->mutable_get_account_assets();
query->set_account_id(accound_id);
query->set_asset_id(asset_id);
return *this;
}

NextBuilder<QueryField> setGetRoles(
const interface::types::AccountIdType &accound_id,
const interface::types::AssetIdType &asset_id) {
query_.mutable_payload()->mutable_get_roles();
return *this;
}

NextBuilder<QueryField> setGetAssetInfo(
const interface::types::AssetIdType &asset_id) {
auto query = query_.mutable_payload()->mutable_get_asset_info();
query->set_asset_id(asset_id);
return *this;
}

NextBuilder<QueryField> setGetRolePermissions(
const interface::types::RoleIdType &role_id) {
auto query = query_.mutable_payload()->mutable_get_role_permissions();
query->set_role_id(role_id);
return *this;
}

NextBuilder<QueryCounter> queryCounter(uint64_t query_counter) {
query_.mutable_payload()->set_query_counter(query_counter);
return *this;
}

// Uncomment on completing proto::Query
void /*Query*/ build() {
static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
return; // Query(std::move(query_));
}
};

using QueryBuilder = TemplateQueryBuilder<>;
}; // namespace proto
} // namespace shared_model
#endif // IROHA_PROTO_QUERY_BUILDER_HPP
16 changes: 7 additions & 9 deletions test/module/shared_model/backend_proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,10 @@
addtest(shared_proto_commands_test
shared_proto_commands_test.cpp
)

target_link_libraries(shared_proto_commands_test
shared_model_proto_backend
)

addtest(shared_proto_queries_test
shared_proto_queries_test.cpp
)

target_link_libraries(shared_proto_queries_test
shared_model_proto_backend
)

addtest(shared_proto_tx_response_test
shared_proto_tx_response_test.cpp
)
Expand All @@ -44,3 +35,10 @@ addtest(shared_proto_transaction_test
target_link_libraries(shared_proto_transaction_test
shared_model_proto_backend
)

addtest(shared_proto_queries_test
shared_proto_queries_test.cpp
)
target_link_libraries(shared_proto_queries_test
shared_model_proto_backend
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "backend/protobuf/queries/proto_query.hpp"
#include "builders/protobuf/queries.hpp"

#include <gtest/gtest.h>

Expand Down Expand Up @@ -43,3 +44,33 @@ TEST(ProtoQuery, QueryLoad) {
ASSERT_EQ(i, shared_model::proto::Query(query).get().which());
});
}

/**
* @given query field values and sample command values, reference query
* @when create query with sample command using query builder
* @then query is built correctly
*/
TEST(ProtoQueryBuilder, Builder) {
uint64_t created_time = 10000000000ull, query_counter = 1;
std::string account_id = "admin@test", asset_id = "coin#test";

iroha::protocol::Query proto_tx;
auto &payload = *proto_tx.mutable_payload();
auto &query = *payload.mutable_get_account_assets();
payload.set_created_time(created_time);
payload.set_creator_account_id(account_id);
query.set_account_id(account_id);
query.set_asset_id(asset_id);

// Uncomment on completing proto::Query
/*auto tx = */ shared_model::proto::QueryBuilder()
.createdTime(created_time)
.creatorAccountId(account_id)
.setGetAccountAssets(account_id, asset_id)
.queryCounter(query_counter)
.build();
// auto &proto = tx.getTransport();

ASSERT_TRUE(true);
// ASSERT_EQ(proto_tx.SerializeAsString(), proto.SerializeAsString());
}
50 changes: 0 additions & 50 deletions test/module/shared_model/backend_proto/shared_proto_query_test.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

#include "backend/protobuf/transaction.hpp"
#include "builders/protobuf/proto_transaction_builder.hpp"
#include "builders/protobuf/transaction.hpp"

#include <gtest/gtest.h>

Expand Down

0 comments on commit 38a788c

Please sign in to comment.