From 5dcb55c99281a8c5fd94d124a4dee387ade06268 Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Sat, 21 Sep 2019 09:23:32 -0300 Subject: [PATCH] add modify logic, test case, modify delete --- .../include/graphene/chain/protocol/son.hpp | 3 +- libraries/chain/son_evaluator.cpp | 26 ++++++++------ tests/tests/son_operations_tests.cpp | 35 +++++++++++++++++++ 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/libraries/chain/include/graphene/chain/protocol/son.hpp b/libraries/chain/include/graphene/chain/protocol/son.hpp index 5d6bb18b..7c007a8e 100644 --- a/libraries/chain/include/graphene/chain/protocol/son.hpp +++ b/libraries/chain/include/graphene/chain/protocol/son.hpp @@ -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) ) diff --git a/libraries/chain/son_evaluator.cpp b/libraries/chain/son_evaluator.cpp index b12363d9..a93cfda0 100644 --- a/libraries/chain/son_evaluator.cpp +++ b/libraries/chain/son_evaluator.cpp @@ -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().indices().get(); + 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().indices().get(); + 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().indices().get(); - FC_ASSERT( idx.find(op.owner_account) != idx.end() ); + const auto& idx = db().get_index_type().indices().get(); + 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().indices().get(); - db().remove(*idx.find(op.owner_account)); + const auto& idx = db().get_index_type().indices().get(); + db().remove(*idx.find(op.son_id)); return void_result(); } FC_CAPTURE_AND_RETHROW( (op) ) } diff --git a/tests/tests/son_operations_tests.cpp b/tests/tests/son_operations_tests.cpp index c60055e2..c4c3baf0 100644 --- a/tests/tests/son_operations_tests.cpp +++ b/tests/tests/son_operations_tests.cpp @@ -18,6 +18,16 @@ public: std::unique_ptr 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 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().indices().get(); + + 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 );