This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added mocks for pallet-feeds testing * added tests * addressing PR comments
- Loading branch information
Showing
3 changed files
with
165 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate as pallet_feeds; | ||
use frame_support::parameter_types; | ||
use sp_core::H256; | ||
use sp_runtime::{ | ||
testing::Header, | ||
traits::{BlakeTwo256, IdentityLookup}, | ||
}; | ||
|
||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; | ||
type Block = frame_system::mocking::MockBlock<Test>; | ||
|
||
frame_support::construct_runtime!( | ||
pub enum Test where | ||
Block = Block, | ||
NodeBlock = Block, | ||
UncheckedExtrinsic = UncheckedExtrinsic, | ||
{ | ||
System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, | ||
Feeds: pallet_feeds::{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 = frame_support::traits::Everything; | ||
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 const ExistentialDeposit: u64 = 1; | ||
} | ||
|
||
impl pallet_feeds::Config for Test { | ||
type Event = Event; | ||
} | ||
|
||
pub fn new_test_ext() -> sp_io::TestExternalities { | ||
let t = frame_system::GenesisConfig::default() | ||
.build_storage::<Test>() | ||
.unwrap(); | ||
|
||
let mut t: sp_io::TestExternalities = t.into(); | ||
|
||
t.execute_with(|| System::set_block_number(1)); | ||
|
||
t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::{mock::*, Error, FeedId, ObjectMetadata, PutDataObject, TotalObjectsAndSize}; | ||
use frame_support::{assert_noop, assert_ok}; | ||
|
||
const FEED_ID: FeedId = 0; | ||
const ACCOUNT_ID: u64 = 100; | ||
|
||
#[test] | ||
fn can_create_feed() { | ||
new_test_ext().execute_with(|| { | ||
// current feed id is 0 by default | ||
assert_eq!(Feeds::current_feed_id(), FEED_ID); | ||
assert_ok!(Feeds::create(Origin::signed(ACCOUNT_ID))); | ||
// current feed id value should be incremented after feed is created | ||
assert_eq!(Feeds::current_feed_id(), 1); | ||
|
||
assert_eq!(Feeds::totals(0), TotalObjectsAndSize::default()); | ||
|
||
System::assert_last_event(Event::Feeds(crate::Event::<Test>::FeedCreated( | ||
FEED_ID, ACCOUNT_ID, | ||
))); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn can_do_put() { | ||
new_test_ext().execute_with(|| { | ||
let data_object: PutDataObject = vec![1, 2, 3, 4, 5]; | ||
let object_size = data_object.len() as u64; | ||
let object_metadata: ObjectMetadata = vec![6, 7, 8, 9, 10]; | ||
// create feed before putting any data | ||
assert_eq!(Feeds::current_feed_id(), FEED_ID); | ||
|
||
assert_ok!(Feeds::put( | ||
Origin::signed(ACCOUNT_ID), | ||
FEED_ID, | ||
data_object.clone(), | ||
object_metadata.clone() | ||
)); | ||
|
||
// check Metadata hashmap for updated metadata | ||
assert_eq!(Feeds::metadata(FEED_ID), Some(object_metadata.clone())); | ||
|
||
// check Totals hashmap | ||
assert_eq!( | ||
Feeds::totals(FEED_ID), | ||
TotalObjectsAndSize { | ||
count: 1, | ||
size: object_size, | ||
} | ||
); | ||
|
||
System::assert_last_event(Event::Feeds(crate::Event::<Test>::DataSubmitted( | ||
object_metadata, | ||
ACCOUNT_ID, | ||
object_size, | ||
))); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn cannot_do_put_with_wrong_feed_id() { | ||
new_test_ext().execute_with(|| { | ||
// don't care about actual data and metadata, because call is supposed to fail | ||
let data_object: PutDataObject = PutDataObject::default(); | ||
let object_metadata: ObjectMetadata = ObjectMetadata::default(); | ||
let wrong_feed_id = 178; | ||
|
||
assert_noop!( | ||
Feeds::put( | ||
Origin::signed(ACCOUNT_ID), | ||
wrong_feed_id, | ||
data_object, | ||
object_metadata | ||
), | ||
Error::<Test>::UnknownFeedId | ||
); | ||
|
||
assert_eq!(System::events().len(), 0); | ||
}); | ||
} |