Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Jul 4, 2024
1 parent fe145d3 commit a41f5fd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
3 changes: 2 additions & 1 deletion custom-pallets/shared-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ pub mod pallet {

#[pallet::storage]
#[pallet::getter(fn reputation_score)]
pub type ReputationScoreOfAccount<T:Config> = StorageMap<_, Blake2_128Concat, T::AccountId, ReputationScore>;
pub type ReputationScoreOfAccount<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, ReputationScore>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
Expand Down
96 changes: 78 additions & 18 deletions custom-pallets/shared-storage/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@ fn correct_error_for_none_value() {
});
}


#[test]
fn test_set_department_reputation_score() {
new_test_ext().execute_with(|| {
let address = 1;
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)
);
});
}

Expand All @@ -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)
);
});
}

Expand All @@ -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)
);
});
}

Expand All @@ -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)
);
});
}

Expand All @@ -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));
});
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions custom-pallets/shared-storage/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -60,7 +59,7 @@ impl ReputationScore {
pub fn subtract_score(&mut self, department: Vec<u8>, 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 {
Expand All @@ -74,4 +73,4 @@ impl ReputationScore {
pub fn get_total_score(&self) -> i64 {
self.total_score
}
}
}

0 comments on commit a41f5fd

Please sign in to comment.