Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the claims controller #514

Merged
merged 3 commits into from
Oct 29, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 102 additions & 4 deletions packages/controllers/src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ mod test {
None,
)
.unwrap();
let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();

assert_eq!(amount, Uint128::zero());
assert_eq!(saved_claims.len(), 0);
}

#[test]
Expand Down Expand Up @@ -244,11 +250,22 @@ mod test {
None,
)
.unwrap();

let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();

assert_eq!(amount, Uint128::zero());
assert_eq!(saved_claims.len(), 2);
assert_eq!(saved_claims[0].amount, (TEST_AMOUNT + 100).into());
assert_eq!(saved_claims[0].release_at, Expiration::AtHeight(10));
assert_eq!(saved_claims[1].amount, (TEST_AMOUNT + 100).into());
assert_eq!(saved_claims[1].release_at, Expiration::AtHeight(100));
}

#[test]
fn test_claim_tokens_with_one_released_claims() {
fn test_claim_tokens_with_one_released_claim() {
let mut deps = mock_dependencies(&[]);
let claims = Claims::new("claims");

Expand Down Expand Up @@ -281,7 +298,16 @@ mod test {
None,
)
.unwrap();

let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();

assert_eq!(amount, TEST_AMOUNT.into());
assert_eq!(saved_claims.len(), 1);
assert_eq!(saved_claims[0].amount, (TEST_AMOUNT + 100).into());
assert_eq!(saved_claims[0].release_at, Expiration::AtHeight(100));
}
sgoya marked this conversation as resolved.
Show resolved Hide resolved

#[test]
Expand Down Expand Up @@ -309,7 +335,7 @@ mod test {

let mut env = mock_env();
env.block.height = 1000;
// the address has two claims and the first one can be released
// the address has two claims and both can be released
let amount = claims
.claim_tokens(
deps.as_mut().storage,
Expand All @@ -318,7 +344,14 @@ mod test {
None,
)
.unwrap();

let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();

assert_eq!(amount, (TEST_AMOUNT + TEST_AMOUNT + 100).into());
assert_eq!(saved_claims.len(), 0);
}
sgoya marked this conversation as resolved.
Show resolved Hide resolved

#[test]
Expand Down Expand Up @@ -346,7 +379,7 @@ mod test {

let mut env = mock_env();
env.block.height = 1000;
// the address has two claims and the first one can be released

let amount = claims
.claim_tokens(
deps.as_mut().storage,
Expand All @@ -355,7 +388,18 @@ mod test {
Some(Uint128::zero()),
)
.unwrap();

let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();

assert_eq!(amount, Uint128::zero());
assert_eq!(saved_claims.len(), 2);
assert_eq!(saved_claims[0].amount, (TEST_AMOUNT).into());
assert_eq!(saved_claims[0].release_at, Expiration::AtHeight(10));
assert_eq!(saved_claims[1].amount, (TEST_AMOUNT + 100).into());
assert_eq!(saved_claims[1].release_at, Expiration::AtHeight(100));
}
sgoya marked this conversation as resolved.
Show resolved Hide resolved

#[test]
Expand Down Expand Up @@ -383,7 +427,7 @@ mod test {

let mut env = mock_env();
env.block.height = 1000;
// the address has two claims and the first one can be released

let amount = claims
.claim_tokens(
deps.as_mut().storage,
Expand All @@ -392,7 +436,14 @@ mod test {
Some(Uint128::from(2100u128)),
)
.unwrap();

let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();

assert_eq!(amount, (TEST_AMOUNT + TEST_AMOUNT + 100).into());
assert_eq!(saved_claims.len(), 0);
}
sgoya marked this conversation as resolved.
Show resolved Hide resolved

#[test]
Expand Down Expand Up @@ -440,6 +491,53 @@ mod test {
assert_eq!(saved_claims[0].release_at, Expiration::AtHeight(10));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

Copy link
Contributor

@maurolacy maurolacy Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to this, add a test where the cap allows to release only part of a claim (i.e. test_claim_tokens_with_cap_only_partial_amount_released()).

Let's say you have a mature claim for 100, but cap is 75.

Confirm / check that claim amounts are properly adjusted, and completed claims are removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the code partial claims are not claimed at all. So in the case when the cap is less than either of the claims nothing will be claimed. Adding a test case based on that.


#[test]
fn test_claim_tokens_with_cap_too_low_no_claims_released() {
let mut deps = mock_dependencies(&[]);
let claims = Claims::new("claims");

claims
.create_claim(
deps.as_mut().storage,
&Addr::unchecked("addr"),
(TEST_AMOUNT + 100).into(),
Expiration::AtHeight(10),
)
.unwrap();

claims
.create_claim(
deps.as_mut().storage,
&Addr::unchecked("addr"),
TEST_AMOUNT.into(),
Expiration::AtHeight(5),
)
.unwrap();

let mut env = mock_env();
env.block.height = 1000;
// the address has two claims and the first one can be released
let amount = claims
.claim_tokens(
deps.as_mut().storage,
&Addr::unchecked("addr"),
&env.block,
Some((TEST_AMOUNT - 50).into()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

)
.unwrap();
assert_eq!(amount, Uint128::zero());

let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();
assert_eq!(saved_claims.len(), 2);
assert_eq!(saved_claims[0].amount, (TEST_AMOUNT + 100).into());
assert_eq!(saved_claims[0].release_at, Expiration::AtHeight(10));
assert_eq!(saved_claims[1].amount, (TEST_AMOUNT).into());
assert_eq!(saved_claims[1].release_at, Expiration::AtHeight(5));
}

#[test]
fn test_query_claims_returns_correct_claims() {
let mut deps = mock_dependencies(&[]);
Expand Down