Browse Source

council - candidacy withdrawal and stake release tests

ondratra 4 years ago
parent
commit
4bce7e6c09
2 changed files with 93 additions and 0 deletions
  1. 45 0
      runtime-modules/council/src/mock.rs
  2. 48 0
      runtime-modules/council/src/tests.rs

+ 45 - 0
runtime-modules/council/src/mock.rs

@@ -711,6 +711,51 @@ where
         );
     }
 
+    pub fn withdraw_candidacy(
+        origin: OriginType<T::AccountId>,
+        member_id: T::MembershipId,
+        expected_result: Result<(), Error<T>>,
+    ) {
+        // check method returns expected result
+        assert_eq!(
+            Module::<T>::withdraw_candidacy(InstanceMockUtils::<T>::mock_origin(origin), member_id,),
+            expected_result,
+        );
+
+        if expected_result.is_err() {
+            return;
+        }
+
+        assert_eq!(
+            system::Module::<Runtime>::events().last().unwrap().event,
+            TestEvent::event_mod(RawEvent::CandidacyWithdraw(member_id.into().into(),)),
+        );
+    }
+
+    pub fn release_candidacy_stake(
+        origin: OriginType<T::AccountId>,
+        member_id: T::MembershipId,
+        expected_result: Result<(), Error<T>>,
+    ) {
+        // check method returns expected result
+        assert_eq!(
+            Module::<T>::release_candidacy_stake(
+                InstanceMockUtils::<T>::mock_origin(origin),
+                member_id,
+            ),
+            expected_result,
+        );
+
+        if expected_result.is_err() {
+            return;
+        }
+
+        assert_eq!(
+            system::Module::<Runtime>::events().last().unwrap().event,
+            TestEvent::event_mod(RawEvent::CandidacyStakeRelease(member_id.into().into(),)),
+        );
+    }
+
     pub fn vote_for_candidate(
         origin: OriginType<T::AccountId>,
         commitment: T::Hash,

+ 48 - 0
runtime-modules/council/src/tests.rs

@@ -208,6 +208,54 @@ fn council_candidacy_invalid_member() {
     });
 }
 
+// Test that candidate can withdraw valid candidacy.
+#[test]
+fn council_candidacy_withdraw_candidacy() {
+    let config = default_genesis_config();
+
+    build_test_externalities(config).execute_with(|| {
+        let council_settings = CouncilSettings::<Runtime>::extract_settings();
+
+        let stake = council_settings.min_candidate_stake;
+        let candidate = MockUtils::generate_candidate(0, stake);
+
+        Mocks::announce_candidacy(
+            candidate.origin.clone(),
+            candidate.account_id.clone(),
+            candidate.candidate.stake.clone(),
+            Ok(()),
+        );
+
+        Mocks::withdraw_candidacy(
+            candidate.origin.clone(),
+            candidate.account_id.clone(),
+            Ok(()),
+        );
+    });
+}
+
+// Test that candidate can withdraw valid candidacy.
+#[test]
+fn council_candidacy_release_candidate_stake() {
+    let config = default_genesis_config();
+
+    build_test_externalities(config).execute_with(|| {
+        let not_elected_candidate_index = 2;
+
+        let params = Mocks::run_full_council_cycle(0, &[], 0);
+
+        Mocks::release_candidacy_stake(
+            params.candidates_announcing[not_elected_candidate_index]
+                .origin
+                .clone(),
+            params.candidates_announcing[not_elected_candidate_index]
+                .account_id
+                .clone(),
+            Ok(()),
+        );
+    });
+}
+
 // Test that only valid members can candidate.
 #[test]
 fn council_announcement_reset_on_insufficient_candidates() {