-
Notifications
You must be signed in to change notification settings - Fork 17
/
tests.rs
123 lines (100 loc) · 3.5 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use super::*;
use crate as kitties;
use sp_core::H256;
use frame_support::{parameter_types, assert_ok, assert_noop};
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup}, testing::Header,
};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
KittiesModule: kitties::{Pallet, Call, Storage, Event<T>},
}
);
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}
impl frame_system::Config for Test {
type BaseCallFilter = ();
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Call = 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 = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
}
parameter_types! {
pub static MockRandom: H256 = Default::default();
}
impl Randomness<H256, u64> for MockRandom {
fn random(_subject: &[u8]) -> (H256, u64) {
(MockRandom::get(), 0)
}
}
impl Config for Test {
type Event = Event;
type Randomness = MockRandom;
type KittyIndex = u32;
}
// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into();
t.execute_with(|| System::set_block_number(1) );
t
}
#[test]
fn can_create() {
new_test_ext().execute_with(|| {
assert_ok!(KittiesModule::create(Origin::signed(100)));
let kitty = Kitty([59, 250, 138, 82, 209, 39, 141, 109, 163, 238, 183, 145, 235, 168, 18, 122]);
assert_eq!(KittiesModule::kitties(100, 0), Some(kitty.clone()));
assert_eq!(KittiesModule::next_kitty_id(), 1);
System::assert_last_event(Event::KittiesModule(crate::Event::<Test>::KittyCreated(100, 0, kitty)));
});
}
#[test]
fn gender() {
assert_eq!(Kitty([0; 16]).gender(), KittyGender::Male);
assert_eq!(Kitty([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).gender(), KittyGender::Female);
}
#[test]
fn can_breed() {
new_test_ext().execute_with(|| {
assert_ok!(KittiesModule::create(Origin::signed(100)));
MockRandom::set(H256::from([2; 32]));
assert_ok!(KittiesModule::create(Origin::signed(100)));
assert_noop!(KittiesModule::breed(Origin::signed(100), 0, 11), Error::<Test>::InvalidKittyId);
assert_noop!(KittiesModule::breed(Origin::signed(100), 0, 0), Error::<Test>::SameGender);
assert_noop!(KittiesModule::breed(Origin::signed(101), 0, 1), Error::<Test>::InvalidKittyId);
assert_ok!(KittiesModule::breed(Origin::signed(100), 0, 1));
let kitty = Kitty([187, 250, 235, 118, 211, 247, 237, 253, 187, 239, 191, 185, 239, 171, 211, 122]);
assert_eq!(KittiesModule::kitties(100, 2), Some(kitty.clone()));
assert_eq!(KittiesModule::next_kitty_id(), 3);
System::assert_last_event(Event::KittiesModule(crate::Event::<Test>::KittyBred(100u64, 2u32, kitty)));
});
}
// TODO: add new test cases for `fn transfer`. Make sure you have covered edge cases