mock.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #![cfg(test)]
  2. pub use super::members::{self, Trait, DEFAULT_PAID_TERM_ID};
  3. pub use crate::currency::GovernanceCurrency;
  4. pub use srml_support::traits::Currency;
  5. pub use system;
  6. pub use primitives::{Blake2Hasher, H256};
  7. pub use runtime_primitives::{
  8. testing::{Digest, DigestItem, Header, UintAuthorityId},
  9. traits::{BlakeTwo256, Convert, IdentityLookup, OnFinalize},
  10. weights::Weight,
  11. BuildStorage, Perbill,
  12. };
  13. use srml_support::{impl_outer_origin, parameter_types};
  14. impl_outer_origin! {
  15. pub enum Origin for Test {}
  16. }
  17. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
  18. #[derive(Clone, PartialEq, Eq, Debug)]
  19. pub struct Test;
  20. parameter_types! {
  21. pub const BlockHashCount: u64 = 250;
  22. pub const MaximumBlockWeight: u32 = 1024;
  23. pub const MaximumBlockLength: u32 = 2 * 1024;
  24. pub const AvailableBlockRatio: Perbill = Perbill::one();
  25. pub const MinimumPeriod: u64 = 5;
  26. }
  27. impl system::Trait for Test {
  28. type Origin = Origin;
  29. type Index = u64;
  30. type BlockNumber = u64;
  31. type Call = ();
  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 MaximumBlockLength = MaximumBlockLength;
  41. type AvailableBlockRatio = AvailableBlockRatio;
  42. type Version = ();
  43. }
  44. impl timestamp::Trait for Test {
  45. type Moment = u64;
  46. type OnTimestampSet = ();
  47. type MinimumPeriod = MinimumPeriod;
  48. }
  49. parameter_types! {
  50. pub const ExistentialDeposit: u32 = 0;
  51. pub const TransferFee: u32 = 0;
  52. pub const CreationFee: u32 = 0;
  53. pub const TransactionBaseFee: u32 = 1;
  54. pub const TransactionByteFee: u32 = 0;
  55. pub const InitialMembersBalance: u64 = 2000;
  56. }
  57. impl balances::Trait for Test {
  58. /// The type for recording an account's balance.
  59. type Balance = u64;
  60. /// What to do if an account's free balance gets zeroed.
  61. type OnFreeBalanceZero = ();
  62. /// What to do if a new account is created.
  63. type OnNewAccount = ();
  64. /// The ubiquitous event type.
  65. type Event = ();
  66. type DustRemoval = ();
  67. type TransferPayment = ();
  68. type ExistentialDeposit = ExistentialDeposit;
  69. type TransferFee = TransferFee;
  70. type CreationFee = CreationFee;
  71. }
  72. impl GovernanceCurrency for Test {
  73. type Currency = balances::Module<Self>;
  74. }
  75. impl members::Trait for Test {
  76. type Event = ();
  77. type MemberId = u32;
  78. type PaidTermId = u32;
  79. type SubscriptionId = u32;
  80. type ActorId = u32;
  81. type InitialMembersBalance = InitialMembersBalance;
  82. }
  83. pub struct TestExternalitiesBuilder<T: Trait> {
  84. system_config: Option<system::GenesisConfig>,
  85. membership_config: Option<members::GenesisConfig<T>>,
  86. }
  87. impl<T: Trait> Default for TestExternalitiesBuilder<T> {
  88. fn default() -> Self {
  89. Self {
  90. system_config: None,
  91. membership_config: None,
  92. }
  93. }
  94. }
  95. impl<T: Trait> TestExternalitiesBuilder<T> {
  96. /*
  97. pub fn set_system_config(mut self, system_config: system::GenesisConfig) -> Self {
  98. self.system_config = Some(system_config);
  99. self
  100. }
  101. */
  102. pub fn set_membership_config(mut self, membership_config: members::GenesisConfig<T>) -> Self {
  103. self.membership_config = Some(membership_config);
  104. self
  105. }
  106. pub fn build(self) -> runtime_io::TestExternalities {
  107. // Add system
  108. let mut t = self
  109. .system_config
  110. .unwrap_or(system::GenesisConfig::default())
  111. .build_storage::<T>()
  112. .unwrap();
  113. // Add membership
  114. self.membership_config
  115. .unwrap_or(members::GenesisConfig::default())
  116. .assimilate_storage(&mut t)
  117. .unwrap();
  118. t.into()
  119. }
  120. }
  121. pub type Balances = balances::Module<Test>;
  122. pub type Members = members::Module<Test>;