Browse Source

runtime: Upgrade working-group pallet tests.

Shamil Gadelshin 4 years ago
parent
commit
d2f8283529

+ 32 - 27
runtime-modules/working-group/src/tests/fixtures.rs

@@ -1,3 +1,8 @@
+use frame_support::dispatch::{DispatchError, DispatchResult};
+use frame_support::storage::{StorageMap, StorageValue};
+use std::collections::BTreeSet;
+use system::{EventRecord, Phase, RawOrigin};
+
 use super::mock::{
     Balances, Membership, System, Test, TestEvent, TestWorkingGroup, TestWorkingGroupInstance,
 };
@@ -6,12 +11,8 @@ use crate::types::{
     Application, Opening, OpeningPolicyCommitment, OpeningType, RewardPolicy, RoleStakeProfile,
     Worker,
 };
-use crate::Error;
 use crate::RawEvent;
 use common::constraints::InputValidationLengthConstraint;
-use srml_support::{StorageLinkedMap, StorageValue};
-use std::collections::BTreeSet;
-use system::{EventRecord, Phase, RawOrigin};
 
 pub struct IncreaseWorkerStakeFixture {
     origin: RawOrigin<u64>,
@@ -38,7 +39,7 @@ impl IncreaseWorkerStakeFixture {
         Self { balance, ..self }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let stake_id = 0;
         let old_stake = <stake::Module<Test>>::stakes(stake_id);
         let old_balance = Balances::free_balance(&self.account_id);
@@ -103,7 +104,7 @@ impl TerminateWorkerRoleFixture {
         }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         <crate::WorkerExitRationaleText<TestWorkingGroupInstance>>::put(self.constraint.clone());
 
         let actual_result = TestWorkingGroup::terminate_role(
@@ -117,7 +118,9 @@ impl TerminateWorkerRoleFixture {
         if actual_result.is_ok() {
             if actual_result.is_ok() {
                 assert!(
-                    !<crate::WorkerById<Test, TestWorkingGroupInstance>>::exists(self.worker_id)
+                    !<crate::WorkerById<Test, TestWorkingGroupInstance>>::contains_key(
+                        self.worker_id
+                    )
                 );
             }
         }
@@ -140,7 +143,7 @@ impl LeaveWorkerRoleFixture {
         Self { origin, ..self }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let rationale_text = b"rationale_text".to_vec();
         let actual_result = TestWorkingGroup::leave_role(
             self.origin.clone().into(),
@@ -150,7 +153,9 @@ impl LeaveWorkerRoleFixture {
         assert_eq!(actual_result, expected_result);
 
         if actual_result.is_ok() {
-            assert!(!<crate::WorkerById<Test, TestWorkingGroupInstance>>::exists(self.worker_id));
+            assert!(
+                !<crate::WorkerById<Test, TestWorkingGroupInstance>>::contains_key(self.worker_id)
+            );
         }
     }
 }
@@ -175,7 +180,7 @@ impl UpdateWorkerRewardAmountFixture {
         Self { origin, ..self }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let actual_result = TestWorkingGroup::update_reward_amount(
             self.origin.clone().into(),
             self.worker_id,
@@ -212,7 +217,7 @@ impl UpdateWorkerRewardAccountFixture {
         Self { origin, ..self }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let actual_result = TestWorkingGroup::update_reward_account(
             self.origin.clone().into(),
             self.worker_id,
@@ -250,7 +255,7 @@ impl UpdateWorkerRoleAccountFixture {
         Self { origin, ..self }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let actual_result = TestWorkingGroup::update_role_account(
             self.origin.clone().into(),
             self.worker_id,
@@ -306,7 +311,7 @@ impl FillWorkerOpeningFixture {
         }
     }
 
-    pub fn call(&self) -> Result<u64, Error> {
+    pub fn call(&self) -> Result<u64, DispatchError> {
         let saved_worker_next_id = TestWorkingGroup::next_worker_id();
         TestWorkingGroup::fill_opening(
             self.origin.clone().into(),
@@ -318,7 +323,7 @@ impl FillWorkerOpeningFixture {
         Ok(saved_worker_next_id)
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) -> u64 {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) -> u64 {
         let saved_worker_next_id = TestWorkingGroup::next_worker_id();
         let actual_result = self.call().map(|_| ());
         assert_eq!(actual_result.clone(), expected_result);
@@ -379,7 +384,7 @@ impl BeginReviewWorkerApplicationsFixture {
     pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
         Self { origin, ..self }
     }
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let actual_result =
             TestWorkingGroup::begin_applicant_review(self.origin.clone().into(), self.opening_id);
         assert_eq!(actual_result, expected_result);
@@ -407,7 +412,7 @@ impl TerminateApplicationFixture {
             worker_application_id: application_id,
         }
     }
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let actual_result = TestWorkingGroup::terminate_application(
             self.origin.clone().into(),
             self.worker_application_id,
@@ -436,7 +441,7 @@ impl WithdrawApplicationFixture {
             worker_application_id: application_id,
         }
     }
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let actual_result = TestWorkingGroup::withdraw_application(
             self.origin.clone().into(),
             self.worker_application_id,
@@ -447,11 +452,11 @@ impl WithdrawApplicationFixture {
 
 pub fn increase_total_balance_issuance_using_account_id(account_id: u64, balance: u64) {
     let _ =
-        <Balances as srml_support::traits::Currency<u64>>::deposit_creating(&account_id, balance);
+        <Balances as frame_support::traits::Currency<u64>>::deposit_creating(&account_id, balance);
 }
 
 pub fn get_balance(account_id: u64) -> u64 {
-    <super::mock::Balances as srml_support::traits::Currency<u64>>::total_balance(&account_id)
+    <super::mock::Balances as frame_support::traits::Currency<u64>>::total_balance(&account_id)
 }
 
 pub fn setup_members(count: u8) {
@@ -524,7 +529,7 @@ impl ApplyOnWorkerOpeningFixture {
         }
     }
 
-    pub fn call(&self) -> Result<u64, Error> {
+    pub fn call(&self) -> Result<u64, DispatchError> {
         let saved_application_next_id = TestWorkingGroup::next_application_id();
         TestWorkingGroup::apply_on_opening(
             self.origin.clone().into(),
@@ -538,7 +543,7 @@ impl ApplyOnWorkerOpeningFixture {
 
         Ok(saved_application_next_id)
     }
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) -> u64 {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) -> u64 {
         let saved_application_next_id = TestWorkingGroup::next_application_id();
 
         let actual_result = self.call().map(|_| ());
@@ -583,7 +588,7 @@ impl AcceptWorkerApplicationsFixture {
         }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let actual_result =
             TestWorkingGroup::accept_applications(self.origin.clone().into(), self.opening_id);
         assert_eq!(actual_result, expected_result);
@@ -698,7 +703,7 @@ impl AddWorkerOpeningFixture {
         }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) -> u64 {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) -> u64 {
         let saved_opening_next_id = TestWorkingGroup::next_opening_id();
         let actual_result = self.call().map(|_| ());
 
@@ -726,7 +731,7 @@ impl AddWorkerOpeningFixture {
         saved_opening_next_id
     }
 
-    pub fn call(&self) -> Result<u64, Error> {
+    pub fn call(&self) -> Result<u64, DispatchError> {
         let saved_opening_next_id = TestWorkingGroup::next_opening_id();
         TestWorkingGroup::add_opening(
             self.origin.clone().into(),
@@ -787,7 +792,7 @@ impl EventFixture {
 
     pub fn assert_last_global_event(expected_event: TestEvent) {
         let expected_event = EventRecord {
-            phase: Phase::ApplyExtrinsic(0),
+            phase: Phase::Initialization,
             event: expected_event,
             topics: vec![],
         };
@@ -824,7 +829,7 @@ impl DecreaseWorkerStakeFixture {
         Self { balance, ..self }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let stake_id = 0;
         let old_balance = Balances::free_balance(&self.account_id);
         let old_stake = <stake::Module<Test>>::stakes(stake_id);
@@ -900,7 +905,7 @@ impl SlashWorkerStakeFixture {
         Self { balance, ..self }
     }
 
-    pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
+    pub fn call_and_assert(&self, expected_result: DispatchResult) {
         let stake_id = 0;
         let old_balance = Balances::free_balance(&self.account_id);
         let old_stake = <stake::Module<Test>>::stakes(stake_id);

+ 6 - 5
runtime-modules/working-group/src/tests/hiring_workflow.rs

@@ -1,12 +1,13 @@
+use frame_support::dispatch::{DispatchError, DispatchResult};
+use system::RawOrigin;
+
 use crate::tests::fixtures::{
     create_mint, increase_total_balance_issuance_using_account_id, set_mint_id, setup_members,
     AddWorkerOpeningFixture, ApplyOnWorkerOpeningFixture, BeginReviewWorkerApplicationsFixture,
     FillWorkerOpeningFixture, SetLeadFixture,
 };
 use crate::tests::mock::TestWorkingGroup;
-use crate::Error;
 use crate::{OpeningPolicyCommitment, OpeningType, RewardPolicy};
-use system::RawOrigin;
 
 #[derive(Clone)]
 struct HiringWorkflowApplication {
@@ -18,7 +19,7 @@ struct HiringWorkflowApplication {
 
 pub struct HiringWorkflow {
     opening_type: OpeningType,
-    expected_result: Result<(), Error>,
+    expected_result: DispatchResult,
     role_stake: Option<u64>,
     applications: Vec<HiringWorkflowApplication>,
     setup_environment: bool,
@@ -39,7 +40,7 @@ impl Default for HiringWorkflow {
 }
 
 impl HiringWorkflow {
-    pub fn expect(self, result: Result<(), Error>) -> Self {
+    pub fn expect(self, result: DispatchResult) -> Self {
         Self {
             expected_result: result,
             ..self
@@ -131,7 +132,7 @@ impl HiringWorkflow {
         result.ok()
     }
 
-    fn fill_worker_position(&self) -> Result<u64, Error> {
+    fn fill_worker_position(&self) -> Result<u64, DispatchError> {
         let origin = match self.opening_type {
             OpeningType::Leader => RawOrigin::Root,
             OpeningType::Worker => {

+ 36 - 18
runtime-modules/working-group/src/tests/mock.rs

@@ -1,18 +1,20 @@
-use crate::{BalanceOf, Module, NegativeImbalance, Trait};
-use common::constraints::InputValidationLengthConstraint;
-use primitives::H256;
-use sr_primitives::{
+use frame_support::storage::StorageMap;
+use frame_support::traits::{OnFinalize, OnInitialize};
+use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
+use sp_core::H256;
+use sp_runtime::{
     testing::Header,
     traits::{BlakeTwo256, IdentityLookup},
     Perbill,
 };
-use srml_support::{
-    impl_outer_event, impl_outer_origin, parameter_types, StorageLinkedMap, StorageMap,
-};
 use std::marker::PhantomData;
+use system;
+
+use crate::{BalanceOf, Module, NegativeImbalance, Trait};
+use common::constraints::InputValidationLengthConstraint;
 
 impl_outer_origin! {
-        pub enum Origin for Test {}
+    pub enum Origin for Test {}
 }
 
 mod working_group {
@@ -29,6 +31,7 @@ impl_outer_event! {
         balances<T>,
         working_group TestWorkingGroupInstance <T>,
         membership_mod<T>,
+        system<T>,
     }
 }
 
@@ -40,8 +43,6 @@ parameter_types! {
     pub const MinimumPeriod: u64 = 5;
     pub const StakePoolId: [u8; 8] = *b"joystake";
     pub const ExistentialDeposit: u32 = 0;
-    pub const TransferFee: u32 = 0;
-    pub const CreationFee: u32 = 0;
 }
 
 // Workaround for https://github.com/rust-lang/rust/issues/26925 - remove when sorted.
@@ -49,6 +50,7 @@ parameter_types! {
 pub struct Test;
 
 impl system::Trait for Test {
+    type BaseCallFilter = ();
     type Origin = Origin;
     type Call = ();
     type Index = u64;
@@ -61,9 +63,17 @@ impl system::Trait for Test {
     type Event = TestEvent;
     type BlockHashCount = BlockHashCount;
     type MaximumBlockWeight = MaximumBlockWeight;
+    type DbWeight = ();
+    type BlockExecutionWeight = ();
+    type ExtrinsicBaseWeight = ();
+    type MaximumExtrinsicWeight = ();
     type MaximumBlockLength = MaximumBlockLength;
     type AvailableBlockRatio = AvailableBlockRatio;
     type Version = ();
+    type ModuleToIndex = ();
+    type AccountData = balances::AccountData<u64>;
+    type OnNewAccount = ();
+    type OnKilledAccount = ();
 }
 
 impl hiring::Trait for Test {
@@ -106,14 +116,10 @@ impl timestamp::Trait for Test {
 
 impl balances::Trait for Test {
     type Balance = u64;
-    type OnFreeBalanceZero = ();
-    type OnNewAccount = ();
-    type TransferPayment = ();
     type DustRemoval = ();
     type Event = TestEvent;
     type ExistentialDeposit = ExistentialDeposit;
-    type TransferFee = TransferFee;
-    type CreationFee = CreationFee;
+    type AccountStore = System;
 }
 
 impl recurringrewards::Trait for Test {
@@ -143,7 +149,7 @@ pub(crate) const WORKING_GROUP_MINT_CAPACITY: u64 = 40000;
 pub(crate) const WORKING_GROUP_CONSTRAINT_MIN: u16 = 1;
 pub(crate) const WORKING_GROUP_CONSTRAINT_DIFF: u16 = 40;
 
-pub fn build_test_externalities() -> runtime_io::TestExternalities {
+pub fn build_test_externalities() -> sp_io::TestExternalities {
     let mut t = system::GenesisConfig::default()
         .build_storage::<Test>()
         .unwrap();
@@ -184,13 +190,13 @@ impl<T: stake::Trait + crate::Trait<TestWorkingGroupInstance>> stake::StakingEve
         remaining_imbalance: NegativeImbalance<T>,
     ) -> NegativeImbalance<T> {
         // Stake not related to a staked role managed by the hiring module.
-        if !hiring::ApplicationIdByStakingId::<T>::exists(*stake_id) {
+        if !hiring::ApplicationIdByStakingId::<T>::contains_key(*stake_id) {
             return remaining_imbalance;
         }
 
         let hiring_application_id = hiring::ApplicationIdByStakingId::<T>::get(*stake_id);
 
-        if crate::MemberIdByHiringApplicationId::<T, TestWorkingGroupInstance>::exists(
+        if crate::MemberIdByHiringApplicationId::<T, TestWorkingGroupInstance>::contains_key(
             hiring_application_id,
         ) {
             return <crate::Module<T, TestWorkingGroupInstance>>::refund_working_group_stake(
@@ -213,3 +219,15 @@ impl<T: stake::Trait + crate::Trait<TestWorkingGroupInstance>> stake::StakingEve
         remaining_imbalance
     }
 }
+
+// Recommendation from Parity on testing on_finalize
+// https://substrate.dev/docs/en/next/development/module/tests
+pub fn run_to_block(n: u64) {
+    while System::block_number() < n {
+        <System as OnFinalize<u64>>::on_finalize(System::block_number());
+        <TestWorkingGroup as OnFinalize<u64>>::on_finalize(System::block_number());
+        System::set_block_number(System::block_number() + 1);
+        <System as OnInitialize<u64>>::on_initialize(System::block_number());
+        <TestWorkingGroup as OnInitialize<u64>>::on_initialize(System::block_number());
+    }
+}

File diff suppressed because it is too large
+ 293 - 88
runtime-modules/working-group/src/tests/mod.rs


Some files were not shown because too many files changed in this diff