Skip to content

Commit

Permalink
fix: FI mock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Sep 26, 2023
1 parent b801636 commit dc71c04
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
14 changes: 5 additions & 9 deletions libs/mocks/src/token_swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ pub mod pallet {
T::CurrencyId,
T::Balance,
T::SellRatio,
T::Balance,
) -> Result<T::OrderId, DispatchError>
+ 'static,
) {
register_call!(move |(a, b, c, d, e, g)| f(a, b, c, d, e, g));
register_call!(move |(a, b, c, d, e)| f(a, b, c, d, e));
}

pub fn mock_update_order(
f: impl Fn(T::AccountId, T::OrderId, T::Balance, T::SellRatio, T::Balance) -> DispatchResult
+ 'static,
f: impl Fn(T::AccountId, T::OrderId, T::Balance, T::SellRatio) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c, d, e)| f(a, b, c, d, e));
register_call!(move |(a, b, c, d)| f(a, b, c, d));
}

pub fn mock_cancel_order(f: impl Fn(T::OrderId) -> DispatchResult + 'static) {
Expand Down Expand Up @@ -79,19 +77,17 @@ pub mod pallet {
c: Self::CurrencyId,
d: Self::Balance,
e: Self::SellRatio,
f: Self::Balance,
) -> Result<Self::OrderId, DispatchError> {
execute_call!((a, b, c, d, e, f))
execute_call!((a, b, c, d, e))
}

fn update_order(
a: T::AccountId,
b: Self::OrderId,
c: Self::Balance,
d: Self::SellRatio,
e: Self::Balance,
) -> DispatchResult {
execute_call!((a, b, c, d, e))
execute_call!((a, b, c, d))
}

fn cancel_order(a: Self::OrderId) -> DispatchResult {
Expand Down
45 changes: 19 additions & 26 deletions pallets/foreign-investments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod util {
MockInvestment::mock_investment_requires_collect(|_, _| false);
MockInvestment::mock_investment(|_, _| Ok(0));
MockInvestment::mock_update_investment(|_, _, _| Ok(()));
MockTokenSwaps::mock_place_order(move |_, _, _, _, _, _| Ok(order_id));
MockTokenSwaps::mock_place_order(move |_, _, _, _, _| Ok(order_id));
MockCurrencyConversion::mock_stable_to_stable(move |_, _, _| Ok(amount) /* 1:1 */);

ForeignInvestment::increase_foreign_investment(
Expand All @@ -37,7 +37,7 @@ mod util {
MockInvestment::mock_investment_requires_collect(|_, _| unimplemented!("no mock"));
MockInvestment::mock_investment(|_, _| unimplemented!("no mock"));
MockInvestment::mock_update_investment(|_, _, _| unimplemented!("no mock"));
MockTokenSwaps::mock_place_order(|_, _, _, _, _, _| unimplemented!("no mock"));
MockTokenSwaps::mock_place_order(|_, _, _, _, _| unimplemented!("no mock"));
MockCurrencyConversion::mock_stable_to_stable(|_, _, _| unimplemented!("no mock"));
}

Expand Down Expand Up @@ -92,17 +92,14 @@ mod increase_investment {
assert_eq!(amount, 0); // We still do not have the swap done.
Ok(())
});
MockTokenSwaps::mock_place_order(
|account_id, curr_in, curr_out, amount, limit, min| {
assert_eq!(account_id, USER);
assert_eq!(curr_in, POOL_CURR);
assert_eq!(curr_out, USER_CURR);
assert_eq!(amount, AMOUNT);
assert_eq!(limit, DefaultTokenSellRatio::get());
assert_eq!(min, AMOUNT);
Ok(ORDER_ID)
},
);
MockTokenSwaps::mock_place_order(|account_id, curr_in, curr_out, amount, limit| {
assert_eq!(account_id, USER);
assert_eq!(curr_in, POOL_CURR);
assert_eq!(curr_out, USER_CURR);
assert_eq!(amount, AMOUNT);
assert_eq!(limit, DefaultTokenSellRatio::get());
Ok(ORDER_ID)
});
MockCurrencyConversion::mock_stable_to_stable(|curr_in, curr_out, amount_out| {
assert_eq!(curr_in, POOL_CURR);
assert_eq!(curr_out, USER_CURR);
Expand Down Expand Up @@ -173,12 +170,11 @@ mod increase_investment {
amount: INITIAL_AMOUNT,
})
});
MockTokenSwaps::mock_update_order(|account_id, order_id, amount, limit, min| {
MockTokenSwaps::mock_update_order(|account_id, order_id, amount, limit| {
assert_eq!(account_id, USER);
assert_eq!(order_id, ORDER_ID);
assert_eq!(amount, INITIAL_AMOUNT + INCREASE_AMOUNT);
assert_eq!(limit, DefaultTokenSellRatio::get());
assert_eq!(min, INITIAL_AMOUNT + INCREASE_AMOUNT);
Ok(())
});
MockCurrencyConversion::mock_stable_to_stable(|curr_in, curr_out, amount_out| {
Expand Down Expand Up @@ -224,17 +220,14 @@ mod increase_investment {
assert_eq!(order_id, ORDER_ID);
false
});
MockTokenSwaps::mock_place_order(
|account_id, curr_in, curr_out, amount, limit, min| {
assert_eq!(account_id, USER);
assert_eq!(curr_in, POOL_CURR);
assert_eq!(curr_out, USER_CURR);
assert_eq!(amount, INCREASE_AMOUNT);
assert_eq!(limit, DefaultTokenSellRatio::get());
assert_eq!(min, INCREASE_AMOUNT);
Ok(ORDER_ID)
},
);
MockTokenSwaps::mock_place_order(|account_id, curr_in, curr_out, amount, limit| {
assert_eq!(account_id, USER);
assert_eq!(curr_in, POOL_CURR);
assert_eq!(curr_out, USER_CURR);
assert_eq!(amount, INCREASE_AMOUNT);
assert_eq!(limit, DefaultTokenSellRatio::get());
Ok(ORDER_ID)
});
MockInvestment::mock_update_investment(|_, _, amount| {
assert_eq!(amount, 0);
Ok(())
Expand Down

0 comments on commit dc71c04

Please sign in to comment.