mock.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #![cfg(test)]
  2. pub use crate::{GenesisConfig, Trait};
  3. pub use frame_support::traits::{Currency, LockIdentifier};
  4. use frame_support::{impl_outer_origin, parameter_types};
  5. pub use frame_system;
  6. use sp_core::H256;
  7. use sp_runtime::{
  8. testing::Header,
  9. traits::{BlakeTwo256, IdentityLookup},
  10. Perbill,
  11. };
  12. pub use common::currency::GovernanceCurrency;
  13. impl_outer_origin! {
  14. pub enum Origin for Test {}
  15. }
  16. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
  17. #[derive(Clone, PartialEq, Eq, Debug)]
  18. pub struct Test;
  19. parameter_types! {
  20. pub const BlockHashCount: u64 = 250;
  21. pub const MaximumBlockWeight: u32 = 1024;
  22. pub const MaximumBlockLength: u32 = 2 * 1024;
  23. pub const AvailableBlockRatio: Perbill = Perbill::one();
  24. pub const MinimumPeriod: u64 = 5;
  25. }
  26. impl frame_system::Trait for Test {
  27. type BaseCallFilter = ();
  28. type Origin = Origin;
  29. type Call = ();
  30. type Index = u64;
  31. type BlockNumber = u64;
  32. type Hash = H256;
  33. type Hashing = BlakeTwo256;
  34. type AccountId = u64;
  35. type Lookup = IdentityLookup<Self::AccountId>;
  36. type Header = Header;
  37. type Event = ();
  38. type BlockHashCount = BlockHashCount;
  39. type MaximumBlockWeight = MaximumBlockWeight;
  40. type DbWeight = ();
  41. type BlockExecutionWeight = ();
  42. type ExtrinsicBaseWeight = ();
  43. type MaximumExtrinsicWeight = ();
  44. type MaximumBlockLength = MaximumBlockLength;
  45. type AvailableBlockRatio = AvailableBlockRatio;
  46. type Version = ();
  47. type PalletInfo = ();
  48. type AccountData = balances::AccountData<u64>;
  49. type OnNewAccount = ();
  50. type OnKilledAccount = ();
  51. type SystemWeightInfo = ();
  52. }
  53. impl pallet_timestamp::Trait for Test {
  54. type Moment = u64;
  55. type OnTimestampSet = ();
  56. type MinimumPeriod = MinimumPeriod;
  57. type WeightInfo = ();
  58. }
  59. parameter_types! {
  60. pub const ExistentialDeposit: u32 = 0;
  61. pub const MembershipFee: u64 = 100;
  62. }
  63. impl balances::Trait for Test {
  64. type Balance = u64;
  65. type DustRemoval = ();
  66. type Event = ();
  67. type ExistentialDeposit = ExistentialDeposit;
  68. type AccountStore = System;
  69. type WeightInfo = ();
  70. type MaxLocks = ();
  71. }
  72. impl common::Trait for Test {
  73. type MemberId = u64;
  74. type ActorId = u32;
  75. }
  76. parameter_types! {
  77. pub const MaxWorkerNumberLimit: u32 = 3;
  78. pub const LockId: LockIdentifier = [9; 8];
  79. }
  80. impl working_group::Trait<crate::MembershipWorkingGroupInstance> for Test {
  81. type Event = ();
  82. type MaxWorkerNumberLimit = MaxWorkerNumberLimit;
  83. type StakingHandler = staking_handler::StakingManager<Self, LockId>;
  84. type MemberOriginValidator = ();
  85. type MinUnstakingPeriodLimit = ();
  86. type RewardPeriod = ();
  87. }
  88. impl common::origin::ActorOriginValidator<Origin, u64, u64> for () {
  89. fn ensure_actor_origin(origin: Origin, _: u64) -> Result<u64, &'static str> {
  90. let account_id = frame_system::ensure_signed(origin)?;
  91. Ok(account_id)
  92. }
  93. }
  94. impl Trait for Test {
  95. type Event = ();
  96. type MembershipFee = MembershipFee;
  97. }
  98. pub struct TestExternalitiesBuilder<T: Trait> {
  99. system_config: Option<frame_system::GenesisConfig>,
  100. membership_config: Option<GenesisConfig<T>>,
  101. }
  102. impl<T: Trait> Default for TestExternalitiesBuilder<T> {
  103. fn default() -> Self {
  104. Self {
  105. system_config: None,
  106. membership_config: None,
  107. }
  108. }
  109. }
  110. impl<T: Trait> TestExternalitiesBuilder<T> {
  111. pub fn set_membership_config(mut self, membership_config: GenesisConfig<T>) -> Self {
  112. self.membership_config = Some(membership_config);
  113. self
  114. }
  115. pub fn build(self) -> sp_io::TestExternalities {
  116. // Add system
  117. let mut t = self
  118. .system_config
  119. .unwrap_or(frame_system::GenesisConfig::default())
  120. .build_storage::<T>()
  121. .unwrap();
  122. // Add membership
  123. self.membership_config
  124. .unwrap_or(GenesisConfig::default())
  125. .assimilate_storage(&mut t)
  126. .unwrap();
  127. t.into()
  128. }
  129. }
  130. pub type Balances = balances::Module<Test>;
  131. pub type Members = crate::Module<Test>;
  132. pub type System = frame_system::Module<Test>;