Unit tests for EVM template (#391)

* EVM unit tests

* fmt & clippy

* toml sort

* fix assert_matches & warnings

* fix fmt

* fix clippy
This commit is contained in:
Nikita Khateev
2024-12-23 21:30:13 +04:00
committed by GitHub
parent 76340e96a5
commit f30d5758d7
10 changed files with 828 additions and 0 deletions
@@ -171,3 +171,167 @@ impl AccountIdAssetIdConversion<AccountId, AssetId> for Runtime {
AccountId::from(data)
}
}
#[cfg(test)]
mod tests {
mod asset_registrar {
use pallet_asset_manager::AssetRegistrar;
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;
let _ = <Registrar as AssetRegistrar<Runtime>>::destroy_asset_dispatch_info_weight(
asset_id,
);
}
#[test]
fn test_destroy_foreign_asset() {
TestExternalities::default().execute_with(|| {
let asset_id: AssetId = 1;
let _ = Assets::force_create(
RuntimeOrigin::root(),
asset_id.into(),
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(|| {
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,
is_frozen: false,
},
false,
);
assert!(res.is_ok());
});
}
}
mod account_asset_id_conversion {
use core::str::FromStr;
use fp_account::AccountId20;
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..4].copy_from_slice(FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX);
data[4..20].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..3].copy_from_slice(&FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX[0..3]);
data[3..4].copy_from_slice(&[0]);
data[4..20].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);
}
#[test]
fn test_asset_id_to_account() {
let expected =
AccountId20::from_str("0xFFFFFFFF00000000000000000000000000000001").unwrap();
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() {
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() {
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: u128 = 114990615950639921045101060455076456094;
let asset_type = AssetType::Xcm(location);
let asset_id = AssetId::from(asset_type);
assert_eq!(asset_id, expected_asset_id);
}
}
}