Skip to content

Commit

Permalink
add tests that fail and shouldn't
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Apr 17, 2024
1 parent da63bf1 commit e22a296
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
82 changes: 82 additions & 0 deletions pallets/foreign-investments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1592,3 +1592,85 @@ mod notifications {
});
}
}

mod zero_amount_order {
use super::*;

#[test]
fn when_increase_with_zero() {
new_test_ext().execute_with(|| {
util::base_configuration();

assert_ok!(ForeignInvestment::increase_foreign_investment(
&USER,
INVESTMENT_ID,
0,
FOREIGN_CURR
));

assert_err!(
Swaps::order_id(&USER, (INVESTMENT_ID, Action::Investment)),
pallet_swaps::Error::<Runtime>::OrderNotFound
);
});
}

#[test]
fn when_increase_after_decrease_but_math_precission() {
new_test_ext().execute_with(|| {
const FOREIGN_AMOUNT: Balance = 100;

util::base_configuration();

MockTokenSwaps::mock_convert_by_market(|to, from, amount_from| {
Ok(match (from, to) {
(POOL_CURR, FOREIGN_CURR) => amount_from / 3 + 1,
(FOREIGN_CURR, POOL_CURR) => amount_from * 3,
_ => unreachable!(),
})
});

assert_ok!(ForeignInvestment::increase_foreign_investment(
&USER,
INVESTMENT_ID,
FOREIGN_AMOUNT,
FOREIGN_CURR
));

util::fulfill_last_swap(Action::Investment, FOREIGN_AMOUNT);

assert!(Swaps::order_id(&USER, (INVESTMENT_ID, Action::Investment)).is_err());

assert_ok!(ForeignInvestment::decrease_foreign_investment(
&USER,
INVESTMENT_ID,
FOREIGN_AMOUNT,
FOREIGN_CURR
));

MockDecreaseInvestHook::mock_notify_status_change(|_, msg| {
assert_eq!(
msg,
ExecutedForeignDecreaseInvest {
amount_decreased: FOREIGN_AMOUNT + 1,
foreign_currency: FOREIGN_CURR,
amount_remaining: FOREIGN_AMOUNT,
}
);
Ok(())
});

assert_ok!(ForeignInvestment::increase_foreign_investment(
&USER,
INVESTMENT_ID,
FOREIGN_AMOUNT + 1,
FOREIGN_CURR
));

assert_err!(
Swaps::order_id(&USER, (INVESTMENT_ID, Action::Investment)),
pallet_swaps::Error::<Runtime>::OrderNotFound
);
});
}
}
84 changes: 84 additions & 0 deletions pallets/swaps/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,87 @@ mod fulfill {
});
}
}

mod zero_amount_order {
use super::*;

#[test]
fn when_apply_over_no_swap() {
new_test_ext().execute_with(|| {
MockTokenSwaps::mock_place_order(|_, _, _, _, _| {
panic!("this mock should not be called");
});

assert_ok!(
<Swaps as TSwaps<AccountId>>::apply_swap(
&USER,
SWAP_ID,
Swap {
currency_in: CURRENCY_B,
currency_out: CURRENCY_A,
amount_out: AMOUNT,
},
),
SwapStatus {
swapped: 0,
pending: 0,
}
);

assert_eq!(OrderIdToSwapId::<Runtime>::get(ORDER_ID), None);
assert_eq!(SwapIdToOrderId::<Runtime>::get((USER, SWAP_ID)), None);
});
}

#[test]
fn when_apply_over_smaller_inverse_swap_but_math_precission() {
const AMOUNT_A: Balance = 100;
const NEW_ORDER_ID: OrderId = ORDER_ID + 1;

new_test_ext().execute_with(|| {
MockTokenSwaps::mock_convert_by_market(|to, from, amount_from| match (from, to) {
(CURRENCY_B, CURRENCY_A) => Ok(amount_from * 3),
(CURRENCY_A, CURRENCY_B) => Ok(amount_from / 3 + 1),
_ => unreachable!(),
});
MockTokenSwaps::mock_get_order_details(|_| {
// Inverse swap
Some(OrderInfo {
swap: Swap {
currency_in: CURRENCY_A,
currency_out: CURRENCY_B,
amount_out: AMOUNT_A / 3,
},
ratio: OrderRatio::Market,
})
});
MockTokenSwaps::mock_cancel_order(|_| Ok(()));

MockTokenSwaps::mock_place_order(|_, _, _, amount, _| {
assert_eq!(amount, 0);
panic!("this mock should not be called");
});

Swaps::update_id(&USER, SWAP_ID, Some(ORDER_ID)).unwrap();

assert_ok!(
<Swaps as TSwaps<AccountId>>::apply_swap(
&USER,
SWAP_ID,
Swap {
currency_out: CURRENCY_A,
currency_in: CURRENCY_B,
amount_out: AMOUNT_A - 1,
},
),
SwapStatus {
swapped: AMOUNT_A / 3,
pending: 0,
}
);

assert_eq!(OrderIdToSwapId::<Runtime>::get(ORDER_ID), None);
assert_eq!(SwapIdToOrderId::<Runtime>::get((USER, SWAP_ID)), None);
});
}
}

0 comments on commit e22a296

Please sign in to comment.