mock.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #![cfg(test)]
  2. pub use crate::*;
  3. use frame_support::traits::{OnFinalize, OnInitialize};
  4. use frame_support::{impl_outer_event, 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. pub use hiring;
  14. pub use membership;
  15. pub use minting;
  16. pub use recurringrewards;
  17. pub use stake;
  18. pub use versioned_store;
  19. pub use versioned_store_permissions;
  20. use crate::genesis;
  21. parameter_types! {
  22. pub const BlockHashCount: u64 = 250;
  23. pub const MaximumBlockWeight: u32 = 1024;
  24. pub const MaximumBlockLength: u32 = 2 * 1024;
  25. pub const AvailableBlockRatio: Perbill = Perbill::one();
  26. pub const MinimumPeriod: u64 = 5;
  27. pub const ExistentialDeposit: u32 = 0;
  28. pub const StakePoolId: [u8; 8] = *b"joystake";
  29. }
  30. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
  31. #[derive(Clone, PartialEq, Eq, Debug)]
  32. pub struct Test;
  33. impl_outer_origin! {
  34. pub enum Origin for Test {}
  35. }
  36. mod lib {
  37. pub use crate::Event;
  38. }
  39. impl_outer_event! {
  40. pub enum TestEvent for Test {
  41. versioned_store<T>,
  42. membership<T>,
  43. balances<T>,
  44. frame_system<T>,
  45. lib<T>,
  46. }
  47. }
  48. pub type RawLibTestEvent = RawEvent<
  49. ChannelId<Test>,
  50. LeadId<Test>,
  51. CuratorOpeningId<Test>,
  52. CuratorApplicationId<Test>,
  53. CuratorId<Test>,
  54. CuratorApplicationIdToCuratorIdMap<Test>,
  55. minting::BalanceOf<Test>,
  56. <Test as frame_system::Trait>::AccountId,
  57. <Test as minting::Trait>::MintId,
  58. >;
  59. pub fn get_last_event_or_panic() -> RawLibTestEvent {
  60. if let TestEvent::lib(ref x) = System::events().last().unwrap().event {
  61. x.clone()
  62. } else {
  63. panic!("No event deposited.");
  64. }
  65. }
  66. impl frame_system::Trait for Test {
  67. type BaseCallFilter = ();
  68. type Origin = Origin;
  69. type Call = ();
  70. type Index = u64;
  71. type BlockNumber = u64;
  72. type Hash = H256;
  73. type Hashing = BlakeTwo256;
  74. type AccountId = u64;
  75. type Lookup = IdentityLookup<Self::AccountId>;
  76. type Header = Header;
  77. type Event = TestEvent;
  78. type BlockHashCount = BlockHashCount;
  79. type MaximumBlockWeight = MaximumBlockWeight;
  80. type DbWeight = ();
  81. type BlockExecutionWeight = ();
  82. type ExtrinsicBaseWeight = ();
  83. type MaximumExtrinsicWeight = ();
  84. type MaximumBlockLength = MaximumBlockLength;
  85. type AvailableBlockRatio = AvailableBlockRatio;
  86. type Version = ();
  87. type PalletInfo = ();
  88. type AccountData = balances::AccountData<u64>;
  89. type OnNewAccount = ();
  90. type OnKilledAccount = ();
  91. type SystemWeightInfo = ();
  92. }
  93. impl pallet_timestamp::Trait for Test {
  94. type Moment = u64;
  95. type OnTimestampSet = ();
  96. type MinimumPeriod = MinimumPeriod;
  97. type WeightInfo = ();
  98. }
  99. impl balances::Trait for Test {
  100. type Balance = u64;
  101. type DustRemoval = ();
  102. type Event = TestEvent;
  103. type ExistentialDeposit = ExistentialDeposit;
  104. type AccountStore = System;
  105. type WeightInfo = ();
  106. type MaxLocks = ();
  107. }
  108. impl GovernanceCurrency for Test {
  109. type Currency = Balances;
  110. }
  111. type TestMintId = u64;
  112. impl minting::Trait for Test {
  113. type Currency = Balances;
  114. type MintId = TestMintId;
  115. }
  116. type TestRecipientId = u64;
  117. type TestRewardRelationshipId = u64;
  118. impl recurringrewards::Trait for Test {
  119. type PayoutStatusHandler = ();
  120. type RecipientId = TestRecipientId;
  121. type RewardRelationshipId = TestRewardRelationshipId;
  122. }
  123. type TestStakeId = u64;
  124. type TestSlashId = u64;
  125. impl stake::Trait for Test {
  126. type Currency = Balances;
  127. type StakePoolId = StakePoolId;
  128. type StakingEventsHandler = ();
  129. type StakeId = TestStakeId;
  130. type SlashId = TestSlashId;
  131. }
  132. type TestOpeningId = u64;
  133. type TestApplicationId = u64;
  134. impl hiring::Trait for Test {
  135. type OpeningId = TestOpeningId;
  136. type ApplicationId = TestApplicationId;
  137. type ApplicationDeactivatedHandler = ();
  138. type StakeHandlerProvider = hiring::Module<Self>;
  139. }
  140. impl versioned_store::Trait for Test {
  141. type Event = TestEvent;
  142. }
  143. type TestPrincipalId = u64;
  144. impl versioned_store_permissions::Trait for Test {
  145. type Credential = TestPrincipalId;
  146. type CredentialChecker = ();
  147. type CreateClassPermissionsChecker = ();
  148. }
  149. type TestMemberId = u64;
  150. impl membership::Trait for Test {
  151. type Event = TestEvent;
  152. type MemberId = TestMemberId;
  153. type PaidTermId = u64;
  154. type SubscriptionId = u64;
  155. type ActorId = u64;
  156. }
  157. impl Trait for Test {
  158. type Event = TestEvent;
  159. }
  160. pub struct TestExternalitiesBuilder<T: Trait> {
  161. system_config: Option<frame_system::GenesisConfig>,
  162. membership_config: Option<membership::GenesisConfig<T>>,
  163. content_wg_config: Option<GenesisConfig<T>>,
  164. }
  165. impl<T: Trait> Default for TestExternalitiesBuilder<T> {
  166. fn default() -> Self {
  167. Self {
  168. system_config: None,
  169. membership_config: None,
  170. content_wg_config: None,
  171. }
  172. }
  173. }
  174. impl<T: Trait> TestExternalitiesBuilder<T> {
  175. pub fn with_content_wg_config(mut self, conteng_wg_config: GenesisConfig<T>) -> Self {
  176. self.content_wg_config = Some(conteng_wg_config);
  177. self
  178. }
  179. pub fn build(self) -> sp_io::TestExternalities {
  180. // Add system
  181. let mut t = self
  182. .system_config
  183. .unwrap_or(frame_system::GenesisConfig::default())
  184. .build_storage::<T>()
  185. .unwrap();
  186. // Add membership
  187. self.membership_config
  188. .unwrap_or(membership::GenesisConfig::default())
  189. .assimilate_storage(&mut t)
  190. .unwrap();
  191. // Add content wg
  192. if self.content_wg_config.is_none() {
  193. genesis::GenesisConfigBuilder::<Test>::default()
  194. .build()
  195. .assimilate_storage(&mut t)
  196. .unwrap();
  197. } else {
  198. self.content_wg_config
  199. .unwrap()
  200. .assimilate_storage(&mut t)
  201. .unwrap();
  202. }
  203. t.into()
  204. }
  205. }
  206. pub type System = frame_system::Module<Test>;
  207. pub type Balances = balances::Module<Test>;
  208. pub type ContentWorkingGroup = Module<Test>;
  209. pub type Minting = minting::Module<Test>;
  210. // Recommendation from Parity on testing on_finalize
  211. // https://substrate.dev/docs/en/next/development/module/tests
  212. pub fn run_to_block(n: u64) {
  213. while System::block_number() < n {
  214. <System as OnFinalize<u64>>::on_finalize(System::block_number());
  215. <ContentWorkingGroup as OnFinalize<u64>>::on_finalize(System::block_number());
  216. System::set_block_number(System::block_number() + 1);
  217. <System as OnInitialize<u64>>::on_initialize(System::block_number());
  218. <ContentWorkingGroup as OnInitialize<u64>>::on_initialize(System::block_number());
  219. }
  220. }