Skip to content

Commit

Permalink
Merge pull request #82 from RegionX-Labs/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
cuteolaf authored Apr 17, 2024
2 parents 92b13f4 + f850876 commit 958c988
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 19 deletions.
17 changes: 16 additions & 1 deletion pallets/regions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ pub mod pallet {
/// The new owner of the Region.
owner: T::AccountId,
},
/// Region record of the given id has been set.
RecordSet {
/// The id of the region that the record has been set.
region_id: RegionId,
},
/// An ISMP GET request was made to get the region record.
RegionRecordRequested {
/// The id of the region that the request was made for.
region_id: RegionId,
/// The account who requested the region record.
account: T::AccountId,
},
}

#[pallet::error]
Expand Down Expand Up @@ -187,6 +199,7 @@ pub mod pallet {
Regions::<T>::insert(region_id, &region);

Self::deposit_event(Event::Transferred { region_id, old_owner, owner: region.owner });

Ok(())
}
}
Expand All @@ -201,6 +214,8 @@ pub mod pallet {
region.record = Record::Available(record);
Regions::<T>::insert(region_id, region);

Self::deposit_event(Event::RecordSet { region_id });

Ok(())
}

Expand Down Expand Up @@ -240,7 +255,7 @@ pub mod pallet {
.dispatch_request(DispatchRequest::Get(get), who.clone(), Zero::zero())
.map_err(|_| Error::<T>::IsmpDispatchError)?;

Check warning on line 256 in pallets/regions/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

pallets/regions/src/lib.rs#L256

Added line #L256 was not covered by tests

// TODO: Emit event
Self::deposit_event(Event::RegionRecordRequested { region_id, account: who });

Ok(())
}
Expand Down
7 changes: 5 additions & 2 deletions pallets/regions/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl pallet_balances::Config for Test {
}

parameter_types! {
pub const CoretimeChain: StateMachine = StateMachine::Kusama(1005);
pub const CoretimeChain: StateMachine = StateMachine::Kusama(1005); // coretime-kusama
}

pub struct MockStateMachineHeightProvider;
Expand All @@ -107,5 +107,8 @@ impl crate::Config for Test {

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::<Test>::default().build_storage().unwrap().into()
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
2 changes: 1 addition & 1 deletion pallets/regions/src/nonfungible_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<T: Config> Mutate<T::AccountId> for Pallet<T> {
}

/// Burning is used for withdrawing a region into the holding registrar.
fn burn(item: &Self::ItemId, maybe_check_owner: Option<&T::AccountId>) -> DispatchResult {
fn burn(item: &Self::ItemId, maybe_check_owner: Option<&T::AccountId>) -> DispatchResult {
let region_id: RegionId = (*item).into();

let region = Regions::<T>::get(region_id).ok_or(Error::<T>::UnknownRegion)?;
Expand Down
94 changes: 82 additions & 12 deletions pallets/regions/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
// along with RegionX. If not, see <https://www.gnu.org/licenses/>.

use crate::{
ismp_mock::requests, mock::*, utils, IsmpCustomError, IsmpModuleCallback, Record, Region,
ismp_mock::requests, mock::*, pallet::Regions as RegionsStorage, utils, Error, Event,
IsmpCustomError, IsmpModuleCallback, Record, Region,
};
use frame_support::{assert_err, assert_ok, pallet_prelude::*, traits::nonfungible::Mutate};
use ismp::{
Expand Down Expand Up @@ -50,21 +51,96 @@ fn nonfungibles_implementation_works() {
#[test]
fn set_record_works() {
new_test_ext().execute_with(|| {
// TODO:
let region_id = RegionId { begin: 112830, core: 81, mask: CoreMask::complete() };
let record: RegionRecord<u64, u64> = RegionRecord { end: 123600, owner: 1, paid: None };

// The region with the given `region_id` does not exist.
assert_err!(Regions::set_record(region_id, record.clone()), Error::<Test>::UnknownRegion);

// `set_record` succeeds
assert_ok!(Regions::mint_into(&region_id.into(), &2));

assert!(Regions::regions(region_id).is_some());
let region = Regions::regions(region_id).unwrap();
assert!(region.record.is_pending());

assert_ok!(Regions::set_record(region_id, record.clone()));
System::assert_last_event(Event::RecordSet { region_id }.into());

// check storage
assert!(Regions::regions(region_id).is_some());
let region = Regions::regions(region_id).unwrap();
assert!(region.record.is_available());
assert_eq!(region.owner, 2);
assert_eq!(region.record, Record::<Test>::Available(record.clone()));

// call `set_record` again with the same record
assert_err!(Regions::set_record(region_id, record), Error::<Test>::RegionRecordAlreadySet);
});
}

#[test]
fn request_region_record_works() {
new_test_ext().execute_with(|| {
// TODO:
let region_id = RegionId { begin: 112830, core: 81, mask: CoreMask::complete() };

// fails to request unknown regions
assert_err!(
Regions::request_region_record(RuntimeOrigin::signed(1), region_id),
Error::<Test>::UnknownRegion
);

assert_ok!(Regions::mint_into(&region_id.into(), &1));

assert!(Regions::regions(region_id).is_some());
let region = Regions::regions(region_id).unwrap();
assert!(region.record.is_pending());
assert_err!(
Regions::request_region_record(RuntimeOrigin::signed(1), region_id),
Error::<Test>::NotUnavailable
);

RegionsStorage::<Test>::mutate_exists(region_id, |val| {
let mut v0 = val.clone().unwrap();
v0.record = Record::Unavailable;
*val = Some(v0);
});

assert_ok!(Regions::request_region_record(RuntimeOrigin::signed(1), region_id));

System::assert_last_event(Event::<Test>::RegionRecordRequested { region_id, account: 1 }.into());
});
}

#[test]
fn transfer_works() {
new_test_ext().execute_with(|| {
// TODO:
// cannot transfer an unknown region
let region_id = RegionId { begin: 112830, core: 72, mask: CoreMask::complete() };
assert!(Regions::regions(region_id).is_none());

assert_err!(
Regions::transfer(RuntimeOrigin::signed(1), region_id, 2),
Error::<Test>::UnknownRegion
);

// only regions owned by the caller are transferable
assert_ok!(Regions::mint_into(&region_id.into(), &1));
assert_err!(
Regions::transfer(RuntimeOrigin::signed(3), region_id, 2),
Error::<Test>::NotOwner
);

// transfer region success
assert_ok!(Regions::transfer(RuntimeOrigin::signed(1), region_id, 2));

System::assert_last_event(Event::Transferred { region_id, old_owner: 1, owner: 2 }.into());

// check storage item
assert!(Regions::regions(region_id).is_some());
let region = Regions::regions(region_id).unwrap();

assert_eq!(region.owner, 2);
});
}

Expand All @@ -84,7 +160,8 @@ fn on_response_works() {

assert_eq!(request.who, 2);

let mock_record: RegionRecord<u64, u64> = RegionRecord { end: 42, owner: 1, paid: None };
let mock_record: RegionRecord<u64, u64> =
RegionRecord { end: 113000, owner: 1, paid: None };

let mock_response = Response::Get(GetResponse {
get: get.clone(),
Expand Down Expand Up @@ -124,13 +201,6 @@ fn on_response_only_handles_get() {
});
}

#[test]
fn on_timeout_only_handles_get() {
new_test_ext().execute_with(|| {
// TODO:
});
}

#[test]
fn on_timeout_works() {
new_test_ext().execute_with(|| {
Expand Down
6 changes: 3 additions & 3 deletions runtime/regionx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl orml_asset_registry::Config for Runtime {
type CustomMetadata = CustomMetadata;
type StringLimit = AssetsStringLimit;
type RuntimeEvent = RuntimeEvent;
// TODO: accurate weight
// TODO: accurate weight
type WeightInfo = ();
}

Expand Down Expand Up @@ -566,7 +566,7 @@ impl StateMachineHeightProviderT for StateMachineHeightProvider {
}

parameter_types! {
pub const CoretimeChain: StateMachine = StateMachine::Kusama(1005);
pub const CoretimeChain: StateMachine = StateMachine::Kusama(1005); // coretime-kusama
}

impl pallet_regions::Config for Runtime {
Expand Down Expand Up @@ -614,7 +614,7 @@ construct_runtime!(
// ISMP
Ismp: pallet_ismp = 50,
IsmpParachain: ismp_parachain = 51,

// Main stage:
Regions: pallet_regions = 60,
}
Expand Down

0 comments on commit 958c988

Please sign in to comment.