Skip to content

Commit

Permalink
Working on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ueco-jb committed Nov 5, 2021
1 parent 5dc32ca commit 12375c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
11 changes: 4 additions & 7 deletions contracts/cw3-flex-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ mod tests {
None,
)
.unwrap_err();
assert_eq!(ContractError::ZeroThreshold {}, err.downcast().unwrap());
assert_eq!(ContractError::InvalidThreshold {}, err.downcast().unwrap());

// Total weight less than required weight not allowed
let instantiate_msg = InstantiateMsg {
Expand All @@ -646,10 +646,7 @@ mod tests {
None,
)
.unwrap_err();
assert_eq!(
ContractError::UnreachableThreshold {},
err.downcast().unwrap()
);
assert_eq!(ContractError::UnreachableWeight {}, err.downcast().unwrap());

// All valid
let instantiate_msg = InstantiateMsg {
Expand Down Expand Up @@ -1441,7 +1438,7 @@ mod tests {

// 51% required, which is 12 of the initial 24
let threshold = Threshold::ThresholdQuorum {
threshold: Decimal::percent(50),
threshold: Decimal::percent(51),
quorum: Decimal::percent(1),
};
let voting_period = Duration::Time(20000);
Expand Down Expand Up @@ -1523,7 +1520,7 @@ mod tests {
let (flex_addr, group_addr) = setup_test_case(
&mut app,
Threshold::ThresholdQuorum {
threshold: Decimal::percent(50),
threshold: Decimal::percent(51),
quorum: Decimal::percent(33),
},
voting_period,
Expand Down
11 changes: 7 additions & 4 deletions contracts/cw3-flex-multisig/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("Required threshold cannot be zero")]
ZeroThreshold {},
#[error("Required threshold must be higher then 50% and smaller then 100%")]
InvalidThreshold {},

#[error("Not possible to reach required (passing) threshold")]
UnreachableThreshold {},
#[error("Required weight cannot be zero")]
ZeroWeight {},

#[error("Not possible to reach required (passing) weight")]
UnreachableWeight {},

#[error("Group contract invalid address '{addr}'")]
InvalidGroup { addr: String },
Expand Down
40 changes: 22 additions & 18 deletions contracts/cw3-flex-multisig/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ impl Threshold {
weight: weight_needed,
} => {
if *weight_needed == 0 {
Err(ContractError::ZeroThreshold {})
Err(ContractError::ZeroWeight {})
} else if *weight_needed > total_weight {
Err(ContractError::UnreachableThreshold {})
Err(ContractError::UnreachableWeight {})
} else {
Ok(())
}
Expand Down Expand Up @@ -91,12 +91,10 @@ impl Threshold {
}
}

/// Asserts that the 0.0 < percent <= 1.0
/// Asserts that the 0.5 < percent <= 1.0
fn valid_percentage(percent: &Decimal) -> Result<(), ContractError> {
if percent.is_zero() {
Err(ContractError::ZeroThreshold {})
} else if *percent > Decimal::one() {
Err(ContractError::UnreachableThreshold {})
if *percent > Decimal::percent(100) || *percent < Decimal::percent(50) {
Err(ContractError::InvalidThreshold {})
} else {
Ok(())
}
Expand Down Expand Up @@ -172,7 +170,10 @@ mod tests {

// 0 is never a valid percentage
let err = valid_percentage(&Decimal::zero()).unwrap_err();
assert_eq!(err.to_string(), ContractError::ZeroThreshold {}.to_string());
assert_eq!(
err.to_string(),
ContractError::InvalidThreshold {}.to_string()
);

// 100% is
valid_percentage(&Decimal::one()).unwrap();
Expand All @@ -181,18 +182,18 @@ mod tests {
let err = valid_percentage(&Decimal::percent(101)).unwrap_err();
assert_eq!(
err.to_string(),
ContractError::UnreachableThreshold {}.to_string()
ContractError::InvalidThreshold {}.to_string()
);
// not 100.1%
let err = valid_percentage(&Decimal::permille(1001)).unwrap_err();
assert_eq!(
err.to_string(),
ContractError::UnreachableThreshold {}.to_string()
ContractError::InvalidThreshold {}.to_string()
);

// other values in between 0 and 1 are valid
valid_percentage(&Decimal::permille(1)).unwrap();
valid_percentage(&Decimal::percent(17)).unwrap();
// other values in between 0.5 and 1 are valid
valid_percentage(&Decimal::percent(51)).unwrap();
valid_percentage(&Decimal::percent(67)).unwrap();
valid_percentage(&Decimal::percent(99)).unwrap();
}

Expand All @@ -203,13 +204,13 @@ mod tests {
.validate(5)
.unwrap_err();
// TODO: remove to_string() when PartialEq implemented
assert_eq!(err.to_string(), ContractError::ZeroThreshold {}.to_string());
assert_eq!(err.to_string(), ContractError::ZeroWeight {}.to_string());
let err = Threshold::AbsoluteCount { weight: 6 }
.validate(5)
.unwrap_err();
assert_eq!(
err.to_string(),
ContractError::UnreachableThreshold {}.to_string()
ContractError::UnreachableWeight {}.to_string()
);

Threshold::AbsoluteCount { weight: 1 }.validate(5).unwrap();
Expand All @@ -221,7 +222,10 @@ mod tests {
}
.validate(5)
.unwrap_err();
assert_eq!(err.to_string(), ContractError::ZeroThreshold {}.to_string());
assert_eq!(
err.to_string(),
ContractError::InvalidThreshold {}.to_string()
);
Threshold::AbsolutePercentage {
percentage: Decimal::percent(51),
}
Expand All @@ -243,15 +247,15 @@ mod tests {
.unwrap_err();
assert_eq!(
err.to_string(),
ContractError::UnreachableThreshold {}.to_string()
ContractError::UnreachableWeight {}.to_string()
);
let err = Threshold::ThresholdQuorum {
threshold: Decimal::percent(51),
quorum: Decimal::percent(0),
}
.validate(5)
.unwrap_err();
assert_eq!(err.to_string(), ContractError::ZeroThreshold {}.to_string());
assert_eq!(err.to_string(), ContractError::ZeroWeight {}.to_string());
}

#[test]
Expand Down

0 comments on commit 12375c4

Please sign in to comment.