Skip to content

Commit

Permalink
Remove tests and mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpolaczyk committed Jul 26, 2023
1 parent 2b7635a commit 0d17627
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 218 deletions.
50 changes: 3 additions & 47 deletions pallets/migrations/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

use super::*;
use crate as pallet_migrations;
use frame_support::traits::EqualPrivilegeOnly;
use frame_support::{
construct_runtime,
pallet_prelude::*,
parameter_types,
traits::{Everything, GenesisBuild},
traits::{EqualPrivilegeOnly, Everything, GenesisBuild},
weights::{constants::RocksDbWeight, Weight},
};
use frame_system::{EnsureRoot, EnsureSigned};
use frame_system::EnsureRoot;
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
Expand All @@ -49,10 +48,8 @@ construct_runtime!(
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Migrations: pallet_migrations::{Pallet, Storage, Config, Event<T>, Call},
Democracy: pallet_democracy::{Pallet, Storage, Config<T>, Event<T>, Call},
Migrations: pallet_migrations::{Pallet, Storage, Config, Event<T>},
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>},
Preimage: pallet_preimage::{Pallet, Event<T>, Call},
}
);

Expand Down Expand Up @@ -123,37 +120,6 @@ parameter_types! {
pub const InstantAllowed: bool = false;
}

impl pallet_democracy::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type EnactmentPeriod = EnactmentPeriod;
type LaunchPeriod = LaunchPeriod;
type VotingPeriod = VotingPeriod;
type VoteLockingPeriod = VoteLockingPeriod;
type FastTrackVotingPeriod = FastTrackVotingPeriod;
type MinimumDeposit = MinimumDeposit;
type ExternalOrigin = EnsureRoot<AccountId>;
type ExternalMajorityOrigin = EnsureRoot<AccountId>;
type ExternalDefaultOrigin = EnsureRoot<AccountId>;
type FastTrackOrigin = EnsureRoot<AccountId>;
type InstantOrigin = EnsureRoot<AccountId>;
type CancellationOrigin = EnsureRoot<AccountId>;
type CancelProposalOrigin = EnsureRoot<AccountId>;
type BlacklistOrigin = EnsureRoot<AccountId>;
type VetoOrigin = EnsureSigned<AccountId>;
type CooloffPeriod = CooloffPeriod;
type Slash = ();
type InstantAllowed = InstantAllowed;
type Scheduler = Scheduler;
type MaxVotes = MaxVotes;
type PalletsOrigin = OriginCaller;
type WeightInfo = ();
type MaxProposals = MaxProposals;
type Preimages = Preimage;
type MaxDeposits = ConstU32<1000>;
type MaxBlacklisted = ConstU32<5>;
type SubmitOrigin = EnsureSigned<AccountId>;
}
impl pallet_scheduler::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
Expand All @@ -172,15 +138,6 @@ parameter_types! {
pub const ByteDeposit: u64 = 10;
}

impl pallet_preimage::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Currency = Balances;
type ManagerOrigin = EnsureRoot<AccountId>;
type BaseDeposit = BaseDeposit;
type ByteDeposit = ByteDeposit;
}

/// MockMigrationManager stores the test-side callbacks/closures used in the Migrations list glue.
/// It is is expected to exist as a singleton, but only remain relevant within the scope of a test.
///
Expand Down Expand Up @@ -347,7 +304,6 @@ impl Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type MigrationsList = MockMigrations;
type XcmExecutionManager = ();
type WeightInfo = ();
}

/// Externality builder for pallet migration's mock runtime
Expand Down
175 changes: 4 additions & 171 deletions pallets/migrations/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
//! Unit testing
use {
crate::{
democracy_preimages::*,
mock::{
events, ExtBuilder, Migrations, MockMigrationManager, Runtime, RuntimeOrigin, System,
events, ExtBuilder, Migrations, MockMigrationManager, Runtime, System,
},
Error, Event,
Event,
},
frame_support::{assert_err, assert_ok, traits::OnRuntimeUpgrade, weights::Weight, BoundedVec},
pallet_preimage::RequestStatus,
sp_runtime::traits::{Get, Hash},
frame_support::{traits::OnRuntimeUpgrade, weights::Weight },
sp_runtime::traits::Get,
std::sync::{Arc, Mutex},
};

Expand Down Expand Up @@ -335,170 +333,5 @@ fn try_runtime_functions_work() {
);
}

#[test]
fn preimage_lazy_migration_works() {
ExtBuilder::default().build().execute_with(|| {
// Setup mock preimage
let data = b"hello world!".to_vec();
let bounded_data: BoundedVec<_, _> = data.clone().try_into().expect("fits in bound");
let len = data.len() as u32;
let hash = <Runtime as frame_system::Config>::Hashing::hash_of(&data);

DeprecatedDemocracyPreimages::<Runtime>::insert(
hash,
PreimageStatus::Available {
data,
provider: 42,
deposit: 142,
since: 0,
expiry: None,
},
);

// Call migration
assert_ok!(Migrations::migrate_democracy_preimage(
RuntimeOrigin::signed(1),
hash,
len,
));

// Check migration was successful.
assert!(DeprecatedDemocracyPreimages::<Runtime>::get(hash).is_none());
assert_eq!(
StatusFor::<Runtime>::get(hash),
Some(RequestStatus::Unrequested {
deposit: (42, 142),
len
})
);
assert_eq!(PreimageFor::<Runtime>::get((hash, len)), Some(bounded_data));
});
}

#[test]
fn preimage_lazy_migration_fails_if_their_is_nothing_to_migrate() {
ExtBuilder::default().build().execute_with(|| {
// Setup mock preimage
let data = b"hello world!".to_vec();
let len = data.len() as u32;
let hash = <Runtime as frame_system::Config>::Hashing::hash_of(&data);

// (we don't insert it, there is nothing to migrate)

// Call migration
assert_err!(
Migrations::migrate_democracy_preimage(RuntimeOrigin::signed(1), hash, len,),
Error::<Runtime>::PreimageMissing
);
});
}

#[test]
fn preimage_lazy_migration_fails_if_preimage_already_exists() {
ExtBuilder::default().build().execute_with(|| {
// Setup mock preimage
let data = b"hello world!".to_vec();
let len = data.len() as u32;
let hash = <Runtime as frame_system::Config>::Hashing::hash_of(&data);

DeprecatedDemocracyPreimages::<Runtime>::insert(
hash,
PreimageStatus::Available {
data,
provider: 42,
deposit: 142,
since: 0,
expiry: None,
},
);

// Setup mock preimage in new pallet
StatusFor::<Runtime>::insert(
hash,
RequestStatus::Unrequested {
deposit: (42, 142),
len,
},
);

// Call migration
assert_err!(
Migrations::migrate_democracy_preimage(RuntimeOrigin::signed(1), hash, len,),
Error::<Runtime>::PreimageAlreadyExists
);

// Check there was no migration.
assert!(DeprecatedDemocracyPreimages::<Runtime>::get(hash).is_some());
});
}

#[test]
fn preimage_lazy_migration_fails_if_len_hint_is_wrong() {
ExtBuilder::default().build().execute_with(|| {
// Setup mock preimage
let data = b"hello world!".to_vec();
let len = data.len() as u32;
let hash = <Runtime as frame_system::Config>::Hashing::hash_of(&data);

DeprecatedDemocracyPreimages::<Runtime>::insert(
hash,
PreimageStatus::Available {
data,
provider: 42,
deposit: 142,
since: 0,
expiry: None,
},
);

// Call migration
assert_err!(
Migrations::migrate_democracy_preimage(
RuntimeOrigin::signed(1),
hash,
len - 1, // too short !
),
Error::<Runtime>::WrongUpperBound
);

// Check there was no migration.
assert!(DeprecatedDemocracyPreimages::<Runtime>::get(hash).is_some());
assert!(StatusFor::<Runtime>::get(hash).is_none());
assert!(PreimageFor::<Runtime>::get((hash, len)).is_none());
});
}

#[test]
fn preimage_lazy_migration_fails_if_preimage_is_too_big() {
ExtBuilder::default().build().execute_with(|| {
// Setup mock preimage
let data = [1; (MAX_SIZE as usize) + 1].to_vec();
let len = data.len() as u32;
let hash = <Runtime as frame_system::Config>::Hashing::hash_of(&data);

DeprecatedDemocracyPreimages::<Runtime>::insert(
hash,
PreimageStatus::Available {
data,
provider: 42,
deposit: 142,
since: 0,
expiry: None,
},
);

// Call migration
assert_err!(
Migrations::migrate_democracy_preimage(RuntimeOrigin::signed(1), hash, len,),
Error::<Runtime>::PreimageIsTooBig
);

// Check there was no migration.
assert!(DeprecatedDemocracyPreimages::<Runtime>::get(hash).is_some());
assert!(StatusFor::<Runtime>::get(hash).is_none());
assert!(PreimageFor::<Runtime>::get((hash, len)).is_none());
});
}

// TODO: a test to ensure that post_upgrade invokes the same set of migrations that pre_upgrade
// does would be useful

0 comments on commit 0d17627

Please sign in to comment.