Skip to content

Commit

Permalink
Loans: Integration tests (#1600)
Browse files Browse the repository at this point in the history
* finish basic_loan_flow

* remove deprecated tests

* reorganization of the generic module

* add Readme

* support for external priced loans

* oracle example working

* refactor using calls

* add maturity extension changed test

* minor doc fixes

* fix block by seconds issue

* Support jumping to future blocks
  • Loading branch information
lemunozm authored Oct 27, 2023
1 parent c7e1704 commit f46d327
Show file tree
Hide file tree
Showing 27 changed files with 840 additions and 1,282 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions libs/types/src/fixed_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ impl<const DIV: u128> FixedU128<DIV> {
pub const fn from_inner(inner: u128) -> Self {
Self(inner)
}

/// const version of `FixedPointNumber::saturating_from_integer`.
pub const fn from_integer(n: u128) -> Self {
Self::from_inner(n * DIV)
}
}

impl<const DIV: u128> Saturating for FixedU128<DIV> {
Expand Down
2 changes: 1 addition & 1 deletion pallets/loans/docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ package policy {
}
enum WriteOffTrigger {
PrincipalOverdueDays,
PrincipalOverdue,
PriceOutdated,
}
Expand Down
8 changes: 4 additions & 4 deletions pallets/loans/src/entities/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,16 +523,16 @@ impl<T: Config> ActiveLoan<T> {
#[scale_info(skip_type_params(T))]
pub struct ActiveLoanInfo<T: Config> {
/// Related active loan
active_loan: ActiveLoan<T>,
pub active_loan: ActiveLoan<T>,

/// Present value of the loan
present_value: T::Balance,
pub present_value: T::Balance,

/// Current outstanding principal of this loan
outstanding_principal: T::Balance,
pub outstanding_principal: T::Balance,

/// Current outstanding interest of this loan
outstanding_interest: T::Balance,
pub outstanding_interest: T::Balance,
}

impl<T: Config> TryFrom<(T::PoolId, ActiveLoan<T>)> for ActiveLoanInfo<T> {
Expand Down
1 change: 1 addition & 0 deletions runtime/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ orml-asset-registry = { git = "https://github.com/open-web3-stack/open-runtime-m
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.38" }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.38" }
orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.38" }
orml-oracle = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.38" }

# Misc
xcm-emulator = { git = "https://github.com/shaunxw/xcm-simulator", rev = "754f3b90ecc65af735a6c9a2e1792c5253926ff6" }
Expand Down
23 changes: 23 additions & 0 deletions runtime/integration-tests/src/generic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Runtime Generic tests

The aim of this module is to make integration-tests independently for all runtimes at once.

You can choose the environment for each of your use cases:
- `RuntimeEnv`: Simple environment that acts as a wrapper over the runtime
- `FudgeEnv`: Advanced environment that use a client and connect the runtime to a relay chain.

Both environment uses the same interface so jumping from one to the another should be something "smooth".

## Where I start?
- Create a new file in `cases/<file.rs>` for the use case you want to test.
- Maybe you need to update the `Runtime` trait in `config.rs` file with extra information from your new pallet.
This could imply:
- Adding bounds to the `Runtime` trait with your new pallet.
- Adding bounds to `T::RuntimeCallExt` to support calls from your pallet.
- Adding bounds to `T::EventExt` to support events from your pallet.
- Adding bounds to `T::Api` to support new api calls.
- You can add `GenesisBuild` builders for setting the initial state of your pallet for others in `utils/genesis.rs`.
Please be as generic and simple as possible to leave others to compose its own requirement using your method,
without hidden initializations.
- You can add any utility that helps to initialize states for others under `utils` folder.
Again, focus in simplity but without side effects or hidden / non-obvious state changes.
21 changes: 18 additions & 3 deletions runtime/integration-tests/src/generic/cases/example.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use cfg_primitives::{Balance, CFG};
use cfg_primitives::{Balance, CFG, SECONDS_PER_YEAR};
use cfg_traits::IntoSeconds;
use frame_support::traits::Get;
use sp_api::runtime_decl_for_Core::CoreV4;

use crate::{
generic::{
environment::{Blocks, Env},
config::Runtime,
env::{Blocks, Env},
envs::{
fudge_env::{FudgeEnv, FudgeSupport},
runtime_env::RuntimeEnv,
},
runtime::Runtime,
utils::genesis::Genesis,
},
utils::accounts::Keyring,
Expand Down Expand Up @@ -156,7 +157,21 @@ fn fudge_call_api<T: Runtime + FudgeSupport>() {
})
}

fn pass_time_one_block<T: Runtime>() {
let mut env = RuntimeEnv::<T>::from_storage(Default::default());

let before = env.state(|| pallet_timestamp::Pallet::<T>::get());

// Not supported in fudge
env.pass(Blocks::JumpBySeconds(SECONDS_PER_YEAR));

let after = env.state(|| pallet_timestamp::Pallet::<T>::get());

assert_eq!((after - before).into_seconds(), SECONDS_PER_YEAR)
}

crate::test_for_runtimes!([development, altair, centrifuge], transfer_balance);
crate::test_for_runtimes!(all, call_api);
crate::test_for_runtimes!(all, fudge_transfer_balance);
crate::test_for_runtimes!(all, fudge_call_api);
crate::test_for_runtimes!(all, pass_time_one_block);
Loading

0 comments on commit f46d327

Please sign in to comment.