From 446b3abfbc4933d7f7563e10e67ad517809d4d22 Mon Sep 17 00:00:00 2001 From: Tarek Mohamed Abdalla Date: Mon, 14 Nov 2022 18:47:45 +0200 Subject: [PATCH] mock params --- Cargo.lock | 102 +---------------------- pallets/subscriptions/Cargo.toml | 2 +- pallets/subscriptions/src/lib.rs | 2 + pallets/subscriptions/src/mock.rs | 106 +++++++++++++----------- pallets/subscriptions/src/test_utils.rs | 43 ++++++++++ pallets/subscriptions/src/tests.rs | 35 ++------ 6 files changed, 114 insertions(+), 176 deletions(-) create mode 100644 pallets/subscriptions/src/test_utils.rs diff --git a/Cargo.lock b/Cargo.lock index c90661f8..a197a81a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1960,12 +1960,6 @@ dependencies = [ "syn", ] -[[package]] -name = "difflib" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" - [[package]] name = "digest" version = "0.8.1" @@ -2046,12 +2040,6 @@ dependencies = [ "quick-error", ] -[[package]] -name = "downcast" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1" - [[package]] name = "downcast-rs" version = "1.2.0" @@ -2395,15 +2383,6 @@ dependencies = [ "miniz_oxide", ] -[[package]] -name = "float-cmp" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" -dependencies = [ - "num-traits", -] - [[package]] name = "fnv" version = "1.0.7" @@ -2428,12 +2407,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "fragile" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c2141d6d6c8512188a7891b4b01590a45f6dac67afb4f255c4124dbb86d4eaa" - [[package]] name = "frame-benchmarking" version = "4.0.0-dev" @@ -4629,33 +4602,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "mockall" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50e4a1c770583dac7ab5e2f6c139153b783a53a1bbee9729613f193e59828326" -dependencies = [ - "cfg-if 1.0.0", - "downcast", - "fragile", - "lazy_static", - "mockall_derive", - "predicates", - "predicates-tree", -] - -[[package]] -name = "mockall_derive" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "832663583d5fa284ca8810bf7015e46c9fff9622d3cf34bd1eea5003fec06dd0" -dependencies = [ - "cfg-if 1.0.0", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "more-asserts" version = "0.2.2" @@ -4886,12 +4832,6 @@ dependencies = [ "minimal-lexical", ] -[[package]] -name = "normalize-line-endings" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" - [[package]] name = "num-bigint" version = "0.2.6" @@ -6018,10 +5958,10 @@ dependencies = [ "frame-support", "frame-system", "lazy_static", - "mockall", "pallet-balances", "pallet-permissions", "parity-scale-codec", + "paste", "scale-info", "smallvec", "sp-core", @@ -6369,9 +6309,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.7" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc" +checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1" [[package]] name = "pbkdf2" @@ -7679,36 +7619,6 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" -[[package]] -name = "predicates" -version = "2.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5aab5be6e4732b473071984b3164dbbfb7a3674d30ea5ff44410b6bcd960c3c" -dependencies = [ - "difflib", - "float-cmp", - "itertools", - "normalize-line-endings", - "predicates-core", - "regex", -] - -[[package]] -name = "predicates-core" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da1c2388b1513e1b605fcec39a95e0a9e8ef088f71443ef37099fa9ae6673fcb" - -[[package]] -name = "predicates-tree" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d86de6de25020a36c6d3643a86d9a6a9f552107c0559c60ea03551b5e16c032" -dependencies = [ - "predicates-core", - "termtree", -] - [[package]] name = "primitive-types" version = "0.11.1" @@ -11149,12 +11059,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "termtree" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b" - [[package]] name = "textwrap" version = "0.15.0" diff --git a/pallets/subscriptions/Cargo.toml b/pallets/subscriptions/Cargo.toml index 6d4509a3..7e3c8f30 100644 --- a/pallets/subscriptions/Cargo.toml +++ b/pallets/subscriptions/Cargo.toml @@ -33,8 +33,8 @@ pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "p sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false } sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.24", default-features = false } smallvec = "1.6.1" -mockall = "0.11.3" lazy_static = "1.4.0" +paste = "1.0.9" [features] default = ["std"] diff --git a/pallets/subscriptions/src/lib.rs b/pallets/subscriptions/src/lib.rs index abc5a289..01c28d56 100644 --- a/pallets/subscriptions/src/lib.rs +++ b/pallets/subscriptions/src/lib.rs @@ -14,6 +14,8 @@ mod tests; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; +#[cfg(test)] +mod test_utils; #[frame_support::pallet] pub mod pallet { diff --git a/pallets/subscriptions/src/mock.rs b/pallets/subscriptions/src/mock.rs index 2bc48277..68687ab6 100644 --- a/pallets/subscriptions/src/mock.rs +++ b/pallets/subscriptions/src/mock.rs @@ -1,10 +1,11 @@ +use std::borrow::Borrow; + use codec::Decode; use frame_support::{ dispatch::{DispatchError, DispatchResult}, parameter_types, - traits::Everything, + traits::{Everything, Get}, }; -use mockall::mock; use sp_core::H256; use sp_io::TestExternalities; use sp_runtime::{ @@ -20,6 +21,7 @@ use subsocial_support::{ }; pub(crate) use crate as pallet_subscriptions; +use crate::clearable_parameter_type; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; @@ -90,63 +92,79 @@ impl pallet_balances::Config for Test { type ReserveIdentifier = (); } -mock! { - pub Spaces {} - impl SpacesInterface for Spaces { - fn get_space_owner(space_id: SpaceId) -> Result; +pub struct MockSpaces; - fn create_space(owner: &AccountId, content: Content) -> Result; - } +parameter_types! { + pub static get_space_owner__return: Result = Ok(AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()).unwrap()); + pub static create_space__return: Result = Ok(101); } -mock! { - pub Roles {} - impl RolesInterface for Roles { - fn get_role_space(role_id: RoleId) -> Result; +clearable_parameter_type!(pub static get_space_owner__space_id: SpaceId); +clearable_parameter_type!(pub static create_space__owner: AccountId); +clearable_parameter_type!(pub static create_space__content: Content); - fn grant_role(account_id: AccountId, role_id: RoleId) -> DispatchResult; - fn create_role( - space_owner: &AccountId, - space_id: SpaceId, - time_to_live: Option, - content: Content, - permissions: Vec, - ) -> Result; +impl SpacesInterface for MockSpaces { + fn get_space_owner(space_id: SpaceId) -> Result { + get_space_owner__space_id::set(space_id); + get_space_owner__return::get() } -} - -pub struct BenchSpaces; -impl SpacesInterface for BenchSpaces { - fn get_space_owner(_space_id: SpaceId) -> Result { - Ok(AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()).unwrap()) + fn create_space(owner: &AccountId, content: Content) -> Result { + create_space__owner::set(owner.clone()); + create_space__content::set(content.clone()); + create_space__return::get() } +} - fn create_space(_owner: &AccountId, _content: Content) -> Result { - Ok(101) - } +pub struct MockRoles; + +parameter_types! { + pub static get_role_space__return: Result = Ok(101); + pub static grant_role__return: DispatchResult = Ok(()); + pub static create_role__return: Result = Ok(111); } -pub struct BenchRoles; +clearable_parameter_type!(pub static get_role_space__role_id: RoleId); + +clearable_parameter_type!(pub static grant_role__account_id: AccountId); +clearable_parameter_type!(pub static grant_role__role_id: RoleId); + +clearable_parameter_type!(pub static grant_role_space__role_id: RoleId); +clearable_parameter_type!(pub static grant_role__owner: AccountId); +clearable_parameter_type!(pub static grant_role__content: Content); + +clearable_parameter_type!(pub static create_role__space_owner: RoleId); +clearable_parameter_type!(pub static create_role__space_id: SpaceId); +clearable_parameter_type!(pub static create_role__time_to_live: Option); +clearable_parameter_type!(pub static create_role__content: Content); +clearable_parameter_type!(pub static create_role__permissions: Vec); -impl RolesInterface for BenchRoles { - fn get_role_space(_role_id: RoleId) -> Result { - Ok(101) +impl RolesInterface for MockRoles { + fn get_role_space(role_id: RoleId) -> Result { + get_space_owner__space_id::set(role_id); + get_role_space__return::get() } - fn grant_role(_account_id: AccountId, _role_id: RoleId) -> DispatchResult { - Ok(()) + fn grant_role(account_id: AccountId, role_id: RoleId) -> DispatchResult { + grant_role__account_id::set(account_id.clone()); + grant_role__role_id::set(role_id); + grant_role__return::get() } fn create_role( - _space_owner: &AccountId, - _space_id: SpaceId, - _time_to_live: Option, - _content: Content, - _permissions: Vec, + space_owner: &AccountId, + space_id: SpaceId, + time_to_live: Option, + content: Content, + permissions: Vec, ) -> Result { - Ok(111) + create_role__space_owner::set(space_owner.clone()); + create_role__space_id::set(space_id); + create_role__time_to_live::set(time_to_live); + create_role__content::set(content.clone()); + create_role__permissions::set(permissions.clone()); + create_role__return::get() } } @@ -154,15 +172,9 @@ impl pallet_subscriptions::Config for Test { type Event = Event; type Currency = Balances; type SpaceId = SpaceId; - #[cfg(not(feature = "runtime-benchmarks"))] type SpacesInterface = MockSpaces; - #[cfg(feature = "runtime-benchmarks")] - type SpacesInterface = BenchSpaces; type RoleId = RoleId; - #[cfg(not(feature = "runtime-benchmarks"))] type RolesInterface = MockRoles; - #[cfg(feature = "runtime-benchmarks")] - type RolesInterface = BenchRoles; type WeightInfo = pallet_subscriptions::weights::SubstrateWeight; } diff --git a/pallets/subscriptions/src/test_utils.rs b/pallets/subscriptions/src/test_utils.rs new file mode 100644 index 00000000..438d9cca --- /dev/null +++ b/pallets/subscriptions/src/test_utils.rs @@ -0,0 +1,43 @@ +use frame_support::{traits::Get}; +pub use paste::paste; +use sp_std::borrow::{Borrow, BorrowMut}; +pub use frame_support::parameter_types; + + +#[macro_export] +macro_rules! clearable_parameter_type { + ($vis:vis static $name:ident: $type:ty) => { + paste::paste! { + std::thread_local! { $vis static [<$name:snake:upper>]: std::cell::RefCell> = std::cell::RefCell::new(None); } + struct $name; + impl $name { + /// Returns the value of this parameter type. + pub fn get() -> Option<$type> { + [<$name:snake:upper>].with(|v| v.borrow().clone()) + } + + /// Clear the internal value. + pub fn clear() { + [<$name:snake:upper>].with(|v| *v.borrow_mut() = None); + } + + /// Set the internal value. + pub fn set(t: $type) { + [<$name:snake:upper>].with(|v| *v.borrow_mut() = Some(t)); + } + } + } + }; +} + + +clearable_parameter_type!(pub static TestValue: u32); + +#[test] +fn test() { + assert_eq!(TestValue::get(), None); + TestValue::set(121); + assert_eq!(TestValue::get(), Some(121)); + TestValue::clear(); + assert_eq!(TestValue::get(), None); +} \ No newline at end of file diff --git a/pallets/subscriptions/src/tests.rs b/pallets/subscriptions/src/tests.rs index 9577d8af..fb05d1a7 100644 --- a/pallets/subscriptions/src/tests.rs +++ b/pallets/subscriptions/src/tests.rs @@ -1,22 +1,9 @@ use frame_support::{assert_noop, assert_ok}; -use mockall::lazy_static; use sp_runtime::DispatchError; use sp_std::sync::{Mutex, MutexGuard}; use crate::{mock::*, types::SubscriptionSettings, Error, Event}; -lazy_static! { - static ref MTX: Mutex<()> = Mutex::new(()); -} - -/// mockall create static method mocking required this synchronization. -fn use_static_mock() -> MutexGuard<'static, ()> { - match MTX.lock() { - Ok(guard) => guard, - Err(poisoned) => poisoned.into_inner(), - } -} - #[test] fn update_subscription_settings_should_fail_when_caller_not_signed() { ExtBuilder::default().build().execute_with(|| { @@ -34,14 +21,11 @@ fn update_subscription_settings_should_fail_when_caller_not_signed() { #[test] fn update_subscription_settings_should_fail_when_caller_not_space_owner() { ExtBuilder::default().build().execute_with(|| { - let _z = use_static_mock(); - let space_owner: AccountId = 1; let not_space_owner: AccountId = 10; let space_id: SpaceId = 66; - let ctx = MockSpaces::get_space_owner_context(); - ctx.expect().return_const(Ok(space_owner)); + get_space_owner__return::set(Ok(space_owner)); assert_noop!( Subscriptions::update_subscription_settings( @@ -57,18 +41,15 @@ fn update_subscription_settings_should_fail_when_caller_not_space_owner() { #[test] fn update_subscription_settings_should_fail_when_role_not_in_space() { ExtBuilder::default().build().execute_with(|| { - let _z = use_static_mock(); - let space_owner: AccountId = 1; let space1_id: SpaceId = 66; let space2_id: SpaceId = 45; let role_id_in_space2: RoleId = 10; - let ctx1 = MockSpaces::get_space_owner_context(); - ctx1.expect().return_const(Ok(space_owner)); + get_space_owner__return::set(Ok(space_owner)); + + get_role_space__return::set(Ok(space2_id)); - let ctx2 = MockRoles::get_role_space_context(); - ctx2.expect().return_const(Ok(space2_id)); assert_noop!( Subscriptions::update_subscription_settings( @@ -88,17 +69,13 @@ fn update_subscription_settings_should_fail_when_role_not_in_space() { #[test] fn update_subscription_settings_should_work_correctly() { ExtBuilder::default().build().execute_with(|| { - let _z = use_static_mock(); - let space_owner: AccountId = 1; let space_id: SpaceId = 66; let role_id: RoleId = 10; - let ctx1 = MockSpaces::get_space_owner_context(); - ctx1.expect().return_const(Ok(space_owner)); + get_space_owner__return::set(Ok(space_owner)); - let ctx2 = MockRoles::get_role_space_context(); - ctx2.expect().return_const(Ok(space_id)); + get_role_space__return::set(Ok(space_id)); let subscription_settings = SubscriptionSettings { subscription: 55, disabled: false, role_id };