Skip to content

Commit

Permalink
Use new function for new mock_dependencies_with_contract_balance func…
Browse files Browse the repository at this point in the history
…tionality
  • Loading branch information
chipshort committed Aug 16, 2024
1 parent 616b7d9 commit 453841e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
8 changes: 4 additions & 4 deletions contracts/burner/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn cleanup(storage: &mut dyn Storage, mut limit: usize) -> usize {
mod tests {
use super::*;
use cosmwasm_std::testing::{
message_info, mock_dependencies, mock_dependencies_with_balance, mock_environment,
message_info, mock_dependencies, mock_dependencies_with_contract_balance, mock_environment,
};
use cosmwasm_std::{coins, Attribute, StdError, Storage, SubMsg};

Expand Down Expand Up @@ -119,7 +119,7 @@ mod tests {

#[test]
fn migrate_sends_funds() {
let mut deps = mock_dependencies_with_balance(&coins(123456, "gold"));
let mut deps = mock_dependencies_with_contract_balance(&coins(123456, "gold"));
let env = mock_environment(&deps.api);

// change the verifier via migrate
Expand All @@ -143,7 +143,7 @@ mod tests {

#[test]
fn migrate_with_delete() {
let mut deps = mock_dependencies_with_balance(&coins(123456, "gold"));
let mut deps = mock_dependencies_with_contract_balance(&coins(123456, "gold"));
let env = mock_environment(&deps.api);

// store some sample data
Expand All @@ -167,7 +167,7 @@ mod tests {

#[test]
fn execute_cleans_up_data() {
let mut deps = mock_dependencies_with_balance(&coins(123456, "gold"));
let mut deps = mock_dependencies_with_contract_balance(&coins(123456, "gold"));
let env = mock_environment(&deps.api);

let anon = deps.api.addr_make("anon");
Expand Down
4 changes: 2 additions & 2 deletions contracts/hackatom/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn query_int() -> IntResponse {
mod tests {
use super::*;
use cosmwasm_std::testing::{
message_info, mock_dependencies, mock_dependencies_with_balances, mock_environment,
message_info, mock_dependencies, mock_dependencies_with_balances_valid, mock_environment,
MOCK_CONTRACT_ADDR,
};
// import trait Storage to get access to read
Expand Down Expand Up @@ -430,7 +430,7 @@ mod tests {
fn querier_callbacks_work() {
let rich_addr = String::from("foobar");
let rich_balance = coins(10000, "gold");
let deps = mock_dependencies_with_balances(&[(&rich_addr, &rich_balance)]);
let deps = mock_dependencies_with_balances_valid(&[(&rich_addr, &rich_balance)]);

// querying with balance gets the balance
let bal = query_other_balance(deps.as_ref(), rich_addr).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions contracts/queue/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ fn query_open_iterators(deps: Deps, count: u32) -> Empty {
mod tests {
use super::*;
use cosmwasm_std::testing::{
message_info, mock_dependencies_with_balance, mock_environment, MockApi, MockQuerier,
MockStorage,
message_info, mock_dependencies_with_contract_balance, mock_environment, MockApi,
MockQuerier, MockStorage,
};
use cosmwasm_std::{coins, from_json, OwnedDeps};

/// Instantiates a contract with no elements
fn create_contract() -> (OwnedDeps<MockStorage, MockApi, MockQuerier>, MessageInfo) {
let mut deps = mock_dependencies_with_balance(&coins(1000, "earth"));
let mut deps = mock_dependencies_with_contract_balance(&coins(1000, "earth"));
let env = mock_environment(&deps.api);
let creator = deps.api.addr_make("creator");
let info = message_info(&creator, &coins(1000, "earth"));
Expand Down
34 changes: 28 additions & 6 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,28 @@ pub fn mock_dependencies() -> OwnedDeps<MockStorage, MockApi, MockQuerier, Empty
/// Creates all external requirements that can be injected for unit tests.
///
/// It sets the given balance for the contract itself, nothing else.
#[deprecated(
note = "This works only with mock_env, not mock_environment. Use mock_dependencies_with_contract_balance instead"
)]
pub fn mock_dependencies_with_balance(
contract_balance: &[Coin],
) -> OwnedDeps<MockStorage, MockApi, MockQuerier, Empty> {
let mut deps = mock_dependencies();
deps.querier.bank.update_balance(
deps.api.addr_make(MOCK_CONTRACT_ADDR),
contract_balance.to_vec(),
);
#[allow(deprecated)]
mock_dependencies_with_balances(&[(MOCK_CONTRACT_ADDR, contract_balance)])
}

deps
/// Creates all external requirements that can be injected for unit tests.
///
/// It sets the given balance for the contract itself, nothing else.
pub fn mock_dependencies_with_contract_balance(
contract_balance: &[Coin],
) -> OwnedDeps<MockStorage, MockApi, MockQuerier, Empty> {
mock_dependencies_with_balances_valid(&[(MOCK_CONTRACT_ADDR, contract_balance)])
}

/// Initializes the querier along with the mock_dependencies.
/// Sets all balances provided (you must explicitly set contract balance if desired).
#[deprecated = "This works only with mock_env, not mock_environment. Use mock_dependencies_with_balances_valid instead"]
pub fn mock_dependencies_with_balances(
balances: &[(&str, &[Coin])],
) -> OwnedDeps<MockStorage, MockApi, MockQuerier> {
Expand All @@ -95,6 +103,20 @@ pub fn mock_dependencies_with_balances(
}
}

/// Initializes the querier along with the mock_dependencies.
/// Sets all balances provided (you must explicitly set contract balance if desired).
pub fn mock_dependencies_with_balances_valid(
balances: &[(&str, &[Coin])],
) -> OwnedDeps<MockStorage, MockApi, MockQuerier, Empty> {
let mut deps = mock_dependencies();
for (addr, coins) in balances {
deps.querier
.bank
.update_balance(deps.api.addr_make(addr), coins.to_vec());
}
deps
}

// Use MemoryStorage implementation (which is valid in non-testcode)
// We can later make simplifications here if needed
pub type MockStorage = MemoryStorage;
Expand Down
3 changes: 2 additions & 1 deletion packages/std/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub use mock::DistributionQuerier;
pub use mock::StakingQuerier;
#[allow(deprecated)]
pub use mock::{
mock_dependencies, mock_dependencies_with_balance, mock_dependencies_with_balances, mock_env,
mock_dependencies, mock_dependencies_with_balance, mock_dependencies_with_balances,
mock_dependencies_with_balances_valid, mock_dependencies_with_contract_balance, mock_env,
mock_environment, mock_wasmd_attr, BankQuerier, MockApi, MockQuerier,
MockQuerierCustomHandlerResult, MockStorage, MOCK_CONTRACT_ADDR,
};
Expand Down

0 comments on commit 453841e

Please sign in to comment.