Skip to content

Commit

Permalink
Add test for query execution
Browse files Browse the repository at this point in the history
  • Loading branch information
grimadas committed Jul 20, 2017
1 parent 027a331 commit 7b7904a
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 55 deletions.
63 changes: 30 additions & 33 deletions irohad/model/impl/query_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

#include "model/query_execution.hpp"
#include <ametsuchi/block_query.hpp>
#include "common/types.hpp"
#include "model/queries/responses/account_assets_response.hpp"
#include "model/queries/responses/account_response.hpp"
Expand All @@ -25,10 +24,8 @@
#include "model/queries/responses/transactions_response.hpp"

iroha::model::QueryProcessingFactory::QueryProcessingFactory(
ametsuchi::WsvQuery& wsvQuery, ametsuchi::BlockQuery& blockQuery) {
_wsvQuery = wsvQuery;
_blockQuery = blockQuery;
}
ametsuchi::WsvQuery& wsvQuery, ametsuchi::BlockQuery& blockQuery)
: _wsvQuery(wsvQuery), _blockQuery(blockQuery) {}

bool iroha::model::QueryProcessingFactory::validate(
const model::GetAccount& query) {
Expand All @@ -53,7 +50,7 @@ bool iroha::model::QueryProcessingFactory::validate(
}

bool iroha::model::QueryProcessingFactory::validate(
const model::GetAccountAssets& query) {
const model::GetAccountAsset& query) {
auto creator = _wsvQuery.getAccount(query.creator_account_id);
return
// Creator account exits
Expand Down Expand Up @@ -85,104 +82,104 @@ bool iroha::model::QueryProcessingFactory::validate(
query.account_id == query.creator_account_id);
}

iroha::model::QueryResponse
std::shared_ptr<iroha::model::QueryResponse>
iroha::model::QueryProcessingFactory::executeGetAccount(
const model::GetAccount& query) {
auto acc = _wsvQuery.getAccount(query.account_id);
if (!acc.has_value()) {
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "No account";
return response;
return std::make_shared<ErrorResponse>(response);
}
iroha::model::AccountResponse response;
response.account = acc.value();
response.query = query;
return response;
return std::make_shared<iroha::model::AccountResponse>(response);
}

iroha::model::QueryResponse
iroha::model::QueryProcessingFactory::executeGetAccountAssets(
const model::GetAccountAssets& query) {
std::shared_ptr<iroha::model::QueryResponse>
iroha::model::QueryProcessingFactory::executeGetAccountAsset(
const model::GetAccountAsset& query) {
auto acc_assets = _wsvQuery.getAccountAsset(query.account_id, query.asset_id);
if (!acc_assets.has_value()) {
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "No Account Asset";
return response;
return std::make_shared<iroha::model::ErrorResponse>(response);
}
iroha::model::AccountAssetsResponse response;
response.= acc_assets.value();
iroha::model::AccountAssetResponse response;
response.acc_asset = acc_assets.value();
response.query = query;
return response;
return std::make_shared<iroha::model::AccountAssetResponse>(response);
}

iroha::model::QueryResponse
std::shared_ptr<iroha::model::QueryResponse>
iroha::model::QueryProcessingFactory::executeGetAccountAssetTransactions(
const model::GetAccountAssetTransactions& query) {
// auto acc_asset_tx = _blockQuery.
// TODO: implement
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "Not implemented";
return response;
return std::make_shared<iroha::model::ErrorResponse>(response);
}

iroha::model::QueryResponse
std::shared_ptr<iroha::model::QueryResponse>
iroha::model::QueryProcessingFactory::executeGetAccountTransactions(
const model::GetAccountTransactions& query) {
auto acc_tx = _blockQuery.getAccountTransactions(query.account_id);
iroha::model::TransactionsResponse response;
response.query = query;
response.transactions = acc_tx;
return response;
return std::make_shared<iroha::model::TransactionsResponse>(response);
}

iroha::model::QueryResponse
std::shared_ptr<iroha::model::QueryResponse>
iroha::model::QueryProcessingFactory::executeGetSignatories(
const model::GetSignatories& query) {
auto signs = _wsvQuery.getSignatories(query.account_id);
if (!signs.has_value()) {
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "No signatories";
return response;
return std::make_shared<iroha::model::ErrorResponse>(response);
}
iroha::model::SignatoriesResponse response;
response.query = query;
response.keys = signs.value();
return response;
return std::make_shared<iroha::model::SignatoriesResponse>(response);
}

iroha::model::QueryResponse iroha::model::QueryProcessingFactory::execute(
std::shared_ptr<iroha::model::QueryResponse> iroha::model::QueryProcessingFactory::execute(
const model::Query& query) {
if (instanceof <iroha::model::GetAccount>(query)) {
auto qry = static_cast<const iroha::model::GetAccount&>(query);
if (!validate(qry)) {
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
return std::make_shared<ErrorResponse>(response);
}
return executeGetAccount(qry);
}
if (instanceof <iroha::model::GetAccountAssets>(query)) {
auto qry = static_cast<const iroha::model::GetAccountAssets&>(query);
if (instanceof <iroha::model::GetAccountAsset>(query)) {
auto qry = static_cast<const iroha::model::GetAccountAsset&>(query);
if (!validate(qry)) {
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
return std::make_shared<iroha::model::ErrorResponse>(response);
}
return executeGetAccountAssets(qry);
return executeGetAccountAsset(qry);
}
if (instanceof <iroha::model::GetSignatories>(query)) {
auto qry = static_cast<const iroha::model::GetSignatories&>(query);
if (!validate(qry)) {
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
return std::make_shared<iroha::model::ErrorResponse>(response);
}
return executeGetSignatories(qry);
}
Expand All @@ -192,7 +189,7 @@ iroha::model::QueryResponse iroha::model::QueryProcessingFactory::execute(
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
return std::make_shared<iroha::model::ErrorResponse>(response);
}
return executeGetAccountTransactions(qry);
}
Expand All @@ -203,12 +200,12 @@ iroha::model::QueryResponse iroha::model::QueryProcessingFactory::execute(
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return response;
return std::make_shared<iroha::model::ErrorResponse>(response);
}
return executeGetAccountAssetTransactions(qry);
}
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "Not implemented";
return response;
return std::make_shared<iroha::model::ErrorResponse>(response);
}
2 changes: 1 addition & 1 deletion irohad/model/queries/get_account_assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace iroha {
/**
* Query for get all account's assets and balance
*/
struct GetAccountAssets : Query {
struct GetAccountAsset : Query {
std::string account_id;
std::string asset_id;
};
Expand Down
9 changes: 4 additions & 5 deletions irohad/model/queries/responses/account_assets_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
#ifndef IROHA_ACCOUNT_ASSETS_RESPONSE_HPP
#define IROHA_ACCOUNT_ASSETS_RESPONSE_HPP

#include "model/query_response.hpp"
#include "model/account_asset.hpp"
#include <vector>
#include "model/account_asset.hpp"
#include "model/query_response.hpp"

namespace iroha {
namespace model {

struct AccountAssetsResponse : public QueryResponse {

std::vector<AccountAsset> assets;
struct AccountAssetResponse : public QueryResponse {
AccountAsset acc_asset;
};
} // namespace model
} // namespace iroha
Expand Down
21 changes: 11 additions & 10 deletions irohad/model/query_execution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "model/queries/get_signatories.hpp"
#include "model/queries/get_transactions.hpp"

#include "ametsuchi/block_query.hpp"
#include "ametsuchi/wsv_query.hpp"

namespace iroha {
Expand All @@ -36,29 +37,29 @@ namespace iroha {
*/
class QueryProcessingFactory {
public:
model::QueryResponse execute(const model::Query& query);
std::shared_ptr<iroha::model::QueryResponse> execute(const model::Query& query);
QueryProcessingFactory(ametsuchi::WsvQuery& wsvQuery,
ametsuchi::BlockQuery& blockQuery);

private:
bool validate(const model::GetAccountAssets& query);
bool validate(const model::GetAccountAsset& query);
bool validate(const model::GetAccount& query);
bool validate(const model::GetSignatories& query);
bool validate(const model::GetAccountAssetTransactions& query);
bool validate(const model::GetAccountTransactions& query);

model::QueryResponse executeGetAccountAssets(
const model::GetAccountAssets& query);
model::QueryResponse executeGetAccount(const model::GetAccount& query);
model::QueryResponse executeGetSignatories(
std::shared_ptr<iroha::model::QueryResponse> executeGetAccountAsset(
const model::GetAccountAsset& query);
std::shared_ptr<iroha::model::QueryResponse> executeGetAccount(const model::GetAccount& query);
std::shared_ptr<iroha::model::QueryResponse> executeGetSignatories(
const model::GetSignatories& query);
model::QueryResponse executeGetAccountAssetTransactions(
std::shared_ptr<iroha::model::QueryResponse> executeGetAccountAssetTransactions(
const model::GetAccountAssetTransactions& query);
model::QueryResponse executeGetAccountTransactions(
std::shared_ptr<iroha::model::QueryResponse> executeGetAccountTransactions(
const model::GetAccountTransactions& query);

ametsuchi::WsvQuery _wsvQuery;
ametsuchi::BlockQuery _blockQuery;
ametsuchi::WsvQuery& _wsvQuery;
ametsuchi::BlockQuery& _blockQuery;
};

} // namespace model
Expand Down
11 changes: 5 additions & 6 deletions irohad/model/query_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@
#ifndef IROHA_QUERY_RESPONSE_HPP
#define IROHA_QUERY_RESPONSE_HPP

#include <model/query.hpp>
#include <model/client.hpp>
#include <model/query.hpp>

namespace iroha {
namespace model {
/**
* Interface of query response for user
*/
struct QueryResponse {

/**
* Client query
*/
Query query;


virtual ~QueryResponse() {}
};
} //namespace model
} //namespace iroha
#endif //IROHA_QUERY_RESPONSE_HPP
} // namespace model
} // namespace iroha
#endif // IROHA_QUERY_RESPONSE_HPP
6 changes: 6 additions & 0 deletions test/module/irohad/validation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ target_link_libraries(stateful_validation_test
model
)

addtest(query_execution query_execution.cpp)
target_link_libraries(query_execution
model
)


Loading

0 comments on commit 7b7904a

Please sign in to comment.