Skip to content

Commit

Permalink
Create Transaction builder tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
motxx committed Feb 21, 2017
1 parent c76a63c commit 7e3a57a
Show file tree
Hide file tree
Showing 5 changed files with 574 additions and 112 deletions.
7 changes: 4 additions & 3 deletions test/transaction_builder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ link_directories(

add_executable(transaction_builder_test
transaction_builder_add_test.cpp
# transaction_builder_transfer_test.cpp
# transaction_builder_update_test.cpp
# transaction_builder_remove_test.cpp
transaction_builder_transfer_test.cpp
transaction_builder_update_test.cpp
transaction_builder_remove_test.cpp
transaction_builder_unset_test.cpp
)
target_link_libraries(transaction_builder_test
event_with_grpc
Expand Down
187 changes: 151 additions & 36 deletions test/transaction_builder/transaction_builder_remove_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,172 @@ limitations under the License.
*/

#include <gtest/gtest.h>
#include <infra/protobuf/api.pb.h>
#include <transaction_builder/transaction_builder.hpp>
#include <util/exception.hpp>

using transaction::TransactionBuilder;
using type_signatures::Remove;
using type_signatures::Domain;
using type_signatures::Account;
using type_signatures::Asset;
using type_signatures::SimpleAsset;
using type_signatures::Peer;

/***************************************************************************
Remove
***************************************************************************/
TEST(transaction_builder, create_remove) {
TEST(transaction_builder, create_remove_domain) {
Api::Domain domain;
domain.set_ownerpublickey("pubkey1");
domain.set_name("name");
auto txDomain = TransactionBuilder<Remove<Domain>>()
.setSenderPublicKey("karin")
.setDomain(domain)
.build();

ASSERT_STREQ(txDomain.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txDomain.type().c_str(), "Remove");

auto obj = txDomain.domain();
ASSERT_STREQ(obj.ownerpublickey().c_str(), "pubkey1");
ASSERT_STREQ(obj.name().c_str(), "name");
}

TEST(transaction_builder, create_remove_account) {

Api::Account account;
{
account.set_publickey("pubkey1");
account.set_name("name");
}

std::vector<std::string> assets = {
"asset1",
"asset2"
};
for (auto&& e: assets) {
account.add_assets(e);
}
auto txAccount = TransactionBuilder<Remove<Account>>()
.setSenderPublicKey("karin")
.setAccount(account)
.build();

ASSERT_STREQ(txAccount.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txAccount.type().c_str(), "Remove");

auto obj = txAccount.account();
ASSERT_STREQ(obj.publickey().c_str(), "pubkey1");
ASSERT_STREQ(obj.name().c_str(), "name");
for (int i = 0; i < obj.assets_size(); i++) {
ASSERT_STREQ(obj.assets(i).c_str(), assets[i].c_str());
}
}

TEST(transaction_builder, create_remove_asset) {
std::unordered_map<std::string, Api::BaseObject> value;
{
{
Api::BaseObject baseObject;
baseObject.set_valuestring("value1");
value.emplace("key1", baseObject);
}
{
Api::BaseObject baseObject;
baseObject.set_valueint(123456);
value.emplace("key2", baseObject);
}
}

// Needs create helper function?
Api::Asset asset;
{
asset.set_domain("domainID");
asset.set_name("name");
google::protobuf::Map<std::string, Api::BaseObject> protoValue(value.begin(), value.end());
*asset.mutable_value() = protoValue;
asset.set_smartcontractname("contract_func");
}

auto txAsset = TransactionBuilder<Remove<Asset>>()
.setSenderPublicKey("karin")
.setAsset(asset)
.build();

// Verify
ASSERT_STREQ(txAsset.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txAsset.type().c_str(), "Remove");

auto obj = txAsset.asset();
ASSERT_STREQ(obj.domain().c_str(), "domainID");
ASSERT_STREQ(obj.name().c_str(), "name");

ASSERT_TRUE(obj.value().find("key1")->second.value_case() == Api::BaseObject::ValueCase::kValueString);
ASSERT_TRUE(obj.value().find("key2")->second.value_case() == Api::BaseObject::ValueCase::kValueInt);
ASSERT_STREQ(obj.value().find("key1")->second.valuestring().c_str(), "value1");
ASSERT_TRUE(obj.value().find("key2")->second.valueint() == 123456);
ASSERT_STREQ(obj.smartcontractname().c_str(), "contract_func");
}

TEST(transaction_builder, create_remove_peer) {

Api::Trust trust;
{
trust.set_value(1.23);
}

Api::Peer peer;
{
auto txDomain = TransactionBuilder<Remove<object::Domain>>()
.setSender("karin")
.setDomain(object::Domain({"pubkey1", "pubkey2"}, "name"))
.build();

ASSERT_TRUE(txDomain.senderPublicKey == "karin");
auto addc = txDomain.command.AsRemove();
auto domo = addc->object.AsDomain();
ASSERT_TRUE(domo->ownerPublicKey == std::vector<std::string>({"pubkey1", "pubkey2"}));
ASSERT_TRUE(domo->name == "name");
peer.set_publickey("publickey");
peer.set_address("address");
*peer.mutable_trust() = trust;
}

auto txPeer = TransactionBuilder<Remove<Peer>>()
.setSenderPublicKey("karin")
.setPeer(peer)
.build();

// Verify
ASSERT_STREQ(txPeer.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txPeer.type().c_str(), "Remove");

auto obj = txPeer.peer();
ASSERT_STREQ(obj.publickey().c_str(), "publickey");
ASSERT_STREQ(obj.address().c_str(), "address");
ASSERT_TRUE(obj.trust().value() == 1.23);
}

TEST(transaction_builder, create_remove_simpleasset) {

Api::BaseObject baseObject;
{
auto txAccount = TransactionBuilder<Remove<object::Account>>()
.setSender("karin")
.setAccount(object::Account("pubkey", "name"))
.build();

ASSERT_TRUE(txAccount.senderPublicKey == "karin");
auto addc = txAccount.command.AsRemove();
auto acco = addc->object.AsAccount();
ASSERT_TRUE(acco->ownerPublicKey == "pubkey");
ASSERT_TRUE(acco->name == "name");
baseObject.set_valueint(123456);
}

Api::SimpleAsset simpleAsset;
{
auto value = std::unordered_map<std::string, object::BaseObject>(
{{"key1", object::BaseObject("value")}, {"key2", object::BaseObject(123456)}}
);
auto txAsset = TransactionBuilder<Remove<object::Asset>>()
.setSender("karin")
.setAsset(object::Asset("domainID", "name", value))
.build();

ASSERT_TRUE(txAsset.senderPublicKey == "karin");
auto addc = txAsset.command.AsRemove();
auto asso = addc->object.AsAsset();
ASSERT_TRUE(asso->domain == "domainID");
ASSERT_TRUE(asso->name == "name");
ASSERT_TRUE(asso->value == value);
simpleAsset.set_domain("domainID");
simpleAsset.set_name("name");
*simpleAsset.mutable_value() = baseObject;
simpleAsset.set_smartcontractname("contract_func");
}
}

auto txSimpleAsset = TransactionBuilder<Remove<SimpleAsset>>()
.setSenderPublicKey("karin")
.setSimpleAsset(simpleAsset)
.build();

// Verify
ASSERT_STREQ(txSimpleAsset.senderpubkey().c_str(), "karin");
ASSERT_STREQ(txSimpleAsset.type().c_str(), "Remove");

auto obj = txSimpleAsset.simpleasset();
ASSERT_STREQ(obj.domain().c_str(), "domainID");
ASSERT_STREQ(obj.name().c_str(), "name");

ASSERT_TRUE(obj.value().value_case() == Api::BaseObject::ValueCase::kValueInt);
ASSERT_TRUE(obj.value().valueint() == 123456);
ASSERT_STREQ(obj.smartcontractname().c_str(), "contract_func");
}
Loading

0 comments on commit 7e3a57a

Please sign in to comment.