Browse Source

runtime: storage-v2: Add testing infrastructure.

Shamil Gadelshin 3 years ago
parent
commit
fdedb13d99

+ 1 - 0
Cargo.lock

@@ -4283,6 +4283,7 @@ dependencies = [
  "serde",
  "sp-core",
  "sp-io",
+ "sp-runtime",
  "sp-std",
 ]
 

+ 1 - 0
runtime-modules/storage-v2/Cargo.toml

@@ -16,6 +16,7 @@ common = { package = 'pallet-common', default-features = false, path = '../commo
 [dev-dependencies]
 sp-io = { package = 'sp-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '2cd20966cc09b059817c3ebe12fc130cdd850d62'}
 sp-core = { package = 'sp-core', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '2cd20966cc09b059817c3ebe12fc130cdd850d62'}
+sp-runtime = { package = 'sp-runtime', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '2cd20966cc09b059817c3ebe12fc130cdd850d62'}
 
 [features]
 default = ['std']

+ 5 - 2
runtime-modules/storage-v2/src/lib.rs

@@ -8,6 +8,9 @@
 // TODO: add types comments
 // TODO: add benchmarks
 
+#[cfg(test)]
+mod tests;
+
 use codec::{Decode, Encode};
 use frame_support::dispatch::DispatchResult;
 use frame_support::{decl_error, decl_event, decl_module, decl_storage};
@@ -143,7 +146,7 @@ pub struct Voucher {
 pub enum StorageBucketOperatorStatus {
     Missing,
     InvitedStorageWorker(WorkerId),
-    StorageWorker(WorkerId)
+    StorageWorker(WorkerId),
 }
 
 impl Default for StorageBucketOperatorStatus {
@@ -158,7 +161,7 @@ pub struct StorageBucket {
     pub operator_status: StorageBucketOperatorStatus,
     pub accepting_new_bags: bool,
     pub number_of_pending_data_objects: u32,
-    pub voucher: Voucher
+    pub voucher: Voucher,
 }
 
 decl_storage! {

+ 96 - 0
runtime-modules/storage-v2/src/tests/mock.rs

@@ -0,0 +1,96 @@
+#![cfg(test)]
+
+use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
+pub use frame_system;
+use sp_core::H256;
+use sp_runtime::{
+    testing::Header,
+    traits::{BlakeTwo256, IdentityLookup},
+    Perbill,
+};
+
+// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct Test;
+
+impl_outer_origin! {
+    pub enum Origin for Test {}
+}
+
+mod storage {
+    pub use crate::Event;
+}
+
+impl_outer_event! {
+    pub enum TestEvent for Test {
+        balances<T>,
+        storage<T>,
+        frame_system<T>,
+    }
+}
+
+parameter_types! {
+    pub const ExistentialDeposit: u32 = 0;
+}
+
+impl balances::Trait for Test {
+    type Balance = u64;
+    type DustRemoval = ();
+    type Event = TestEvent;
+    type ExistentialDeposit = ExistentialDeposit;
+    type AccountStore = System;
+    type WeightInfo = ();
+    type MaxLocks = ();
+}
+
+impl crate::Trait for Test {
+    type Event = TestEvent;
+}
+
+parameter_types! {
+    pub const BlockHashCount: u64 = 250;
+    pub const MaximumBlockWeight: u32 = 1024;
+    pub const MaximumBlockLength: u32 = 2 * 1024;
+    pub const AvailableBlockRatio: Perbill = Perbill::one();
+    pub const MinimumPeriod: u64 = 5;
+}
+
+impl frame_system::Trait for Test {
+    type BaseCallFilter = ();
+    type Origin = Origin;
+    type Call = ();
+    type Index = u64;
+    type BlockNumber = u64;
+    type Hash = H256;
+    type Hashing = BlakeTwo256;
+    type AccountId = u64;
+    type Lookup = IdentityLookup<Self::AccountId>;
+    type Header = Header;
+    type Event = TestEvent;
+    type BlockHashCount = BlockHashCount;
+    type MaximumBlockWeight = MaximumBlockWeight;
+    type DbWeight = ();
+    type BlockExecutionWeight = ();
+    type ExtrinsicBaseWeight = ();
+    type MaximumExtrinsicWeight = ();
+    type MaximumBlockLength = MaximumBlockLength;
+    type AvailableBlockRatio = AvailableBlockRatio;
+    type Version = ();
+    type AccountData = balances::AccountData<u64>;
+    type OnNewAccount = ();
+    type OnKilledAccount = ();
+    type PalletInfo = ();
+    type SystemWeightInfo = ();
+}
+
+pub fn initial_test_ext() -> sp_io::TestExternalities {
+    let t = frame_system::GenesisConfig::default()
+        .build_storage::<Test>()
+        .unwrap();
+
+    t.into()
+}
+
+pub type Storage = crate::Module<Test>;
+pub type System = frame_system::Module<Test>;
+pub type Balances = balances::Module<Test>;

+ 12 - 0
runtime-modules/storage-v2/src/tests/mod.rs

@@ -0,0 +1,12 @@
+#![cfg(test)]
+
+mod mock;
+
+use mock::{initial_test_ext, Storage};
+
+#[test]
+fn test() {
+    initial_test_ext().execute_with(|| {
+        assert_eq!(Storage::council_bag(), Default::default());
+    });
+}