Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Remove amount from factories
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/module/shared_model/backend_proto/CMakeLists.txt
  • Loading branch information
nickaleks committed Jul 17, 2018
1 parent d0ee0c6 commit 653b982
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "backend/protobuf/common_objects/account.hpp"
#include "backend/protobuf/common_objects/account_asset.hpp"
#include "backend/protobuf/common_objects/amount.hpp"
#include "backend/protobuf/common_objects/asset.hpp"
#include "backend/protobuf/common_objects/domain.hpp"
#include "backend/protobuf/common_objects/peer.hpp"
Expand Down Expand Up @@ -86,10 +85,7 @@ namespace shared_model {
iroha::protocol::AccountAsset asset;
asset.set_account_id(account_id);
asset.set_asset_id(asset_id);
auto proto_balance = asset.mutable_balance();
convertToProtoAmount(*proto_balance->mutable_value(),
balance.intValue());
proto_balance->set_precision(balance.precision());
asset.set_balance(balance.toStringRepr());

auto proto_asset = std::make_unique<AccountAsset>(std::move(asset));

Expand All @@ -107,60 +103,6 @@ namespace shared_model {
std::unique_ptr<interface::AccountAsset>>(std::move(proto_asset));
}

FactoryResult<std::unique_ptr<interface::Amount>> createAmount(
boost::multiprecision::uint256_t value,
interface::types::PrecisionType precision) override {
iroha::protocol::Amount amount;
amount.set_precision(precision);
convertToProtoAmount(*amount.mutable_value(), value);

auto proto_amount = std::make_unique<Amount>(std::move(amount));

auto errors =
validate(*proto_amount, [this](const auto &amount, auto &reasons) {
validator_.validateAmount(reasons, amount);
});

if (errors) {
return iroha::expected::makeError(errors.reason());
}

return iroha::expected::makeValue<std::unique_ptr<interface::Amount>>(
std::move(proto_amount));
}

FactoryResult<std::unique_ptr<interface::Amount>> createAmount(
std::string amount) override {
// taken from iroha::model::Amount
// check if valid number
const static std::regex e("([0-9]*\\.[0-9]+|[0-9]+)");
if (!std::regex_match(amount, e)) {
return iroha::expected::makeError("number string is invalid");
}

// get precision
auto dot_place = amount.find('.');
interface::types::PrecisionType precision;
if (dot_place > amount.size()) {
precision = 0;
} else {
precision = amount.size() - dot_place - 1;
// erase dot from the string
amount.erase(std::remove(amount.begin(), amount.end(), '.'),
amount.end());
}

auto begin = amount.find_first_not_of('0');

// create uint256 value from obtained string
boost::multiprecision::uint256_t value = 0;
if (begin <= amount.size()) {
value = boost::multiprecision::uint256_t(amount.substr(begin));
}

return createAmount(value, precision);
}

FactoryResult<std::unique_ptr<interface::Asset>> createAsset(
const interface::types::AssetIdType &asset_id,
const interface::types::DomainIdType &domain_id,
Expand Down
19 changes: 0 additions & 19 deletions shared_model/interfaces/common_objects/common_objects_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,6 @@ namespace shared_model {
const types::AssetIdType &asset_id,
const Amount &balance) = 0;

/**
* Create amount instance from string
*
* @param value integer will be divided by 10 * precision,
* so value 123 with precision 2 will become Amount of 1.23
*/
virtual FactoryResult<std::unique_ptr<Amount>> createAmount(
boost::multiprecision::uint256_t value,
types::PrecisionType precision) = 0;

/**
* Create amount instance from string
*
* @param amount must represent valid number.
* For example: "1.23", "10" etc.
*/
virtual FactoryResult<std::unique_ptr<Amount>> createAmount(
std::string amount) = 0;

/**
* Create asset instance
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ class AccountAssetTest : public ::testing::Test {
public:
interface::types::AccountIdType valid_account_id = "hello@world";
interface::types::AssetIdType valid_asset_id = "bit#connect";
std::shared_ptr<interface::Amount> valid_amount =
val(builder::DefaultAmountBuilder::fromString("10.00"))->value;
interface::Amount valid_amount = interface::Amount("10.00");

interface::types::AccountIdType invalid_account_id = "hello123";
};
Expand All @@ -115,13 +114,13 @@ class AccountAssetTest : public ::testing::Test {
*/
TEST_F(AccountAssetTest, ValidAccountAssetInitialization) {
auto account_asset = factory.createAccountAsset(
valid_account_id, valid_asset_id, *valid_amount);
valid_account_id, valid_asset_id, valid_amount);

account_asset.match(
[&](const ValueOf<decltype(account_asset)> &v) {
ASSERT_EQ(v.value->accountId(), valid_account_id);
ASSERT_EQ(v.value->assetId(), valid_asset_id);
ASSERT_EQ(v.value->balance(), *valid_amount);
ASSERT_EQ(v.value->balance(), valid_amount);
},
[](const ErrorOf<decltype(account_asset)> &e) { FAIL() << e.error; });
}
Expand All @@ -133,7 +132,7 @@ TEST_F(AccountAssetTest, ValidAccountAssetInitialization) {
*/
TEST_F(AccountAssetTest, InvalidAccountAssetInitialization) {
auto account_asset = factory.createAccountAsset(
invalid_account_id, valid_asset_id, *valid_amount);
invalid_account_id, valid_asset_id, valid_amount);

account_asset.match(
[](const ValueOf<decltype(account_asset)> &v) {
Expand All @@ -142,62 +141,6 @@ TEST_F(AccountAssetTest, InvalidAccountAssetInitialization) {
[](const ErrorOf<decltype(account_asset)> &e) { SUCCEED(); });
}

class AmountTest : public ::testing::Test {
public:
boost::multiprecision::uint256_t valid_value = 123;
interface::types::PrecisionType valid_precision = 2;

std::string valid_amount_str = "1.23";
std::string invalid_amount_str = "hello there";
};

/**
* @given valid data for amount
* @when amount is created via factory
* @then amount is successfully initialized
*/
TEST_F(AmountTest, ValidAmountInitialization) {
auto amount = factory.createAmount(valid_value, valid_precision);

amount.match(
[&](const ValueOf<decltype(amount)> &v) {
ASSERT_EQ(v.value->intValue(), valid_value);
ASSERT_EQ(v.value->precision(), valid_precision);
},
[](const ErrorOf<decltype(amount)> &e) { FAIL() << e.error; });
}

/**
* @given valid string for amount
* @when amount is created via factory
* @then amount is successfully initialized
*/
TEST_F(AmountTest, ValidStringAmountInitialization) {
auto amount = factory.createAmount(valid_amount_str);

amount.match(
[&](const ValueOf<decltype(amount)> &v) {
ASSERT_EQ(v.value->intValue(), valid_value);
ASSERT_EQ(v.value->precision(), valid_precision);
},
[](const ErrorOf<decltype(amount)> &e) { FAIL() << e.error; });
}

/**
* @given invalid string for amount
* @when amount is created via factory
* @then amount is not initialized correctly
*/
TEST_F(AmountTest, InvalidStringAmountInitialization) {
auto amount = factory.createAmount(invalid_amount_str);

amount.match(
[](const ValueOf<decltype(amount)> &v) {
FAIL() << "Expected error case";
},
[](const ErrorOf<decltype(amount)> &e) { SUCCEED(); });
}

class AssetTest : public ::testing::Test {
public:
interface::types::AssetIdType valid_asset_id = "bit#connect";
Expand Down

0 comments on commit 653b982

Please sign in to comment.