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

New mock_environment function with valid contract address #2215

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 15 additions & 10 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_env,
message_info, mock_dependencies, mock_dependencies_with_contract_balance, mock_environment,
};
use cosmwasm_std::{coins, Attribute, StdError, Storage, SubMsg};

Expand All @@ -101,13 +101,14 @@ mod tests {
#[test]
fn instantiate_fails() {
let mut deps = mock_dependencies();
let env = mock_environment(&deps.api);

let creator = deps.api.addr_make("creator");

let msg = InstantiateMsg {};
let info = message_info(&creator, &coins(1000, "earth"));
// we can just call .unwrap() to assert this was a success
let res = instantiate(deps.as_mut(), mock_env(), info, msg);
let res = instantiate(deps.as_mut(), env, info, msg);
match res.unwrap_err() {
StdError::GenericErr { msg, .. } => {
assert_eq!(msg, "You can only use this contract for migrations")
Expand All @@ -118,15 +119,16 @@ 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
let payout = String::from("someone else");
let msg = MigrateMsg {
payout: payout.clone(),
delete: 0,
};
let res = migrate(deps.as_mut(), mock_env(), msg).unwrap();
let res = migrate(deps.as_mut(), env, msg).unwrap();
// check payout
assert_eq!(1, res.messages.len());
let msg = res.messages.first().expect("no message");
Expand All @@ -141,7 +143,8 @@ 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
deps.storage.set(b"foo", b"bar");
Expand All @@ -155,7 +158,7 @@ mod tests {
payout: "user".to_string(),
delete: 100,
};
migrate(deps.as_mut(), mock_env(), msg).unwrap();
migrate(deps.as_mut(), env, msg).unwrap();

// no more data
let cnt = deps.storage.range(None, None, Order::Ascending).count();
Expand All @@ -164,7 +167,8 @@ 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 All @@ -178,11 +182,11 @@ mod tests {
// change the verifier via migrate
let payout = String::from("someone else");
let msg = MigrateMsg { payout, delete: 0 };
let _res = migrate(deps.as_mut(), mock_env(), msg).unwrap();
let _res = migrate(deps.as_mut(), env.clone(), msg).unwrap();

let res = execute(
deps.as_mut(),
mock_env(),
env.clone(),
message_info(&anon, &[]),
ExecuteMsg::Cleanup { limit: Some(2) },
)
Expand All @@ -193,9 +197,10 @@ mod tests {
let cnt = deps.storage.range(None, None, Order::Ascending).count();
assert_eq!(cnt, 1);

let env = mock_environment(&deps.api);
let res = execute(
deps.as_mut(),
mock_env(),
env,
message_info(&anon, &[]),
ExecuteMsg::Cleanup { limit: Some(2) },
)
Expand Down
17 changes: 11 additions & 6 deletions contracts/burner/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)

use cosmwasm_std::{coins, Attribute, BankMsg, ContractResult, Order, Response, SubMsg};
use cosmwasm_vm::testing::{execute, instantiate, migrate, mock_env, mock_info, mock_instance};
use cosmwasm_vm::testing::{
execute, instantiate, migrate, mock_environment, mock_info, mock_instance,
};

use burner::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg};
use cosmwasm_vm::Storage;
Expand All @@ -42,11 +44,12 @@ fn first_attr(data: impl AsRef<[Attribute]>, search_key: &str) -> Option<String>
#[test]
fn instantiate_fails() {
let mut deps = mock_instance(WASM, &[]);
let env = mock_environment(deps.api());

let msg = InstantiateMsg {};
let info = mock_info("creator", &coins(1000, "earth"));
// we can just call .unwrap() to assert this was a success
let res: ContractResult<Response> = instantiate(&mut deps, mock_env(), info, msg);
let res: ContractResult<Response> = instantiate(&mut deps, env, info, msg);
let msg = res.unwrap_err();
assert_eq!(
msg,
Expand All @@ -57,14 +60,15 @@ fn instantiate_fails() {
#[test]
fn migrate_sends_funds() {
let mut deps = mock_instance(WASM, &coins(123456, "gold"));
let env = mock_environment(deps.api());

// change the verifier via migrate
let payout = String::from("someone else");
let msg = MigrateMsg {
payout: payout.clone(),
delete: 0,
};
let res: Response = migrate(&mut deps, mock_env(), msg).unwrap();
let res: Response = migrate(&mut deps, env, msg).unwrap();
// check payout
assert_eq!(1, res.messages.len());
let msg = res.messages.first().expect("no message");
Expand Down Expand Up @@ -93,14 +97,15 @@ fn execute_cleans_up_data() {
})
.unwrap();

let env = mock_environment(deps.api());
// change the verifier via migrate
let payout = String::from("someone else");
let msg = MigrateMsg { payout, delete: 0 };
let _res: Response = migrate(&mut deps, mock_env(), msg).unwrap();
let _res: Response = migrate(&mut deps, env.clone(), msg).unwrap();

let res: Response = execute(
&mut deps,
mock_env(),
env.clone(),
mock_info("anon", &[]),
ExecuteMsg::Cleanup { limit: Some(2) },
)
Expand All @@ -118,7 +123,7 @@ fn execute_cleans_up_data() {

let res: Response = execute(
&mut deps,
mock_env(),
env,
mock_info("anon", &[]),
ExecuteMsg::Cleanup { limit: Some(2) },
)
Expand Down
Loading
Loading