add modify logic, test case, modify delete

This commit is contained in:
Alfredo Garcia 2019-09-21 09:23:32 -03:00
parent ba77a33711
commit 5dcb55c992
3 changed files with 53 additions and 11 deletions

View file

@ -33,6 +33,7 @@ namespace graphene { namespace chain {
struct fee_parameters_type { uint64_t fee = 0; };
asset fee;
son_id_type son_id;
account_id_type owner_account;
account_id_type fee_payer()const { return owner_account; }
@ -48,4 +49,4 @@ FC_REFLECT( graphene::chain::son_update_operation::fee_parameters_type, (fee) )
FC_REFLECT( graphene::chain::son_update_operation, (fee)(son_id)(owner_account)(new_url) )
FC_REFLECT( graphene::chain::son_delete_operation::fee_parameters_type, (fee) )
FC_REFLECT( graphene::chain::son_delete_operation, (fee)(owner_account) )
FC_REFLECT( graphene::chain::son_delete_operation, (fee)(son_id)(owner_account) )

View file

@ -27,30 +27,36 @@ object_id_type create_son_evaluator::do_apply(const son_create_operation& op)
} FC_CAPTURE_AND_RETHROW( (op) ) }
void_result update_son_evaluator::do_evaluate(const son_update_operation& op)
{ try{
{ try {
const auto& idx = db().get_index_type<son_member_index>().indices().get<by_id>();
FC_ASSERT( idx.find(op.son_id) != idx.end() );
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
object_id_type update_son_evaluator::do_apply(const son_update_operation& op)
{ try {
return son_id_type(0);
const auto& idx = db().get_index_type<son_member_index>().indices().get<by_id>();
auto itr = idx.find(op.son_id);
if(itr != idx.end())
{
db().modify(*itr, [&op](son_object &so) {
so.url = op.new_url;
});
}
return op.son_id;
} FC_CAPTURE_AND_RETHROW( (op) ) }
void_result delete_son_evaluator::do_evaluate(const son_delete_operation& op)
{ try {
database& d = db();
const auto& idx = d.get_index_type<son_member_index>().indices().get<by_account>();
FC_ASSERT( idx.find(op.owner_account) != idx.end() );
const auto& idx = db().get_index_type<son_member_index>().indices().get<by_id>();
FC_ASSERT( idx.find(op.son_id) != idx.end() );
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
void_result delete_son_evaluator::do_apply(const son_delete_operation& op)
{ try {
const auto& idx = db().get_index_type<son_member_index>().indices().get<by_account>();
db().remove(*idx.find(op.owner_account));
const auto& idx = db().get_index_type<son_member_index>().indices().get<by_id>();
db().remove(*idx.find(op.son_id));
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }

View file

@ -18,6 +18,16 @@ public:
std::unique_ptr<transaction_evaluation_state> ptr_trx_state;
};
class test_update_son_member_evaluator: public update_son_evaluator {
public:
test_update_son_member_evaluator( database& link_db ):
ptr_trx_state( new transaction_evaluation_state( &link_db ) )
{
trx_state = ptr_trx_state.get();
}
std::unique_ptr<transaction_evaluation_state> ptr_trx_state;
};
class test_delete_son_member_evaluator: public delete_son_evaluator {
public:
test_delete_son_member_evaluator( database& link_db ):
@ -49,12 +59,37 @@ BOOST_AUTO_TEST_CASE( create_son_test ){
BOOST_CHECK( id == obj->id );
}
BOOST_AUTO_TEST_CASE( update_son_test ){
INVOKE(create_son_test);
std::string new_url = "https://anewurl.com";
test_update_son_member_evaluator test_eval( db );
son_update_operation op;
op.owner_account = account_id_type(1);
op.new_url = new_url;
op.son_id = son_id_type(0);
BOOST_CHECK_NO_THROW( test_eval.do_evaluate( op ) );
auto id = test_eval.do_apply( op );
const auto& idx = db.get_index_type<son_member_index>().indices().get<by_account>();
BOOST_REQUIRE( idx.size() == 1 );
auto obj = idx.find( op.owner_account );
BOOST_REQUIRE( obj != idx.end() );
BOOST_CHECK( obj->url == new_url );
BOOST_CHECK( id == obj->id );
}
BOOST_AUTO_TEST_CASE( delete_son_test ){
INVOKE(create_son_test);
test_delete_son_member_evaluator test_eval( db );
son_delete_operation delete_op;
delete_op.owner_account = account_id_type(1);
delete_op.son_id = son_id_type(0);
BOOST_CHECK_NO_THROW( test_eval.do_evaluate( delete_op ) );
test_eval.do_apply( delete_op );