Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Pallet feeds tests (paritytech#122)
Browse files Browse the repository at this point in the history
* added mocks for pallet-feeds testing

* added tests

* addressing PR comments
  • Loading branch information
isSerge authored Nov 11, 2021
1 parent 51cc88f commit 614f2c3
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 6 deletions.
19 changes: 13 additions & 6 deletions crates/pallet-feeds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub use pallet::*;
use sp_std::vec::Vec;
use subspace_core_primitives::{crypto, Sha256Hash};

#[cfg(all(feature = "std", test))]
mod mock;
#[cfg(all(feature = "std", test))]
mod tests;

#[frame_support::pallet]
mod pallet {
use super::*;
Expand Down Expand Up @@ -51,20 +56,22 @@ mod pallet {
pub(super) type ObjectMetadata = Vec<u8>;

/// Total amount of data and number of objects stored in a feed
#[derive(Decode, Encode, TypeInfo, Default)]
#[derive(Decode, Encode, TypeInfo, Default, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub(super) struct TotalObjectsAndSize {
pub struct TotalObjectsAndSize {
/// Total size of objects in bytes
pub(super) size: u64,
pub size: u64,
/// Total number of objects
pub(super) count: u64,
pub count: u64,
}

#[pallet::storage]
pub(super) type Feeds<T: Config> =
#[pallet::getter(fn metadata)]
pub(super) type Metadata<T: Config> =
StorageMap<_, Blake2_128Concat, FeedId, ObjectMetadata, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn totals)]
pub(super) type Totals<T: Config> =
StorageMap<_, Blake2_128Concat, FeedId, TotalObjectsAndSize, ValueQuery>;

Expand Down Expand Up @@ -129,7 +136,7 @@ mod pallet {

ensure!(current_feed_id >= feed_id, Error::<T>::UnknownFeedId);

Feeds::<T>::insert(feed_id, metadata.clone());
Metadata::<T>::insert(feed_id, metadata.clone());

Totals::<T>::mutate(feed_id, |feed_totals| {
feed_totals.size += object_size;
Expand Down
72 changes: 72 additions & 0 deletions crates/pallet-feeds/src/mock.rs
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
}
80 changes: 80 additions & 0 deletions crates/pallet-feeds/src/tests.rs
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);
});
}

0 comments on commit 614f2c3

Please sign in to comment.