Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cwlittle committed Sep 13, 2024
1 parent c080215 commit 6d4924e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
56 changes: 45 additions & 11 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Binary, Coin, CosmosMsg, Deps, DepsMut, Env, IbcMsg, IbcTimeout, MessageInfo,
Response, StdResult, Timestamp,
to_json_binary, Binary, CosmosMsg, Deps, DepsMut, Env, IbcMsg, IbcTimeout, MessageInfo,
Response, StdError, StdResult, Timestamp,
};
use cw2::set_contract_version;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, GetBalanceResponse, InstantiateMsg, QueryMsg};
use crate::msg::{ExecuteMsg, GetBalancesResponse, InstantiateMsg, QueryMsg};
use crate::state::{State, STATE};

// version info for migration info
Expand Down Expand Up @@ -85,29 +85,34 @@ pub mod execute {
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetBalance {} => to_json_binary(&query::balance(deps)?),
QueryMsg::GetBalances {} => to_json_binary(&query::balance(deps, env)?),
}
}

pub mod query {
use super::*;

pub fn balance(deps: Deps) -> StdResult<GetBalanceResponse> {
unimplemented!();
pub fn balance(deps: Deps, env: Env) -> StdResult<GetBalancesResponse> {
let coins = deps.querier.query_all_balances(env.contract.address)?;

Ok(GetBalancesResponse { balances: coins })
}
}

#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env};
use cosmwasm_std::{coins, from_json, Addr};
use cosmwasm_std::testing::{message_info, mock_dependencies_with_balance, mock_env};
use cosmwasm_std::{coins, from_json, Addr, Coin, Uint128};

#[test]
fn increment() {
let mut deps = mock_dependencies();
fn disburse_funds() {
let mut deps = mock_dependencies_with_balance(&[Coin {
amount: Uint128::new(2000),
denom: "denom".to_string(),
}]);

let msg = InstantiateMsg {
min_disbursal_amount: 0,
Expand All @@ -123,4 +128,33 @@ mod tests {
};
let _res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
}

#[test]
fn balances() {
let mut deps = mock_dependencies_with_balance(&[Coin {
amount: Uint128::new(2000),
denom: "denom".to_string(),
}]);

let msg = InstantiateMsg {
min_disbursal_amount: 0,
channel_id: "channel-0".to_string(),
ibc_timeout: 1000,
memo: "memo".to_string(),
to_address: "to_address".to_string(),
};
let info = message_info(&Addr::unchecked("123"), &coins(2000, "denom"));
let _res = instantiate(deps.as_mut(), mock_env(), info.clone(), msg).unwrap();
let msg = QueryMsg::GetBalances {};
let res = query(deps.as_ref(), mock_env(), msg).unwrap();

let value: GetBalancesResponse = from_json(&res).unwrap();
assert_eq!(
vec![Coin {
amount: Uint128::new(2000),
denom: "denom".to_string(),
}],
value.balances
);
}
}
8 changes: 4 additions & 4 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cosmwasm_std::{
WasmQuery,
};

use crate::msg::{ExecuteMsg, GetBalanceResponse, QueryMsg};
use crate::msg::{ExecuteMsg, GetBalancesResponse, QueryMsg};

/// CwTemplateContract is a wrapper around Addr that provides a lot of helpers
/// for working with this.
Expand All @@ -29,19 +29,19 @@ impl CwTemplateContract {
}

/// Get Count
pub fn balance<Q, T, CQ>(&self, querier: &Q) -> StdResult<GetBalanceResponse>
pub fn balance<Q, T, CQ>(&self, querier: &Q) -> StdResult<GetBalancesResponse>
where
Q: Querier,
T: Into<String>,
CQ: CustomQuery,
{
let msg = QueryMsg::GetBalance {};
let msg = QueryMsg::GetBalances {};
let query = WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_json_binary(&msg)?,
}
.into();
let res: GetBalanceResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
let res: GetBalancesResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
Ok(res)
}
}
10 changes: 5 additions & 5 deletions src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::IbcTimeout;
use cosmwasm_std::{Coin, IbcTimeout};

#[cw_serde]
pub struct InstantiateMsg {
Expand All @@ -18,12 +18,12 @@ pub enum ExecuteMsg {
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(GetBalanceResponse)]
GetBalance {},
#[returns(GetBalancesResponse)]
GetBalances {},
}

// We define a custom struct for each query response
#[cw_serde]
pub struct GetBalanceResponse {
pub balance: u64,
pub struct GetBalancesResponse {
pub balances: Vec<Coin>,
}

0 comments on commit 6d4924e

Please sign in to comment.