added testing exceptions in betting

This commit is contained in:
Roman Olearski 2017-07-24 11:43:18 +02:00
parent a3710e01af
commit b9badec2f1
3 changed files with 137 additions and 72 deletions

View file

@ -42,9 +42,6 @@
using namespace graphene::chain; using namespace graphene::chain;
using namespace graphene::chain::test; using namespace graphene::chain::test;
BOOST_FIXTURE_TEST_SUITE( betting_tests, database_fixture )
#define CREATE_ICE_HOCKEY_BETTING_MARKET() \ #define CREATE_ICE_HOCKEY_BETTING_MARKET() \
const sport_object& ice_hockey = create_sport({{"en", "Ice Hockey"}, {"zh_Hans", "冰球"}, {"ja", "アイスホッケー"}}); \ const sport_object& ice_hockey = create_sport({{"en", "Ice Hockey"}, {"zh_Hans", "冰球"}, {"ja", "アイスホッケー"}}); \
const event_group_object& nhl = create_event_group({{"en", "NHL"}, {"zh_Hans", "國家冰球聯盟"}, {"ja", "ナショナルホッケーリーグ"}}, ice_hockey.id); \ const event_group_object& nhl = create_event_group({{"en", "NHL"}, {"zh_Hans", "國家冰球聯盟"}, {"ja", "ナショナルホッケーリーグ"}}, ice_hockey.id); \
@ -54,6 +51,9 @@ BOOST_FIXTURE_TEST_SUITE( betting_tests, database_fixture )
const betting_market_object& capitals_win_market = create_betting_market(moneyline_betting_markets.id, {{"en", "Washington Capitals win"}}); \ const betting_market_object& capitals_win_market = create_betting_market(moneyline_betting_markets.id, {{"en", "Washington Capitals win"}}); \
const betting_market_object& blackhawks_win_market = create_betting_market(moneyline_betting_markets.id, {{"en", "Chicago Blackhawks win"}}); const betting_market_object& blackhawks_win_market = create_betting_market(moneyline_betting_markets.id, {{"en", "Chicago Blackhawks win"}});
BOOST_FIXTURE_TEST_SUITE( betting_tests, database_fixture )
#if 0 #if 0
BOOST_AUTO_TEST_CASE(generate_block) BOOST_AUTO_TEST_CASE(generate_block)
{ {
@ -595,97 +595,96 @@ struct simple_bet_test_fixture_2 : database_fixture {
} }
}; };
// testing assertions
BOOST_AUTO_TEST_SUITE(other_betting_tests) BOOST_AUTO_TEST_SUITE(other_betting_tests)
#define TRY_EXPECT_THROW( expr, exc_type, reason ) \ #define TRY_EXPECT_THROW( expr, exc_type, reason ) \
{ \ { \
try \ try \
{ \ { \
expr; \ expr; \
FC_THROW("no error has occured"); \ FC_THROW("no error has occured"); \
} \ } \
catch (exc_type& e) \ catch (exc_type& e) \
{ \ { \
edump(("###")); \ edump(("###")); \
edump((e.to_detail_string())); \ edump((e.to_detail_string())); \
edump(("###")); \ edump(("###")); \
FC_ASSERT(e.to_detail_string().find(reason) != \ FC_ASSERT(e.to_detail_string().find(reason) != \
std::string::npos, "expected error hasn't occured");\ std::string::npos, "expected error hasn't occured");\
} \ } \
catch (...) \ catch (...) \
{ \ { \
FC_THROW("expected throw hasn't occured"); \ FC_THROW("expected throw hasn't occured"); \
} \ } \
} }
BOOST_FIXTURE_TEST_CASE( another_peerplays_event_group_update_test, database_fixture) BOOST_FIXTURE_TEST_CASE( another_event_group_update_test, database_fixture)
{ {
try try
{ {
ACTORS( (alice)(bob) ); ACTORS( (alice)(bob)(nathan) );
CREATE_ICE_HOCKEY_BETTING_MARKET(); CREATE_ICE_HOCKEY_BETTING_MARKET();
transfer(account_id_type(), alice_id, asset(10000000)); transfer(account_id_type(), alice_id, asset(10000000));
transfer(account_id_type(), bob_id, asset(10000000)); transfer(account_id_type(), bob_id, asset(10000000));
place_bet(alice_id, capitals_win_market.id, bet_type::back, asset(1000000, asset_id_type()), 2 * GRAPHENE_BETTING_ODDS_PRECISION, 1000000 / 50 /* chain defaults to 2% fees */); place_bet(alice_id, capitals_win_market.id, bet_type::back, asset(1000000, asset_id_type()), 2 * GRAPHENE_BETTING_ODDS_PRECISION, 1000000 / 50 /* chain defaults to 2% fees */);
internationalized_string_type n = {{"en", "IBM"}, {"zh_Hans", "國家冰球聯"}, {"ja", "ナショナルホッケーリー"}}; internationalized_string_type n = {{"en", "IBM"}, {"zh_Hans", "國家冰球聯"}, {"ja", "ナショナルホッケーリー"}};
const sport_object& ice_on_hockey = create_sport({{"en", "Hockey on Ice"}, {"zh_Hans", "冰球"}, {"ja", "アイスホッケー"}}); \ const sport_object& ice_on_hockey = create_sport({{"en", "Hockey on Ice"}, {"zh_Hans", "冰球"}, {"ja", "アイスホッケー"}}); \
fc::optional<internationalized_string_type> name = n; fc::optional<internationalized_string_type> name = n;
object_id_type object_id = ice_on_hockey.id; object_id_type object_id = ice_on_hockey.id;
fc::optional<object_id_type> sport_id = object_id; fc::optional<object_id_type> sport_id = object_id;
update_event_group(nhl.id, fc::optional<object_id_type>(), name); update_event_group(nhl.id, fc::optional<object_id_type>(), name);
update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>()); update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>());
update_event_group(nhl.id, sport_id, name); update_event_group(nhl.id, sport_id, name);
// #! nothing to change // trx_state->_is_proposed_trx
update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>()); //GRAPHENE_REQUIRE_THROW(try_update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>(), true), fc::exception);
//GRAPHENE_REQUIRE_THROW(update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>()), fc::exception); TRY_EXPECT_THROW(try_update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>(), true), fc::exception, "_is_proposed_trx");
//TRY_EXPECT_THROW(update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>()), fc::exception, "nothing to change" );
// #! sport_id must refer to a sport_id_type // #! nothing to change
object_id = capitals_win_market.id; //GRAPHENE_REQUIRE_THROW(try_update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>()), fc::exception);
sport_id = object_id; TRY_EXPECT_THROW(try_update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>()), fc::exception, "nothing to change");
update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>());
// #! invalid sport specified // #! sport_id must refer to a sport_id_type
object_id = sport_id_type(13); object_id = capitals_win_market.id;
sport_id = object_id; sport_id = object_id;
update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>()); //GRAPHENE_REQUIRE_THROW(try_update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>()), fc::exception);
TRY_EXPECT_THROW(try_update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>()), fc::exception, "sport_id must refer to a sport_id_type");
place_bet(bob_id, capitals_win_market.id, bet_type::lay, asset(1000000, asset_id_type()), 2 * GRAPHENE_BETTING_ODDS_PRECISION, 1000000 / 50 /* chain defaults to 2% fees */); // #! invalid sport specified
object_id = sport_id_type(13);
sport_id = object_id;
//GRAPHENE_REQUIRE_THROW(try_update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>()), fc::exception);
TRY_EXPECT_THROW(try_update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>()), fc::exception, "invalid sport specified");
BOOST_CHECK_EQUAL(get_balance(alice_id, asset_id_type()), 10000000 - 1000000 - 20000); place_bet(bob_id, capitals_win_market.id, bet_type::lay, asset(1000000, asset_id_type()), 2 * GRAPHENE_BETTING_ODDS_PRECISION, 1000000 / 50 /* chain defaults to 2% fees */);
BOOST_CHECK_EQUAL(get_balance(bob_id, asset_id_type()), 10000000 - 1000000 - 20000);
// caps win BOOST_CHECK_EQUAL(get_balance(alice_id, asset_id_type()), 10000000 - 1000000 - 20000);
resolve_betting_market_group(moneyline_betting_markets.id, BOOST_CHECK_EQUAL(get_balance(bob_id, asset_id_type()), 10000000 - 1000000 - 20000);
{{capitals_win_market.id, betting_market_resolution_type::win},
{blackhawks_win_market.id, betting_market_resolution_type::cancel}}); // caps win
resolve_betting_market_group(moneyline_betting_markets.id,
{{capitals_win_market.id, betting_market_resolution_type::win},
{blackhawks_win_market.id, betting_market_resolution_type::cancel}});
uint16_t rake_fee_percentage = db.get_global_properties().parameters.betting_rake_fee_percentage; uint16_t rake_fee_percentage = db.get_global_properties().parameters.betting_rake_fee_percentage;
uint32_t rake_value = (-1000000 + 2000000) * rake_fee_percentage / GRAPHENE_1_PERCENT / 100; uint32_t rake_value = (-1000000 + 2000000) * rake_fee_percentage / GRAPHENE_1_PERCENT / 100;
BOOST_TEST_MESSAGE("Rake value " + std::to_string(rake_value)); BOOST_TEST_MESSAGE("Rake value " + std::to_string(rake_value));
BOOST_CHECK_EQUAL(get_balance(alice_id, asset_id_type()), 10000000 - 1000000 - 20000 + 2000000 - rake_value); BOOST_CHECK_EQUAL(get_balance(alice_id, asset_id_type()), 10000000 - 1000000 - 20000 + 2000000 - rake_value);
BOOST_CHECK_EQUAL(get_balance(bob_id, asset_id_type()), 10000000 - 1000000 - 20000); BOOST_CHECK_EQUAL(get_balance(bob_id, asset_id_type()), 10000000 - 1000000 - 20000);
}
catch (fc::exception& e) } FC_LOG_AND_RETHROW()
{
edump((e.to_detail_string()));
throw;
}
//} FC_LOG_AND_RETHROW()
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
//#define BOOST_TEST_MODULE "C++ Unit Tests for Graphene Blockchain Database" //#define BOOST_TEST_MODULE "C++ Unit Tests for Graphene Blockchain Database"
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>

View file

@ -1150,6 +1150,38 @@ void database_fixture::process_operation_by_witnesses(operation op)
} }
} }
void database_fixture::set_is_proposed_trx(operation op)
{
const flat_set<witness_id_type>& active_witnesses = db.get_global_properties().active_witnesses;
proposal_create_operation proposal_op;
proposal_op.fee_paying_account = (*active_witnesses.begin())(db).witness_account;
proposal_op.proposed_ops.emplace_back(op);
proposal_op.expiration_time = db.head_block_time() + fc::days(1);
signed_transaction tx;
tx.operations.push_back(proposal_op);
set_expiration(db, tx);
sign(tx, init_account_priv_key);
processed_transaction processed_tx = db.push_transaction(tx);
proposal_id_type proposal_id = processed_tx.operation_results[0].get<object_id_type>();
for (const witness_id_type& witness_id : active_witnesses)
{
const witness_object& witness = witness_id(db);
const account_object& witness_account = witness.witness_account(db);
proposal_update_operation pup;
pup.proposal = proposal_id;
pup.fee_paying_account = witness_account.id;
pup.active_approvals_to_add.insert(witness_account.id);
db.push_proposal(pup.proposal(db));
break;
}
}
proposal_id_type database_fixture::propose_operation(operation op) proposal_id_type database_fixture::propose_operation(operation op)
{ {
const flat_set<witness_id_type>& active_witnesses = db.get_global_properties().active_witnesses; const flat_set<witness_id_type>& active_witnesses = db.get_global_properties().active_witnesses;
@ -1227,14 +1259,41 @@ const event_group_object& database_fixture::create_event_group(internationalized
} FC_CAPTURE_AND_RETHROW( (name) ) } } FC_CAPTURE_AND_RETHROW( (name) ) }
void database_fixture::update_event_group(event_group_id_type event_group_id, fc::optional<object_id_type> sport_id, fc::optional<internationalized_string_type> name) void database_fixture::update_event_group(event_group_id_type event_group_id,
fc::optional<object_id_type> sport_id,
fc::optional<internationalized_string_type> name)
{ try { { try {
event_group_update_operation event_group_update_op; event_group_update_operation event_group_update_op;
event_group_update_op.new_name = name; event_group_update_op.new_name = name;
event_group_update_op.new_sport_id = sport_id; event_group_update_op.new_sport_id = sport_id;
event_group_update_op.event_group_id = event_group_id; event_group_update_op.event_group_id = event_group_id;
process_operation_by_witnesses(event_group_update_op); process_operation_by_witnesses(event_group_update_op);
} FC_CAPTURE_AND_RETHROW( (name) ) } } FC_CAPTURE_AND_RETHROW( (name) )
}
void database_fixture::try_update_event_group(event_group_id_type event_group_id,
fc::optional<object_id_type> sport_id,
fc::optional<internationalized_string_type> name,
bool dont_set_is_proposed_trx /* = false */)
{
event_group_update_operation event_group_update_op;
event_group_update_op.new_name = name;
event_group_update_op.new_sport_id = sport_id;
event_group_update_op.event_group_id = event_group_id;
if (!dont_set_is_proposed_trx)
set_is_proposed_trx(event_group_update_op);
const chain_parameters& params = db.get_global_properties().parameters;
signed_transaction trx;
trx.operations = {event_group_update_op};
for( auto& op : trx.operations )
db.current_fee_schedule().set_fee(op);
trx.validate();
trx.set_expiration(db.head_block_time() + fc::seconds( params.block_interval * (params.maintenance_skip_slots + 1) * 3));
sign(trx, init_account_priv_key);
PUSH_TX(db, trx);
}
const event_object& database_fixture::create_event(internationalized_string_type name, internationalized_string_type season, event_group_id_type event_group_id) const event_object& database_fixture::create_event(internationalized_string_type name, internationalized_string_type season, event_group_id_type event_group_id)
{ try { { try {

View file

@ -285,10 +285,17 @@ struct database_fixture {
asset_id_type dividend_payout_asset_type) const; asset_id_type dividend_payout_asset_type) const;
vector< operation_history_object > get_operation_history( account_id_type account_id )const; vector< operation_history_object > get_operation_history( account_id_type account_id )const;
void process_operation_by_witnesses(operation op); void process_operation_by_witnesses(operation op);
void set_is_proposed_trx(operation op);
const sport_object& create_sport(internationalized_string_type name); const sport_object& create_sport(internationalized_string_type name);
void update_sport(sport_id_type sport_id, internationalized_string_type name); void update_sport(sport_id_type sport_id, internationalized_string_type name);
const event_group_object& create_event_group(internationalized_string_type name, sport_id_type sport_id); const event_group_object& create_event_group(internationalized_string_type name, sport_id_type sport_id);
void update_event_group(event_group_id_type event_group_id, fc::optional<object_id_type> sport_id, fc::optional<internationalized_string_type> name); void update_event_group(event_group_id_type event_group_id,
fc::optional<object_id_type> sport_id,
fc::optional<internationalized_string_type> name);
void try_update_event_group(event_group_id_type event_group_id,
fc::optional<object_id_type> sport_id,
fc::optional<internationalized_string_type> name,
bool dont_set_is_proposed_trx = false);
const event_object& create_event(internationalized_string_type name, internationalized_string_type season, event_group_id_type event_group_id); const event_object& create_event(internationalized_string_type name, internationalized_string_type season, event_group_id_type event_group_id);
void update_event(event_id_type event_id, void update_event(event_id_type event_id,
fc::optional<object_id_type> event_group_id, fc::optional<object_id_type> event_group_id,