From 18c4b0574602ed660a3942245284f706e744ea02 Mon Sep 17 00:00:00 2001 From: Jiri Vestfal <45360788+MissingNO57@users.noreply.github.com> Date: Tue, 16 Apr 2024 13:09:31 +0200 Subject: [PATCH] Fixing tests helpers (#4) --- Cargo.lock | 11 --------- Cargo.toml | 5 ++-- src/helpers/mod.rs | 1 - src/lib.rs | 1 + .../tests_helpers.rs => testing/helpers.rs} | 24 +++++++++++++++++-- src/testing/mod.rs | 6 +++++ 6 files changed, 31 insertions(+), 17 deletions(-) rename src/{helpers/tests_helpers.rs => testing/helpers.rs} (56%) create mode 100644 src/testing/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 4b67a4e..219701f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,16 +124,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "cosmwasm-storage" -version = "1.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66de2ab9db04757bcedef2b5984fbe536903ada4a8a9766717a4a71197ef34f6" -dependencies = [ - "cosmwasm-std", - "serde", -] - [[package]] name = "cpufeatures" version = "0.2.12" @@ -191,7 +181,6 @@ dependencies = [ "base64", "bech32", "cosmwasm-std", - "cosmwasm-storage", "cw-storage-plus", "hex", "ripemd", diff --git a/Cargo.toml b/Cargo.toml index 1286a6f..e901e39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,9 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cosmwasm-std = "1.1.3" -cosmwasm-storage = "1.1.3" -cw-storage-plus = "1.0.1" +cosmwasm-std = "1.5.3" +cw-storage-plus = "1.2.0" bech32 = "0.9.1" sha2 = "0.10.6" ripemd = "0.1.3" diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 48fe2f6..b962ace 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -1,2 +1 @@ pub mod iteration_guard; -pub mod tests_helpers; diff --git a/src/lib.rs b/src/lib.rs index 019e586..2b451df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,3 +3,4 @@ pub mod events; pub mod helpers; pub mod permissions; pub mod storage; +pub mod testing; diff --git a/src/helpers/tests_helpers.rs b/src/testing/helpers.rs similarity index 56% rename from src/helpers/tests_helpers.rs rename to src/testing/helpers.rs index 09f5455..1516556 100644 --- a/src/helpers/tests_helpers.rs +++ b/src/testing/helpers.rs @@ -1,9 +1,9 @@ use cosmwasm_std::testing::{mock_dependencies, MockApi, MockQuerier, MockStorage}; -use cosmwasm_std::ContractResult as StdContractResult; use cosmwasm_std::{ to_json_binary, Addr, ContractInfoResponse, Empty, OwnedDeps, SystemError, SystemResult, WasmQuery, }; +use cosmwasm_std::{BankMsg, Coin, ContractResult as StdContractResult, Response, SubMsg, Uint128}; pub fn deps_with_creator( creator: Addr, @@ -13,7 +13,7 @@ pub fn deps_with_creator( let mut querier = MockQuerier::default(); querier.update_wasm(move |request| match request { WasmQuery::ContractInfo { contract_addr } => { - if *contract_addr == contract_address { + if contract_addr == contract_address.as_str() { let mut response = ContractInfoResponse::default(); response.admin = Some(creator.to_string()); SystemResult::Ok(StdContractResult::Ok(to_json_binary(&response).unwrap())) @@ -31,3 +31,23 @@ pub fn deps_with_creator( deps.querier = querier; deps } + +pub fn assert_err(result: &Result, error: &E) { + // Check if result contains specific error + match result { + Ok(_) => panic!("Expected Err, got Ok"), + Err(res_error) => assert_eq!(format!("{:?}", res_error), format!("{:?}", error)), + } +} + +pub fn assert_transfer(res: &Response, address: &Addr, amount: &u128, denom: &str) { + let send_msg = SubMsg::new(BankMsg::Send { + to_address: address.to_string(), + amount: vec![Coin { + denom: denom.to_string(), + amount: Uint128::new(*amount), + }], + }); + + assert_eq!(res.messages[0], send_msg); +} diff --git a/src/testing/mod.rs b/src/testing/mod.rs new file mode 100644 index 0000000..0b66d76 --- /dev/null +++ b/src/testing/mod.rs @@ -0,0 +1,6 @@ +#![cfg(not(target_arch = "wasm32"))] + +// Exposed for testing only +// Both unit tests and integration tests are compiled to native code, so everything in here does not need to compile to Wasm. + +pub mod helpers;