diff --git a/pallets/poe/src/lib.rs b/pallets/poe/src/lib.rs index b9fa871..4b75268 100644 --- a/pallets/poe/src/lib.rs +++ b/pallets/poe/src/lib.rs @@ -3,6 +3,12 @@ /// A module for proof of existence pub use pallet::*; +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod tests; + #[frame_support::pallet] pub mod pallet { use super::*; diff --git a/pallets/poe/src/mock.rs b/pallets/poe/src/mock.rs new file mode 100644 index 0000000..852d889 --- /dev/null +++ b/pallets/poe/src/mock.rs @@ -0,0 +1,61 @@ +use crate as pallet_poe; +use frame_support::{ + derive_impl, + traits::{ConstU16, ConstU32, ConstU64}, +}; +use sp_core::H256; +use sp_runtime::{ + traits::{BlakeTwo256, IdentityLookup}, + BuildStorage, +}; + +type Block = frame_system::mocking::MockBlock; + +// Configure a mock runtime to test the pallet. +frame_support::construct_runtime!( + pub enum Test + { + System: frame_system, + PoeModule: pallet_poe, + } +); + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] +impl frame_system::Config for Test { + type BaseCallFilter = frame_support::traits::Everything; + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Nonce = u64; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type Block = Block; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = ConstU64<250>; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = ConstU16<42>; + type OnSetCode = (); + type MaxConsumers = frame_support::traits::ConstU32<16>; +} + +impl pallet_poe::Config for Test { + type RuntimeEvent = RuntimeEvent; + type MaxClaimLength = ConstU32<10>; +} + +// Build genesis storage according to the mock runtime. +pub fn new_test_ext() -> sp_io::TestExternalities { + frame_system::GenesisConfig::::default() + .build_storage() + .unwrap() + .into() +} diff --git a/pallets/poe/src/tests.rs b/pallets/poe/src/tests.rs new file mode 100644 index 0000000..3afdb78 --- /dev/null +++ b/pallets/poe/src/tests.rs @@ -0,0 +1,107 @@ +use super::*; +use crate::{mock::*, Error}; +use frame_support::{assert_noop, assert_ok, BoundedVec, pallet_prelude::Get}; + +#[test] +fn create_claim_works() { + new_test_ext().execute_with(|| { + let claim = BoundedVec::try_from(vec![0, 1]).unwrap(); + assert_ok!(PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone())); + + assert_eq!( + Proofs::::get(&claim), + Some((1, frame_system::Pallet::::block_number())) + ); + assert_eq!(<::MaxClaimLength as Get>::get(), 10); + }) +} + +#[test] +fn create_claim_failed_when_claim_already_exist() { + new_test_ext().execute_with(|| { + let claim = BoundedVec::try_from(vec![0, 1]).unwrap(); + let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone()); + + assert_noop!( + PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone()), + Error::::ProofAlreadyExist + ); + }) +} + +#[test] +fn revoke_claim_works() { + new_test_ext().execute_with(|| { + let claim = BoundedVec::try_from(vec![0, 1]).unwrap(); + let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone()); + + assert_ok!(PoeModule::revoke_claim(RuntimeOrigin::signed(1), claim.clone())); + }) +} + +#[test] +fn revoke_claim_failed_when_claim_is_not_exist() { + new_test_ext().execute_with(|| { + let claim = BoundedVec::try_from(vec![0, 1]).unwrap(); + + assert_noop!( + PoeModule::revoke_claim(RuntimeOrigin::signed(1), claim.clone()), + Error::::ClaimNotExist + ); + }) +} + +#[test] +fn revoke_claim_failed_with_wrong_owner() { + new_test_ext().execute_with(|| { + let claim = BoundedVec::try_from(vec![0, 1]).unwrap(); + let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone()); + + assert_noop!( + PoeModule::revoke_claim(RuntimeOrigin::signed(2), claim.clone()), + Error::::NotClaimOwner + ); + }) +} + +#[test] +fn transfer_claim_works() { + new_test_ext().execute_with(|| { + let claim = BoundedVec::try_from(vec![0, 1]).unwrap(); + let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone()); + + assert_ok!(PoeModule::transfer_claim(RuntimeOrigin::signed(1), claim.clone(), 2)); + + let bounded_claim = + BoundedVec::::MaxClaimLength>::try_from(claim.clone()).unwrap(); + assert_eq!( + Proofs::::get(&bounded_claim), + Some((2, frame_system::Pallet::::block_number())) + ); + }) +} + +#[test] +fn transfer_claim_failed_when_claim_is_not_exist() { + new_test_ext().execute_with(|| { + let claim = BoundedVec::try_from(vec![0, 1]).unwrap(); + + assert_noop!( + PoeModule::transfer_claim(RuntimeOrigin::signed(1), claim.clone(), 2), + Error::::ClaimNotExist + ); + }) +} + +#[test] +fn transfer_claim_failed_with_wrong_owner() { + new_test_ext().execute_with(|| { + let claim = BoundedVec::try_from(vec![0, 1]).unwrap(); + let _ = PoeModule::create_claim(RuntimeOrigin::signed(1), claim.clone()); + + assert_noop!( + PoeModule::transfer_claim(RuntimeOrigin::signed(2), claim.clone(), 3), + Error::::NotClaimOwner + ); + }) +}