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

Fix raw_query for mock #400

Merged
merged 1 commit into from
May 20, 2024
Merged
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
1 change: 1 addition & 0 deletions contracts/mock_contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface = []

[dependencies]
cosmwasm-std = { workspace = true }
cw2 = { version = "1.1.2" }
serde = { workspace = true }
schemars = "0.8.10"
serde_json = "1.0.79"
Expand Down
4 changes: 3 additions & 1 deletion contracts/mock_contract/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
This folder is used for testing.
# Mock Contract test

This folder is used for testing.
31 changes: 29 additions & 2 deletions contracts/mock_contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@

#[cfg_attr(feature = "export", cosmwasm_std::entry_point)]
pub fn instantiate(
_deps: DepsMut,
deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> StdResult<Response> {
cw2::set_contract_version(deps.storage, "mock-contract", "0")?;
Ok(Response::new().add_attribute("action", "instantiate"))
}

Expand Down Expand Up @@ -163,7 +164,7 @@
mod test {
use super::MockContract as LocalMockContract;
use super::*;
use cosmwasm_std::coins;
use cosmwasm_std::{coins, from_json};
use cw_orch::prelude::*;

#[test]
Expand Down Expand Up @@ -196,4 +197,30 @@

Ok(())
}

#[test]
fn raw_query() -> Result<(), CwOrchError> {
// We need to check we can still call the execute msgs conveniently
let sender = Addr::unchecked("sender");
let mock = Mock::new(&sender);
mock.set_balance(&sender, coins(156 * 2, "ujuno"))?;
let contract = LocalMockContract::new("mock-contract", mock.clone());

contract.upload()?;
contract.instantiate(&InstantiateMsg {}, None, None)?;

let cw2_info: cw2::ContractVersion = from_json(
mock.wasm_querier()
.raw_query(contract.address()?, b"contract_info".to_vec())?,
)?;

Check warning on line 215 in contracts/mock_contract/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

contracts/mock_contract/src/lib.rs#L215

Added line #L215 was not covered by tests

assert_eq!(
cw2_info,
cw2::ContractVersion {
contract: "mock-contract".to_owned(),
version: "0".to_owned()
}
);
Ok(())
}
}
32 changes: 21 additions & 11 deletions packages/cw-orch-mock/src/queriers/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{cell::RefCell, rc::Rc};

use cosmwasm_std::testing::MockApi;
use cosmwasm_std::{instantiate2_address, Api};
use cosmwasm_std::{instantiate2_address, Api, Binary, ContractResult, StdError, SystemResult};
use cosmwasm_std::{to_json_binary, ContractInfoResponse, HexBinary};
use cw_orch_core::{
contract::interface_traits::{ContractInstance, Uploadable},
Expand Down Expand Up @@ -67,21 +67,31 @@
Ok(hash.into())
}

/// Copied implementation from [`cosmwasm_std::QuerierWrapper::query`] but without deserialization
fn raw_query<A: Api, S: StateInterface>(
querier: &MockWasmQuerier<A, S>,
address: impl Into<String>,
query_data: Vec<u8>,
) -> Result<Vec<u8>, CwEnvError> {
Ok(querier
.app
.borrow()
.wrap()
.query(&cosmwasm_std::QueryRequest::Wasm(
cosmwasm_std::WasmQuery::Raw {
contract_addr: address.into(),
key: query_data.into(),
},
))?)
let raw = to_json_binary(&cosmwasm_std::QueryRequest::<cosmwasm_std::Empty>::Wasm(
cosmwasm_std::WasmQuery::Raw {
contract_addr: address.into(),
key: query_data.into(),
},
))
.map_err(|serialize_err| {
StdError::generic_err(format!("Serializing QueryRequest: {serialize_err}"))

Check warning on line 83 in packages/cw-orch-mock/src/queriers/wasm.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/queriers/wasm.rs#L83

Added line #L83 was not covered by tests
})?;
let res: Result<Binary, StdError> = match querier.app.borrow().wrap().raw_query(&raw) {
SystemResult::Err(system_err) => Err(StdError::generic_err(format!(
"Querier system error: {system_err}"
))),
SystemResult::Ok(ContractResult::Err(contract_err)) => Err(StdError::generic_err(format!(
"Querier contract error: {contract_err}"
))),

Check warning on line 91 in packages/cw-orch-mock/src/queriers/wasm.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-mock/src/queriers/wasm.rs#L86-L91

Added lines #L86 - L91 were not covered by tests
SystemResult::Ok(ContractResult::Ok(value)) => Ok(value),
};
Ok(res?.0)
}

fn smart_query<A: Api, S: StateInterface, Q, T>(
Expand Down
Loading