Skip to content
Snippets Groups Projects
mock.rs 2.25 KiB
Newer Older
  • Learn to ignore specific revisions
  • Ermal Kaleci's avatar
    Ermal Kaleci committed
    #![cfg(test)]
    
    use super::*;
    
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    use frame_support::{impl_outer_origin, parameter_types, weights::Weight};
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    use primitives::H256;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    use sp_runtime::{
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	testing::Header,
    	traits::{BlakeTwo256, IdentityLookup},
    	Perbill,
    };
    
    impl_outer_origin! {
    	pub enum Origin for Test {}
    }
    
    // For testing the module, we construct most of a mock runtime. This means
    // first constructing a configuration type (`Test`) which `impl`s each of the
    // configuration traits of modules we want to use.
    #[derive(Clone, Eq, PartialEq)]
    pub struct Test;
    parameter_types! {
    	pub const BlockHashCount: u64 = 250;
    	pub const MaximumBlockWeight: Weight = 1024;
    	pub const MaximumBlockLength: u32 = 2 * 1024;
    	pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
    }
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    impl frame_system::Trait for Test {
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	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 = ();
    	type BlockHashCount = BlockHashCount;
    	type MaximumBlockWeight = MaximumBlockWeight;
    	type MaximumBlockLength = MaximumBlockLength;
    	type AvailableBlockRatio = AvailableBlockRatio;
    	type Version = ();
    }
    
    type AccountId = u64;
    type Key = u32;
    type Value = u32;
    
    
    pub type Timestamp = pallet_timestamp::Module<Test>;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    
    parameter_types! {
    	pub const MinimumPeriod: u64 = 5;
    }
    
    
    impl pallet_timestamp::Trait for Test {
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	type Moment = u64;
    	type OnTimestampSet = ();
    	type MinimumPeriod = MinimumPeriod;
    }
    
    pub struct MockOperatorProvider;
    
    impl OperatorProvider<AccountId> for MockOperatorProvider {
    	fn can_feed_data(who: &AccountId) -> bool {
    		Self::operators().contains(who)
    	}
    
    	fn operators() -> Vec<AccountId> {
    		vec![1, 2, 3]
    	}
    }
    
    impl Trait for Test {
    	type Event = ();
    	type OnNewData = ();
    	type OperatorProvider = MockOperatorProvider;
    	type CombineData = DefaultCombineData<Self>;
    
    	type Time = pallet_timestamp::Module<Self>;
    
    	type OracleKey = Key;
    	type OracleValue = Value;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    }
    pub type ModuleOracle = Module<Test>;
    
    // This function basically just builds a genesis storage key/value store according to
    // our desired mockup.
    pub fn new_test_ext() -> runtime_io::TestExternalities {
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	frame_system::GenesisConfig::default()
    
    		.build_storage::<Test>()
    		.unwrap()
    		.into()
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    }