Skip to content

Commit

Permalink
Integration test: making it cooler! (#1821)
Browse files Browse the repository at this point in the history
* basic structure

* add macro impl

* adapt example test cases

* add macro to tests

* minor doc change

* taplo
  • Loading branch information
lemunozm authored Apr 26, 2024
1 parent 5ab100b commit 987aa11
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 74 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ members = [
"runtime/development",
"runtime/common",
"runtime/integration-tests",
"runtime/integration-tests/procedural",
]

[workspace.package]
Expand Down Expand Up @@ -275,6 +276,7 @@ runtime-common = { path = "runtime/common", default-features = false }
development-runtime = { path = "runtime/development", default-features = false }
altair-runtime = { path = "runtime/altair", default-features = false }
centrifuge-runtime = { path = "runtime/centrifuge", default-features = false }
runtime-integration-tests-proc-macro = { path = "runtime/integration-tests/procedural" }

# Build dependencies
substrate-wasm-builder = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0" }
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 @@ -81,6 +81,7 @@ cfg-utils = { workspace = true, features = ["std"] }
development-runtime = { workspace = true, features = ["std"] }
liquidity-pools-gateway-routers = { workspace = true, features = ["std"] }
runtime-common = { workspace = true, features = ["std"] }
runtime-integration-tests-proc-macro = { workspace = true }

# Pallet list
axelar-gateway-precompile = { workspace = true, features = ["std"] }
Expand Down
24 changes: 24 additions & 0 deletions runtime/integration-tests/procedural/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "runtime-integration-tests-proc-macro"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
documentation.workspace = true

[lib]
proc-macro = true

[dependencies]
prettyplease = "0.2"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0", features = ["full", "extra-traits"] }

[features]
default = []

# Enable this macro to show the proc macros unrolled when compiling
debug-proc-macros = []
70 changes: 70 additions & 0 deletions runtime/integration-tests/procedural/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Expr, ItemFn};

/// Test the function against different runtimes
///
/// ```rust,ignore
/// use crate::generic::config::Runtime;
///
/// #[test_runtimes([development, altair, centrifuge])]
/// fn foo<T: Runtime> {
/// // Your test here...
/// }
/// ```
/// You can test all runtimes also as:
/// ```rust,ignore
/// use crate::generic::config::Runtime;
///
/// #[test_runtimes(all)]
/// fn foo<T: Runtime> {
/// // Your test here...
/// }
/// ```
///
/// You can test for fudge support adding the bound:
/// ```rust,ignore
/// use crate::generic::{config::Runtime, envs::fudge_env::FudgeSupport};
///
/// #[test_runtimes(all)]
/// fn foo<T: Runtime + FudgeSupport> {
/// // Your test here...
/// }
/// ```
///
/// For the following command: `cargo test -p runtime-integration-tests foo`,
/// it will generate the following output:
///
/// ```text
/// test generic::foo::altair ... ok
/// test generic::foo::development ... ok
/// test generic::foo::centrifuge ... ok
/// ```
///
/// Available input for the argument is:
/// - Any combination of `development`, `altair`, `centrifuge` inside `[]`.
/// - The world `all`.
#[proc_macro_attribute]
pub fn test_runtimes(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as Expr);
let func = parse_macro_input!(input as ItemFn);

let func_name = &func.sig.ident;

quote! {
crate::test_for_runtimes!(#args, #func_name);
#func
}
.into()
}

/// Wrapper over test_runtime to print the output
#[proc_macro_attribute]
pub fn __dbg_test_runtimes(args: TokenStream, input: TokenStream) -> TokenStream {
let tokens = test_runtimes(args, input);
let file = syn::parse_file(&tokens.to_string()).unwrap();

println!("{}", prettyplease::unparse(&file));

TokenStream::default()
}
11 changes: 11 additions & 0 deletions runtime/integration-tests/procedural/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![allow(unused)]
#![cfg(feature = "debug-proc-macros")]

#[macro_use]
extern crate runtime_integration_tests_proc_macro;

#[__dbg_test_runtimes(all)]
fn macro_runtimes() {}

#[__dbg_test_runtimes([development, altair, centrifuge])]
fn macro_runtimes_list() {}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn evm_derivation_copy(chain_id: u64) -> AccountId {
AccountId::new(bytes)
}

#[test_runtimes(all)]
fn local_evm_account<T: Runtime>() {
let env = RuntimeEnv::<T>::default();

Expand All @@ -70,6 +71,7 @@ fn local_evm_account<T: Runtime>() {
);
}

#[test_runtimes(all)]
fn lp_evm_account<T: Runtime>() {
let env = RuntimeEnv::<T>::default();

Expand All @@ -87,6 +89,7 @@ fn lp_evm_account<T: Runtime>() {
assert_eq!(evm_derivation_copy(RANDOM_EVM_ID), derived);
}

#[test_runtimes(all)]
fn relay_chain_account<T: Runtime>() {
let env = RuntimeEnv::<T>::default();

Expand Down Expand Up @@ -129,6 +132,7 @@ fn relay_chain_account<T: Runtime>() {
);
}

#[test_runtimes(all)]
fn sibling_chain_account<T: Runtime>() {
let env = RuntimeEnv::<T>::default();

Expand Down Expand Up @@ -177,6 +181,7 @@ fn sibling_chain_account<T: Runtime>() {
);
}

#[test_runtimes(all)]
fn remote_account_on_relay<T: Runtime>() {
let env = RuntimeEnv::<T>::default();

Expand Down Expand Up @@ -205,6 +210,7 @@ fn remote_account_on_relay<T: Runtime>() {
);
}

#[test_runtimes(all)]
fn remote_account_on_sibling<T: Runtime>() {
let env = RuntimeEnv::<T>::default();

Expand Down Expand Up @@ -235,10 +241,3 @@ fn remote_account_on_sibling<T: Runtime>() {
derived
);
}

crate::test_for_runtimes!(all, local_evm_account);
crate::test_for_runtimes!(all, lp_evm_account);
crate::test_for_runtimes!(all, relay_chain_account);
crate::test_for_runtimes!(all, sibling_chain_account);
crate::test_for_runtimes!(all, remote_account_on_relay);
crate::test_for_runtimes!(all, remote_account_on_sibling);
7 changes: 2 additions & 5 deletions runtime/integration-tests/src/generic/cases/block_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
utils::accounts::{default_accounts, Keyring},
};

crate::test_for_runtimes!(all, block_rewards_api);
#[test_runtimes(all)]
fn block_rewards_api<T: Runtime>() {
const STAKER: Keyring = Keyring::Alice;

Expand Down Expand Up @@ -59,10 +59,7 @@ fn block_rewards_api<T: Runtime>() {
});
}

crate::test_for_runtimes!(
[development, altair, centrifuge],
collator_list_synchronized
);
#[test_runtimes(all)]
fn collator_list_synchronized<T: Runtime>() {
RuntimeEnv::<T>::from_parachain_storage(
Genesis::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::generic::{
// }
pub const TEST_CONTRACT_CODE: &str = "608060405234801561001057600080fd5b50610113806100206000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c8063c2985578146037578063febb0f7e146057575b600080fd5b603d605f565b604051808215151515815260200191505060405180910390f35b605d6068565b005b60006001905090565b600060db576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260098152602001807f6572726f725f6d7367000000000000000000000000000000000000000000000081525060200191505060405180910390fd5b56fea2646970667358221220fde68a3968e0e99b16fabf9b2997a78218b32214031f8e07e2c502daf603a69e64736f6c63430006060033";

#[test_runtimes([development])]
fn call<T: Runtime>() {
RuntimeEnv::<T>::default().parachain_state_mut(|| {
// Addresses must be high enough to not map to the precompile space.
Expand Down Expand Up @@ -100,5 +101,3 @@ fn call<T: Runtime>() {
.unwrap();
});
}

crate::test_for_runtimes!([development], call);
11 changes: 5 additions & 6 deletions runtime/integration-tests/src/generic/cases/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
utils::accounts::Keyring,
};

#[test_runtimes([development, altair, centrifuge])]
fn transfer_balance<T: Runtime>() {
const TRANSFER: Balance = 1000 * CFG;
const FOR_FEES: Balance = 1 * CFG;
Expand Down Expand Up @@ -70,6 +71,7 @@ fn transfer_balance<T: Runtime>() {
}

// Identical to `transfer_balance()` test but using fudge.
#[test_runtimes([development, altair, centrifuge])]
fn fudge_transfer_balance<T: Runtime + FudgeSupport>() {
const TRANSFER: Balance = 1000 * CFG;
const FOR_FEES: Balance = 1 * CFG;
Expand Down Expand Up @@ -128,6 +130,7 @@ fn fudge_transfer_balance<T: Runtime + FudgeSupport>() {
});
}

#[test_runtimes(all)]
fn call_api<T: Runtime>() {
let env = RuntimeEnv::<T>::default();

Expand All @@ -141,6 +144,7 @@ fn call_api<T: Runtime>() {
})
}

#[test_runtimes(all)]
fn fudge_call_api<T: Runtime + FudgeSupport>() {
let env = FudgeEnv::<T>::default();

Expand All @@ -157,6 +161,7 @@ fn fudge_call_api<T: Runtime + FudgeSupport>() {
})
}

#[test_runtimes(all)]
fn pass_time_one_block<T: Runtime>() {
let mut env = RuntimeEnv::<T>::default();

Expand All @@ -169,9 +174,3 @@ fn pass_time_one_block<T: Runtime>() {

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);
3 changes: 1 addition & 2 deletions runtime/integration-tests/src/generic/cases/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mod common {
}
}

#[test_runtimes(all)]
fn investment_portfolio_single_tranche<T: Runtime>() {
let mut env = common::initialize_state_for_investments::<RuntimeEnv<T>, T>();

Expand Down Expand Up @@ -189,5 +190,3 @@ fn investment_portfolio_single_tranche<T: Runtime>() {
)]
);
}

crate::test_for_runtimes!(all, investment_portfolio_single_tranche);
11 changes: 5 additions & 6 deletions runtime/integration-tests/src/generic/cases/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ mod call {
/// - borrow from the loan
/// - fully repay the loan until
/// - close the loan
#[test_runtimes(all)]
fn internal_priced<T: Runtime>() {
let mut env = common::initialize_state_for_loans::<RuntimeEnv<T>, T>();

Expand Down Expand Up @@ -310,6 +311,7 @@ fn internal_priced<T: Runtime>() {
}

/// Test using oracles to price the loan
#[test_runtimes(all)]
fn oracle_priced<T: Runtime>() {
let mut env = common::initialize_state_for_loans::<RuntimeEnv<T>, T>();

Expand Down Expand Up @@ -365,6 +367,7 @@ fn oracle_priced<T: Runtime>() {
/// Test using oracles to valuate a portfolio.
/// The oracle values used by the portfilio comes from the oracle
/// collection
#[test_runtimes(all)]
fn portfolio_valuated_by_oracle<T: Runtime>() {
let mut env = common::initialize_state_for_loans::<RuntimeEnv<T>, T>();

Expand Down Expand Up @@ -421,6 +424,7 @@ fn portfolio_valuated_by_oracle<T: Runtime>() {
assert_eq!(present_value_price_b, total_portfolio_value.0);
}

#[test_runtimes(all)]
fn update_maturity_extension<T: Runtime>() {
let mut env = common::initialize_state_for_loans::<RuntimeEnv<T>, T>();

Expand Down Expand Up @@ -457,6 +461,7 @@ fn update_maturity_extension<T: Runtime>() {
.unwrap();
}

#[test_runtimes(all)]
fn fake_oracle_portfolio_api<T: Runtime>() {
let mut env = common::initialize_state_for_loans::<RuntimeEnv<T>, T>();

Expand Down Expand Up @@ -511,9 +516,3 @@ fn fake_oracle_portfolio_api<T: Runtime>() {
);
});
}

crate::test_for_runtimes!(all, internal_priced);
crate::test_for_runtimes!(all, oracle_priced);
crate::test_for_runtimes!(all, portfolio_valuated_by_oracle);
crate::test_for_runtimes!(all, update_maturity_extension);
crate::test_for_runtimes!(all, fake_oracle_portfolio_api);
Loading

0 comments on commit 987aa11

Please sign in to comment.