diff --git a/contracts/burner/src/contract.rs b/contracts/burner/src/contract.rs index 004122a7a..b21fd0a4c 100644 --- a/contracts/burner/src/contract.rs +++ b/contracts/burner/src/contract.rs @@ -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}; @@ -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 @@ -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 @@ -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"); diff --git a/contracts/hackatom/src/contract.rs b/contracts/hackatom/src/contract.rs index 5ef8bfef8..25cf0367b 100644 --- a/contracts/hackatom/src/contract.rs +++ b/contracts/hackatom/src/contract.rs @@ -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 @@ -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(); diff --git a/contracts/queue/src/contract.rs b/contracts/queue/src/contract.rs index 93e4b91d2..85a1e4243 100644 --- a/contracts/queue/src/contract.rs +++ b/contracts/queue/src/contract.rs @@ -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, 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")); diff --git a/packages/std/src/testing/mock.rs b/packages/std/src/testing/mock.rs index 4ffb44074..3159974c0 100644 --- a/packages/std/src/testing/mock.rs +++ b/packages/std/src/testing/mock.rs @@ -70,20 +70,28 @@ pub fn mock_dependencies() -> OwnedDeps OwnedDeps { - 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 { + 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 { @@ -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 { + 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; diff --git a/packages/std/src/testing/mod.rs b/packages/std/src/testing/mod.rs index f97e2b2c3..cd9e55423 100644 --- a/packages/std/src/testing/mod.rs +++ b/packages/std/src/testing/mod.rs @@ -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, };