From a41f5fd952744cf3dbccb4c761f343d660516232 Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Thu, 4 Jul 2024 18:44:00 +0530 Subject: [PATCH] format --- custom-pallets/shared-storage/src/lib.rs | 3 +- custom-pallets/shared-storage/src/tests.rs | 96 ++++++++++++++++++---- custom-pallets/shared-storage/src/types.rs | 5 +- 3 files changed, 82 insertions(+), 22 deletions(-) diff --git a/custom-pallets/shared-storage/src/lib.rs b/custom-pallets/shared-storage/src/lib.rs index 546eb00..9c4c57a 100644 --- a/custom-pallets/shared-storage/src/lib.rs +++ b/custom-pallets/shared-storage/src/lib.rs @@ -66,7 +66,8 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn reputation_score)] - pub type ReputationScoreOfAccount = StorageMap<_, Blake2_128Concat, T::AccountId, ReputationScore>; + pub type ReputationScoreOfAccount = + StorageMap<_, Blake2_128Concat, T::AccountId, ReputationScore>; #[pallet::genesis_config] pub struct GenesisConfig { diff --git a/custom-pallets/shared-storage/src/tests.rs b/custom-pallets/shared-storage/src/tests.rs index ffe10b9..f0369ba 100644 --- a/custom-pallets/shared-storage/src/tests.rs +++ b/custom-pallets/shared-storage/src/tests.rs @@ -32,7 +32,6 @@ fn correct_error_for_none_value() { }); } - #[test] fn test_set_department_reputation_score() { new_test_ext().execute_with(|| { @@ -40,12 +39,19 @@ fn test_set_department_reputation_score() { let department = vec![1, 2, 3]; let score = 10; - assert_ok!(SharedStorage::set_department_reputation_score(address, department.clone(), score)); + assert_ok!(SharedStorage::set_department_reputation_score( + address, + department.clone(), + score + )); let reputation_score = SharedStorage::reputation_score(address); assert!(reputation_score.is_some()); let reputation_score = reputation_score.unwrap(); - assert_eq!(reputation_score.get_department_score(department.clone()), Some(score)); + assert_eq!( + reputation_score.get_department_score(department.clone()), + Some(score) + ); }); } @@ -56,15 +62,26 @@ fn test_update_department_reputation_score() { let department = vec![1, 2, 3]; let score = 10; - assert_ok!(SharedStorage::set_department_reputation_score(address, department.clone(), score)); + assert_ok!(SharedStorage::set_department_reputation_score( + address, + department.clone(), + score + )); let new_score = 20; - assert_ok!(SharedStorage::update_department_reputation_score(address, department.clone(), new_score)); + assert_ok!(SharedStorage::update_department_reputation_score( + address, + department.clone(), + new_score + )); let reputation_score = SharedStorage::reputation_score(address); assert!(reputation_score.is_some()); let reputation_score = reputation_score.unwrap(); - assert_eq!(reputation_score.get_department_score(department.clone()), Some(new_score)); + assert_eq!( + reputation_score.get_department_score(department.clone()), + Some(new_score) + ); }); } @@ -75,15 +92,26 @@ fn test_add_score_to_department() { let department = vec![1, 2, 3]; let score = 10; - assert_ok!(SharedStorage::set_department_reputation_score(address, department.clone(), score)); + assert_ok!(SharedStorage::set_department_reputation_score( + address, + department.clone(), + score + )); let amount = 5; - assert_ok!(SharedStorage::add_reputation_score_to_department(address, department.clone(), amount)); + assert_ok!(SharedStorage::add_reputation_score_to_department( + address, + department.clone(), + amount + )); let reputation_score = SharedStorage::reputation_score(address); assert!(reputation_score.is_some()); let reputation_score = reputation_score.unwrap(); - assert_eq!(reputation_score.get_department_score(department.clone()), Some(score + amount)); + assert_eq!( + reputation_score.get_department_score(department.clone()), + Some(score + amount) + ); }); } @@ -94,15 +122,26 @@ fn test_subtract_score_from_department() { let department = vec![1, 2, 3]; let score = 10; - assert_ok!(SharedStorage::set_department_reputation_score(address, department.clone(), score)); + assert_ok!(SharedStorage::set_department_reputation_score( + address, + department.clone(), + score + )); let amount = 5; - assert_ok!(SharedStorage::subtract_reputation_score_from_department(address, department.clone(), amount)); + assert_ok!(SharedStorage::subtract_reputation_score_from_department( + address, + department.clone(), + amount + )); let reputation_score = SharedStorage::reputation_score(address); assert!(reputation_score.is_some()); let reputation_score = reputation_score.unwrap(); - assert_eq!(reputation_score.get_department_score(department.clone()), Some(score - amount)); + assert_eq!( + reputation_score.get_department_score(department.clone()), + Some(score - amount) + ); }); } @@ -113,9 +152,14 @@ fn test_get_department_reputation_score() { let department = vec![1, 2, 3]; let score = 10; - assert_ok!(SharedStorage::set_department_reputation_score(address, department.clone(), score)); + assert_ok!(SharedStorage::set_department_reputation_score( + address, + department.clone(), + score + )); - let department_score = SharedStorage::get_department_reputation_score(address, department.clone()); + let department_score = + SharedStorage::get_department_reputation_score(address, department.clone()); assert_eq!(department_score, Some(score)); }); } @@ -129,8 +173,16 @@ fn test_get_all_department_reputation_scores() { let score1 = 10; let score2 = 20; - assert_ok!(SharedStorage::set_department_reputation_score(address, department1.clone(), score1)); - assert_ok!(SharedStorage::set_department_reputation_score(address, department2.clone(), score2)); + assert_ok!(SharedStorage::set_department_reputation_score( + address, + department1.clone(), + score1 + )); + assert_ok!(SharedStorage::set_department_reputation_score( + address, + department2.clone(), + score2 + )); let all_departments = SharedStorage::get_all_department_reputation_scores(address); assert_eq!(all_departments.len(), 2); @@ -148,8 +200,16 @@ fn test_get_total_reputation_score() { let score1 = 10; let score2 = 20; - assert_ok!(SharedStorage::set_department_reputation_score(address, department1.clone(), score1)); - assert_ok!(SharedStorage::set_department_reputation_score(address, department2.clone(), score2)); + assert_ok!(SharedStorage::set_department_reputation_score( + address, + department1.clone(), + score1 + )); + assert_ok!(SharedStorage::set_department_reputation_score( + address, + department2.clone(), + score2 + )); let total_score = SharedStorage::get_total_reputation_score(address); assert_eq!(total_score, score1 + score2); diff --git a/custom-pallets/shared-storage/src/types.rs b/custom-pallets/shared-storage/src/types.rs index 9ca98a4..b5d5f1b 100644 --- a/custom-pallets/shared-storage/src/types.rs +++ b/custom-pallets/shared-storage/src/types.rs @@ -4,7 +4,6 @@ use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; - /// Reputation scores that can be used for schelling game. #[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)] @@ -60,7 +59,7 @@ impl ReputationScore { pub fn subtract_score(&mut self, department: Vec, amount: i64) -> bool { if let Some(score) = self.departments.get_mut(&department) { if *score >= amount { - *score = score.checked_sub(amount).unwrap_or(0); + *score = score.checked_sub(amount).unwrap_or(i64::MIN); self.total_score = self.total_score.checked_sub(amount).unwrap_or(i64::MIN); true } else { @@ -74,4 +73,4 @@ impl ReputationScore { pub fn get_total_score(&self) -> i64 { self.total_score } -} \ No newline at end of file +}