mock.rs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #![cfg(test)]
  2. pub use crate::{data_directory, data_object_storage_registry, data_object_type_registry};
  3. pub use common::currency::GovernanceCurrency;
  4. use membership::members;
  5. pub use system;
  6. pub use primitives::{Blake2Hasher, H256};
  7. pub use sr_primitives::{
  8. testing::{Digest, DigestItem, Header, UintAuthorityId},
  9. traits::{BlakeTwo256, Convert, IdentityLookup, OnFinalize},
  10. weights::Weight,
  11. BuildStorage, Perbill,
  12. };
  13. use crate::data_directory::ContentIdExists;
  14. use crate::data_object_type_registry::IsActiveDataObjectType;
  15. pub use crate::StorageWorkingGroupInstance;
  16. use srml_support::{impl_outer_event, impl_outer_origin, parameter_types, StorageLinkedMap};
  17. mod working_group_mod {
  18. pub use super::StorageWorkingGroupInstance;
  19. pub use working_group::Event;
  20. }
  21. impl_outer_origin! {
  22. pub enum Origin for Test {}
  23. }
  24. impl_outer_event! {
  25. pub enum MetaEvent for Test {
  26. data_object_type_registry<T>,
  27. data_directory<T>,
  28. data_object_storage_registry<T>,
  29. balances<T>,
  30. members<T>,
  31. working_group_mod StorageWorkingGroupInstance <T>,
  32. }
  33. }
  34. pub const TEST_FIRST_DATA_OBJECT_TYPE_ID: u64 = 1000;
  35. pub const TEST_FIRST_CONTENT_ID: u64 = 2000;
  36. pub const TEST_FIRST_RELATIONSHIP_ID: u64 = 3000;
  37. pub const TEST_FIRST_METADATA_ID: u64 = 4000;
  38. pub const TEST_MOCK_LIAISON_STORAGE_PROVIDER_ID: u32 = 1;
  39. pub const TEST_MOCK_EXISTING_CID: u64 = 42;
  40. pub struct AnyDataObjectTypeIsActive {}
  41. impl<T: data_object_type_registry::Trait> IsActiveDataObjectType<T> for AnyDataObjectTypeIsActive {
  42. fn is_active_data_object_type(_which: &T::DataObjectTypeId) -> bool {
  43. true
  44. }
  45. }
  46. pub struct MockContent {}
  47. impl ContentIdExists<Test> for MockContent {
  48. fn has_content(which: &<Test as data_directory::Trait>::ContentId) -> bool {
  49. *which == TEST_MOCK_EXISTING_CID
  50. }
  51. fn get_data_object(
  52. which: &<Test as data_directory::Trait>::ContentId,
  53. ) -> Result<data_directory::DataObject<Test>, &'static str> {
  54. match *which {
  55. TEST_MOCK_EXISTING_CID => Ok(data_directory::DataObject {
  56. type_id: 1,
  57. size: 1234,
  58. added_at: data_directory::BlockAndTime {
  59. block: 10,
  60. time: 1024,
  61. },
  62. owner: 1,
  63. liaison: TEST_MOCK_LIAISON_STORAGE_PROVIDER_ID,
  64. liaison_judgement: data_directory::LiaisonJudgement::Pending,
  65. ipfs_content_id: vec![],
  66. }),
  67. _ => Err("nope, missing"),
  68. }
  69. }
  70. }
  71. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
  72. #[derive(Clone, PartialEq, Eq, Debug)]
  73. pub struct Test;
  74. parameter_types! {
  75. pub const BlockHashCount: u64 = 250;
  76. pub const MaximumBlockWeight: u32 = 1024;
  77. pub const MaximumBlockLength: u32 = 2 * 1024;
  78. pub const AvailableBlockRatio: Perbill = Perbill::one();
  79. pub const MinimumPeriod: u64 = 5;
  80. }
  81. impl system::Trait for Test {
  82. type Origin = Origin;
  83. type Index = u64;
  84. type BlockNumber = u64;
  85. type Call = ();
  86. type Hash = H256;
  87. type Hashing = BlakeTwo256;
  88. type AccountId = u64;
  89. type Lookup = IdentityLookup<Self::AccountId>;
  90. type Header = Header;
  91. type Event = MetaEvent;
  92. type BlockHashCount = BlockHashCount;
  93. type MaximumBlockWeight = MaximumBlockWeight;
  94. type MaximumBlockLength = MaximumBlockLength;
  95. type AvailableBlockRatio = AvailableBlockRatio;
  96. type Version = ();
  97. }
  98. impl timestamp::Trait for Test {
  99. type Moment = u64;
  100. type OnTimestampSet = ();
  101. type MinimumPeriod = MinimumPeriod;
  102. }
  103. parameter_types! {
  104. pub const ExistentialDeposit: u32 = 0;
  105. pub const TransferFee: u32 = 0;
  106. pub const CreationFee: u32 = 0;
  107. pub const TransactionBaseFee: u32 = 1;
  108. pub const TransactionByteFee: u32 = 0;
  109. pub const InitialMembersBalance: u32 = 2000;
  110. pub const StakePoolId: [u8; 8] = *b"joystake";
  111. }
  112. impl balances::Trait for Test {
  113. /// The type for recording an account's balance.
  114. type Balance = u64;
  115. /// What to do if an account's free balance gets zeroed.
  116. type OnFreeBalanceZero = ();
  117. /// What to do if a new account is created.
  118. type OnNewAccount = ();
  119. /// The ubiquitous event type.
  120. type Event = MetaEvent;
  121. type DustRemoval = ();
  122. type TransferPayment = ();
  123. type ExistentialDeposit = ExistentialDeposit;
  124. type TransferFee = TransferFee;
  125. type CreationFee = CreationFee;
  126. }
  127. impl GovernanceCurrency for Test {
  128. type Currency = balances::Module<Self>;
  129. }
  130. parameter_types! {
  131. pub const MaxWorkerNumberLimit: u32 = 3;
  132. }
  133. impl working_group::Trait<StorageWorkingGroupInstance> for Test {
  134. type Event = MetaEvent;
  135. type MaxWorkerNumberLimit = MaxWorkerNumberLimit;
  136. }
  137. impl data_object_type_registry::Trait for Test {
  138. type Event = MetaEvent;
  139. type DataObjectTypeId = u64;
  140. }
  141. impl data_directory::Trait for Test {
  142. type Event = MetaEvent;
  143. type ContentId = u64;
  144. type StorageProviderHelper = ();
  145. type IsActiveDataObjectType = AnyDataObjectTypeIsActive;
  146. type MemberOriginValidator = ();
  147. }
  148. impl crate::data_directory::StorageProviderHelper<Test> for () {
  149. fn get_random_storage_provider() -> Result<u32, &'static str> {
  150. Ok(1)
  151. }
  152. }
  153. impl common::origin::ActorOriginValidator<Origin, u64, u64> for () {
  154. fn ensure_actor_origin(origin: Origin, _account_id: u64) -> Result<u64, &'static str> {
  155. let signed_account_id = system::ensure_signed(origin)?;
  156. Ok(signed_account_id)
  157. }
  158. }
  159. impl data_object_storage_registry::Trait for Test {
  160. type Event = MetaEvent;
  161. type DataObjectStorageRelationshipId = u64;
  162. type ContentIdExists = MockContent;
  163. }
  164. impl members::Trait for Test {
  165. type Event = MetaEvent;
  166. type MemberId = u64;
  167. type SubscriptionId = u32;
  168. type PaidTermId = u32;
  169. type ActorId = u32;
  170. type InitialMembersBalance = InitialMembersBalance;
  171. }
  172. impl stake::Trait for Test {
  173. type Currency = Balances;
  174. type StakePoolId = StakePoolId;
  175. type StakingEventsHandler = ();
  176. type StakeId = u64;
  177. type SlashId = u64;
  178. }
  179. impl minting::Trait for Test {
  180. type Currency = Balances;
  181. type MintId = u64;
  182. }
  183. impl recurringrewards::Trait for Test {
  184. type PayoutStatusHandler = ();
  185. type RecipientId = u64;
  186. type RewardRelationshipId = u64;
  187. }
  188. impl hiring::Trait for Test {
  189. type OpeningId = u64;
  190. type ApplicationId = u64;
  191. type ApplicationDeactivatedHandler = ();
  192. type StakeHandlerProvider = hiring::Module<Self>;
  193. }
  194. pub struct ExtBuilder {
  195. first_data_object_type_id: u64,
  196. first_content_id: u64,
  197. first_relationship_id: u64,
  198. first_metadata_id: u64,
  199. }
  200. impl Default for ExtBuilder {
  201. fn default() -> Self {
  202. Self {
  203. first_data_object_type_id: 1,
  204. first_content_id: 2,
  205. first_relationship_id: 3,
  206. first_metadata_id: 4,
  207. }
  208. }
  209. }
  210. impl ExtBuilder {
  211. pub fn first_data_object_type_id(mut self, first_data_object_type_id: u64) -> Self {
  212. self.first_data_object_type_id = first_data_object_type_id;
  213. self
  214. }
  215. pub fn first_content_id(mut self, first_content_id: u64) -> Self {
  216. self.first_content_id = first_content_id;
  217. self
  218. }
  219. pub fn first_relationship_id(mut self, first_relationship_id: u64) -> Self {
  220. self.first_relationship_id = first_relationship_id;
  221. self
  222. }
  223. pub fn first_metadata_id(mut self, first_metadata_id: u64) -> Self {
  224. self.first_metadata_id = first_metadata_id;
  225. self
  226. }
  227. pub fn build(self) -> runtime_io::TestExternalities {
  228. let mut t = system::GenesisConfig::default()
  229. .build_storage::<Test>()
  230. .unwrap();
  231. data_object_type_registry::GenesisConfig::<Test> {
  232. first_data_object_type_id: self.first_data_object_type_id,
  233. }
  234. .assimilate_storage(&mut t)
  235. .unwrap();
  236. data_object_storage_registry::GenesisConfig::<Test> {
  237. first_relationship_id: self.first_relationship_id,
  238. }
  239. .assimilate_storage(&mut t)
  240. .unwrap();
  241. membership::members::GenesisConfig::<Test> {
  242. default_paid_membership_fee: 0,
  243. members: vec![(1, "alice".into(), "".into(), "".into())],
  244. }
  245. .assimilate_storage(&mut t)
  246. .unwrap();
  247. t.into()
  248. }
  249. }
  250. pub type Balances = balances::Module<Test>;
  251. pub type System = system::Module<Test>;
  252. pub type TestDataObjectTypeRegistry = data_object_type_registry::Module<Test>;
  253. pub type TestDataObjectType = data_object_type_registry::DataObjectType;
  254. pub type TestDataDirectory = data_directory::Module<Test>;
  255. pub type TestDataObjectStorageRegistry = data_object_storage_registry::Module<Test>;
  256. pub fn with_default_mock_builder<R, F: FnOnce() -> R>(f: F) -> R {
  257. ExtBuilder::default()
  258. .first_data_object_type_id(TEST_FIRST_DATA_OBJECT_TYPE_ID)
  259. .first_content_id(TEST_FIRST_CONTENT_ID)
  260. .first_relationship_id(TEST_FIRST_RELATIONSHIP_ID)
  261. .first_metadata_id(TEST_FIRST_METADATA_ID)
  262. .build()
  263. .execute_with(|| f())
  264. }
  265. pub(crate) fn hire_storage_provider() -> (u64, u32) {
  266. let storage_provider_id = 1;
  267. let role_account_id = 1;
  268. let storage_provider = working_group::Worker {
  269. member_id: 1,
  270. role_account_id,
  271. reward_relationship: None,
  272. role_stake_profile: None,
  273. };
  274. <working_group::WorkerById<Test, StorageWorkingGroupInstance>>::insert(
  275. storage_provider_id,
  276. storage_provider,
  277. );
  278. (role_account_id, storage_provider_id)
  279. }