Skip to content

Commit

Permalink
mock params
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed Nov 14, 2022
1 parent f4fa056 commit 446b3ab
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 176 deletions.
102 changes: 3 additions & 99 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion pallets/subscriptions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions pallets/subscriptions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod test_utils;

#[frame_support::pallet]
pub mod pallet {
Expand Down
106 changes: 59 additions & 47 deletions pallets/subscriptions/src/mock.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
Expand Down Expand Up @@ -90,79 +92,89 @@ impl pallet_balances::Config for Test {
type ReserveIdentifier = ();
}

mock! {
pub Spaces {}
impl SpacesInterface<AccountId, SpaceId> for Spaces {
fn get_space_owner(space_id: SpaceId) -> Result<AccountId, DispatchError>;
pub struct MockSpaces;

fn create_space(owner: &AccountId, content: Content) -> Result<SpaceId, DispatchError>;
}
parameter_types! {
pub static get_space_owner__return: Result<AccountId, DispatchError> = Ok(AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()).unwrap());
pub static create_space__return: Result<SpaceId, DispatchError> = Ok(101);
}

mock! {
pub Roles {}
impl RolesInterface<RoleId, SpaceId, AccountId, SpacePermission, BlockNumber> for Roles {
fn get_role_space(role_id: RoleId) -> Result<SpaceId, DispatchError>;
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<BlockNumber>,
content: Content,
permissions: Vec<SpacePermission>,
) -> Result<RoleId, DispatchError>;
impl SpacesInterface<AccountId, SpaceId> for MockSpaces {
fn get_space_owner(space_id: SpaceId) -> Result<AccountId, DispatchError> {
get_space_owner__space_id::set(space_id);
get_space_owner__return::get()
}
}

pub struct BenchSpaces;

impl SpacesInterface<AccountId, SpaceId> for BenchSpaces {
fn get_space_owner(_space_id: SpaceId) -> Result<AccountId, DispatchError> {
Ok(AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()).unwrap())
fn create_space(owner: &AccountId, content: Content) -> Result<SpaceId, DispatchError> {
create_space__owner::set(owner.clone());
create_space__content::set(content.clone());
create_space__return::get()
}
}

fn create_space(_owner: &AccountId, _content: Content) -> Result<SpaceId, DispatchError> {
Ok(101)
}
pub struct MockRoles;

parameter_types! {
pub static get_role_space__return: Result<SpaceId, DispatchError> = Ok(101);
pub static grant_role__return: DispatchResult = Ok(());
pub static create_role__return: Result<RoleId, DispatchError> = 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<BlockNumber>);
clearable_parameter_type!(pub static create_role__content: Content);
clearable_parameter_type!(pub static create_role__permissions: Vec<SpacePermission>);

impl RolesInterface<RoleId, SpaceId, AccountId, SpacePermission, BlockNumber> for BenchRoles {
fn get_role_space(_role_id: RoleId) -> Result<SpaceId, DispatchError> {
Ok(101)
impl RolesInterface<RoleId, SpaceId, AccountId, SpacePermission, BlockNumber> for MockRoles {
fn get_role_space(role_id: RoleId) -> Result<SpaceId, DispatchError> {
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<BlockNumber>,
_content: Content,
_permissions: Vec<SpacePermission>,
space_owner: &AccountId,
space_id: SpaceId,
time_to_live: Option<BlockNumber>,
content: Content,
permissions: Vec<SpacePermission>,
) -> Result<RoleId, DispatchError> {
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()
}
}

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<Test>;
}

Expand Down
43 changes: 43 additions & 0 deletions pallets/subscriptions/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -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<Option<$type>> = 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);
}
Loading

0 comments on commit 446b3ab

Please sign in to comment.