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
394 changes: 393 additions & 1 deletion packages/controllers/src/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,396 @@ impl<'a> Claims<'a> {
}
}

// TODO: add test coverage
#[cfg(test)]
mod test {
use cosmwasm_std::{
testing::{mock_dependencies, mock_env},
Order,
};

use super::*;
const TEST_AMOUNT: u128 = 1000u128;
const TEST_EXPIRATION: Expiration = Expiration::AtHeight(10);

#[test]
fn can_create_claim() {
let claim = Claim::new(TEST_AMOUNT, TEST_EXPIRATION);
assert_eq!(claim.amount, TEST_AMOUNT.into());
assert_eq!(claim.release_at, TEST_EXPIRATION);
}

#[test]
fn can_create_claims() {
let deps = mock_dependencies(&[]);
let claims = Claims::new("claims");
// Assert that claims creates a map and there are no keys in the map.
assert_eq!(
claims
.0
.range(&deps.storage, None, None, Order::Ascending)
.collect::<StdResult<Vec<_>>>()
.unwrap()
.len(),
0
);
}

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

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

// Assert that claims creates a map and there is one claim for the address.
let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();
assert_eq!(saved_claims.len(), 1);
assert_eq!(saved_claims[0].amount, TEST_AMOUNT.into());
assert_eq!(saved_claims[0].release_at, TEST_EXPIRATION);

// Adding another claim to same address, make sure that both claims are saved.
claims
.create_claim(
deps.as_mut().storage,
&Addr::unchecked("addr"),
(TEST_AMOUNT + 100).into(),
TEST_EXPIRATION,
)
.unwrap();

// Assert that both claims exist for the address.
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.into());
assert_eq!(saved_claims[0].release_at, TEST_EXPIRATION);
assert_eq!(saved_claims[1].amount, (TEST_AMOUNT + 100).into());
assert_eq!(saved_claims[1].release_at, TEST_EXPIRATION);

// Adding another claim to different address, make sure that other address only has one claim.
claims
.create_claim(
deps.as_mut().storage,
&Addr::unchecked("addr2"),
(TEST_AMOUNT + 100).into(),
TEST_EXPIRATION,
)
.unwrap();

// Assert that both claims exist for the address.
let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();

let saved_claims_addr2 = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr2"))
.unwrap();
assert_eq!(saved_claims.len(), 2);
assert_eq!(saved_claims_addr2.len(), 1);
}

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

let amount = claims
.claim_tokens(
deps.as_mut().storage,
&Addr::unchecked("addr"),
&mock_env().block,
None,
)
.unwrap();
assert_eq!(amount, Uint128::zero());
}

#[test]
fn test_claim_tokens_with_no_released_claims() {
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 + 100).into(),
Expiration::AtHeight(100),
)
.unwrap();

let mut env = mock_env();
env.block.height = 0;
// the address has two claims however they are both not expired
let amount = claims
.claim_tokens(
deps.as_mut().storage,
&Addr::unchecked("addr"),
&env.block,
None,
)
.unwrap();
assert_eq!(amount, Uint128::zero());
}

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

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

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

let mut env = mock_env();
env.block.height = 20;
// 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,
None,
)
.unwrap();
assert_eq!(amount, TEST_AMOUNT.into());
}
sgoya marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

let mut env = mock_env();
env.block.height = 1000;
// the address has two claims and the first one can be released
sgoya marked this conversation as resolved.
Show resolved Hide resolved
let amount = claims
.claim_tokens(
deps.as_mut().storage,
&Addr::unchecked("addr"),
&env.block,
None,
)
.unwrap();
assert_eq!(amount, (TEST_AMOUNT + TEST_AMOUNT + 100).into());
}
sgoya marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

let mut env = mock_env();
env.block.height = 1000;
// the address has two claims and the first one can be released
sgoya marked this conversation as resolved.
Show resolved Hide resolved
let amount = claims
.claim_tokens(
deps.as_mut().storage,
&Addr::unchecked("addr"),
&env.block,
Some(Uint128::zero()),
)
.unwrap();
assert_eq!(amount, Uint128::zero());
}
sgoya marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

let mut env = mock_env();
env.block.height = 1000;
// the address has two claims and the first one can be released
sgoya marked this conversation as resolved.
Show resolved Hide resolved
let amount = claims
.claim_tokens(
deps.as_mut().storage,
&Addr::unchecked("addr"),
&env.block,
Some(Uint128::from(2100u128)),
)
.unwrap();
assert_eq!(amount, (TEST_AMOUNT + TEST_AMOUNT + 100).into());
}
sgoya marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn test_claim_tokens_with_cap_only_one_claim_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()),
)
.unwrap();
assert_eq!(amount, (TEST_AMOUNT).into());

let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();
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(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_query_claims_returns_correct_claims() {
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();

let queried_claims = claims
.query_claims(deps.as_ref(), &Addr::unchecked("addr"))
.unwrap();
let saved_claims = claims
.0
.load(deps.as_mut().storage, &Addr::unchecked("addr"))
.unwrap();
assert_eq!(queried_claims.claims, saved_claims);
}

#[test]
fn test_query_claims_returns_empty_for_non_existent_user() {
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();

let queried_claims = claims
.query_claims(deps.as_ref(), &Addr::unchecked("addr2"))
.unwrap();

assert_eq!(queried_claims.claims.len(), 0);
}
}