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::test;
BOOST_FIXTURE_TEST_SUITE( betting_tests, database_fixture )
#define CREATE_ICE_HOCKEY_BETTING_MARKET() \
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); \
@ -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& blackhawks_win_market = create_betting_market(moneyline_betting_markets.id, {{"en", "Chicago Blackhawks win"}});
BOOST_FIXTURE_TEST_SUITE( betting_tests, database_fixture )
#if 0
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)
#define TRY_EXPECT_THROW( expr, exc_type, reason ) \
{ \
try \
{ \
expr; \
FC_THROW("no error has occured"); \
} \
catch (exc_type& e) \
{ \
edump(("###")); \
edump((e.to_detail_string())); \
edump(("###")); \
FC_ASSERT(e.to_detail_string().find(reason) != \
std::string::npos, "expected error hasn't occured");\
} \
catch (...) \
{ \
FC_THROW("expected throw hasn't occured"); \
} \
try \
{ \
expr; \
FC_THROW("no error has occured"); \
} \
catch (exc_type& e) \
{ \
edump(("###")); \
edump((e.to_detail_string())); \
edump(("###")); \
FC_ASSERT(e.to_detail_string().find(reason) != \
std::string::npos, "expected error hasn't occured");\
} \
catch (...) \
{ \
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
{
ACTORS( (alice)(bob) );
CREATE_ICE_HOCKEY_BETTING_MARKET();
try
{
ACTORS( (alice)(bob)(nathan) );
CREATE_ICE_HOCKEY_BETTING_MARKET();
transfer(account_id_type(), alice_id, asset(10000000));
transfer(account_id_type(), bob_id, asset(10000000));
transfer(account_id_type(), alice_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", "ナショナルホッケーリー"}};
const sport_object& ice_on_hockey = create_sport({{"en", "Hockey on Ice"}, {"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", "アイスホッケー"}}); \
fc::optional<internationalized_string_type> name = n;
fc::optional<internationalized_string_type> name = n;
object_id_type object_id = ice_on_hockey.id;
fc::optional<object_id_type> sport_id = object_id;
object_id_type object_id = ice_on_hockey.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, sport_id, fc::optional<internationalized_string_type>());
update_event_group(nhl.id, sport_id, 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, name);
// #! nothing to change
update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>());
//GRAPHENE_REQUIRE_THROW(update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>()), fc::exception);
//TRY_EXPECT_THROW(update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>()), fc::exception, "nothing to change" );
// trx_state->_is_proposed_trx
//GRAPHENE_REQUIRE_THROW(try_update_event_group(nhl.id, fc::optional<object_id_type>(), fc::optional<internationalized_string_type>(), true), 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");
// #! sport_id must refer to a sport_id_type
object_id = capitals_win_market.id;
sport_id = object_id;
update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>());
// #! nothing to change
//GRAPHENE_REQUIRE_THROW(try_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>()), fc::exception, "nothing to change");
// #! invalid sport specified
object_id = sport_id_type(13);
sport_id = object_id;
update_event_group(nhl.id, sport_id, fc::optional<internationalized_string_type>());
// #! sport_id must refer to a sport_id_type
object_id = capitals_win_market.id;
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, "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);
BOOST_CHECK_EQUAL(get_balance(bob_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 */);
// 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}});
BOOST_CHECK_EQUAL(get_balance(alice_id, asset_id_type()), 10000000 - 1000000 - 20000);
BOOST_CHECK_EQUAL(get_balance(bob_id, asset_id_type()), 10000000 - 1000000 - 20000);
// 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;
uint32_t rake_value = (-1000000 + 2000000) * rake_fee_percentage / GRAPHENE_1_PERCENT / 100;
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(bob_id, asset_id_type()), 10000000 - 1000000 - 20000);
}
catch (fc::exception& e)
{
edump((e.to_detail_string()));
throw;
}
//} FC_LOG_AND_RETHROW()
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;
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(bob_id, asset_id_type()), 10000000 - 1000000 - 20000);
} FC_LOG_AND_RETHROW()
}
BOOST_AUTO_TEST_SUITE_END()
//#define BOOST_TEST_MODULE "C++ Unit Tests for Graphene Blockchain Database"
#include <cstdlib>
#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)
{
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) ) }
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 {
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;
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)
{ try {

View file

@ -285,10 +285,17 @@ struct database_fixture {
asset_id_type dividend_payout_asset_type) const;
vector< operation_history_object > get_operation_history( account_id_type account_id )const;
void process_operation_by_witnesses(operation op);
void set_is_proposed_trx(operation op);
const sport_object& create_sport(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);
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);
void update_event(event_id_type event_id,
fc::optional<object_id_type> event_group_id,