Skip to content

Commit

Permalink
new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KitHat committed Dec 13, 2024
1 parent bdc9af1 commit fd3eef9
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ docs/build
**/zombienet-linux-arm64
**/zombienet-linux-x64
**/bin-*

coverage
158 changes: 158 additions & 0 deletions generic-template/runtime/src/configs/asset_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,161 @@ impl pallet_asset_manager::AssetRegistrar<Runtime> for AssetRegistrar {
.weight
}
}

#[cfg(test)]
mod tests {
mod asset_registrar {
use pallet_asset_manager::AssetRegistrar;

Check warning on line 165 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
use sp_io::TestExternalities;

use crate::{configs::{AssetRegistrar as Registrar, AssetRegistrarMetadata}, types::AssetId, AssetManager, Assets, Runtime, RuntimeOrigin};

#[test]
fn test_destroy_asset_dispatch_info_weight() {
let asset_id: AssetId = 1;

Check warning on line 172 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
let weight = <Registrar as AssetRegistrar<Runtime>>::destroy_asset_dispatch_info_weight(asset_id);
}

#[test]
fn test_destroy_foreign_asset() {
TestExternalities::default().execute_with(|| {

Check warning on line 178 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
let asset_id: AssetId = 1;
let _ = Assets::force_create(
RuntimeOrigin::root(),
asset_id.into(),
sp_runtime::MultiAddress::Id(AssetManager::account_id()),
true,
1
);
let res = <Registrar as AssetRegistrar<Runtime>>::destroy_foreign_asset(asset_id);
assert!(res.is_ok());
});
}

#[test]
fn test_create_foreign_asset() {
TestExternalities::default().execute_with(|| {

Check warning on line 194 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
let asset_id = 1;
let res = <Registrar as AssetRegistrar<Runtime>>::create_foreign_asset(
asset_id,
1,
AssetRegistrarMetadata {
name: vec![0, 1, 2, 3],
symbol: vec![4, 5, 6, 7],
decimals: 6,

Check warning on line 202 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
is_frozen: false
},
false
);
assert!(res.is_ok());
});
}
}

mod account_asset_id_conversion {

Check warning on line 212 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
use sp_runtime::AccountId32;

use crate::{configs::{asset_config::FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX, AccountIdAssetIdConversion}, types::AssetId, AccountId, Runtime};

#[test]
fn test_account_to_asset_id_success() {
let expected_asset_id: AssetId = 1;
let mut data = [0u8; 32];
data[0..28].copy_from_slice(FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX);

Check warning on line 221 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
data[28..32].copy_from_slice(&expected_asset_id.to_be_bytes());
let account_id = AccountId::from(data);
let (prefix, asset_id) = Runtime::account_to_asset_id(account_id).expect("Account to asset id conversion failed");
assert_eq!(prefix, FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX);
assert_eq!(asset_id, expected_asset_id);
}

#[test]
fn test_account_to_asset_id_error() {
let expected_asset_id: AssetId = 1;
let mut data = [0u8; 32];
data[0..27].copy_from_slice(&FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX[0..27]);
data[27..28].copy_from_slice(&[0]);
data[28..32].copy_from_slice(&expected_asset_id.to_be_bytes());
let account_id = AccountId::from(data);
let res = Runtime::account_to_asset_id(account_id);
assert_eq!(res, None);
}

Check warning on line 240 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
#[test]
fn test_asset_id_to_account() {
let expected = AccountId32::new([255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,1]);
let asset_id = 1;
let result = Runtime::asset_id_to_account(FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX, asset_id);
assert_eq!(result, expected);
}
}

mod asset_type {
use crate::{configs::AssetType, types::AssetId};

#[test]
fn test_asset_type_default() {

Check warning on line 254 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
let default_asset_type = AssetType::default();
assert_eq!(
default_asset_type,
AssetType::Xcm(
xcm::v3::Location {
parents: 0,
interior: xcm::v3::Junctions::Here
}
)
);
}

#[test]
fn test_asset_type_from_location_v3() {

Check warning on line 268 in generic-template/runtime/src/configs/asset_config.rs

View workflow job for this annotation

GitHub Actions / clippy-fmt-test (ubuntu-22.04, false)

Diff in /home/runner/work/polkadot-runtime-templates/polkadot-runtime-templates/generic-template/runtime/src/configs/asset_config.rs
let location = xcm::v3::Location {
parents: 0,
interior: xcm::v3::Junctions::X1(xcm::v3::Junction::OnlyChild)
};
let asset_type = AssetType::from(location);

assert_eq!(asset_type, AssetType::Xcm(location));
}

#[test]
fn test_asset_type_try_from_location_v4() {
let location = xcm::latest::Location {
parents: 0,
interior: xcm::latest::Junctions::Here
};
let old_location: xcm::v3::Location = xcm::v3::Location {
parents: 0,
interior: xcm::v3::Junctions::Here
};
let asset_type = AssetType::try_from(location).expect("AssetType conversion from location v4 failed");

assert_eq!(asset_type, AssetType::Xcm(old_location));
}

#[test]
fn test_asset_type_into_location() {
let location = xcm::v3::Location {
parents: 0,
interior: xcm::v3::Junctions::Here
};
let asset_type = AssetType::Xcm(location);
let converted: Option<xcm::v3::Location> = asset_type.into();
assert_eq!(converted, Some(location));
}

#[test]
fn test_asset_type_into_asset_id() {
let location = xcm::v3::Location {
parents: 0,
interior: xcm::v3::Junctions::Here
};
let expected_asset_id: u32 = 3068126878;
let asset_type = AssetType::Xcm(location);

let asset_id = AssetId::from(asset_type);

assert_eq!(asset_id, expected_asset_id);
}
}
}
21 changes: 21 additions & 0 deletions generic-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,26 @@ mod runtime {
struct Governance;
}

#[cfg(test)]
mod test {
use frame_support::weights::WeightToFeePolynomial;

use crate::{constants::{POLY_DEGREE, VERSION}, native_version, WeightToFee};

#[test]
fn test_native_version() {
let version = native_version();
assert_eq!(version.runtime_version, VERSION);
}

#[test]
fn test_weight_to_fee() {
let mut fee = WeightToFee::polynomial();
let coef = fee.pop().expect("no coef");
assert!(!coef.negative);
assert_eq!(coef.degree, POLY_DEGREE);
}
}

#[cfg(feature = "runtime-benchmarks")]
mod benchmark;
64 changes: 64 additions & 0 deletions generic-template/runtime/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,67 @@ parameter_types! {
pub TreasuryInteriorLocation: InteriorLocation = PalletInstance(13).into();
pub MessageQueueServiceWeight: Weight = Perbill::from_percent(35) * RuntimeBlockWeights::get().max_block;
}

#[cfg(test)]
mod test {
mod filter {
use frame_support::traits::InstanceFilter;
use parity_scale_codec::MaxEncodedLen;
use scale_info::TypeInfo;
use sp_core::H256;

use crate::{types::ProxyType, AssetManager, RuntimeCall};

#[test]
fn test_filter_any() {
let call = RuntimeCall::CollatorSelection(pallet_collator_selection::Call::set_desired_candidates { max: 10 });
let proxy_type = ProxyType::Any;
assert!(proxy_type.filter(&call));
}

#[test]
fn test_filter_nontransfer() {
let proxy_type = ProxyType::NonTransfer;
let valid_call = RuntimeCall::CollatorSelection(pallet_collator_selection::Call::set_desired_candidates { max: 10 });
assert!(proxy_type.filter(&valid_call));
let invalid_call = RuntimeCall::Balances(pallet_balances::Call::burn { value: 1, keep_alive: true });
assert!(!proxy_type.filter(&invalid_call));
}

#[test]
fn test_filter_cancel_proxy() {
let proxy_type = ProxyType::CancelProxy;
let invalid_call = RuntimeCall::CollatorSelection(pallet_collator_selection::Call::set_desired_candidates { max: 10 });
assert!(!proxy_type.filter(&invalid_call));
let valid_call = RuntimeCall::Proxy(
pallet_proxy::Call::reject_announcement {
delegate: sp_runtime::MultiAddress::Id(AssetManager::account_id()),
call_hash: H256::zero()
}
);
assert!(proxy_type.filter(&valid_call));
}


#[test]
fn test_filter_collator() {
let proxy_type = ProxyType::Collator;
let valid_call = RuntimeCall::CollatorSelection(pallet_collator_selection::Call::set_desired_candidates { max: 10 });
assert!(proxy_type.filter(&valid_call));
let invalid_call = RuntimeCall::Balances(pallet_balances::Call::burn { value: 1, keep_alive: true });
assert!(!proxy_type.filter(&invalid_call));
}

#[test]
fn test_filter_default() {
let expected = ProxyType::Any;
assert_eq!(expected, ProxyType::default());
}

#[test]
fn test_filter_derives() {
let r#type = ProxyType::type_info();
let len = ProxyType::max_encoded_len();
}
}
}

0 comments on commit fd3eef9

Please sign in to comment.