mock.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #![cfg(test)]
  2. pub use crate::{GenesisConfig, Trait, DEFAULT_PAID_TERM_ID};
  3. pub use frame_support::traits::Currency;
  4. use frame_support::{impl_outer_origin, parameter_types};
  5. use sp_core::H256;
  6. use sp_runtime::{
  7. testing::Header,
  8. traits::{BlakeTwo256, IdentityLookup},
  9. Perbill,
  10. };
  11. pub use system;
  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 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 ModuleToIndex = ();
  48. type AccountData = balances::AccountData<u64>;
  49. type OnNewAccount = ();
  50. type OnKilledAccount = ();
  51. }
  52. impl pallet_timestamp::Trait for Test {
  53. type Moment = u64;
  54. type OnTimestampSet = ();
  55. type MinimumPeriod = MinimumPeriod;
  56. }
  57. parameter_types! {
  58. pub const ExistentialDeposit: u32 = 0;
  59. }
  60. impl balances::Trait for Test {
  61. type Balance = u64;
  62. type DustRemoval = ();
  63. type Event = ();
  64. type ExistentialDeposit = ExistentialDeposit;
  65. type AccountStore = System;
  66. }
  67. impl GovernanceCurrency for Test {
  68. type Currency = balances::Module<Self>;
  69. }
  70. impl Trait for Test {
  71. type Event = ();
  72. type MemberId = u64;
  73. type PaidTermId = u32;
  74. type SubscriptionId = u32;
  75. type ActorId = u32;
  76. }
  77. pub struct TestExternalitiesBuilder<T: Trait> {
  78. system_config: Option<system::GenesisConfig>,
  79. membership_config: Option<GenesisConfig<T>>,
  80. }
  81. impl<T: Trait> Default for TestExternalitiesBuilder<T> {
  82. fn default() -> Self {
  83. Self {
  84. system_config: None,
  85. membership_config: None,
  86. }
  87. }
  88. }
  89. impl<T: Trait> TestExternalitiesBuilder<T> {
  90. pub fn set_membership_config(mut self, membership_config: GenesisConfig<T>) -> Self {
  91. self.membership_config = Some(membership_config);
  92. self
  93. }
  94. pub fn build(self) -> sp_io::TestExternalities {
  95. // Add system
  96. let mut t = self
  97. .system_config
  98. .unwrap_or(system::GenesisConfig::default())
  99. .build_storage::<T>()
  100. .unwrap();
  101. // Add membership
  102. self.membership_config
  103. .unwrap_or(GenesisConfig::default())
  104. .assimilate_storage(&mut t)
  105. .unwrap();
  106. t.into()
  107. }
  108. }
  109. pub type Balances = balances::Module<Test>;
  110. pub type Members = crate::Module<Test>;
  111. pub type System = system::Module<Test>;