pub struct MockQuerierBuilder {
pub api: MockApi,
/* private fields */
}
Expand description
MockQuerierBuilder
is a helper to build a MockQuerier
.
Usage:
use cosmwasm_std::{from_json, to_json_binary};
use abstract_testing::MockQuerierBuilder;
use cosmwasm_std::testing::{MockQuerier, MockApi};
use abstract_sdk::mock_module::MockModuleExecuteMsg;
let api = MockApi::default();
let contract_address = api.addr_make("contract_address");
let querier = MockQuerierBuilder::default().with_smart_handler(&contract_address, |msg| {
// handle the message
let res = match from_json::<MockModuleExecuteMsg>(msg).unwrap() {
// handle the message
_ => panic!("unexpected message"),
};
Ok(to_json_binary(&msg).unwrap())
}).build();
Fields§
§api: MockApi
Implementations§
Source§impl MockQuerierBuilder
impl MockQuerierBuilder
pub fn with_fallback_smart_handler<SH>(self, handler: SH) -> Self
pub fn with_fallback_raw_handler<RH>(self, handler: RH) -> Self
Sourcepub fn with_smart_handler<SH>(self, contract: &Addr, handler: SH) -> Self
pub fn with_smart_handler<SH>(self, contract: &Addr, handler: SH) -> Self
Add a smart query contract handler to the mock querier. The handler will be called when the contract address is queried with the given message. Usage:
use cosmwasm_std::{from_json, to_json_binary};
use abstract_testing::MockQuerierBuilder;
use cosmwasm_std::testing::{MockQuerier, MockApi};
use abstract_sdk::mock_module::{MockModuleQueryMsg, MockModuleQueryResponse};
let api = MockApi::default();
let contract_address = api.addr_make("contract_address");
let querier = MockQuerierBuilder::default().with_smart_handler(&contract_address, |msg| {
// handle the message
let res = match from_json::<MockModuleQueryMsg>(msg).unwrap() {
// handle the message
MockModuleQueryMsg =>
return to_json_binary(&MockModuleQueryResponse {}).map_err(|e| e.to_string())
};
}).build();
Sourcepub fn with_raw_handler<RH>(self, contract: &Addr, handler: RH) -> Self
pub fn with_raw_handler<RH>(self, contract: &Addr, handler: RH) -> Self
Add a raw query contract handler to the mock querier. The handler will be called when the contract address is queried with the given message. Usage:
use cosmwasm_std::{from_json, to_json_binary};
use abstract_testing::MockQuerierBuilder;
use cosmwasm_std::testing::{MockQuerier, MockApi};
use abstract_sdk::mock_module::{MockModuleQueryMsg, MockModuleQueryResponse};
let api = MockApi::default();
let contract_address = api.addr_make("contract1");
let querier = MockQuerierBuilder::default().with_raw_handler(&contract_address, |key: &str| {
// Example: Let's say, in the raw storage, the key "the key" maps to the value "the value"
match key {
"the key" => to_json_binary("the value").map_err(|e| e.to_string()),
_ => to_json_binary("").map_err(|e| e.to_string())
}
}).build();
Sourcepub fn with_contract_map_entry<'a, K, V>(
self,
contract: &Addr,
cw_map: Map<K, V>,
entry: (K, V),
) -> Self
pub fn with_contract_map_entry<'a, K, V>( self, contract: &Addr, cw_map: Map<K, V>, entry: (K, V), ) -> Self
Add a map entry to the querier for the given contract.
use cw_storage_plus::Map;
use cosmwasm_std::testing::MockApi;
use abstract_testing::MockQuerierBuilder;
let api = MockApi::default();
let contract_address = api.addr_make("contract1");
const MAP: Map<String, String> = Map::new("map");
MockQuerierBuilder::default()
.with_contract_map_entry(
&contract_address,
MAP,
("key".to_string(), "value".to_string())
);
pub fn with_contract_map_entries<'a, K, V>( self, contract: &Addr, cw_map: Map<K, V>, entries: Vec<(K, V)>, ) -> Self
Sourcepub fn with_contract_map_key<'a, K, V>(
self,
contract: &Addr,
cw_map: Map<K, V>,
key: K,
) -> Self
pub fn with_contract_map_key<'a, K, V>( self, contract: &Addr, cw_map: Map<K, V>, key: K, ) -> Self
Add an empty map key to the querier for the given contract. This is useful when you want the item to exist, but not have a value.
Sourcepub fn with_empty_contract_item<T>(
self,
contract: &Addr,
cw_item: Item<T>,
) -> Selfwhere
T: Serialize + DeserializeOwned,
pub fn with_empty_contract_item<T>(
self,
contract: &Addr,
cw_item: Item<T>,
) -> Selfwhere
T: Serialize + DeserializeOwned,
Add an empty item key to the querier for the given contract. This is useful when you want the item to exist, but not have a value.
Sourcepub fn with_contract_item<T>(
self,
contract: &Addr,
cw_item: Item<T>,
value: &T,
) -> Selfwhere
T: Serialize + DeserializeOwned,
pub fn with_contract_item<T>(
self,
contract: &Addr,
cw_item: Item<T>,
value: &T,
) -> Selfwhere
T: Serialize + DeserializeOwned,
Include a contract item in the mock querier.
use cw_storage_plus::Item;
use cosmwasm_std::testing::MockApi;
use abstract_testing::MockQuerierBuilder;
let api = MockApi::default();
let contract_address = api.addr_make("contract1");
const ITEM: Item<String> = Item::new("item");
MockQuerierBuilder::default()
.with_contract_item(
&contract_address,
ITEM,
&"value".to_string(),
);
Sourcepub fn with_contract_version(
self,
contract: &Addr,
name: impl Into<String>,
version: impl Into<String>,
) -> Self
pub fn with_contract_version( self, contract: &Addr, name: impl Into<String>, version: impl Into<String>, ) -> Self
Add a specific version of the contract to the mock querier.
use abstract_testing::MockQuerierBuilder;
use cosmwasm_std::testing::MockApi;
let api = MockApi::default();
let contract_address = api.addr_make("contract1");
MockQuerierBuilder::default()
.with_contract_version(&contract_address, "contract1", "v1.0.0");
Sourcepub fn with_contract_admin(self, contract: &Addr, admin: &Addr) -> Self
pub fn with_contract_admin(self, contract: &Addr, admin: &Addr) -> Self
set the SDK-level contract admin for a contract.
Sourcepub fn build(self) -> MockQuerier
pub fn build(self) -> MockQuerier
Build the MockQuerier
.
Trait Implementations§
Source§impl AbstractMockQuerier for MockQuerierBuilder
impl AbstractMockQuerier for MockQuerierBuilder
Source§fn account(self, account: &Account, account_id: AccountId) -> Self
fn account(self, account: &Account, account_id: AccountId) -> Self
Mock the existence of an Account by setting the Account id for the account along with registering the account to registry.
fn contracts(self, contracts: Vec<(&ContractEntry, Addr)>) -> Self
fn channels(self, channels: Vec<(&ChannelEntry, String)>) -> Self
fn addrs(&self) -> AbstractMockAddrs
fn set_account_admin_call_to(self, account: &Account) -> Self
Auto Trait Implementations§
impl Freeze for MockQuerierBuilder
impl !RefUnwindSafe for MockQuerierBuilder
impl !Send for MockQuerierBuilder
impl !Sync for MockQuerierBuilder
impl Unpin for MockQuerierBuilder
impl !UnwindSafe for MockQuerierBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more