fix: google mock warrnings about json

pull/93/merge
moneroexamples 6 years ago
parent 506fb68c0d
commit a87eb7f70c

@ -791,8 +791,8 @@ CurrentBlockchainStatus::find_txs_in_mempool(
return false;
}
transactions = get_search_thread(address_str)
.find_txs_in_mempool(mempool_txs);
get_search_thread(address_str)
.find_txs_in_mempool(mempool_txs, &transactions);
return true;
}
@ -840,7 +840,7 @@ CurrentBlockchainStatus::find_key_images_in_mempool(
for (auto const& mtx: mempool_txs)
{
const transaction &m_tx = mtx.second;
transaction const& m_tx = mtx.second;
vector<txin_to_key> input_key_imgs
= xmreg::get_key_images(m_tx);

@ -588,10 +588,13 @@ TxSearch::get_known_outputs_keys()
return known_outputs_keys;
};
json
TxSearch::find_txs_in_mempool(TxSearch::pool_txs_t mempool_txs)
{
json j_transactions = json::array();
void
TxSearch::find_txs_in_mempool(
TxSearch::pool_txs_t mempool_txs,
json* j_transactions)
{
*j_transactions = json::array();
uint64_t current_height = current_bc_status->get_current_blockchain_height();
@ -655,7 +658,7 @@ TxSearch::find_txs_in_mempool(TxSearch::pool_txs_t mempool_txs)
j_tx["mixin"] = oi_identification.get_mixin_no();
j_tx["mempool"] = true;
j_transactions.push_back(j_tx);
j_transactions->push_back(j_tx);
}
@ -710,7 +713,7 @@ TxSearch::find_txs_in_mempool(TxSearch::pool_txs_t mempool_txs)
// exisiting j_tx. we add spending info
// to j_tx created before.
json& j_tx = j_transactions.back();
json& j_tx = j_transactions->back();
j_tx["total_sent"] = total_sent;
j_tx["spent_outputs"] = spend_keys;
@ -745,7 +748,7 @@ TxSearch::find_txs_in_mempool(TxSearch::pool_txs_t mempool_txs)
j_tx["mempool"] = true;
j_tx["spent_outputs"] = spend_keys;
j_transactions.push_back(j_tx);
j_transactions->push_back(j_tx);
} // else of if (!oi_identification.identified_outputs.empty())
@ -754,9 +757,6 @@ TxSearch::find_txs_in_mempool(TxSearch::pool_txs_t mempool_txs)
} // if (!oi_identification.identified_inputs.empty())
} // for (const transaction& tx: txs_to_check)
return j_transactions;
}

@ -132,8 +132,9 @@ public:
*
* @return json
*/
virtual json
find_txs_in_mempool(pool_txs_t mempool_txs);
virtual void
find_txs_in_mempool(pool_txs_t mempool_txs,
json* j_transactions);
virtual addr_view_t
get_xmr_address_viewkey() const;

@ -0,0 +1,3 @@
## Testing OpenMonero
Description how to run tests etc.

@ -15,7 +15,7 @@ namespace
{
using json = nlohmann::json;
//using json = nlohmann::json;
using namespace std;
using namespace cryptonote;
using namespace epee::string_tools;
@ -30,6 +30,7 @@ using ::testing::Return;
using ::testing::Throw;
using ::testing::DoAll;
using ::testing::SetArgReferee;
using ::testing::SetArgPointee;
using ::testing::_;
using ::testing::internal::FilePath;
@ -119,7 +120,7 @@ class MockTxSearch : public xmreg::TxSearch
{
public:
MOCK_METHOD0(operator_fcall, void());
void operator()() override {operator_fcall();}
virtual void operator()() {operator_fcall();}
MOCK_METHOD0(ping, void());
@ -133,8 +134,31 @@ public:
MOCK_CONST_METHOD0(get_xmr_address_viewkey,
xmreg::TxSearch::addr_view_t());
MOCK_METHOD1(find_txs_in_mempool,
json(xmreg::TxSearch::pool_txs_t mempool_txs));
// google mock has some issues with nlohmann::json
// as input or return parameters in functions it mocks.
// it resutls in wornings, so to avoid it we are going
// to proxy json as strings.
MOCK_METHOD2(mock_find_txs_in_mempool,
void(xmreg::TxSearch::pool_txs_t mempool_txs,
vector<string>& transactions_json_str));
// now we need to manualy overwrite TxSearch::find_txs_in_mempool
// to return j_transactions based on transactions_json_str
// from the mock_find_txs_in_mempool
virtual void
find_txs_in_mempool(pool_txs_t mempool_txs,
nlohmann::json* j_transactions)
{
vector<string> transactions_json_str;
mock_find_txs_in_mempool(mempool_txs, transactions_json_str);
*j_transactions = nlohmann::json::array();
for (auto& json_str: transactions_json_str)
j_transactions->push_back(nlohmann::json::parse(json_str));
}
};
@ -179,11 +203,11 @@ protected:
MockMicroCore* mcore_ptr;
MockRPCCalls* rpc_ptr;
static json config_json;
static nlohmann::json config_json;
};
json BCSTATUS_TEST::config_json;
nlohmann::json BCSTATUS_TEST::config_json;
TEST_P(BCSTATUS_TEST, DefaultConstruction)
{
@ -1050,21 +1074,27 @@ TEST_P(BCSTATUS_TEST, FindTxsInMempool)
auto tx_search = std::make_unique<MockTxSearch>();
json txs_to_return = json::array();
nlohmann::json txs_to_return = nlohmann::json::array();
txs_to_return.push_back(json {"tx_hash1", "some_tx_hash1"});
txs_to_return.push_back(json {"tx_hash2", "some_tx_hash2"});
txs_to_return.push_back(json {"tx_hash3", "some_tx_hash3"});
txs_to_return.push_back(nlohmann::json {"tx_hash1", "some_tx_hash1"});
txs_to_return.push_back(nlohmann::json {"tx_hash2", "some_tx_hash2"});
txs_to_return.push_back(nlohmann::json {"tx_hash3", "some_tx_hash3"});
EXPECT_CALL(*tx_search, find_txs_in_mempool(_))
.WillRepeatedly(Return(txs_to_return));
vector<string> txs_to_return_json_str;
for (auto& tx_json: txs_to_return)
txs_to_return_json_str.push_back(tx_json.dump());
EXPECT_CALL(*tx_search, mock_find_txs_in_mempool(_,_))
.WillRepeatedly(SetArgReferee<1>(txs_to_return_json_str));
EXPECT_CALL(*tx_search, operator_fcall()) // mock operator()
.WillRepeatedly(MockSearchWhile2());
ASSERT_TRUE(bcs->start_tx_search_thread(acc, std::move(tx_search)));
json txs;
nlohmann::json txs;
EXPECT_TRUE(bcs->find_txs_in_mempool(acc.address, txs));
@ -1168,6 +1198,28 @@ TEST_P(BCSTATUS_TEST, FindTxsInMempool2)
crypto::hash non_existing_tx_hash = crypto::rand<crypto::hash>();
EXPECT_FALSE(bcs->find_tx_in_mempool(non_existing_tx_hash, tx));
}
TEST_P(BCSTATUS_TEST, FindKeyImagesInMempool)
{
vector<tx_info> mempool_txs_to_return;
vector<string> txs_blobs = get_mock_txs_as_str(true);
for (auto const& tx_blob: txs_blobs)
{
mempool_txs_to_return.emplace_back();
mempool_txs_to_return.back().tx_blob = tx_blob;
}
EXPECT_CALL(*mcore_ptr, get_mempool_txs(_, _))
.WillRepeatedly(DoAll(
SetArgReferee<0>(mempool_txs_to_return),
Return(true)));
ASSERT_TRUE(bcs->read_mempool());
}

Loading…
Cancel
Save