frame-support-test: migrate tests from decl_* macros to the new pallet macros (#12445)

* frame-support: migrate some tests from decl macros to new pallet attribute macros

* Remove useless type alias

* Remove useless type alias

* frame-support-test: migrate old decl_macros to new pallet attribute macros

* fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix features

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Remove deprecated stuff

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update UI tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix UI test

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix test

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update UI tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Cleanup

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: parity-processbot <>
This commit is contained in:
Qinxuan Chen
2023-05-09 17:22:55 +08:00
committed by GitHub
parent 1d42bdf664
commit dcc0858f67
41 changed files with 1139 additions and 2988 deletions
@@ -20,46 +20,86 @@
use frame_support::{
assert_noop, assert_ok, assert_storage_noop,
dispatch::{DispatchError, DispatchResult},
dispatch::DispatchResult,
storage::{with_transaction, TransactionOutcome::*},
transactional, StorageMap, StorageValue,
transactional,
};
use sp_core::sr25519;
use sp_io::TestExternalities;
use sp_runtime::TransactionOutcome;
use sp_std::result;
use sp_runtime::{
generic,
traits::{BlakeTwo256, Verify},
TransactionOutcome,
};
pub trait Config: frame_support_test::Config {}
pub use self::pallet::*;
frame_support::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {
#[weight = 0]
#[frame_support::pallet]
pub mod pallet {
use self::frame_system::pallet_prelude::*;
use super::*;
use frame_support::pallet_prelude::*;
use frame_support_test as frame_system;
#[pallet::pallet]
#[pallet::generate_store(pub (super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(0)]
#[transactional]
fn value_commits(_origin, v: u32) {
Value::set(v);
pub fn value_commits(_origin: OriginFor<T>, v: u32) -> DispatchResult {
<Value<T>>::set(v);
Ok(())
}
#[weight = 0]
#[pallet::weight(0)]
#[transactional]
fn value_rollbacks(_origin, v: u32) -> DispatchResult {
Value::set(v);
pub fn value_rollbacks(_origin: OriginFor<T>, v: u32) -> DispatchResult {
<Value<T>>::set(v);
Err(DispatchError::Other("nah"))
}
}
#[pallet::storage]
pub type Value<T: Config> = StorageValue<_, u32, ValueQuery>;
#[pallet::storage]
#[pallet::unbounded]
pub type Map<T: Config> = StorageMap<_, Twox64Concat, String, u32, ValueQuery>;
}
frame_support::decl_storage! {
trait Store for Module<T: Config> as StorageTransactions {
pub Value: u32;
pub Map: map hasher(twox_64_concat) String => u32;
pub type BlockNumber = u32;
pub type Signature = sr25519::Signature;
pub type AccountId = <Signature as Verify>::Signer;
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
frame_support::construct_runtime!(
pub enum Runtime
where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_support_test,
MyPallet: pallet,
}
}
struct Runtime;
);
impl frame_support_test::Config for Runtime {
type RuntimeOrigin = u32;
type BlockNumber = u32;
type PalletInfo = frame_support_test::PanicPalletInfo;
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type BaseCallFilter = frame_support::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type PalletInfo = PalletInfo;
type DbWeight = ();
}
@@ -68,6 +108,9 @@ impl Config for Runtime {}
#[test]
fn storage_transaction_basic_commit() {
TestExternalities::default().execute_with(|| {
type Value = pallet::Value<Runtime>;
type Map = pallet::Map<Runtime>;
assert_eq!(Value::get(), 0);
assert!(!Map::contains_key("val0"));
@@ -87,6 +130,9 @@ fn storage_transaction_basic_commit() {
#[test]
fn storage_transaction_basic_rollback() {
TestExternalities::default().execute_with(|| {
type Value = pallet::Value<Runtime>;
type Map = pallet::Map<Runtime>;
assert_eq!(Value::get(), 0);
assert_eq!(Map::get("val0"), 0);
@@ -119,6 +165,9 @@ fn storage_transaction_basic_rollback() {
#[test]
fn storage_transaction_rollback_then_commit() {
TestExternalities::default().execute_with(|| {
type Value = pallet::Value<Runtime>;
type Map = pallet::Map<Runtime>;
Value::set(1);
Map::insert("val1", 1);
@@ -162,6 +211,9 @@ fn storage_transaction_rollback_then_commit() {
#[test]
fn storage_transaction_commit_then_rollback() {
TestExternalities::default().execute_with(|| {
type Value = pallet::Value<Runtime>;
type Map = pallet::Map<Runtime>;
Value::set(1);
Map::insert("val1", 1);
@@ -204,19 +256,21 @@ fn storage_transaction_commit_then_rollback() {
#[test]
fn transactional_annotation() {
type Value = pallet::Value<Runtime>;
fn set_value(v: u32) -> DispatchResult {
Value::set(v);
Ok(())
}
#[transactional]
fn value_commits(v: u32) -> result::Result<u32, &'static str> {
fn value_commits(v: u32) -> Result<u32, &'static str> {
set_value(v)?;
Ok(v)
}
#[transactional]
fn value_rollbacks(v: u32) -> result::Result<u32, &'static str> {
fn value_rollbacks(v: u32) -> Result<u32, &'static str> {
set_value(v)?;
Err("nah")?;
Ok(v)
@@ -229,14 +283,3 @@ fn transactional_annotation() {
assert_noop!(value_rollbacks(3), "nah");
});
}
#[test]
fn transactional_annotation_in_decl_module() {
TestExternalities::default().execute_with(|| {
let origin = 0;
assert_ok!(<Module<Runtime>>::value_commits(origin, 2));
assert_eq!(Value::get(), 2);
assert_noop!(<Module<Runtime>>::value_rollbacks(origin, 3), "nah");
});
}