Skip to content
Snippets Groups Projects
mock.rs 2.76 KiB
Newer Older
  • Learn to ignore specific revisions
  • Ermal Kaleci's avatar
    Ermal Kaleci committed
    #![cfg(test)]
    
    use super::*;
    
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    use frame_support::{impl_outer_dispatch, impl_outer_event, impl_outer_origin, parameter_types};
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    use sp_core::H256;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    use sp_runtime::{
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	traits::{BlakeTwo256, IdentityLookup},
    };
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    use std::cell::RefCell;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    
    
    mod oracle {
    	pub use super::super::*;
    }
    
    impl_outer_event! {
    	pub enum TestEvent for Test {
    		frame_system<T>,
    		oracle<T>,
    	}
    }
    
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    impl_outer_origin! {
    	pub enum Origin for Test {}
    }
    
    
    impl_outer_dispatch! {
    	pub enum Call for Test where origin: Origin {
    		oracle::ModuleOracle,
    	}
    }
    
    
    pub type AccountId = u128;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    type Key = u32;
    type Value = u32;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    // 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, Debug)]
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    pub struct Test;
    parameter_types! {
    	pub const BlockHashCount: u64 = 250;
    }
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    impl frame_system::Config for Test {
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	type Origin = Origin;
    
    	type Call = Call;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	type Index = u64;
    	type BlockNumber = u64;
    	type Hash = H256;
    	type Hashing = BlakeTwo256;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type AccountId = AccountId;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	type Lookup = IdentityLookup<Self::AccountId>;
    	type Header = Header;
    
    	type Event = TestEvent;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	type BlockHashCount = BlockHashCount;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type BlockWeights = ();
    	type BlockLength = ();
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	type Version = ();
    
    	type PalletInfo = ();
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type AccountData = ();
    	type OnNewAccount = ();
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type OnKilledAccount = ();
    
    	type DbWeight = ();
    
    	type BaseCallFilter = ();
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type SystemWeightInfo = ();
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    }
    
    pub type System = frame_system::Module<Test>;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    thread_local! {
    	static TIME: RefCell<u32> = RefCell::new(0);
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    pub struct Timestamp;
    impl Time for Timestamp {
    	type Moment = u32;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	fn now() -> Self::Moment {
    		TIME.with(|v| *v.borrow())
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	}
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    }
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    impl Timestamp {
    	pub fn set_timestamp(val: u32) {
    		TIME.with(|v| *v.borrow_mut() = val);
    
    parameter_types! {
    	pub const MinimumCount: u32 = 3;
    	pub const ExpiresIn: u32 = 600;
    
    	pub const RootOperatorAccountId: AccountId = 4;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    impl Config for Test {
    
    	type Event = TestEvent;
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    	type OnNewData = ();
    
    	type CombineData = DefaultCombineData<Self, MinimumCount, ExpiresIn>;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type Time = Timestamp;
    
    	type OracleKey = Key;
    	type OracleValue = Value;
    
    	type RootOperatorAccountId = RootOperatorAccountId;
    
    zjb0807's avatar
    zjb0807 committed
    	type WeightInfo = ();
    
    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.
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    pub fn new_test_ext() -> sp_io::TestExternalities {
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	let mut storage = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
    
    
    	let _ = GenesisConfig::<Test> {
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    		members: vec![1, 2, 3].into(),
    
    		phantom: Default::default(),
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	}
    	.assimilate_storage(&mut storage);
    
    	let mut t: sp_io::TestExternalities = storage.into();
    
    	t.execute_with(|| {
    		Timestamp::set_timestamp(12345);
    	});
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	t
    
    Ermal Kaleci's avatar
    Ermal Kaleci committed
    }