Skip to content

Commit 15b3818

Browse files
authored
Migrate mocks (#367)
* Migrated auction mock file to construct_runtime macro * Migrated currencies mock file to contruct_runtime * Migrated gradually-update mock file to construct_runtime * Migrated nft mock file to construct_runtime macro * Migrated oracle mock file to construct_runtime * Migrated rewards mock file to construct_runtime macro * Migrated mock file to construct_runtime macro * Migrated mock file to construct_runtime macro
1 parent 8c150f3 commit 15b3818

File tree

15 files changed

+202
-215
lines changed

15 files changed

+202
-215
lines changed

auction/src/mock.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,13 @@
33
#![cfg(test)]
44

55
use super::*;
6-
use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
6+
use frame_support::{construct_runtime, parameter_types};
77
use orml_traits::OnNewBidResult;
88
use sp_core::H256;
99
use sp_runtime::{testing::Header, traits::IdentityLookup};
1010

11-
impl_outer_origin! {
12-
pub enum Origin for Runtime {}
13-
}
14-
15-
mod auction {
16-
pub use crate::Event;
17-
}
18-
19-
impl_outer_event! {
20-
pub enum TestEvent for Runtime {
21-
frame_system<T>,
22-
auction<T>,
23-
}
24-
}
11+
use crate as auction;
2512

26-
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
27-
#[derive(Clone, PartialEq, Eq, Debug)]
28-
pub struct Runtime;
2913
parameter_types! {
3014
pub const BlockHashCount: u64 = 250;
3115
}
@@ -39,18 +23,18 @@ impl frame_system::Config for Runtime {
3923
type Origin = Origin;
4024
type Index = u64;
4125
type BlockNumber = BlockNumber;
42-
type Call = ();
26+
type Call = Call;
4327
type Hash = H256;
4428
type Hashing = ::sp_runtime::traits::BlakeTwo256;
4529
type AccountId = AccountId;
4630
type Lookup = IdentityLookup<Self::AccountId>;
4731
type Header = Header;
48-
type Event = TestEvent;
32+
type Event = Event;
4933
type BlockHashCount = BlockHashCount;
5034
type BlockWeights = ();
5135
type BlockLength = ();
5236
type Version = ();
53-
type PalletInfo = ();
37+
type PalletInfo = PalletInfo;
5438
type AccountData = ();
5539
type OnNewAccount = ();
5640
type OnKilledAccount = ();
@@ -59,7 +43,6 @@ impl frame_system::Config for Runtime {
5943
type SystemWeightInfo = ();
6044
type SS58Prefix = ();
6145
}
62-
pub type System = frame_system::Module<Runtime>;
6346

6447
pub struct Handler;
6548

@@ -87,13 +70,26 @@ impl AuctionHandler<AccountId, Balance, BlockNumber, AuctionId> for Handler {
8770
}
8871

8972
impl Config for Runtime {
90-
type Event = TestEvent;
73+
type Event = Event;
9174
type Balance = Balance;
9275
type AuctionId = AuctionId;
9376
type Handler = Handler;
9477
type WeightInfo = ();
9578
}
96-
pub type AuctionModule = Module<Runtime>;
79+
80+
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
81+
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, u32, ()>;
82+
83+
construct_runtime!(
84+
pub enum Runtime where
85+
Block = Block,
86+
NodeBlock = Block,
87+
UncheckedExtrinsic = UncheckedExtrinsic,
88+
{
89+
System: frame_system::{Module, Call, Storage, Config, Event<T>},
90+
AuctionModule: auction::{Module, Storage, Call, Event<T>},
91+
}
92+
);
9793

9894
pub const ALICE: AccountId = 1;
9995
pub const BOB: AccountId = 2;

auction/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use super::*;
66
use frame_support::{assert_noop, assert_ok};
7-
use mock::*;
7+
use mock::{Event, *};
88

99
#[test]
1010
fn new_auction_should_work() {
@@ -68,7 +68,7 @@ fn bid_should_work() {
6868
})
6969
);
7070
assert_ok!(AuctionModule::bid(Origin::signed(ALICE), 0, 20));
71-
let bid_event = TestEvent::auction(Event::Bid(0, ALICE, 20));
71+
let bid_event = Event::auction(crate::Event::Bid(0, ALICE, 20));
7272
assert!(System::events().iter().any(|record| record.event == bid_event));
7373
assert_eq!(
7474
AuctionModule::auction_info(0),

currencies/src/mock.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![cfg(test)]
44

55
use super::*;
6-
use frame_support::{impl_outer_event, impl_outer_origin, parameter_types, traits::GenesisBuild};
6+
use frame_support::{construct_runtime, parameter_types, traits::GenesisBuild};
77
use orml_traits::parameter_type_with_key;
88
use sp_core::H256;
99
use sp_runtime::{
@@ -12,47 +12,29 @@ use sp_runtime::{
1212
AccountId32, ModuleId,
1313
};
1414

15-
mod currencies {
16-
pub use crate::Event;
17-
}
18-
19-
impl_outer_event! {
20-
pub enum TestEvent for Runtime {
21-
frame_system<T>,
22-
currencies<T>,
23-
orml_tokens<T>,
24-
pallet_balances<T>,
25-
}
26-
}
27-
28-
impl_outer_origin! {
29-
pub enum Origin for Runtime {}
30-
}
15+
use crate as currencies;
3116

32-
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
33-
#[derive(Clone, PartialEq, Eq, Debug)]
34-
pub struct Runtime;
3517
parameter_types! {
3618
pub const BlockHashCount: u64 = 250;
3719
}
3820

3921
pub type AccountId = AccountId32;
4022
impl frame_system::Config for Runtime {
4123
type Origin = Origin;
42-
type Call = ();
24+
type Call = Call;
4325
type Index = u64;
4426
type BlockNumber = u64;
4527
type Hash = H256;
4628
type Hashing = ::sp_runtime::traits::BlakeTwo256;
4729
type AccountId = AccountId;
4830
type Lookup = IdentityLookup<Self::AccountId>;
4931
type Header = Header;
50-
type Event = TestEvent;
32+
type Event = Event;
5133
type BlockHashCount = BlockHashCount;
5234
type BlockWeights = ();
5335
type BlockLength = ();
5436
type Version = ();
55-
type PalletInfo = ();
37+
type PalletInfo = PalletInfo;
5638
type AccountData = pallet_balances::AccountData<u64>;
5739
type OnNewAccount = ();
5840
type OnKilledAccount = ();
@@ -61,7 +43,6 @@ impl frame_system::Config for Runtime {
6143
type SystemWeightInfo = ();
6244
type SS58Prefix = ();
6345
}
64-
pub type System = frame_system::Module<Runtime>;
6546

6647
type CurrencyId = u32;
6748
type Balance = u64;
@@ -73,13 +54,12 @@ parameter_types! {
7354
impl pallet_balances::Config for Runtime {
7455
type Balance = Balance;
7556
type DustRemoval = ();
76-
type Event = TestEvent;
57+
type Event = Event;
7758
type ExistentialDeposit = ExistentialDeposit;
7859
type AccountStore = frame_system::Module<Runtime>;
7960
type MaxLocks = ();
8061
type WeightInfo = ();
8162
}
82-
pub type PalletBalances = pallet_balances::Module<Runtime>;
8363

8464
parameter_type_with_key! {
8565
pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance {
@@ -92,15 +72,14 @@ parameter_types! {
9272
}
9373

9474
impl orml_tokens::Config for Runtime {
95-
type Event = TestEvent;
75+
type Event = Event;
9676
type Balance = Balance;
9777
type Amount = i64;
9878
type CurrencyId = CurrencyId;
9979
type WeightInfo = ();
10080
type ExistentialDeposits = ExistentialDeposits;
10181
type OnDust = orml_tokens::TransferDust<Runtime, DustAccount>;
10282
}
103-
pub type Tokens = orml_tokens::Module<Runtime>;
10483

10584
pub const NATIVE_CURRENCY_ID: CurrencyId = 1;
10685
pub const X_TOKEN_ID: CurrencyId = 2;
@@ -110,16 +89,32 @@ parameter_types! {
11089
}
11190

11291
impl Config for Runtime {
113-
type Event = TestEvent;
92+
type Event = Event;
11493
type MultiCurrency = Tokens;
11594
type NativeCurrency = AdaptedBasicCurrency;
11695
type GetNativeCurrencyId = GetNativeCurrencyId;
11796
type WeightInfo = ();
11897
}
119-
pub type Currencies = Module<Runtime>;
12098
pub type NativeCurrency = NativeCurrencyOf<Runtime>;
12199
pub type AdaptedBasicCurrency = BasicCurrencyAdapter<Runtime, PalletBalances, i64, u64>;
122100

101+
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
102+
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, u32, ()>;
103+
104+
construct_runtime!(
105+
pub enum Runtime where
106+
Block = Block,
107+
NodeBlock = Block,
108+
UncheckedExtrinsic = UncheckedExtrinsic,
109+
{
110+
System: frame_system::{Module, Call, Storage, Config, Event<T>},
111+
Currencies: currencies::{Module, Call, Event<T>},
112+
Tokens: orml_tokens::{Module, Storage, Event<T>, Config<T>},
113+
PalletBalances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
114+
115+
}
116+
);
117+
123118
pub const ALICE: AccountId = AccountId32::new([1u8; 32]);
124119
pub const BOB: AccountId = AccountId32::new([2u8; 32]);
125120
pub const EVA: AccountId = AccountId32::new([5u8; 32]);

currencies/src/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use super::*;
66
use frame_support::{assert_noop, assert_ok};
7-
use mock::*;
7+
use mock::{Event, *};
88
use sp_runtime::traits::BadOrigin;
99

1010
#[test]
@@ -255,7 +255,7 @@ fn call_event_should_work() {
255255
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 50);
256256
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &BOB), 150);
257257

258-
let transferred_event = TestEvent::currencies(Event::Transferred(X_TOKEN_ID, ALICE, BOB, 50));
258+
let transferred_event = Event::currencies(crate::Event::Transferred(X_TOKEN_ID, ALICE, BOB, 50));
259259
assert!(System::events().iter().any(|record| record.event == transferred_event));
260260

261261
assert_ok!(<Currencies as MultiCurrency<AccountId>>::transfer(
@@ -264,23 +264,23 @@ fn call_event_should_work() {
264264
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 40);
265265
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &BOB), 160);
266266

267-
let transferred_event = TestEvent::currencies(Event::Transferred(X_TOKEN_ID, ALICE, BOB, 10));
267+
let transferred_event = Event::currencies(crate::Event::Transferred(X_TOKEN_ID, ALICE, BOB, 10));
268268
assert!(System::events().iter().any(|record| record.event == transferred_event));
269269

270270
assert_ok!(<Currencies as MultiCurrency<AccountId>>::deposit(
271271
X_TOKEN_ID, &ALICE, 100
272272
));
273273
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 140);
274274

275-
let transferred_event = TestEvent::currencies(Event::Deposited(X_TOKEN_ID, ALICE, 100));
275+
let transferred_event = Event::currencies(crate::Event::Deposited(X_TOKEN_ID, ALICE, 100));
276276
assert!(System::events().iter().any(|record| record.event == transferred_event));
277277

278278
assert_ok!(<Currencies as MultiCurrency<AccountId>>::withdraw(
279279
X_TOKEN_ID, &ALICE, 20
280280
));
281281
assert_eq!(Currencies::free_balance(X_TOKEN_ID, &ALICE), 120);
282282

283-
let transferred_event = TestEvent::currencies(Event::Withdrawn(X_TOKEN_ID, ALICE, 20));
283+
let transferred_event = Event::currencies(crate::Event::Withdrawn(X_TOKEN_ID, ALICE, 20));
284284
assert!(System::events().iter().any(|record| record.event == transferred_event));
285285
});
286286
}

gradually-update/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authors = ["Laminar Developers <[email protected]>"]
88
edition = "2018"
99

1010
[dependencies]
11+
serde = { version = "1.0.111", optional = true }
1112
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
1213
frame-support = { version = "2.0.1", default-features = false }
1314
frame-system = { version = "2.0.1", default-features = false }
@@ -19,6 +20,7 @@ sp-runtime = { version = "2.0.1", default-features = false }
1920
[features]
2021
default = ["std"]
2122
std = [
23+
"serde",
2224
"codec/std",
2325
"frame-support/std",
2426
"frame-system/std",

gradually-update/src/mock.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,12 @@
33
#![cfg(test)]
44

55
use super::*;
6-
use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
6+
use frame_support::{construct_runtime, parameter_types};
77
use sp_core::H256;
88
use sp_runtime::{testing::Header, traits::IdentityLookup};
99

10-
impl_outer_origin! {
11-
pub enum Origin for Runtime {}
12-
}
13-
14-
mod gradually_update {
15-
pub use crate::Event;
16-
}
17-
18-
impl_outer_event! {
19-
pub enum TestEvent for Runtime {
20-
frame_system<T>,
21-
gradually_update<T>,
22-
}
23-
}
10+
use crate as gradually_update;
2411

25-
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
26-
#[derive(Clone, PartialEq, Eq, Debug)]
27-
pub struct Runtime;
2812
parameter_types! {
2913
pub const BlockHashCount: u64 = 250;
3014
}
@@ -36,18 +20,18 @@ impl frame_system::Config for Runtime {
3620
type Origin = Origin;
3721
type Index = u64;
3822
type BlockNumber = BlockNumber;
39-
type Call = ();
23+
type Call = Call;
4024
type Hash = H256;
4125
type Hashing = ::sp_runtime::traits::BlakeTwo256;
4226
type AccountId = AccountId;
4327
type Lookup = IdentityLookup<Self::AccountId>;
4428
type Header = Header;
45-
type Event = TestEvent;
29+
type Event = Event;
4630
type BlockHashCount = BlockHashCount;
4731
type BlockWeights = ();
4832
type BlockLength = ();
4933
type Version = ();
50-
type PalletInfo = ();
34+
type PalletInfo = PalletInfo;
5135
type AccountData = ();
5236
type OnNewAccount = ();
5337
type OnKilledAccount = ();
@@ -56,19 +40,32 @@ impl frame_system::Config for Runtime {
5640
type SystemWeightInfo = ();
5741
type SS58Prefix = ();
5842
}
59-
pub type System = frame_system::Module<Runtime>;
6043

6144
parameter_types! {
6245
pub const UpdateFrequency: BlockNumber = 10;
6346
}
6447

6548
impl Config for Runtime {
66-
type Event = TestEvent;
49+
type Event = Event;
6750
type UpdateFrequency = UpdateFrequency;
6851
type DispatchOrigin = frame_system::EnsureRoot<AccountId>;
6952
type WeightInfo = ();
7053
}
71-
pub type GraduallyUpdateModule = Module<Runtime>;
54+
55+
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
56+
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, u32, ()>;
57+
58+
construct_runtime!(
59+
pub enum Runtime where
60+
Block = Block,
61+
NodeBlock = Block,
62+
UncheckedExtrinsic = UncheckedExtrinsic,
63+
{
64+
System: frame_system::{Module, Call, Storage, Config, Event<T>},
65+
GraduallyUpdateModule: gradually_update::{Module, Storage, Call, Event<T>},
66+
67+
}
68+
);
7269

7370
pub struct ExtBuilder;
7471

0 commit comments

Comments
 (0)