mock.rs 8.4 KB

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