Simplify backend mock access for unittests (#1062)

This commit is contained in:
Alex Kremer
2024-01-02 13:35:57 +00:00
committed by GitHub
parent 781f3b3c48
commit d077093a8d
38 changed files with 2200 additions and 2926 deletions

View File

@@ -53,16 +53,13 @@ class RPCAccountCurrenciesHandlerTest : public HandlerBaseTest {};
TEST_F(RPCAccountCurrenciesHandlerTest, AccountNotExist)
{
auto const rawBackendPtr = dynamic_cast<MockBackend*>(mockBackendPtr.get());
ASSERT_NE(rawBackendPtr, nullptr);
mockBackendPtr->updateRange(10); // min
mockBackendPtr->updateRange(30); // max
backend->setRange(10, 30);
auto const ledgerinfo = CreateLedgerInfo(LEDGERHASH, 30);
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence).Times(1);
EXPECT_CALL(*backend, fetchLedgerBySequence).Times(1);
ON_CALL(*rawBackendPtr, fetchLedgerBySequence).WillByDefault(Return(ledgerinfo));
ON_CALL(*rawBackendPtr, doFetchLedgerObject).WillByDefault(Return(std::optional<Blob>{}));
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObject).Times(1);
ON_CALL(*backend, fetchLedgerBySequence).WillByDefault(Return(ledgerinfo));
ON_CALL(*backend, doFetchLedgerObject).WillByDefault(Return(std::optional<Blob>{}));
EXPECT_CALL(*backend, doFetchLedgerObject).Times(1);
auto static const input = json::parse(fmt::format(
R"({{
@@ -70,7 +67,7 @@ TEST_F(RPCAccountCurrenciesHandlerTest, AccountNotExist)
}})",
ACCOUNT
));
auto const handler = AnyHandler{AccountCurrenciesHandler{mockBackendPtr}};
auto const handler = AnyHandler{AccountCurrenciesHandler{backend}};
runSpawn([&](auto yield) {
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
@@ -82,13 +79,10 @@ TEST_F(RPCAccountCurrenciesHandlerTest, AccountNotExist)
TEST_F(RPCAccountCurrenciesHandlerTest, LedgerNonExistViaIntSequence)
{
auto const rawBackendPtr = dynamic_cast<MockBackend*>(mockBackendPtr.get());
ASSERT_NE(rawBackendPtr, nullptr);
mockBackendPtr->updateRange(10); // min
mockBackendPtr->updateRange(30); // max
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence).Times(1);
backend->setRange(10, 30);
EXPECT_CALL(*backend, fetchLedgerBySequence).Times(1);
// return empty ledgerinfo
ON_CALL(*rawBackendPtr, fetchLedgerBySequence(30, _)).WillByDefault(Return(std::optional<ripple::LedgerInfo>{}));
ON_CALL(*backend, fetchLedgerBySequence(30, _)).WillByDefault(Return(std::optional<ripple::LedgerInfo>{}));
auto static const input = json::parse(fmt::format(
R"({{
@@ -96,7 +90,7 @@ TEST_F(RPCAccountCurrenciesHandlerTest, LedgerNonExistViaIntSequence)
}})",
ACCOUNT
));
auto const handler = AnyHandler{AccountCurrenciesHandler{mockBackendPtr}};
auto const handler = AnyHandler{AccountCurrenciesHandler{backend}};
runSpawn([&](auto yield) {
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
@@ -109,13 +103,11 @@ TEST_F(RPCAccountCurrenciesHandlerTest, LedgerNonExistViaIntSequence)
TEST_F(RPCAccountCurrenciesHandlerTest, LedgerNonExistViaStringSequence)
{
auto constexpr seq = 12;
auto const rawBackendPtr = dynamic_cast<MockBackend*>(mockBackendPtr.get());
ASSERT_NE(rawBackendPtr, nullptr);
mockBackendPtr->updateRange(10); // min
mockBackendPtr->updateRange(30); // max
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence).Times(1);
backend->setRange(10, 30);
EXPECT_CALL(*backend, fetchLedgerBySequence).Times(1);
// return empty ledgerinfo
ON_CALL(*rawBackendPtr, fetchLedgerBySequence(12, _)).WillByDefault(Return(std::optional<ripple::LedgerInfo>{}));
ON_CALL(*backend, fetchLedgerBySequence(12, _)).WillByDefault(Return(std::optional<ripple::LedgerInfo>{}));
auto static const input = json::parse(fmt::format(
R"({{
@@ -125,7 +117,7 @@ TEST_F(RPCAccountCurrenciesHandlerTest, LedgerNonExistViaStringSequence)
ACCOUNT,
seq
));
auto const handler = AnyHandler{AccountCurrenciesHandler{mockBackendPtr}};
auto const handler = AnyHandler{AccountCurrenciesHandler{backend}};
runSpawn([&](auto yield) {
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
@@ -137,13 +129,10 @@ TEST_F(RPCAccountCurrenciesHandlerTest, LedgerNonExistViaStringSequence)
TEST_F(RPCAccountCurrenciesHandlerTest, LedgerNonExistViaHash)
{
auto const rawBackendPtr = dynamic_cast<MockBackend*>(mockBackendPtr.get());
ASSERT_NE(rawBackendPtr, nullptr);
mockBackendPtr->updateRange(10); // min
mockBackendPtr->updateRange(30); // max
EXPECT_CALL(*rawBackendPtr, fetchLedgerByHash).Times(1);
backend->setRange(10, 30);
EXPECT_CALL(*backend, fetchLedgerByHash).Times(1);
// return empty ledgerinfo
ON_CALL(*rawBackendPtr, fetchLedgerByHash(ripple::uint256{LEDGERHASH}, _))
ON_CALL(*backend, fetchLedgerByHash(ripple::uint256{LEDGERHASH}, _))
.WillByDefault(Return(std::optional<ripple::LedgerInfo>{}));
auto static const input = json::parse(fmt::format(
@@ -154,7 +143,7 @@ TEST_F(RPCAccountCurrenciesHandlerTest, LedgerNonExistViaHash)
ACCOUNT,
LEDGERHASH
));
auto const handler = AnyHandler{AccountCurrenciesHandler{mockBackendPtr}};
auto const handler = AnyHandler{AccountCurrenciesHandler{backend}};
runSpawn([&](auto yield) {
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
@@ -179,24 +168,22 @@ TEST_F(RPCAccountCurrenciesHandlerTest, DefaultParameter)
"USD"
]
})";
auto const rawBackendPtr = dynamic_cast<MockBackend*>(mockBackendPtr.get());
ASSERT_NE(rawBackendPtr, nullptr);
mockBackendPtr->updateRange(10); // min
mockBackendPtr->updateRange(30); // max
backend->setRange(10, 30);
// return valid ledgerinfo
auto const ledgerinfo = CreateLedgerInfo(LEDGERHASH, 30);
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence).Times(1);
ON_CALL(*rawBackendPtr, fetchLedgerBySequence(30, _)).WillByDefault(Return(ledgerinfo));
EXPECT_CALL(*backend, fetchLedgerBySequence).Times(1);
ON_CALL(*backend, fetchLedgerBySequence(30, _)).WillByDefault(Return(ledgerinfo));
// return valid account
auto const accountKk = ripple::keylet::account(GetAccountIDWithString(ACCOUNT)).key;
ON_CALL(*rawBackendPtr, doFetchLedgerObject(accountKk, 30, _)).WillByDefault(Return(Blob{'f', 'a', 'k', 'e'}));
ON_CALL(*backend, doFetchLedgerObject(accountKk, 30, _)).WillByDefault(Return(Blob{'f', 'a', 'k', 'e'}));
auto const ownerDir =
CreateOwnerDirLedgerObject({ripple::uint256{INDEX1}, ripple::uint256{INDEX2}, ripple::uint256{INDEX2}}, INDEX1);
auto const ownerDirKk = ripple::keylet::ownerDir(GetAccountIDWithString(ACCOUNT)).key;
ON_CALL(*rawBackendPtr, doFetchLedgerObject(ownerDirKk, 30, _))
ON_CALL(*backend, doFetchLedgerObject(ownerDirKk, 30, _))
.WillByDefault(Return(ownerDir.getSerializer().peekData()));
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObject).Times(2);
EXPECT_CALL(*backend, doFetchLedgerObject).Times(2);
// ACCOUNT can receive USD 10 from ACCOUNT2 and send USD 20 to ACCOUNT2, now
// the balance is 100, ACCOUNT can only send USD to ACCOUNT2
@@ -212,15 +199,15 @@ TEST_F(RPCAccountCurrenciesHandlerTest, DefaultParameter)
bbs.push_back(line2.getSerializer().peekData());
bbs.push_back(line3.getSerializer().peekData());
ON_CALL(*rawBackendPtr, doFetchLedgerObjects).WillByDefault(Return(bbs));
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObjects).Times(1);
ON_CALL(*backend, doFetchLedgerObjects).WillByDefault(Return(bbs));
EXPECT_CALL(*backend, doFetchLedgerObjects).Times(1);
auto static const input = json::parse(fmt::format(
R"({{
"account":"{}"
}})",
ACCOUNT
));
auto const handler = AnyHandler{AccountCurrenciesHandler{mockBackendPtr}};
auto const handler = AnyHandler{AccountCurrenciesHandler{backend}};
runSpawn([&](auto yield) {
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
@@ -230,29 +217,26 @@ TEST_F(RPCAccountCurrenciesHandlerTest, DefaultParameter)
TEST_F(RPCAccountCurrenciesHandlerTest, RequestViaLegderHash)
{
auto const rawBackendPtr = dynamic_cast<MockBackend*>(mockBackendPtr.get());
ASSERT_NE(rawBackendPtr, nullptr);
mockBackendPtr->updateRange(10); // min
mockBackendPtr->updateRange(30); // max
backend->setRange(10, 30);
// return valid ledgerinfo
auto const ledgerinfo = CreateLedgerInfo(LEDGERHASH, 30);
EXPECT_CALL(*rawBackendPtr, fetchLedgerByHash).Times(1);
ON_CALL(*rawBackendPtr, fetchLedgerByHash(ripple::uint256{LEDGERHASH}, _)).WillByDefault(Return(ledgerinfo));
EXPECT_CALL(*backend, fetchLedgerByHash).Times(1);
ON_CALL(*backend, fetchLedgerByHash(ripple::uint256{LEDGERHASH}, _)).WillByDefault(Return(ledgerinfo));
// return valid account
auto const accountKk = ripple::keylet::account(GetAccountIDWithString(ACCOUNT)).key;
ON_CALL(*rawBackendPtr, doFetchLedgerObject(accountKk, 30, _)).WillByDefault(Return(Blob{'f', 'a', 'k', 'e'}));
ON_CALL(*backend, doFetchLedgerObject(accountKk, 30, _)).WillByDefault(Return(Blob{'f', 'a', 'k', 'e'}));
auto const ownerDir = CreateOwnerDirLedgerObject({ripple::uint256{INDEX1}}, INDEX1);
auto const ownerDirKk = ripple::keylet::ownerDir(GetAccountIDWithString(ACCOUNT)).key;
ON_CALL(*rawBackendPtr, doFetchLedgerObject(ownerDirKk, 30, _))
ON_CALL(*backend, doFetchLedgerObject(ownerDirKk, 30, _))
.WillByDefault(Return(ownerDir.getSerializer().peekData()));
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObject).Times(2);
EXPECT_CALL(*backend, doFetchLedgerObject).Times(2);
std::vector<Blob> bbs;
auto const line1 = CreateRippleStateLedgerObject("USD", ISSUER, 100, ACCOUNT, 10, ACCOUNT2, 20, TXNID, 123, 0);
bbs.push_back(line1.getSerializer().peekData());
ON_CALL(*rawBackendPtr, doFetchLedgerObjects).WillByDefault(Return(bbs));
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObjects).Times(1);
ON_CALL(*backend, doFetchLedgerObjects).WillByDefault(Return(bbs));
EXPECT_CALL(*backend, doFetchLedgerObjects).Times(1);
auto static const input = json::parse(fmt::format(
R"({{
"account":"{}",
@@ -261,7 +245,7 @@ TEST_F(RPCAccountCurrenciesHandlerTest, RequestViaLegderHash)
ACCOUNT,
LEDGERHASH
));
auto const handler = AnyHandler{AccountCurrenciesHandler{mockBackendPtr}};
auto const handler = AnyHandler{AccountCurrenciesHandler{backend}};
runSpawn([&](auto yield) {
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
@@ -270,31 +254,27 @@ TEST_F(RPCAccountCurrenciesHandlerTest, RequestViaLegderHash)
TEST_F(RPCAccountCurrenciesHandlerTest, RequestViaLegderSeq)
{
auto const rawBackendPtr = dynamic_cast<MockBackend*>(mockBackendPtr.get());
ASSERT_NE(rawBackendPtr, nullptr);
mockBackendPtr->updateRange(10); // min
mockBackendPtr->updateRange(30); // max
backend->setRange(10, 30);
auto const ledgerSeq = 29;
// return valid ledgerinfo
auto const ledgerinfo = CreateLedgerInfo(LEDGERHASH, ledgerSeq);
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence).Times(1);
ON_CALL(*rawBackendPtr, fetchLedgerBySequence(ledgerSeq, _)).WillByDefault(Return(ledgerinfo));
EXPECT_CALL(*backend, fetchLedgerBySequence).Times(1);
ON_CALL(*backend, fetchLedgerBySequence(ledgerSeq, _)).WillByDefault(Return(ledgerinfo));
// return valid account
auto const accountKk = ripple::keylet::account(GetAccountIDWithString(ACCOUNT)).key;
ON_CALL(*rawBackendPtr, doFetchLedgerObject(accountKk, ledgerSeq, _))
.WillByDefault(Return(Blob{'f', 'a', 'k', 'e'}));
ON_CALL(*backend, doFetchLedgerObject(accountKk, ledgerSeq, _)).WillByDefault(Return(Blob{'f', 'a', 'k', 'e'}));
auto const ownerDir = CreateOwnerDirLedgerObject({ripple::uint256{INDEX1}}, INDEX1);
auto const ownerDirKk = ripple::keylet::ownerDir(GetAccountIDWithString(ACCOUNT)).key;
ON_CALL(*rawBackendPtr, doFetchLedgerObject(ownerDirKk, ledgerSeq, _))
ON_CALL(*backend, doFetchLedgerObject(ownerDirKk, ledgerSeq, _))
.WillByDefault(Return(ownerDir.getSerializer().peekData()));
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObject).Times(2);
EXPECT_CALL(*backend, doFetchLedgerObject).Times(2);
std::vector<Blob> bbs;
auto const line1 = CreateRippleStateLedgerObject("USD", ISSUER, 100, ACCOUNT, 10, ACCOUNT2, 20, TXNID, 123, 0);
bbs.push_back(line1.getSerializer().peekData());
ON_CALL(*rawBackendPtr, doFetchLedgerObjects).WillByDefault(Return(bbs));
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObjects).Times(1);
ON_CALL(*backend, doFetchLedgerObjects).WillByDefault(Return(bbs));
EXPECT_CALL(*backend, doFetchLedgerObjects).Times(1);
auto static const input = json::parse(fmt::format(
R"({{
"account":"{}",
@@ -303,7 +283,7 @@ TEST_F(RPCAccountCurrenciesHandlerTest, RequestViaLegderSeq)
ACCOUNT,
ledgerSeq
));
auto const handler = AnyHandler{AccountCurrenciesHandler{mockBackendPtr}};
auto const handler = AnyHandler{AccountCurrenciesHandler{backend}};
runSpawn([&](auto yield) {
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);